fun RichTextStyleRow(
modifier: Modifier = Modifier,
value: RichTextValue,
onValueChanged: (RichTextValue) -> Unit,
) {
LazyRow(
modifier = modifier
) {
item {
RichTextStyleButton(
style = RichTextStyle.Bold,
value = value,
onValueChanged = onValueChanged,
icon = R.drawable.ic_bold,
tint = <TintColor>
)
}
item {
RichTextStyleButton(
style = RichTextStyle.Italic,
value = value,
onValueChanged = onValueChanged,
icon = R.drawable.ic_italic,
tint = <TintColor>
)
}
}
LazyRow(
modifier = modifier
) {
item {
RichTextStyleButton(
style = RichTextStyle.H1,
value = value,
onValueChanged = onValueChanged,
icon = R.drawable.ic_heading_1,
tint = <TintColor>
)
}
item {
RichTextStyleButton(
style = RichTextStyle.H2,
value = value,
onValueChanged = onValueChanged,
icon = R.drawable.ic_heading_2,
tint = <TintColor>
)
}
item {
RichTextStyleButton(
style = RichTextStyle.H3,
value = value,
onValueChanged = onValueChanged,
icon = R.drawable.ic_heading_3,
tint = <TintColor>
)
}
item {
RichTextStyleButton(
style = RichTextStyle.H4,
value = value,
onValueChanged = onValueChanged,
icon = R.drawable.ic_heading_4,
tint = <TintColor>
)
}
item {
RichTextStyleButton(
style = RichTextStyle.H5,
value = value,
onValueChanged = onValueChanged,
icon = R.drawable.ic_heading_5,
tint = <TintColor>
)
}
item {
RichTextStyleButton(
style = RichTextStyle.H6,
value = value,
onValueChanged = onValueChanged,
icon = R.drawable.ic_heading_6,
tint = <TintColor>
)
}
}
}
----------------
@Composable
fun RichTextStyleButton(
style: RichTextStyle,
value: RichTextValue,
onValueChanged: (RichTextValue) -> Unit,
icon: ImageVector,
tint: Color? = null
) {
IconButton(
modifier = Modifier
.focusProperties { canFocus = false },
onClick = {
onValueChanged(value.toggleStyle(style))
},
colors = IconButtonDefaults.iconButtonColors(
contentColor = if (value.currentStyles.contains(style)) {
MaterialTheme.colorScheme.onPrimary
} else {
MaterialTheme.colorScheme.onBackground
},
),
) {
Icon(
icon,
contentDescription = icon.name,
tint = if (value.currentStyles.contains(style)) tint
?: LocalContentColor.current else <DiasabledColor>
)
}
}
@Composable
fun RichTextStyleButton(
style: RichTextStyle,
value: RichTextValue,
onValueChanged: (RichTextValue) -> Unit,
icon: Int,
tint: Color? = null
) {
IconButton(
modifier = Modifier
.size(30.dp)
.focusProperties { canFocus = false },
onClick = {
onValueChanged(value.toggleStyle(style))
},
) {
Icon(
painter = painterResource(id = icon),
contentDescription = "",
tint = if (value.currentStyles.contains(style)) tint
?: LocalContentColor.current else <DiasabledColor>
)
}
}
--------------