Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion colorpicker-foundation/api/colorpicker-foundation.klib.api

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion colorpicker-foundation/api/jvm/colorpicker-foundation.api

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import androidx.compose.ui.semantics.semantics
*
* @param onRestoreOriginal called when the original half is pressed, which makes that half a button that puts
* the original back; `null` leaves it a swatch.
* @param enabled when false the original half, when it is a button, is a disabled one: pressing it restores
* nothing, it takes no focus, and a screen reader hears that it is disabled.
* @param originalLabel what a screen reader calls the original half, "Original color" by default; `null` says
* nothing.
* @param currentLabel what a screen reader calls the current half, "New color" by default; `null` says
Expand All @@ -31,6 +33,7 @@ public fun BasicColorComparison(
current: Color,
onRestoreOriginal: (() -> Unit)?,
modifier: Modifier = Modifier,
enabled: Boolean = true,
checkerboardLight: Color = Color.White,
checkerboardDark: Color = Color.LightGray,
originalLabel: String? = ColorPickerStrings.current.originalColor(),
Expand All @@ -42,7 +45,7 @@ public fun BasicColorComparison(
// as in a scrolling column, is one fillMaxHeight cannot fill.
Row(Modifier.matchParentSize()) {
val restore = if (onRestoreOriginal != null) {
Modifier.clickable(onClickLabel = restoreLabel, role = Role.Button, onClick = onRestoreOriginal)
Modifier.clickable(enabled = enabled, onClickLabel = restoreLabel, role = Role.Button, onClick = onRestoreOriginal)
} else {
Modifier
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package codes.side.colorpicker.foundation

import androidx.compose.foundation.focusGroup
import androidx.compose.foundation.gestures.Orientation
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
Expand All @@ -9,12 +10,15 @@ import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.runtime.Composable
import androidx.compose.runtime.SideEffect
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.snapshots.Snapshot
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.onFocusChanged
import androidx.compose.ui.layout.SubcomposeLayout
import androidx.compose.ui.platform.LocalFocusManager
import androidx.compose.ui.semantics.clearAndSetSemantics
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
Expand Down Expand Up @@ -69,6 +73,13 @@ public fun BasicColorPickerDialogContent(
// reads to keep out of the semantics tree: a composed node that is never placed is still in it, and a
// screen reader would find every slider twice.
val sideBySide = remember { mutableStateOf(false) }
// Whether a part of the stacked form holds keyboard focus. Laid out side by side, that part is not placed and
// nothing shows where focus is, so it is let go.
val stackedHasFocus = remember { mutableStateOf(false) }
if (sideBySide.value && stackedHasFocus.value) {
val focusManager = LocalFocusManager.current
SideEffect { focusManager.clearFocus(force = true) }
}
val stackedScroll = rememberScrollState()
val sideBySideScroll = rememberScrollState()
// Built here rather than in the measure block, so every measure pass hands SubcomposeLayout the same content,
Expand All @@ -77,6 +88,8 @@ public fun BasicColorPickerDialogContent(
StackedParts(
modifier = Modifier
.then(if (sideBySide.value) Modifier.clearAndSetSemantics {} else Modifier)
.onFocusChanged { stackedHasFocus.value = it.hasFocus }
.focusGroup()
.verticalScroll(stackedScroll),
header = header,
spaceSwitcher = spaceSwitcher,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public class ColorPickerDialogState internal constructor(
* @param spaces the spaces the dialog offers, in the order a switcher lists them.
* @param initialSpace [initialValue]'s own space when [spaces] holds it, else the first of [spaces].
* @throws IllegalArgumentException if [spaces] lists a space twice or does not hold [initialSpace].
* @throws NoSuchElementException if [spaces] is empty and [initialSpace] is left to its default.
*/
public constructor(
initialValue: ColorValue,
Expand Down Expand Up @@ -105,6 +106,9 @@ public class ColorPickerDialogState internal constructor(
* them. A different [initialValue] or [spaces] starts the state over.
*
* The saver knows every space in [spaces] and [initialValue]'s own.
*
* @throws IllegalArgumentException if [spaces] lists a space twice or does not hold [initialSpace].
* @throws NoSuchElementException if [spaces] is empty and [initialSpace] is left to its default.
*/
@Composable
public fun rememberColorPickerDialogState(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import androidx.compose.ui.test.ExperimentalTestApi
import androidx.compose.ui.test.SemanticsMatcher
import androidx.compose.ui.test.assert
import androidx.compose.ui.test.assertHeightIsEqualTo
import androidx.compose.ui.test.assertIsNotEnabled
import androidx.compose.ui.test.captureToImage
import androidx.compose.ui.test.onNodeWithContentDescription
import androidx.compose.ui.test.onNodeWithTag
Expand Down Expand Up @@ -54,6 +55,20 @@ class BasicColorComparisonTest {
assertEquals(1, restored)
}

@Test
fun aDisabledRestoreIsAButtonThatRestoresNothing() = runComposeUiTest {
var restored = 0
setContent {
BasicColorComparison(Color.Red, Color.Blue, onRestoreOriginal = { restored++ }, Modifier.size(96.dp, 48.dp), enabled = false)
}
onNodeWithContentDescription("Original color")
.assert(SemanticsMatcher.expectValue(SemanticsProperties.Role, Role.Button))
.assertIsNotEnabled()
.assert(SemanticsMatcher.keyNotDefined(SemanticsActions.RequestFocus))
.performClick()
assertEquals(0, restored)
}

@Test
fun withNothingToRestoreTheOriginalHalfIsNoButton() = runComposeUiTest {
setContent { BasicColorComparison(Color.Red, Color.Blue, onRestoreOriginal = null, Modifier.size(96.dp, 48.dp)) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import androidx.compose.ui.test.SemanticsMatcher
import androidx.compose.ui.test.assert
import androidx.compose.ui.test.assertCountEquals
import androidx.compose.ui.test.assertHeightIsEqualTo
import androidx.compose.ui.test.assertIsFocused
import androidx.compose.ui.test.assertWidthIsEqualTo
import androidx.compose.ui.test.getBoundsInRoot
import androidx.compose.ui.test.onAllNodesWithTag
Expand All @@ -43,6 +44,7 @@ import codes.side.color.Srgb
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertTrue

@OptIn(ExperimentalTestApi::class)
class BasicColorPickerDialogContentTest {
Expand Down Expand Up @@ -194,6 +196,44 @@ class BasicColorPickerDialogContentTest {
assertFalse(hiddenFocused, "focus reached the stacked form, which is not shown")
}

@Test
fun theStackedFormLetsGoOfFocusWhenItStopsBeingShown() = runComposeUiTest {
var tall by mutableStateOf(false)
var stackedFocused = false
setContent {
Content(width = 600.dp, height = 300.dp) { orientation ->
val stacked = orientation == Orientation.Vertical
Box(
Modifier
.testTag("picker $orientation")
.fillMaxWidth()
.height(if (!stacked) 200.dp else if (tall) 400.dp else 100.dp)
.onFocusChanged { if (stacked) stackedFocused = it.isFocused }
.focusable(),
)
}
}
onNodeWithTag("picker ${Orientation.Vertical}").requestFocus()
assertTrue(stackedFocused)
tall = true
waitForIdle()
onNodeWithTag("picker ${Orientation.Horizontal}").assertExists()
assertFalse(stackedFocused, "the stacked form, no longer shown, still holds focus")
}

@Test
fun focusInTheFormShownIsKept() = runComposeUiTest {
setContent {
Content(width = 600.dp, height = 300.dp) { orientation ->
val height = if (orientation == Orientation.Vertical) 400.dp else 200.dp
Box(Modifier.testTag("picker $orientation").fillMaxWidth().height(height).focusable())
}
}
onNodeWithTag("picker ${Orientation.Horizontal}").requestFocus()
waitForIdle()
onNodeWithTag("picker ${Orientation.Horizontal}").assertIsFocused()
}

@Test
fun itStacksAgainOnceThatFits() = runComposeUiTest {
var tall by mutableStateOf(true)
Expand Down
Loading
Loading