From 623fb3fb621d1634d4133d913ce46f3fef17943b Mon Sep 17 00:00:00 2001 From: Kurt Himebauch <136133082+xXJSONDeruloXx@users.noreply.github.com> Date: Tue, 13 Oct 2026 07:01:05 -0400 Subject: [PATCH] Fix FEX dropdown scaling and Discover search focus (#681) --- .../droiddeck/launcher/ui/SettingsWidgets.kt | 10 +- .../com/droiddeck/launcher/ui/StorePage.kt | 20 +++- .../droiddeck/launcher/ui/FlathubFocusTest.kt | 107 ++++++++++++++++++ .../droiddeck/launcher/ui/PresetScaleTest.kt | 79 +++++++++++++ .../launcher/ui/RobolectricComposeRule.kt | 4 + 5 files changed, 217 insertions(+), 3 deletions(-) create mode 100644 app/src/test/java/com/droiddeck/launcher/ui/FlathubFocusTest.kt create mode 100644 app/src/test/java/com/droiddeck/launcher/ui/PresetScaleTest.kt diff --git a/app/src/main/java/com/droiddeck/launcher/ui/SettingsWidgets.kt b/app/src/main/java/com/droiddeck/launcher/ui/SettingsWidgets.kt index 9faabe7b66..9de13ee8cf 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/SettingsWidgets.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/SettingsWidgets.kt @@ -62,6 +62,7 @@ import androidx.compose.material3.Icon import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Text import androidx.compose.runtime.Composable +import androidx.compose.runtime.CompositionLocalProvider import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf @@ -98,6 +99,7 @@ import androidx.compose.ui.text.style.TextAlign import kotlin.math.roundToInt import androidx.compose.ui.input.InputMode import androidx.compose.ui.platform.LocalDensity +import androidx.compose.ui.platform.LocalConfiguration import androidx.compose.ui.platform.LocalInputModeManager import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.text.style.TextOverflow @@ -281,11 +283,16 @@ fun AnchoredMenu( android.util.Log.i("AnchoredMenu", "requested first option focus") } } - val gap = with(LocalDensity.current) { 6.dp.roundToPx() } + // Popup installs Android's window density. Keep the app scale from the anchor, including + // the matching dp window dimensions used to cap and scroll the menu. + val density = LocalDensity.current + val configuration = LocalConfiguration.current + val gap = with(density) { 6.dp.roundToPx() } val provider = remember(gap, compact) { if (compact) BelowStartProvider(gap) else BelowEndProvider(gap) } val colors = MaterialTheme.colorScheme val pal = LocalPalette.current Popup(popupPositionProvider = provider, onDismissRequest = onDismiss, properties = PopupProperties(focusable = true)) { + CompositionLocalProvider(LocalDensity provides density, LocalConfiguration provides configuration) { AnimatedVisibility( visibleState = state, enter = fadeIn(Motion.tw(180)) + scaleIn(Motion.sp(0.7f), initialScale = 0.94f, transformOrigin = TransformOrigin(if (compact) 0f else 1f, 0f)), @@ -345,6 +352,7 @@ fun AnchoredMenu( } } } + } } } diff --git a/app/src/main/java/com/droiddeck/launcher/ui/StorePage.kt b/app/src/main/java/com/droiddeck/launcher/ui/StorePage.kt index aeb1654191..495fb9ba20 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/StorePage.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/StorePage.kt @@ -70,6 +70,7 @@ import androidx.compose.ui.draw.clip import androidx.compose.ui.draw.clipToBounds import androidx.compose.ui.draw.drawBehind import androidx.compose.ui.focus.focusRequester +import androidx.compose.ui.focus.focusProperties import androidx.compose.ui.geometry.Offset import androidx.compose.ui.geometry.Rect import androidx.compose.ui.graphics.Brush @@ -85,7 +86,10 @@ import androidx.compose.ui.layout.onSizeChanged import androidx.compose.ui.layout.positionInRoot import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.LocalDensity +import androidx.compose.ui.platform.LocalFocusManager import androidx.compose.ui.platform.LocalInputModeManager +import androidx.compose.ui.platform.LocalSoftwareKeyboardController +import androidx.compose.ui.platform.LocalView import androidx.compose.ui.res.stringResource import androidx.compose.ui.semantics.Role import androidx.compose.ui.semantics.contentDescription @@ -267,8 +271,9 @@ private fun SetupButton(s: FrontEndState, busy: Boolean, full: Dp, ring: Int, on // ---- storefront ------------------------------------------------------------------------------------ +@OptIn(androidx.compose.ui.ExperimentalComposeUiApi::class) @Composable -private fun Storefront(s: FrontEndState, a: FrontEndActions) { +internal fun Storefront(s: FrontEndState, a: FrontEndActions) { val ctx = LocalContext.current val narrow = LocalNarrowPane.current val colors = MaterialTheme.colorScheme @@ -290,6 +295,9 @@ private fun Storefront(s: FrontEndState, a: FrontEndActions) { } val ff = LocalFrontFocus.current val inputMode = LocalInputModeManager.current + val focusManager = LocalFocusManager.current + val keyboard = LocalSoftwareKeyboardController.current + val view = LocalView.current // Cards, where they are, for the page that opens from one. val cards = remember { HashMap() } var viewportAt by remember { mutableStateOf(Offset.Zero) } @@ -300,6 +308,11 @@ private fun Storefront(s: FrontEndState, a: FrontEndActions) { LaunchedEffect(openApp) { val id = openApp if (id != null) { + // The grid stays composed for the return animation. Release its native search field + // before covering it, so its focus ring and keyboard do not follow onto the app page. + focusManager.clearFocus(force = true) + view.findFocus()?.clearFocus() + keyboard?.hide() detailFrom = cards[id]?.let { Origin(it.translate(-viewportAt), 16f * density.density) } leaving = false detail = id @@ -350,7 +363,10 @@ private fun Storefront(s: FrontEndState, a: FrontEndActions) { Spacer(Modifier.height(8.dp)) Box(Modifier.weight(1f).fillMaxWidth().clipToBounds().onGloballyPositioned { viewportAt = it.positionInRoot() }) { Column( - Modifier.fillMaxSize().graphicsLayer { scaleX = sink; scaleY = sink; alpha = fade }.verticalScroll(rememberScrollState()).padding(bottom = 16.dp), + Modifier.fillMaxSize().focusProperties { + enter = { if (detail == null) androidx.compose.ui.focus.FocusRequester.Default else androidx.compose.ui.focus.FocusRequester.Cancel } + }.focusGroup() + .graphicsLayer { scaleX = sink; scaleY = sink; alpha = fade }.verticalScroll(rememberScrollState()).padding(bottom = 16.dp), ) { val open: (String) -> Unit = { openApp = it } when (mode) { diff --git a/app/src/test/java/com/droiddeck/launcher/ui/FlathubFocusTest.kt b/app/src/test/java/com/droiddeck/launcher/ui/FlathubFocusTest.kt new file mode 100644 index 0000000000..b98cd76609 --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/ui/FlathubFocusTest.kt @@ -0,0 +1,107 @@ +package com.droiddeck.launcher.ui + +import android.view.View +import android.view.ViewGroup +import android.widget.EditText +import androidx.compose.runtime.SideEffect +import androidx.compose.runtime.CompositionLocalProvider +import androidx.compose.ui.Modifier +import androidx.compose.ui.platform.LocalView +import androidx.compose.ui.semantics.SemanticsActions +import androidx.compose.ui.test.onNodeWithContentDescription +import androidx.compose.ui.test.onNodeWithText +import androidx.compose.ui.test.hasText +import androidx.compose.ui.test.hasClickAction +import androidx.compose.ui.test.assertIsNotFocused +import androidx.compose.ui.test.performClick +import androidx.compose.ui.test.performSemanticsAction +import com.droiddeck.launcher.R +import com.droiddeck.launcher.store.FlathubApi +import com.droiddeck.launcher.store.StoreState +import org.junit.After +import org.junit.Assert.assertFalse +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 + +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [28], qualifiers = "w960dp-h540dp-land", application = android.app.Application::class) +class FlathubFocusTest { + @get:Rule val compose = createRobolectricComposeRule() + + @After fun clearStore() { + StoreState.sections.clear() + StoreState.details.clear() + StoreState.openRequest = null + } + + @Test fun openingAnAppReleasesTheCoveredNativeSearchField() { + val id = "io.github.test.Datcord" + StoreState.sections["popular"] = listOf( + FlathubApi.AppSummary(id, "Datcord", "Discord client", "", null, false, 0), + ) + StoreState.details[id] = FlathubApi.AppDetails( + id, "Datcord", "Discord client", "", null, "", null, null, null, + emptyList(), emptyList(), null, 0, 0, listOf("aarch64"), + ) + lateinit var root: View + val focus = FrontFocus() + compose.setContent { + DroidDeckTheme { + val view = LocalView.current + SideEffect { root = view.rootView } + CompositionLocalProvider(LocalFrontFocus provides focus) { + FocusGlideHost { + Storefront(FrontEndState( + installed = "r9", ready = true, available = null, busy = false, stage = "", percent = -1, + desktopInstalled = true, offlineAccount = null, offline = false, frameGenLabel = "", + romsDir = null, logsEnabled = false, steamGames = emptyList(), emulators = emptyList(), running = null, + ), FrontEndActions( + onPlay = {}, onPlayDesktopUi = {}, onSteamGame = {}, onDesktop = {}, onEmulator = {}, + onRom = {}, onResume = {}, onSteamSettings = {}, onDesktopSettings = {}, + onInstallPackage = {}, onRemovePackage = {}, onRuntime = {}, onFrameGenPick = {}, + onImportLossless = {}, onProtons = {}, onComponents = {}, onPerformance = {}, + onRoms = {}, onFiles = {}, onLogs = {}, onOffline = {}, + onWirelessAdbPair = { _, _, _, _ -> }, onWirelessAdbApply = { _, _, _, _ -> }, + onSetPhantomProcessLimit = { _, _ -> }, + )) + } + } + } + } + val context = RuntimeEnvironment.getApplication() + compose.onNodeWithContentDescription(context.getString(R.string.stores_search_open)).performClick() + compose.waitForIdle() + val field = editText(root)!! + compose.runOnIdle { + // Keep a nonempty query, so clearing focus cannot simply dispose the search field. + // Whitespace skips the network search while retaining the field just like typed text. + field.setText(" ") + field.requestFocus() + assertTrue(field.isFocused) + } + compose.waitForIdle() + // Touching a card does not implicitly focus a Compose control or release an AndroidView. + compose.onNodeWithText("Datcord").performSemanticsAction(SemanticsActions.OnClick) { it() } + compose.onNodeWithText(context.getString(R.string.store_install)).assertExists() + compose.runOnIdle { + assertFalse("Covered search field must release focus", field.isFocused) + assertEquals(" ", field.text.toString()) + focus.items.getValue("tile:store:$id").requestFocus() + } + compose.onNode(hasText("Datcord") and hasClickAction()).assertIsNotFocused() + } + + private fun editText(view: View): EditText? { + if (view is EditText) return view + if (view is ViewGroup) for (i in 0 until view.childCount) { + editText(view.getChildAt(i))?.let { return it } + } + return null + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/ui/PresetScaleTest.kt b/app/src/test/java/com/droiddeck/launcher/ui/PresetScaleTest.kt new file mode 100644 index 0000000000..d498d7f44a --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/ui/PresetScaleTest.kt @@ -0,0 +1,79 @@ +package com.droiddeck.launcher.ui + +import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.size +import androidx.compose.runtime.SideEffect +import androidx.compose.runtime.mutableIntStateOf +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.snapshots.Snapshot +import androidx.compose.ui.Modifier +import androidx.compose.ui.platform.testTag +import androidx.compose.ui.platform.LocalDensity +import androidx.compose.ui.test.onNodeWithTag +import androidx.compose.ui.test.onNodeWithText +import androidx.compose.ui.test.performClick +import androidx.compose.ui.unit.dp +import org.junit.Assert.assertEquals +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) +// Render the component without starting runtime services or installing the app crash handler. +@Config(sdk = [28], application = android.app.Application::class) +class PresetScaleTest { + @get:Rule val compose = createRobolectricComposeRule() + + @Test fun presetPopupAndPageKeepTheScaleChosenInSetup() { + val scale = mutableIntStateOf(100) + val preset = mutableStateOf("PERFORMANCE") + val open = mutableStateOf(false) + compose.setContent { + DroidDeckTheme(appScale = scale.intValue) { + Box(Modifier.size(100.dp).testTag("page-size")) + FexPresetChip(preset.value, false, open.value, true, + onToggle = { open.value = true }, onDismiss = { open.value = false }, + onPick = { preset.value = it!! }) + } + } + val originalWidth = compose.onNodeWithTag("page-size").fetchSemanticsNode().boundsInRoot.width + // Notify changes driven by the test before asking Compose to become idle again. + compose.runOnIdle { + scale.intValue = 75 + Snapshot.sendApplyNotifications() + } + compose.onNodeWithText("Performance").performClick() + compose.onNodeWithText("Intermediate").performClick() + compose.waitForIdle() + assertEquals(originalWidth * 0.75f, + compose.onNodeWithTag("page-size").fetchSemanticsNode().boundsInRoot.width, 1f) + compose.runOnIdle { + open.value = true + Snapshot.sendApplyNotifications() + } + compose.onNodeWithText("Compatibility").performClick() + compose.waitForIdle() + assertEquals(originalWidth * 0.75f, + compose.onNodeWithTag("page-size").fetchSemanticsNode().boundsInRoot.width, 1f) + } + + @Test fun menuUsesItsAnchorsDensity() { + var pageDensity = 0f + var menuDensity = 0f + compose.setContent { + DroidDeckTheme(appScale = 75) { + val density = LocalDensity.current.density + SideEffect { pageDensity = density } + AnchoredMenu(true, onDismiss = {}) { + val popupDensity = LocalDensity.current.density + SideEffect { menuDensity = popupDensity } + MenuItem("Preset", false) {} + } + } + } + compose.waitForIdle() + assertEquals(pageDensity, menuDensity, 0f) + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/ui/RobolectricComposeRule.kt b/app/src/test/java/com/droiddeck/launcher/ui/RobolectricComposeRule.kt index 12c382f5df..e6efbb1309 100644 --- a/app/src/test/java/com/droiddeck/launcher/ui/RobolectricComposeRule.kt +++ b/app/src/test/java/com/droiddeck/launcher/ui/RobolectricComposeRule.kt @@ -3,6 +3,7 @@ package com.droiddeck.launcher.ui import android.content.ComponentName import androidx.activity.ComponentActivity import androidx.compose.ui.test.junit4.createComposeRule +import androidx.compose.runtime.snapshots.Snapshot import org.robolectric.RuntimeEnvironment import org.robolectric.Shadows.shadowOf @@ -12,4 +13,7 @@ fun createRobolectricComposeRule() = createComposeRule().also { shadowOf(context.packageManager).addActivityIfNotPresent( ComponentName(context, ComponentActivity::class.java), ) + // Pure state tests can leave unapplied snapshots before a Compose host starts its snapshot + // manager. Apply them before the rule's initial idle check, which runs before the test body. + Snapshot.sendApplyNotifications() }