diff --git a/compose-rust/android-kotlin/shared/HostButton.kt b/compose-rust/android-kotlin/shared/HostButton.kt index db0326cbf..543f7f37e 100644 --- a/compose-rust/android-kotlin/shared/HostButton.kt +++ b/compose-rust/android-kotlin/shared/HostButton.kt @@ -29,6 +29,7 @@ import androidx.compose.ui.unit.Dp import androidx.compose.ui.semantics.Role import androidx.compose.ui.semantics.contentDescription import androidx.compose.ui.semantics.semantics +import androidx.compose.ui.text.style.TextOverflow as ComposeOverflow import dev.darkpyonix.composerust.protocol.ButtonKind import dev.darkpyonix.composerust.protocol.ButtonVariant import dev.darkpyonix.composerust.protocol.HostEvent @@ -39,6 +40,8 @@ import dev.darkpyonix.composerust.design.ButtonStyle import dev.darkpyonix.composerust.design.ResolvedTheme import dev.darkpyonix.composerust.runtime.EventDispatcher import dev.darkpyonix.composerust.ui.node.Node +import dev.darkpyonix.composerust.ui.intProp +import dev.darkpyonix.composerust.ui.overflow import dev.darkpyonix.composerust.ui.paintProp import dev.darkpyonix.composerust.ui.role import dev.darkpyonix.composerust.ui.textStyle @@ -149,6 +152,11 @@ internal fun HostButton( val label = node.textStyle(theme, style.typeRole).copy(color = tint) val text = node.text(PropertyKind.Text) val icon = node.role(PropertyKind.Icon, IconRole.entries.toTypedArray()) + // A label is one line unless the Host asked for more. Left to wrap, a squeezed button + // is measured at the narrowest a Hangul label can be, one syllable, and '보내' breaks + // into '보' over '내'. With no soft wrap the label's narrowest width is its whole line. + val lines = node.intProp(PropertyKind.MaxLines)?.toInt()?.takeIf { it > 0 } ?: 1 + val ellipsis = node.overflow(ComposeOverflow.Ellipsis) // A button with a glyph and no word is still a button with a name. The role is what // the glyph means, so it is what the name is made of, and assistive technology never // meets an unnamed control. @@ -159,7 +167,7 @@ internal fun HostButton( } Box(named, contentAlignment = Alignment.Center) { if (icon == null) { - BasicText(text = text, style = label) + BasicText(text = text, style = label, maxLines = lines, softWrap = lines > 1, overflow = ellipsis) } else { Row( // The gap between the glyph and the word is the design system's smallest step, @@ -168,7 +176,7 @@ internal fun HostButton( verticalAlignment = Alignment.CenterVertically, ) { RoleIcon(icon, tint, theme) - if (text.isNotEmpty()) BasicText(text = text, style = label) + if (text.isNotEmpty()) BasicText(text = text, style = label, maxLines = lines, softWrap = lines > 1, overflow = ellipsis) } } } diff --git a/compose-rust/android-kotlin/shared/HostChip.kt b/compose-rust/android-kotlin/shared/HostChip.kt index ce1a2568c..aae8fedf0 100644 --- a/compose-rust/android-kotlin/shared/HostChip.kt +++ b/compose-rust/android-kotlin/shared/HostChip.kt @@ -20,6 +20,7 @@ import androidx.compose.ui.draw.clip import androidx.compose.ui.semantics.Role import androidx.compose.ui.semantics.selected import androidx.compose.ui.semantics.semantics +import androidx.compose.ui.text.style.TextOverflow import dev.darkpyonix.composerust.design.ChipStyle import dev.darkpyonix.composerust.design.ResolvedTheme import dev.darkpyonix.composerust.protocol.HostEvent @@ -98,7 +99,15 @@ internal fun HostChip( val leading = if (chosen && style.leadingCheck) IconRole.Check else icon if (leading != null) RoleIcon(leading, content, theme) if (label.isNotEmpty()) { - BasicText(text = label, style = node.textStyle(theme, style.typeRole).copy(color = content)) + // One line, never wrapped: a wrapped chip label is measured at one syllable of + // Hangul and its second line is clipped by the chip's height. + BasicText( + text = label, + style = node.textStyle(theme, style.typeRole).copy(color = content), + maxLines = 1, + softWrap = false, + overflow = TextOverflow.Ellipsis, + ) } } } diff --git a/compose-rust/android-kotlin/shared/NodeStyle.kt b/compose-rust/android-kotlin/shared/NodeStyle.kt index 3ef6b9ca3..77b32dbcd 100644 --- a/compose-rust/android-kotlin/shared/NodeStyle.kt +++ b/compose-rust/android-kotlin/shared/NodeStyle.kt @@ -97,12 +97,12 @@ internal fun Node.textStyle(theme: ResolvedTheme, defaultRole: TypeRole = TypeRo internal fun Node.maxLines(): Int = intProp(PropertyKind.MaxLines)?.toInt() ?: Int.MAX_VALUE -internal fun Node.overflow(): ComposeOverflow = +internal fun Node.overflow(default: ComposeOverflow = ComposeOverflow.Clip): ComposeOverflow = when (role(PropertyKind.Overflow, TextOverflow.entries.toTypedArray())) { TextOverflow.Clip -> ComposeOverflow.Clip TextOverflow.Ellipsis -> ComposeOverflow.Ellipsis TextOverflow.Visible -> ComposeOverflow.Visible - null -> ComposeOverflow.Clip + null -> default } /** `spacing` is a literal dp, `space_role` goes through the table; the literal wins. */ diff --git a/renderer/desktop/src/renderer/HostButton.kt b/renderer/desktop/src/renderer/HostButton.kt index db0326cbf..543f7f37e 100644 --- a/renderer/desktop/src/renderer/HostButton.kt +++ b/renderer/desktop/src/renderer/HostButton.kt @@ -29,6 +29,7 @@ import androidx.compose.ui.unit.Dp import androidx.compose.ui.semantics.Role import androidx.compose.ui.semantics.contentDescription import androidx.compose.ui.semantics.semantics +import androidx.compose.ui.text.style.TextOverflow as ComposeOverflow import dev.darkpyonix.composerust.protocol.ButtonKind import dev.darkpyonix.composerust.protocol.ButtonVariant import dev.darkpyonix.composerust.protocol.HostEvent @@ -39,6 +40,8 @@ import dev.darkpyonix.composerust.design.ButtonStyle import dev.darkpyonix.composerust.design.ResolvedTheme import dev.darkpyonix.composerust.runtime.EventDispatcher import dev.darkpyonix.composerust.ui.node.Node +import dev.darkpyonix.composerust.ui.intProp +import dev.darkpyonix.composerust.ui.overflow import dev.darkpyonix.composerust.ui.paintProp import dev.darkpyonix.composerust.ui.role import dev.darkpyonix.composerust.ui.textStyle @@ -149,6 +152,11 @@ internal fun HostButton( val label = node.textStyle(theme, style.typeRole).copy(color = tint) val text = node.text(PropertyKind.Text) val icon = node.role(PropertyKind.Icon, IconRole.entries.toTypedArray()) + // A label is one line unless the Host asked for more. Left to wrap, a squeezed button + // is measured at the narrowest a Hangul label can be, one syllable, and '보내' breaks + // into '보' over '내'. With no soft wrap the label's narrowest width is its whole line. + val lines = node.intProp(PropertyKind.MaxLines)?.toInt()?.takeIf { it > 0 } ?: 1 + val ellipsis = node.overflow(ComposeOverflow.Ellipsis) // A button with a glyph and no word is still a button with a name. The role is what // the glyph means, so it is what the name is made of, and assistive technology never // meets an unnamed control. @@ -159,7 +167,7 @@ internal fun HostButton( } Box(named, contentAlignment = Alignment.Center) { if (icon == null) { - BasicText(text = text, style = label) + BasicText(text = text, style = label, maxLines = lines, softWrap = lines > 1, overflow = ellipsis) } else { Row( // The gap between the glyph and the word is the design system's smallest step, @@ -168,7 +176,7 @@ internal fun HostButton( verticalAlignment = Alignment.CenterVertically, ) { RoleIcon(icon, tint, theme) - if (text.isNotEmpty()) BasicText(text = text, style = label) + if (text.isNotEmpty()) BasicText(text = text, style = label, maxLines = lines, softWrap = lines > 1, overflow = ellipsis) } } } diff --git a/renderer/desktop/src/renderer/HostChip.kt b/renderer/desktop/src/renderer/HostChip.kt index ce1a2568c..aae8fedf0 100644 --- a/renderer/desktop/src/renderer/HostChip.kt +++ b/renderer/desktop/src/renderer/HostChip.kt @@ -20,6 +20,7 @@ import androidx.compose.ui.draw.clip import androidx.compose.ui.semantics.Role import androidx.compose.ui.semantics.selected import androidx.compose.ui.semantics.semantics +import androidx.compose.ui.text.style.TextOverflow import dev.darkpyonix.composerust.design.ChipStyle import dev.darkpyonix.composerust.design.ResolvedTheme import dev.darkpyonix.composerust.protocol.HostEvent @@ -98,7 +99,15 @@ internal fun HostChip( val leading = if (chosen && style.leadingCheck) IconRole.Check else icon if (leading != null) RoleIcon(leading, content, theme) if (label.isNotEmpty()) { - BasicText(text = label, style = node.textStyle(theme, style.typeRole).copy(color = content)) + // One line, never wrapped: a wrapped chip label is measured at one syllable of + // Hangul and its second line is clipped by the chip's height. + BasicText( + text = label, + style = node.textStyle(theme, style.typeRole).copy(color = content), + maxLines = 1, + softWrap = false, + overflow = TextOverflow.Ellipsis, + ) } } } diff --git a/renderer/desktop/src/renderer/NodeStyle.kt b/renderer/desktop/src/renderer/NodeStyle.kt index 3ef6b9ca3..77b32dbcd 100644 --- a/renderer/desktop/src/renderer/NodeStyle.kt +++ b/renderer/desktop/src/renderer/NodeStyle.kt @@ -97,12 +97,12 @@ internal fun Node.textStyle(theme: ResolvedTheme, defaultRole: TypeRole = TypeRo internal fun Node.maxLines(): Int = intProp(PropertyKind.MaxLines)?.toInt() ?: Int.MAX_VALUE -internal fun Node.overflow(): ComposeOverflow = +internal fun Node.overflow(default: ComposeOverflow = ComposeOverflow.Clip): ComposeOverflow = when (role(PropertyKind.Overflow, TextOverflow.entries.toTypedArray())) { TextOverflow.Clip -> ComposeOverflow.Clip TextOverflow.Ellipsis -> ComposeOverflow.Ellipsis TextOverflow.Visible -> ComposeOverflow.Visible - null -> ComposeOverflow.Clip + null -> default } /** `spacing` is a literal dp, `space_role` goes through the table; the literal wins. */ diff --git a/renderer/desktop/test/SingleLineLabelTest.kt b/renderer/desktop/test/SingleLineLabelTest.kt new file mode 100644 index 000000000..e48ef4d2b --- /dev/null +++ b/renderer/desktop/test/SingleLineLabelTest.kt @@ -0,0 +1,68 @@ +package dev.darkpyonix.composerust.test + +import androidx.compose.ui.test.ExperimentalTestApi +import androidx.compose.ui.test.getUnclippedBoundsInRoot +import androidx.compose.ui.test.onNodeWithText +import androidx.compose.ui.test.runComposeUiTest +import androidx.compose.ui.unit.DpRect +import dev.darkpyonix.composerust.protocol.ColorScheme +import dev.darkpyonix.composerust.protocol.DesignSystem +import dev.darkpyonix.composerust.protocol.Modifier as ProtocolModifier +import dev.darkpyonix.composerust.protocol.Mutation +import dev.darkpyonix.composerust.protocol.PropertyKind +import dev.darkpyonix.composerust.protocol.PropertyValue +import dev.darkpyonix.composerust.protocol.Theme +import dev.darkpyonix.composerust.protocol.WidgetKind +import dev.darkpyonix.composerust.runtime.ComposeRustContent +import dev.darkpyonix.composerust.runtime.rememberComposeRustHost +import dev.darkpyonix.composerust.tooling.FakeHostConnection +import kotlin.test.Test +import kotlin.test.assertEquals + +private const val PARENT = 1 +private const val CONTROL = 2 + +/** + * A Button or Chip label is one line. Squeezed into a parent narrower than the label, the + * control used to be measured at the narrowest a Hangul label can be (one syllable) and + * '보내' wrapped to '보' over '내' (compose-rust#328). The label is now one line high + * however little room the parent offers (cut with an ellipsis, never wrapped). + */ +@OptIn(ExperimentalTestApi::class) +class SingleLineLabelTest { + + private fun tree(system: DesignSystem, kind: WidgetKind, label: String, parentWidth: Float) = listOf( + Mutation.SetTheme(Theme(system, system, ColorScheme.Light, false)), + Mutation.Create(PARENT, WidgetKind.Box), + Mutation.SetModifier(PARENT, 0, ProtocolModifier.Width(parentWidth)), + Mutation.Create(CONTROL, kind), + Mutation.SetProp(CONTROL, PropertyKind.Text, PropertyValue.Text(label)), + Mutation.Insert(PARENT, CONTROL, 0), + ) + + private fun labelBounds(system: DesignSystem, kind: WidgetKind, label: String, parentWidth: Float): DpRect { + var bounds: DpRect? = null + runComposeUiTest { + setContent { + ComposeRustContent( + rememberComposeRustHost(FakeHostConnection(tree(system, kind, label, parentWidth))), + ) + } + waitForIdle() + bounds = onNodeWithText(label, useUnmergedTree = true).getUnclippedBoundsInRoot() + } + return bounds!! + } + + @Test + fun fr13_button_and_chip_labels_do_not_wrap_when_the_parent_is_narrow() { + listOf("보내" to WidgetKind.Button, "명령마다 확인" to WidgetKind.Chip).forEach { (label, kind) -> + DesignSystem.entries.forEach { system -> + val roomy = labelBounds(system, kind, label, 600f) + val squeezed = labelBounds(system, kind, label, 12f) + val single = roomy.bottom - roomy.top + assertEquals(single.value, (squeezed.bottom - squeezed.top).value, 0.5f, "$system $kind '$label' stays on one line") + } + } + } +}