fix(ui): restore consistent focus glide transitions (#1588)

This commit is contained in:
Kurt Himebauch authored and GitHub committed 2026-10-25 23:20:57 -04:00
1 parent 5978a13f09
commit 3fd6b1f00d
47 files changed
+705 -183

No files matched your search

@@ -0,0 +1,121 @@
package com.droiddeck.launcher.ui
import android.graphics.Bitmap
import android.graphics.Canvas
import android.view.View
import androidx.compose.foundation.background
import androidx.compose.foundation.focusable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.size
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.focus.focusRequester
import androidx.compose.ui.geometry.Rect
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.RectangleShape
import androidx.compose.ui.layout.boundsInRoot
import androidx.compose.ui.layout.onGloballyPositioned
import androidx.compose.ui.platform.LocalView
import androidx.compose.ui.unit.dp
import com.droiddeck.launcher.session.SessionPrefs
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.RuntimeEnvironment
import org.robolectric.annotation.Config
import org.robolectric.annotation.GraphicsMode
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [28], qualifiers = "w960dp-h540dp-land")
@GraphicsMode(GraphicsMode.Mode.NATIVE)
class FocusGlideRenderingTest {
@get:Rule val compose = createRobolectricComposeRule()
@Test fun largeJumpCollapsesIntoABallAndExpandsAtTheDestination() {
val context = RuntimeEnvironment.getApplication()
SessionPrefs.setAnimationsEnabled(context, true)
Motion.refresh(context)
compose.mainClock.autoAdvance = false
lateinit var view: View
val from = FocusRequester()
val to = FocusRequester()
compose.setContent {
view = LocalView.current
FocusGlideHost(Modifier.fillMaxSize().background(Color.Black)) {
Box(Modifier.offset(10.dp, 10.dp).size(80.dp, 40.dp).focusRequester(from)
.focusGlide(RectangleShape, Color.White).focusable())
Box(Modifier.offset(650.dp, 400.dp).size(80.dp, 40.dp).focusRequester(to)
.focusGlide(RectangleShape, Color.White).focusable())
}
}
fun ringBounds(): Rect = compose.runOnIdle {
val bitmap = Bitmap.createBitmap(view.width, view.height, Bitmap.Config.ARGB_8888)
view.draw(Canvas(bitmap))
var left = view.width
var top = view.height
var right = 0
var bottom = 0
for (y in 0 until bitmap.height) for (x in 0 until bitmap.width) {
if (android.graphics.Color.red(bitmap.getPixel(x, y)) > 200) {
left = minOf(left, x); top = minOf(top, y)
right = maxOf(right, x + 1); bottom = maxOf(bottom, y + 1)
}
}
bitmap.recycle()
Rect(left.toFloat(), top.toFloat(), right.toFloat(), bottom.toFloat())
}
compose.runOnIdle { from.requestFocus() }
compose.mainClock.advanceTimeBy(1_000)
assertTrue(ringBounds().width > 60f)
compose.runOnIdle { to.requestFocus() }
compose.mainClock.advanceTimeBy(160)
val ball = ringBounds()
assertTrue("Ball bounds: $ball", ball.width in 8f..16f && ball.height in 8f..16f)
compose.mainClock.advanceTimeBy(1_500)
val landed = ringBounds()
assertTrue("Landed bounds: $landed", landed.width > 60f && landed.left > 600f)
}
@Test fun hollowGlideKeepsItsOutlineWithoutTintingTheContent() {
val context = RuntimeEnvironment.getApplication()
SessionPrefs.setAnimationsEnabled(context, false)
Motion.refresh(context)
try {
lateinit var view: View
var bounds = Rect.Zero
var hollow by mutableStateOf(true)
compose.setContent {
view = LocalView.current
FocusGlideHost {
Box(Modifier.size(120.dp).background(Color.Black)
.glideBorder(true, RectangleShape, Color.White, hotFillAlpha = if (hollow) 0f else 0.14f)
.onGloballyPositioned { bounds = it.boundsInRoot() })
}
}
fun sample(): Pair<Int, Int> = compose.runOnIdle {
val bitmap = Bitmap.createBitmap(view.width, view.height, Bitmap.Config.ARGB_8888)
view.draw(Canvas(bitmap))
val center = bitmap.getPixel(bounds.center.x.toInt(), bounds.center.y.toInt())
val edge = bitmap.getPixel(bounds.left.toInt(), bounds.center.y.toInt())
bitmap.recycle()
center to edge
}
val (center, edge) = sample()
assertEquals(android.graphics.Color.BLACK, center)
assertEquals(android.graphics.Color.WHITE, edge)
compose.runOnIdle { hollow = false }
assertTrue(android.graphics.Color.red(sample().first) > 0)
} finally {
SessionPrefs.setAnimationsEnabled(context, true)
Motion.refresh(context)
}
}
}
@@ -0,0 +1,152 @@
package com.droiddeck.launcher.ui
import androidx.compose.foundation.layout.Column
import androidx.compose.material3.DropdownMenuItem
import androidx.compose.material3.IconButton
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Slider
import androidx.compose.material3.Switch
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.SideEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.focus.focusRequester
import androidx.compose.ui.platform.LocalView
import com.droiddeck.launcher.stores.xbox.XboxMenuEntry
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertNotSame
import org.junit.Assert.assertSame
import org.junit.Assert.assertTrue
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [28], qualifiers = "w960dp-h540dp-land")
class FocusGlideWindowsTest {
@get:Rule val compose = createRobolectricComposeRule()
@Test fun standaloneThemedControlsShareTheGlide() {
lateinit var glide: FocusGlide
val focus = List(6) { FocusRequester() }
compose.setContent {
DroidDeckTheme {
glide = LocalFocusGlide.current!!
Column {
TextButton(onClick = {}, modifier = Modifier.focusRequester(focus[0]).focusGlide()) { Text("Action") }
IconButton(onClick = {}, modifier = Modifier.focusRequester(focus[1]).focusGlide()) { Text("Icon") }
Switch(false, {}, modifier = Modifier.focusRequester(focus[2]).focusGlide())
Slider(0.5f, {}, modifier = Modifier.focusRequester(focus[3]).focusGlide())
OutlinedTextField("Value", {}, modifier = Modifier.focusRequester(focus[4]).focusGlide())
XboxMenuEntry("Xbox action", null, focus = focus[5], onClick = {})
}
}
}
var previous: GlideSource? = null
for (requester in focus) {
compose.runOnIdle { requester.requestFocus() }
compose.runOnIdle {
val source = glide.current
assertNotNull(source)
assertNotSame(previous, source)
assertEquals(1, glide.hot.size)
assertNotNull(glide.boundsOf(source!!))
previous = source
}
}
}
@Test fun alertDialogButtonsShareTheirOwnWindowGlide() {
lateinit var parent: FocusGlide
lateinit var dialog: FocusGlide
val focus = List(2) { FocusRequester() }
compose.setContent {
DroidDeckTheme {
parent = LocalFocusGlide.current!!
GlideAlertDialog(
onDismissRequest = {},
title = { Text("Dialog") },
confirmButton = {
dialog = LocalFocusGlide.current!!
val view = LocalView.current
SideEffect { assertSame(view, dialog.view) }
TextButton(onClick = {}, modifier = Modifier.focusRequester(focus[0]).focusGlide()) { Text("Confirm") }
},
dismissButton = {
TextButton(onClick = {}, modifier = Modifier.focusRequester(focus[1]).focusGlide()) { Text("Cancel") }
},
)
}
}
assertNotSame(parent, dialog)
assertWindowHandoff(dialog, focus)
}
@Test fun customDialogsHaveAHostInsideTheirWindow() {
lateinit var parent: FocusGlide
lateinit var dialog: FocusGlide
val focus = List(2) { FocusRequester() }
compose.setContent {
DroidDeckTheme {
parent = LocalFocusGlide.current!!
GlideDialog(onDismissRequest = {}) {
dialog = LocalFocusGlide.current!!
Column {
SecondaryButton("First", modifier = Modifier.focusRequester(focus[0])) {}
SecondaryButton("Second", modifier = Modifier.focusRequester(focus[1])) {}
}
}
}
}
assertNotSame(parent, dialog)
assertWindowHandoff(dialog, focus)
}
@Test fun dropdownItemsShareTheirOwnWindowGlide() {
lateinit var parent: FocusGlide
lateinit var menu: FocusGlide
val focus = List(2) { FocusRequester() }
var expanded by mutableStateOf(true)
compose.setContent {
DroidDeckTheme {
parent = LocalFocusGlide.current!!
GlideDropdownMenu(expanded = expanded, onDismissRequest = {}) {
menu = LocalFocusGlide.current!!
for (i in focus.indices) DropdownMenuItem(
text = { Text("Item $i") }, onClick = {},
modifier = Modifier.focusRequester(focus[i]).focusGlide(),
)
}
}
}
assertNotSame(parent, menu)
assertWindowHandoff(menu, focus)
compose.runOnIdle { expanded = false }
compose.waitForIdle()
compose.runOnIdle { expanded = true }
compose.waitForIdle()
assertWindowHandoff(menu, focus)
}
private fun assertWindowHandoff(glide: FocusGlide, focus: List<FocusRequester>) {
var previous: GlideSource? = null
for (requester in focus) {
compose.runOnIdle { requester.requestFocus() }
compose.runOnIdle {
assertNotNull(glide.host)
val current = glide.current
assertNotNull(current)
assertNotSame(previous, current)
assertTrue(glide.boundsOf(current!!)!!.width > 0f)
previous = current
}
}
}
}
@@ -1,5 +1,6 @@
package com.droiddeck.launcher.ui
import androidx.compose.foundation.layout.Column
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.remember
import androidx.compose.ui.platform.LocalView
@@ -9,7 +10,9 @@ import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import androidx.compose.ui.test.performSemanticsAction
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNull
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertNotSame
import org.junit.Assert.assertSame
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
@@ -21,7 +24,7 @@ import org.robolectric.annotation.Config
class PrimaryButtonFocusContrastTest {
@get:Rule val compose = createRobolectricComposeRule()
@Test fun focusedPrimaryButtonDoesNotClaimAColorOverlayAboveItsLabel() {
@Test fun focusedPrimaryButtonClaimsTheGlideWithoutAColorOverlayAboveItsLabel() {
lateinit var glide: FocusGlide
var clicks = 0
compose.setContent {
@@ -36,11 +39,51 @@ class PrimaryButtonFocusContrastTest {
val button = compose.onNodeWithText("Play")
button.performSemanticsAction(SemanticsActions.RequestFocus)
button.assertIsFocused()
compose.runOnIdle { assertNull(glide.current) }
compose.runOnIdle {
assertNotNull(glide.current)
assertEquals(0f, glide.current!!.fillAlpha)
}
button.performClick()
compose.runOnIdle {
assertEquals(1, clicks)
assertNull(glide.current)
assertNotNull(glide.current)
assertEquals(0f, glide.current!!.fillAlpha)
}
}
@Test fun focusCanGlideIntoAndBackOutOfPrimaryButtons() {
lateinit var glide: FocusGlide
val focus = FrontFocus()
compose.setContent {
val view = LocalView.current
glide = remember(view) { FocusGlide(view) }
DroidDeckTheme {
CompositionLocalProvider(LocalFocusGlide provides glide, LocalFrontFocus provides focus) {
Column {
SecondaryButton("Tab") {}
PrimaryButton("Play Steam", main = true, large = true) {}
}
}
}
}
val tab = compose.onNodeWithText("Tab")
val play = compose.onNodeWithText("Play Steam")
tab.performSemanticsAction(SemanticsActions.RequestFocus)
tab.assertIsFocused()
val tabSource = compose.runOnIdle { glide.current!! }
compose.runOnIdle { focus.primary.requestFocus() }
play.assertIsFocused()
compose.runOnIdle {
assertNotNull(glide.current)
assertNotSame(tabSource, glide.current)
assertEquals(0f, glide.current!!.fillAlpha)
assertEquals(1, glide.hot.size)
}
tab.performSemanticsAction(SemanticsActions.RequestFocus)
tab.assertIsFocused()
compose.runOnIdle {
assertSame(tabSource, glide.current)
assertEquals(1, glide.hot.size)
}
}
}