Modern Apps git

← Modern-Apps.git

ui: category picker opener is a named button, not an unnamed toggle

Commit
1053877d8479c3b8e80e1882d7983f828a9a6ccb
Author
Vayun Mathur <[email protected]>
When
2026-11-21 12:20 UTC (15 days ago)
Parents
7b1bd8b
MultiCategoryPicker's opener used toggleable with no role,
so TalkBack announced an unnamed switch. It's a dropdown
opener: now a clickable with Role.Button, the field label as
its content description, and Expanded/Collapsed on
stateDescription (this Compose version has no Expanded
semantics key; sliders here already use stateDescription).
The onAddClick variant's opener gets the same naming. Chips
keep their own remove semantics; both app callers compile
unchanged.

Verified: library:ui 1085 tests pass incl. 3 new semantics
tests (no toggleable nodes remain in the field).

Diff

library/ui/src/main/java/com/vayunmathur/library/ui/MultiCategoryPicker.kt
diff --git a/library/ui/src/main/java/com/vayunmathur/library/ui/MultiCategoryPicker.kt b/library/ui/src/main/java/com/vayunmathur/library/ui/MultiCategoryPicker.ktindex 8375323ba3..fd2e5a45b8 100644--- a/library/ui/src/main/java/com/vayunmathur/library/ui/MultiCategoryPicker.kt+++ b/library/ui/src/main/java/com/vayunmathur/library/ui/MultiCategoryPicker.kt@@ -8,13 +8,16 @@ import androidx.compose.foundation.layout.FlowRow import androidx.compose.foundation.layout.PaddingValues import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.size-import androidx.compose.foundation.selection.toggleable import androidx.compose.runtime.Composable import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.setValue import androidx.compose.ui.Modifier+import androidx.compose.ui.semantics.Role+import androidx.compose.ui.semantics.contentDescription+import androidx.compose.ui.semantics.semantics+import androidx.compose.ui.semantics.stateDescription import androidx.compose.ui.text.input.VisualTransformation import androidx.compose.ui.unit.dp @@ -30,6 +33,13 @@ import androidx.compose.ui.unit.dp  * Tapping anywhere on the field opens the menu, via an overlay that covers it. That is the point of the  * component: a plain read-only text field still raises the IME when focused, which is wrong for a value  * that cannot be typed.+ *+ * Accessibility: the overlay is a [Role.Button], not a toggleable — opening a menu is not switching+ * something on, so a Switch/Checkbox role would mislabel it. It carries [label] as its content+ * description and the menu state as its state description ("Expanded"/"Collapsed"; this Compose+ * version has no dedicated expanded semantics property), so TalkBack announces e.g. "Tags,+ * Collapsed, Button". The overlay has no children of its own, so the chips keep their own remove+ * semantics instead of being merged into the opener.  */ @Composable fun <T> MultiCategoryPicker(@@ -53,12 +63,16 @@ fun <T> MultiCategoryPicker(             Box(                 Modifier                     .matchParentSize()-                    .toggleable(-                        value = expanded,-                        onValueChange = { expanded = it },+                    .clickable(                         interactionSource = interactionSource,                         indication = null,+                        role = Role.Button,+                        onClick = { expanded = !expanded },                     )+                    .semantics {+                        contentDescription = label+                        stateDescription = if (expanded) "Expanded" else "Collapsed"+                    }             )         }         CategoryChipField(@@ -108,14 +122,18 @@ fun <T> MultiCategoryPicker(     Box(modifier.fillMaxWidth()) {         // Below the chips so chip remove taps (drawn later, hit-tested first) win,         // while taps on empty field area fall through to this overlay and open the picker.+        // Named after the field, like the menu overload's opener, so TalkBack does not+        // land on an unnamed button; there is no expanded state to report here.         Box(             Modifier                 .matchParentSize()                 .clickable(                     interactionSource = interactionSource,                     indication = null,+                    role = Role.Button,                     onClick = onAddClick,                 )+                .semantics { contentDescription = label }         )         CategoryChipField(             label = label,
library/ui/src/test/java/com/vayunmathur/library/ui/A11ySemanticsSliceTest.kt
diff --git a/library/ui/src/test/java/com/vayunmathur/library/ui/A11ySemanticsSliceTest.kt b/library/ui/src/test/java/com/vayunmathur/library/ui/A11ySemanticsSliceTest.ktindex 027d064926..9aac2aa84d 100644--- a/library/ui/src/test/java/com/vayunmathur/library/ui/A11ySemanticsSliceTest.kt+++ b/library/ui/src/test/java/com/vayunmathur/library/ui/A11ySemanticsSliceTest.kt@@ -11,14 +11,18 @@ import androidx.compose.ui.Modifier import androidx.compose.ui.platform.testTag import androidx.compose.ui.semantics.CustomAccessibilityAction import androidx.compose.ui.semantics.LiveRegionMode+import androidx.compose.ui.semantics.Role import androidx.compose.ui.semantics.SemanticsActions import androidx.compose.ui.semantics.SemanticsProperties import androidx.compose.ui.test.SemanticsMatcher import androidx.compose.ui.test.assert import androidx.compose.ui.test.assertIsDisplayed import androidx.compose.ui.test.assertIsFocused+import androidx.compose.ui.test.hasContentDescription+import androidx.compose.ui.test.isToggleable import androidx.compose.ui.test.junit4.createComposeRule import androidx.compose.ui.test.onNodeWithText+import androidx.compose.ui.test.performClick import androidx.compose.ui.unit.dp import kotlin.test.Test import kotlin.test.assertEquals@@ -162,4 +166,81 @@ class A11ySemanticsSliceTest {         ).fetchSemanticsNodes()         assertTrue(withActions.isEmpty())     }++    @Test fun multiCategoryPickerOpenerIsANamedCollapsedButtonNotAToggle() {+        rule.setContent {+            MultiCategoryPicker(+                label = "Tags",+                selected = listOf("a"),+                available = listOf("a", "b"),+                itemLabel = { it },+                onAdd = {},+                onRemove = {},+            )+        }+        // The opener is the one node named after the field: a Button whose+        // state description carries the collapsed/expanded state. Before the+        // fix it was an unnamed ToggleableState=Off toggleable, which TalkBack+        // mislabels as a switch/checkbox.+        val opener = rule.onNode(hasContentDescription("Tags")).fetchSemanticsNode()+        assertEquals(Role.Button, opener.config[SemanticsProperties.Role])+        assertEquals("Collapsed", opener.config[SemanticsProperties.StateDescription])+        assertTrue(+            SemanticsProperties.ToggleableState !in opener.config,+            "opener must not be toggleable: ${opener.config}",+        )+        assertTrue(SemanticsActions.OnClick in opener.config, "opener must be clickable")+        // Nothing in the whole field poses as a toggle any more.+        val toggles = rule.onAllNodes(isToggleable()).fetchSemanticsNodes()+        assertTrue(toggles.isEmpty(), "unexpected toggleables: $toggles")+    }++    @Test fun multiCategoryPickerOpenerExpandsOnClickAndPicks() {+        var added: String? = null+        rule.setContent {+            MultiCategoryPicker(+                label = "Tags",+                selected = emptyList(),+                available = listOf("a", "b"),+                itemLabel = { it },+                onAdd = { added = it },+                onRemove = {},+            )+        }+        rule.onNode(hasContentDescription("Tags")).performClick()+        // While the menu is open the opener itself reports Expanded, so the+        // state change is announced on the control that caused it.+        val open = rule.onNode(hasContentDescription("Tags")).fetchSemanticsNode()+        assertEquals("Expanded", open.config[SemanticsProperties.StateDescription])+        rule.onNodeWithText("b").performClick()+        assertEquals("b", added)+        // Choosing closes the menu, so the opener collapses again.+        val closed = rule.onNode(hasContentDescription("Tags")).fetchSemanticsNode()+        assertEquals("Collapsed", closed.config[SemanticsProperties.StateDescription])+    }++    @Test fun multiCategoryPickerChipsKeepTheirOwnRemoveSemantics() {+        var removed: String? = null+        rule.setContent {+            MultiCategoryPicker(+                label = "Tags",+                selected = listOf("a"),+                available = listOf("a", "b"),+                itemLabel = { it },+                onAdd = {},+                onRemove = { removed = it },+            )+        }+        // The chip is its own node, not merged into the opener: the opener+        // carries no chip text, and tapping the chip removes it.+        val opener = rule.onNode(hasContentDescription("Tags")).fetchSemanticsNode()+        val openerTexts = if (SemanticsProperties.Text in opener.config) {+            opener.config[SemanticsProperties.Text].map { it.text }+        } else {+            emptyList()+        }+        assertTrue("a" !in openerTexts, "chip leaked into opener node: $openerTexts")+        rule.onNodeWithText("a").performClick()+        assertEquals("a", removed)+    } }