From 60b9ad797af68a1811d0e53ed94ae1022096882f Mon Sep 17 00:00:00 2001 From: 4di-hello <4di-hello@users.noreply.github.com> Date: Sun, 22 Nov 2026 11:11:55 +0100 Subject: [PATCH] Settings: sections can be collapsed (optional) Long settings screens are easier to overview when sections that are not needed can be collapsed. A tap on a section title collapses or expands it, the state is remembered per screen. A jump from the search opens a collapsed section while the setting is shown, without saving that. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01VuiaMhLXwhErf9shhrr1uD --- .../keyboard/settings/SearchScreen.kt | 70 +++++++++++++++++-- app/src/main/res/values-de/strings.xml | 1 + app/src/main/res/values/strings.xml | 1 + 3 files changed, 66 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/helium314/keyboard/settings/SearchScreen.kt b/app/src/main/java/helium314/keyboard/settings/SearchScreen.kt index 01c48d436..91ab870ad 100644 --- a/app/src/main/java/helium314/keyboard/settings/SearchScreen.kt +++ b/app/src/main/java/helium314/keyboard/settings/SearchScreen.kt @@ -3,6 +3,7 @@ package helium314.keyboard.settings import androidx.activity.compose.BackHandler import androidx.compose.animation.AnimatedVisibility +import androidx.compose.foundation.clickable import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.ColumnScope @@ -114,7 +115,17 @@ fun SearchSettingsScreen( } // one lazy row per setting (a whole card as one item composed every setting of the screen at once, // which is what made opening a large screen slow); the rows of a group are drawn as one card - val rows = remember(groups) { settingRows(groups) } + // sections with a title can be collapsed, this is remembered; a section with the setting a jump + // shows is open while it is shown (that is not saved) + var collapsedSections by remember { mutableStateOf(collapsedSections(context)) } + var revealKey by remember { mutableStateOf(null) } + val jumpTargetNow = jumpTarget() + LaunchedEffect(jumpTargetNow) { if (jumpTargetNow != null) revealKey = jumpTargetNow } + val rows = remember(groups, collapsedSections, revealKey, jumpTargetNow) { + settingRows(groups, { sectionId(context, title, it) }) { id, keys -> + id in collapsedSections && revealKey !in keys && jumpTargetNow !in keys + } + } // jump from a long press in the search results (see SettingsJump): show the setting // here, opening its fine-tune section if needed, scroll to it and highlight it @@ -182,7 +193,28 @@ fun SearchSettingsScreen( ) ) { if (row.key == null) { - PreferenceCategory(stringResource(row.titleRes!!)) + val sectionId = row.sectionId!! + androidx.compose.foundation.layout.Row( + verticalAlignment = androidx.compose.ui.Alignment.CenterVertically, + modifier = Modifier + .fillMaxWidth() + .clickable(onClickLabel = stringResource(R.string.settings_category_collapse)) { + val collapse = !row.collapsed + if (collapse && revealKey in row.sectionKeys) revealKey = null + setSectionCollapsed(context, sectionId, collapse) + collapsedSections = if (collapse) collapsedSections + sectionId else collapsedSections - sectionId + } + ) { + Box(Modifier.weight(1f)) { PreferenceCategory(stringResource(row.titleRes!!)) } + Icon( + painterResource(R.drawable.ic_arrow_right), + null, + tint = MaterialTheme.colorScheme.primary, + modifier = Modifier + .padding(top = 12.dp, end = 16.dp) + .rotate(if (row.collapsed) 0f else 90f) + ) + } } else { val key = row.key val highlight by animateColorAsState( @@ -236,18 +268,29 @@ fun SearchSettingsScreen( * A row of a settings screen: the title of a category ([key] is null) or a setting; first and last row of a card. * [nested]: the setting is inside an open fine-tune section, i.e. after a fine-tune header in the same card. * [inOpenSection]: the row is nested or the header of a fine-tune section with settings shown inside. + * A title row knows its section ([sectionId], [sectionKeys]) and whether that is collapsed. */ private data class SettingRow( val id: String, val titleRes: Int?, val key: String?, val first: Boolean = false, val last: Boolean = false, - val nested: Boolean = false, val inOpenSection: Boolean = false + val nested: Boolean = false, val inOpenSection: Boolean = false, + val sectionId: String? = null, val sectionKeys: List = emptyList(), val collapsed: Boolean = false, ) -private fun settingRows(groups: List>>): List = buildList { +private fun settingRows( + groups: List>>, + sectionId: (Int) -> String, + isCollapsed: (String, List) -> Boolean, +): List = buildList { groups.forEachIndexed { index, (titleRes, keys) -> val group = buildList { - if (titleRes != null) add(SettingRow("title_${index}_$titleRes", titleRes, null)) + val collapsed = if (titleRes != null) { + val id = sectionId(titleRes) + val collapsed = isCollapsed(id, keys) + add(SettingRow("title_${index}_$titleRes", titleRes, null, sectionId = id, sectionKeys = keys, collapsed = collapsed)) + collapsed + } else false var inFineTune = false - keys.forEach { + if (!collapsed) keys.forEach { val isFineTuneHeader = it.startsWith(FINE_TUNE_SECTION_PREFIX) add(SettingRow("${index}_$it", null, it, nested = inFineTune && !isFineTuneHeader)) if (isFineTuneHeader) inFineTune = true @@ -390,6 +433,21 @@ fun SearchScreen( } } +private const val PREF_COLLAPSED_SECTIONS = "collapsed_settings_sections" + +private fun sectionId(context: android.content.Context, screenTitle: String, titleRes: Int) = + "$screenTitle/" + runCatching { context.resources.getResourceEntryName(titleRes) }.getOrDefault(titleRes.toString()) + +private fun collapsedSections(context: android.content.Context): Set = + context.prefs().getStringSet(PREF_COLLAPSED_SECTIONS, null).orEmpty().toSet() + +private fun setSectionCollapsed(context: android.content.Context, id: String, collapsed: Boolean) { + val prefs = context.prefs() + val set = prefs.getStringSet(PREF_COLLAPSED_SECTIONS, null).orEmpty().toMutableSet() + if (collapsed) set.add(id) else set.remove(id) + prefs.edit().putStringSet(PREF_COLLAPSED_SECTIONS, set).apply() +} + // from StreetComplete /** Expandable text field that can be dismissed and requests focus when it is expanded */ @Composable diff --git a/app/src/main/res/values-de/strings.xml b/app/src/main/res/values-de/strings.xml index 3d443dfec..7957863fd 100644 --- a/app/src/main/res/values-de/strings.xml +++ b/app/src/main/res/values-de/strings.xml @@ -10,6 +10,7 @@ Optionen "Textkorrektur" "Erweitert" + Abschnitt ein- oder ausklappen "Design" "Geteilte Tastatur aktivieren" "Eingabemethoden wechseln" diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index d6d569fac..5900b6422 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -41,6 +41,7 @@ Suggestions Advanced + Collapse or expand section Theme