(almost) complete toolbar screen

This commit is contained in:
Helium314 2025-01-29 05:51:41 +01:00
parent 724b292edb
commit 52e0e891fe
5 changed files with 134 additions and 43 deletions

View file

@ -76,6 +76,7 @@ object NonSettingsPrefs {
const val HIDDEN_FEATURES = "hidden_features"
const val GITHUB = "github"
const val SAVE_LOG = "save_log"
const val CUSTOM_KEY_CODES = "customize_key_codes"
}
@JvmField

View file

@ -173,7 +173,6 @@ fun SwitchPreference(
fun SwitchPreference(
def: PrefDef,
default: Boolean,
modifier: Modifier = Modifier,
allowCheckedChange: (Boolean) -> Boolean = { true },
onCheckedChange: (Boolean) -> Unit = { }
) {
@ -182,7 +181,6 @@ fun SwitchPreference(
description = def.description,
pref = def.key,
default = default,
modifier = modifier,
allowCheckedChange = allowCheckedChange,
onCheckedChange = onCheckedChange
)

View file

@ -14,11 +14,11 @@ import helium314.keyboard.latin.utils.DeviceProtectedUtils
import kotlinx.coroutines.flow.MutableStateFlow
// todo
// add reorder / enable dialog
// maybe a more generic / reusable wrapper dialog
// need to adjust icons, because compose refuses to work with rotated drawables (arrows)
// drag handle should have "dimmed" color, iirc there are some theme defaults with the localCompositionProvider?
// more pref screens, and other super-custom things
// need to adjust icons, because compose refuses to work with rotated drawables (arrows on toolbar keys)
// more pref screens
// other super-custom things
// toolbar key customizer (missing from toolbar screen)
// icon selector
// consider IME insets when searching
// improve performance when loading screens with many settings (lazyColumn?)
// screens could have a lazy column of preferences and category separators, and the list has an if-setting-then-null for hiding

View file

@ -79,7 +79,12 @@ fun <T: Any> ReorderDialog(
val elevation by animateDpAsState(if (dragging) 4.dp else 0.dp)
Surface(shadowElevation = elevation) {
Row(modifier = Modifier.longPressDraggableHandle(), verticalAlignment = Alignment.CenterVertically) {
Icon(painterResource(R.drawable.ic_drag_indicator), "Reorder", Modifier.padding(end = 8.dp))
Icon(
painterResource(R.drawable.ic_drag_indicator),
"Reorder",
Modifier.padding(end = 8.dp),
MaterialTheme.colorScheme.onSurfaceVariant
)
displayItem(item)
}
}

View file

@ -23,14 +23,17 @@ import androidx.compose.ui.unit.dp
import helium314.keyboard.keyboard.internal.KeyboardIconsSet
import helium314.keyboard.latin.R
import helium314.keyboard.latin.settings.Settings
import helium314.keyboard.latin.utils.Log
import helium314.keyboard.latin.utils.defaultClipboardToolbarPref
import helium314.keyboard.latin.utils.defaultPinnedToolbarPref
import helium314.keyboard.latin.utils.defaultToolbarPref
import helium314.keyboard.latin.utils.getStringResourceOrName
import helium314.keyboard.settings.AllPrefs
import helium314.keyboard.settings.NonSettingsPrefs
import helium314.keyboard.settings.PrefDef
import helium314.keyboard.settings.Preference
import helium314.keyboard.settings.SearchPrefScreen
import helium314.keyboard.settings.SettingsActivity2
import helium314.keyboard.settings.SwitchPreference
import helium314.keyboard.settings.Theme
import helium314.keyboard.settings.dialogs.ReorderDialog
import helium314.keyboard.settings.prefs
@ -44,11 +47,32 @@ fun ToolbarScreen(
onClickBack = onClickBack,
title = stringResource(R.string.settings_screen_toolbar),
) {
SettingsActivity2.allPrefs.map[Settings.PREF_TOOLBAR_KEYS]!!.Preference()
SettingsActivity2.allPrefs.map[Settings.PREF_PINNED_TOOLBAR_KEYS]!!.Preference()
SettingsActivity2.allPrefs.map[Settings.PREF_CLIPBOARD_TOOLBAR_KEYS]!!.Preference()
SettingsActivity2.allPrefs.map[Settings.PREF_TOOLBAR_CUSTOM_KEY_CODES]!!.Preference()
SettingsActivity2.allPrefs.map[Settings.PREF_QUICK_PIN_TOOLBAR_KEYS]!!.Preference()
SettingsActivity2.allPrefs.map[Settings.PREF_AUTO_SHOW_TOOLBAR]!!.Preference()
SettingsActivity2.allPrefs.map[Settings.PREF_AUTO_HIDE_TOOLBAR]!!.Preference()
SettingsActivity2.allPrefs.map[Settings.PREF_VARIABLE_TOOLBAR_DIRECTION]!!.Preference()
}
}
fun createToolbarPrefs(context: Context) = listOf(
PrefDef(context, Settings.PREF_TOOLBAR_KEYS, R.string.toolbar_keys) { def ->
var showDialog by remember { mutableStateOf(false) }
Preference(
name = def.title,
onClick = { showDialog = true },
)
if (showDialog) {
ToolbarKeyReorderDialog(
def.key,
defaultToolbarPref,
def.title,
) { showDialog = false }
}
},
PrefDef(context, Settings.PREF_PINNED_TOOLBAR_KEYS, R.string.pinned_toolbar_keys) { def ->
var showDialog by remember { mutableStateOf(false) }
Preference(
@ -56,30 +80,97 @@ fun createToolbarPrefs(context: Context) = listOf(
onClick = { showDialog = true },
)
if (showDialog) {
ToolbarKeyReorderDialog(
def.key,
defaultPinnedToolbarPref,
def.title,
) { showDialog = false }
}
},
PrefDef(context, Settings.PREF_CLIPBOARD_TOOLBAR_KEYS, R.string.clipboard_toolbar_keys) { def ->
var showDialog by remember { mutableStateOf(false) }
Preference(
name = def.title,
onClick = { showDialog = true },
)
if (showDialog) {
ToolbarKeyReorderDialog(
def.key,
defaultClipboardToolbarPref,
def.title,
) { showDialog = false }
}
},
PrefDef(context, NonSettingsPrefs.CUSTOM_KEY_CODES, R.string.customize_toolbar_key_codes) { def ->
var showDialog by remember { mutableStateOf(false) }
Preference(
name = def.title,
onClick = { showDialog = true },
)
// todo: needs the dialog!
// which actually changes a different pref key... see if we can avoid it without too much work
},
PrefDef(context, Settings.PREF_QUICK_PIN_TOOLBAR_KEYS, R.string.quick_pin_toolbar_keys, R.string.quick_pin_toolbar_keys_summary) { def ->
SwitchPreference(
def,
false,
) { themeChanged = true }
},
PrefDef(context, Settings.PREF_AUTO_SHOW_TOOLBAR, R.string.auto_show_toolbar, R.string.auto_show_toolbar_summary) { def ->
SwitchPreference(
def,
false,
)
},
PrefDef(context, Settings.PREF_AUTO_HIDE_TOOLBAR, R.string.auto_hide_toolbar, R.string.auto_hide_toolbar_summary) { def ->
SwitchPreference(
def,
false,
)
},
PrefDef(context, Settings.PREF_VARIABLE_TOOLBAR_DIRECTION, R.string.var_toolbar_direction, R.string.var_toolbar_direction_summary) { def ->
SwitchPreference(
def,
true,
)
}
)
private class KeyAndState(var name: String, var state: Boolean)
@Composable
fun ToolbarKeyReorderDialog(
prefKey: String,
default: String,
title: String,
onDismiss: () -> Unit
) {
val ctx = LocalContext.current
val prefs = ctx.prefs()
val items = prefs.getString(def.key, defaultPinnedToolbarPref)!!.split(";").mapTo(ArrayList()) {
val items = prefs.getString(prefKey, default)!!.split(";").mapTo(ArrayList()) {
val both = it.split(",")
KeyAndState(both.first(), both.last().toBoolean())
}
ReorderDialog(
onConfirmed = { reorderedItems ->
val value = reorderedItems.joinToString(";") { it.name + "," + it.state }
prefs.edit().putString(def.key, value).apply()
prefs.edit().putString(prefKey, value).apply()
themeChanged = true
},
onDismissRequest = { showDialog = false },
onDismissRequest = { onDismiss },
items = items,
title = { Text(def.title)},
title = { Text(title)},
displayItem = { item ->
var checked by remember { mutableStateOf(item.state) }
Row(verticalAlignment = Alignment.CenterVertically) {
Box(modifier = Modifier.width(40.dp)) {
val iconId = KeyboardIconsSet.instance.iconIds[item.name.lowercase()]
if (iconId != null) // using ids makes having user-provided icons more difficult...
if (iconId != null)
// using ids makes having user-provided icons more difficult
// probably best would be some KeyboardIconsSet.getComposable that does "the right thing"
Icon(painterResource(iconId), null)
}
val text = item.name.lowercase().getStringResourceOrName("", context).toString()
val text = item.name.lowercase().getStringResourceOrName("", ctx).toString()
Text(text, Modifier.weight(1f))
Switch(
checked = checked,
@ -87,13 +178,9 @@ fun createToolbarPrefs(context: Context) = listOf(
)
}
},
getKey = { it.hashCode() }
getKey = { it.name }
)
}
},
)
private class KeyAndState(var name: String, var state: Boolean)
@Preview
@Composable