a11y: settings switch rows announce label and state together
- Commit
e0a5b499fa6a79a8253972c369ad325715f3ac59- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-20 21:40 UTC (15 days ago)
- Parents
5401cfc
SettingsSwitchRow rendered an unmerged Row { Text; Switch },
so TalkBack focused the switch alone ('Switch, on') without
its label. The row now carries Modifier.toggleable(role =
Switch) with merged semantics - one node speaks title,
supporting text and state. The trailing Switch is visual-only
(onCheckedChange = null), so label and switch taps each fire
exactly one toggle - proven by a switch-area touch test.
Central fix in the shared component: astronomy (4 rows), maps
settings (4) and maps' layers sheet pick it up without per-app
changes; no hand-rolled switches exist in those screens.
Verified: :library:ui 1062 tests, astronomy 126, maps compile.Diff
library/ui/src/main/java/com/vayunmathur/library/ui/SettingsComponents.kt
diff --git a/library/ui/src/main/java/com/vayunmathur/library/ui/SettingsComponents.kt b/library/ui/src/main/java/com/vayunmathur/library/ui/SettingsComponents.ktindex 6d1ee21a29..233bc2ab3e 100644--- a/library/ui/src/main/java/com/vayunmathur/library/ui/SettingsComponents.kt+++ b/library/ui/src/main/java/com/vayunmathur/library/ui/SettingsComponents.kt@@ -5,6 +5,7 @@ import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.padding+import androidx.compose.foundation.selection.toggleable import androidx.compose.material3.HorizontalDivider import androidx.compose.material3.ListItemDefaults import androidx.compose.material3.MaterialTheme@@ -17,6 +18,7 @@ import androidx.compose.runtime.remember import androidx.compose.runtime.setValue import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color+import androidx.compose.ui.semantics.Role import androidx.compose.ui.unit.dp import com.vayunmathur.library.util.sharedText @@ -114,6 +116,16 @@ fun SettingsRow( * * The whole row toggles, not just the switch - a switch is a small target and * every app that hand-rolled this made a different call about it.+ *+ * Accessibility: the row itself is the single [Role.Switch] toggleable. Its+ * `toggleable` semantics merge descendants, so the title (and supporting+ * text) and the checked state live on one semantics node and TalkBack+ * announces e.g. "Night mode, Switch, on" as one item instead of focusing+ * the bare switch and reading only "Switch, on". The trailing [Switch] is+ * visual only (`onCheckedChange = null`): it contributes no toggleable or+ * click handler of its own, so a tap anywhere on the row โ the label area+ * or the switch thumb/track โ fires [onCheckedChange] exactly once and+ * cannot double-toggle. */ @Composable fun SettingsSwitchRow(@@ -127,13 +139,18 @@ fun SettingsSwitchRow( ) { SettingsRow( title = title,- modifier = modifier,+ modifier = modifier.toggleable(+ value = checked,+ enabled = enabled,+ role = Role.Switch,+ onValueChange = onCheckedChange,+ ), supportingText = supportingText, enabled = enabled,- onClick = { onCheckedChange(!checked) },+ onClick = null, leadingContent = leadingContent, trailingContent = {- Switch(checked = checked, onCheckedChange = onCheckedChange, enabled = enabled)+ Switch(checked = checked, onCheckedChange = null, enabled = enabled) }, ) }
library/ui/src/test/java/com/vayunmathur/library/ui/Slice2FormSettingsTest.kt
diff --git a/library/ui/src/test/java/com/vayunmathur/library/ui/Slice2FormSettingsTest.kt b/library/ui/src/test/java/com/vayunmathur/library/ui/Slice2FormSettingsTest.ktindex 5d4d39f745..38a107cd0d 100644--- a/library/ui/src/test/java/com/vayunmathur/library/ui/Slice2FormSettingsTest.kt+++ b/library/ui/src/test/java/com/vayunmathur/library/ui/Slice2FormSettingsTest.kt@@ -2,14 +2,21 @@ package com.vayunmathur.library.ui import androidx.compose.ui.test.assertIsDisplayed import androidx.compose.ui.test.assertIsNotEnabled+import androidx.compose.ui.geometry.Offset+import androidx.compose.ui.semantics.Role+import androidx.compose.ui.semantics.SemanticsProperties+import androidx.compose.ui.state.ToggleableState import androidx.compose.ui.test.hasContentDescription import androidx.compose.ui.test.hasText+import androidx.compose.ui.test.isToggleable import androidx.compose.ui.test.junit4.createComposeRule import androidx.compose.ui.test.onNodeWithContentDescription import androidx.compose.ui.test.onNodeWithText import androidx.compose.ui.test.performClick import androidx.compose.ui.test.performTextInput import androidx.compose.ui.test.performTextReplacement+import androidx.compose.ui.test.click+import androidx.compose.ui.test.performTouchInput import org.junit.Rule import org.junit.runner.RunWith import org.robolectric.RobolectricTestRunner@@ -238,6 +245,52 @@ class Slice2FormSettingsTest { assertEquals(listOf(true), seen) } + @Test+ fun `switch row is one merged switch node carrying label and state`() {+ rule.setContent {+ SettingsSwitchRow(+ title = "Slice2 Toggle",+ supportingText = "slice2 toggle hint",+ checked = true,+ onCheckedChange = {},+ )+ }+ // Exactly one toggleable in the (merged) tree: the row, not a bare+ // switch next to a separate label. That single node must carry the+ // label text, the Switch role and the on state together, which is+ // what TalkBack announces as one item ("Slice2 Toggle, Switch, on").+ val toggleables = rule.onAllNodes(isToggleable()).fetchSemanticsNodes()+ assertEquals(1, toggleables.size)+ val node = toggleables.single()+ assertEquals(Role.Switch, node.config[SemanticsProperties.Role])+ assertEquals(ToggleableState.On, node.config[SemanticsProperties.ToggleableState])+ val texts = node.config[SemanticsProperties.Text].map { it.text }+ assertTrue("Slice2 Toggle" in texts, "merged node texts: $texts")+ assertTrue("slice2 toggle hint" in texts, "merged node texts: $texts")+ // The same node is findable by label + toggleable together.+ rule.onNode(hasText("Slice2 Toggle") and isToggleable()).assertIsDisplayed()+ }++ @Test+ fun `switch row switch-area tap toggles exactly once`() {+ val seen = mutableListOf<Boolean>()+ rule.setContent {+ SettingsSwitchRow(title = "Slice2 Toggle", checked = false, onCheckedChange = seen::add)+ }+ // Tap the trailing switch area (right end of the row), not the label:+ // the visual switch has no handler of its own, so this must fire the+ // row toggle once โ a double-toggle would append two entries.+ val row = rule.onNode(hasText("Slice2 Toggle") and isToggleable())+ val bounds = row.fetchSemanticsNode().boundsInRoot+ row.performTouchInput {+ click(Offset(bounds.width - 24f, bounds.height / 2f))+ }+ assertEquals(listOf(true), seen)+ // And a label-area tap likewise fires exactly once more.+ rule.onNodeWithText("Slice2 Toggle").performClick()+ assertEquals(listOf(true, true), seen)+ }+ // --- SettingsSelectRow -------------------------------------------------------- @Test
library/ui/src/test/java/com/vayunmathur/library/ui/Slice3Round14Test.kt
diff --git a/library/ui/src/test/java/com/vayunmathur/library/ui/Slice3Round14Test.kt b/library/ui/src/test/java/com/vayunmathur/library/ui/Slice3Round14Test.ktindex b4f40b65e0..428245e24f 100644--- a/library/ui/src/test/java/com/vayunmathur/library/ui/Slice3Round14Test.kt+++ b/library/ui/src/test/java/com/vayunmathur/library/ui/Slice3Round14Test.kt@@ -209,11 +209,10 @@ class Slice3Round14Test { ) } rule.onNodeWithText("21:05").assertIsDisplayed()- // The switch itself carries the toggle semantics.+ // The row itself is the merged Switch toggleable carrying its label. rule.onNode(- androidx.compose.ui.test.hasAnyAncestor(- androidx.compose.ui.test.hasText("Daily puzzle reminder"),- ) and androidx.compose.ui.test.isToggleable(),+ androidx.compose.ui.test.hasText("Daily puzzle reminder") and+ androidx.compose.ui.test.isToggleable(), ).performClick() assertEquals(listOf(true), toggles) }