feat(settings): Keep search history and center opened settings

Recall recent Settings searches and bring the setting opened from search into view.
This commit is contained in:
Tushar Gandhe authored and GitHub committed 2026-12-06 00:34:44 -05:00
1 parent f50691ebc9
commit a4751dacbe
40 files changed
+290 -14

No files matched your search

@@ -10,6 +10,7 @@ import com.droiddeck.launcher.gpu.Cardboard
import com.droiddeck.launcher.gpu.ScreenEffects
import com.droiddeck.launcher.gpu.SystemVulkanDriver
import com.droiddeck.launcher.gpu.TurnipDriver
import org.json.JSONArray
import org.json.JSONObject
import java.io.File
@@ -317,6 +318,14 @@ object SessionPrefs {
prefs(context).edit().putBoolean("telemetryAuto", on).apply()
}
fun settingsSearches(context: Context): List<String> = runCatching {
JSONArray(prefs(context).getString("settingsSearches", null) ?: "[]").let { a -> List(a.length()) { a.getString(it) } }
}.getOrDefault(emptyList())
fun setSettingsSearches(context: Context, searches: List<String>) {
prefs(context).edit().putString("settingsSearches", JSONArray(searches).toString()).apply()
}
fun telemetryOff(context: Context): Set<String> = prefs(context).getStringSet("telemetryOff", null)?.toSet().orEmpty()
fun setTelemetryOff(context: Context, keys: Set<String>) {
@@ -231,7 +231,10 @@ internal fun SetupPanel(
}
val chipFocus = remember { List(tabs.size) { FocusRequester() } }
var tabTurned by remember { mutableStateOf(false) }
val pick: (Int) -> Unit = { i -> tab = i; tabTurned = true; searching = false; query = ""; if (i == DEVELOPER_TAB) logsRefresh++ }
remember { SettingsSearchHistory.load(ctx) }
var fromHistory by remember { mutableStateOf(false) }
val keepSearch = { if (searching && SettingsIndex.search(ctx, query, frostRow).isNotEmpty()) SettingsSearchHistory.add(ctx, query) }
val pick: (Int) -> Unit = { i -> keepSearch(); tab = i; tabTurned = true; searching = false; query = ""; if (i == DEVELOPER_TAB) logsRefresh++ }
val inputModeManager = LocalInputModeManager.current
LaunchedEffect(Unit) {
while (true) {
@@ -258,8 +261,9 @@ internal fun SetupPanel(
) {
val sectionBar: @Composable (Modifier) -> Unit = { m ->
SettingsPillSearch(
m, searching, query, { query = it },
onOpen = { searching = true }, onClose = { searching = false; query = "" },
m, searching, query, { query = it; fromHistory = false },
onOpen = { searching = true }, onClose = { keepSearch(); searching = false; query = "" },
keepOpen = SettingsSearchHistory.items.isNotEmpty(), onSearch = keepSearch,
) { SheetChips(tabs, tab, chipFocus, onPick = pick) }
}
val portrait = androidx.compose.ui.platform.LocalConfiguration.current.orientation == android.content.res.Configuration.ORIENTATION_PORTRAIT
@@ -268,9 +272,11 @@ internal fun SetupPanel(
Row(Modifier.fillMaxWidth().padding(bottom = 10.dp)) { sectionBar(Modifier.weight(1f)) }
} else PageHeader(stringResource(R.string.setup_title)) { sectionBar(Modifier.weight(1f)) }
val listing = searching && query.isNotBlank()
val fixed = tab == CONTROLLER_TAB && !listing
val history = searching && query.isBlank() && SettingsSearchHistory.items.isNotEmpty()
val fixed = tab == CONTROLLER_TAB && !listing && !history
Column(modifier = Modifier.weight(1f).fillMaxWidth().then(if (fixed) Modifier else Modifier.verticalScroll(rememberScrollState())).padding(bottom = if (fixed) 0.dp else 24.dp)) {
if (listing) SettingsSearchResults(query, frostRow) { section -> pick(section.ordinal) }
if (listing) SettingsSearchResults(query, frostRow, focusFirst = fromHistory, onSearched = keepSearch) { section -> pick(section.ordinal) }
else if (history) SettingsSearchHistoryList(onPick = { fromHistory = true; query = it }) { SettingsSearchHistory.clear(ctx) }
else when (tab) {
OVERVIEW_TAB -> {
OnboardingCard(limitBlocks, a.onShowPhantomGate)
@@ -16,6 +16,10 @@ import androidx.compose.runtime.setValue
import androidx.compose.runtime.withFrameNanos
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.drawBehind
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.geometry.Rect
import androidx.compose.ui.layout.LayoutCoordinates
import androidx.compose.ui.layout.onPlaced
/** The row a settings search result opens, so the page it lands on can scroll to it and flash it. */
internal object SettingsJump {
@@ -31,12 +35,32 @@ private const val JUMP_WINDOW_MS = 5_000L
/** False in the search results, whose rows carry the same labels as the rows they open. */
internal val LocalSearchTargets = staticCompositionLocalOf { true }
/** How far a page has to be scrolled to show a row in its middle, or as near to it as the page's ends allow. */
internal fun centeredScroll(rowTop: Float, rowHeight: Float, page: Float, content: Float): Float =
(rowTop + rowHeight / 2f - page / 2f).coerceIn(0f, (content - page).coerceAtLeast(0f))
/** The part of the page to show around a row to have it in the middle, relative to the row. Null when nothing around it scrolls. */
private fun pageAround(row: LayoutCoordinates): Rect? {
var content = row
while (true) {
val page = content.parentCoordinates ?: return null
if (content.size.height > page.size.height) {
val top = content.localPositionOf(row, Offset.Zero).y
val height = page.size.height.toFloat()
val from = centeredScroll(top, row.size.height.toFloat(), height, content.size.height.toFloat()) - top
return Rect(0f, from, row.size.width.toFloat(), from + height)
}
content = page
}
}
@OptIn(androidx.compose.foundation.ExperimentalFoundationApi::class)
@Composable
internal fun Modifier.searchTarget(label: String): Modifier {
val pal = LocalPalette.current
val flash = remember { Animatable(0f) }
val reveal = remember { BringIntoViewRequester() }
val row = remember { arrayOfNulls<LayoutCoordinates>(1) }
var claimed by remember { mutableStateOf<SettingsJump.Target?>(null) }
val target = SettingsJump.target.takeIf { LocalSearchTargets.current }
if (target != null && target.label == label) SideEffect {
@@ -48,7 +72,7 @@ internal fun Modifier.searchTarget(label: String): Modifier {
claimed?.let { mine ->
LaunchedEffect(mine) {
withFrameNanos { }
reveal.bringIntoView()
reveal.bringIntoView(row[0]?.takeIf { it.isAttached }?.let(::pageAround))
repeat(2) {
flash.animateTo(1f, tween(220))
flash.animateTo(0f, tween(420))
@@ -56,7 +80,7 @@ internal fun Modifier.searchTarget(label: String): Modifier {
claimed = null
}
}
return bringIntoViewRequester(reveal).drawBehind {
return bringIntoViewRequester(reveal).onPlaced { row[0] = it }.drawBehind {
if (flash.value > 0f) drawRect(pal.signal.copy(alpha = 0.28f * flash.value))
}
}
@@ -42,7 +42,7 @@ import androidx.compose.ui.semantics.clearAndSetSemantics
@Composable
internal fun SettingsPillSearch(
modifier: Modifier, searching: Boolean, query: String, onQuery: (String) -> Unit,
onOpen: () -> Unit, onClose: () -> Unit, pills: @Composable () -> Unit,
onOpen: () -> Unit, onClose: () -> Unit, keepOpen: Boolean = false, onSearch: () -> Unit = {}, pills: @Composable () -> Unit,
) {
val colors = MaterialTheme.colorScheme
val open by animateFloatAsState(if (searching) 1f else 0f, Motion.sp(0.75f, 420f), label = "settingsSearch")
@@ -53,10 +53,11 @@ internal fun SettingsPillSearch(
var fieldFocused by remember { mutableStateOf(false) }
CancelPendingFocus(focusOnOpen, focusTap, fieldFocused) {
focusOnOpen = false
if (query.isEmpty()) onClose()
if (query.isEmpty() && !keepOpen) onClose()
}
val close = { toIcon = true; focusOnOpen = false; onClose() }
BackHandler(enabled = searching) { close() }
LaunchedEffect(keepOpen) { if (!keepOpen && searching && !fieldFocused && !focusOnOpen && query.isEmpty()) close() }
LaunchedEffect(toIcon, searching, open <= 0.01f) {
if (searching) toIcon = false
else if (toIcon && open <= 0.01f) { focusSoon { icon }; toIcon = false }
@@ -80,10 +81,13 @@ internal fun SettingsPillSearch(
AdbTextField(
value = query, onValueChange = onQuery, label = "", keyboardType = KeyboardType.Text, imeAction = ImeAction.Search,
placeholder = stringResource(R.string.settings_search), compact = true, showLabel = false, focusOnShow = focusOnOpen, focusAtTap = focusTap,
clearFocus = !searching,
clearFocus = !searching, onDone = onSearch,
onFocusChange = { focused ->
fieldFocused = focused
if (!focused && query.isEmpty() && searching) { focusOnOpen = false; onClose() }
if (!focused && query.isEmpty() && searching) {
focusOnOpen = false
if (!keepOpen) onClose()
}
},
)
}
@@ -96,13 +100,19 @@ internal fun SettingsPillSearch(
}
@Composable
internal fun SettingsSearchResults(query: String, frost: Boolean, onOpen: (SettingsSection) -> Unit) {
internal fun SettingsSearchResults(query: String, frost: Boolean, focusFirst: Boolean = false, onSearched: () -> Unit = {}, onOpen: (SettingsSection) -> Unit) {
val ctx = LocalContext.current
val found = remember(query, frost) { SettingsIndex.search(ctx, query, frost) }
if (found.isEmpty()) {
Note(stringResource(R.string.settings_search_none, query.trim()))
return
}
val first = remember { FocusRequester() }
val firstToOpen = found.groupBy { it.place }.values.flatten().firstOrNull { it.section != null || it.tab != null }
val inputMode = androidx.compose.ui.platform.LocalInputModeManager.current
LaunchedEffect(Unit) {
if (focusFirst && firstToOpen != null && inputMode.inputMode == androidx.compose.ui.input.InputMode.Keyboard) focusSoon { first }
}
androidx.compose.runtime.CompositionLocalProvider(LocalSearchTargets provides false) { Column {
found.groupBy { it.place }.forEach { (place, entries) ->
SettingsGroup(stringResource(place.title)) {
@@ -111,10 +121,12 @@ internal fun SettingsSearchResults(query: String, frost: Boolean, onOpen: (Setti
val title = stringResource(entry.title)
val section = entry.section
val tab = entry.tab
val button = if (entry === firstToOpen) Modifier.focusRequester(first) else Modifier
if (section != null) {
ActionRow(title, within, stringResource(R.string.store_open), onClick = { SettingsJump.to(title); onOpen(section) })
ActionRow(title, within, stringResource(R.string.store_open), modifier = button, onClick = { onSearched(); SettingsJump.to(title); onOpen(section) })
} else if (tab != null) {
ActionRow(title, within, stringResource(R.string.store_open), onClick = {
ActionRow(title, within, stringResource(R.string.store_open), modifier = button, onClick = {
onSearched()
SettingsJump.to(title)
if (tab == NavTabs.MORE) MoreTabs.editing = true
if (tab == "downloads") DownloadsNav.showSettings = true
@@ -0,0 +1,101 @@
package com.droiddeck.launcher.ui
import android.content.Context
import androidx.compose.foundation.LocalIndication
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.hoverable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ExperimentalLayoutApi
import androidx.compose.foundation.layout.FlowRow
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.heightIn
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.widthIn
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.outlined.Delete
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
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.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.semantics.Role
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.droiddeck.launcher.R
import com.droiddeck.launcher.session.SessionPrefs
/** What was searched for in Settings, newest first. */
internal object SettingsSearchHistory {
const val MAX = 8
var items by mutableStateOf<List<String>>(emptyList())
private set
fun load(context: Context) { items = SessionPrefs.settingsSearches(context) }
fun add(context: Context, query: String) {
val next = withSearch(items, query)
if (next == items) return
items = next
SessionPrefs.setSettingsSearches(context, next)
}
fun clear(context: Context) {
items = emptyList()
SessionPrefs.setSettingsSearches(context, emptyList())
}
fun withSearch(items: List<String>, query: String): List<String> {
val text = query.trim().replace(Regex("\\s+"), " ")
if (text.isEmpty()) return items
return (listOf(text) + items.filterNot { it.equals(text, ignoreCase = true) }).take(MAX)
}
}
@OptIn(ExperimentalLayoutApi::class)
@Composable
internal fun SettingsSearchHistoryList(onPick: (String) -> Unit, onClear: () -> Unit) {
val colors = MaterialTheme.colorScheme
Column(Modifier.fillMaxWidth()) {
Row(verticalAlignment = Alignment.CenterVertically, modifier = Modifier.fillMaxWidth().padding(top = 12.dp, bottom = 8.dp)) {
Text(
stringResource(R.string.settings_search_history), fontSize = 16.sp, fontWeight = FontWeight.SemiBold, color = colors.onSurfaceVariant,
modifier = Modifier.weight(1f).padding(start = 4.dp),
)
SmallIconButton(Icons.Outlined.Delete, stringResource(R.string.settings_search_history_clear), tint = colors.onSurfaceVariant, onClick = onClear)
}
FlowRow(horizontalArrangement = Arrangement.spacedBy(8.dp), verticalArrangement = Arrangement.spacedBy(8.dp)) {
SettingsSearchHistory.items.forEach { HistoryChip(it) { onPick(it) } }
}
}
}
@Composable
private fun HistoryChip(text: String, onClick: () -> Unit) {
val colors = MaterialTheme.colorScheme
val pal = LocalPalette.current
val src = remember { MutableInteractionSource() }
val hot = rememberHot(src)
Box(
contentAlignment = Alignment.Center,
modifier = Modifier.heightIn(min = 40.dp).widthIn(min = 56.dp, max = 260.dp).clip(CircleShape)
.background(colors.surface).glideBorder(hot, CircleShape, pal.signal, pal.line)
.hoverable(src).clickable(interactionSource = src, indication = LocalIndication.current, role = Role.Button, onClick = onClick)
.controllerConfirm(onClick = onClick)
.padding(horizontal = 16.dp, vertical = 8.dp),
) { Text(text, fontSize = 14.sp, color = colors.onBackground, maxLines = 1, overflow = TextOverflow.Ellipsis) }
}