From 27e2a6f584e69551be46994cf54cae84f22624d2 Mon Sep 17 00:00:00 2001 From: Helium314 Date: Fri, 28 Feb 2025 21:29:38 +0100 Subject: [PATCH] determine whether to show shift key for scripts without uppercase --- .../keyboard/latin/utils/SubtypeSettings.kt | 2 +- .../keyboard/latin/utils/SubtypeUtils.kt | 9 +++++-- .../settings/dialogs/SubtypeDialog.kt | 26 ++++++++++++++++--- app/src/main/res/values/strings.xml | 2 +- 4 files changed, 32 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/helium314/keyboard/latin/utils/SubtypeSettings.kt b/app/src/main/java/helium314/keyboard/latin/utils/SubtypeSettings.kt index 033b95265..7471f6159 100644 --- a/app/src/main/java/helium314/keyboard/latin/utils/SubtypeSettings.kt +++ b/app/src/main/java/helium314/keyboard/latin/utils/SubtypeSettings.kt @@ -80,7 +80,7 @@ object SubtypeSettings { val subtype = enabledSubtypes.firstOrNull { it.toSettingsSubtype() == selectedSubtype } if (subtype != null) { return subtype - } else { + } else if (enabledSubtypes.isNotEmpty()) { Log.w(TAG, "selected subtype $selectedSubtype / ${prefs.getString(Settings.PREF_SELECTED_SUBTYPE, Defaults.PREF_SELECTED_SUBTYPE)} not found") } if (enabledSubtypes.isNotEmpty()) diff --git a/app/src/main/java/helium314/keyboard/latin/utils/SubtypeUtils.kt b/app/src/main/java/helium314/keyboard/latin/utils/SubtypeUtils.kt index c0f2e0357..f9fdd7b27 100644 --- a/app/src/main/java/helium314/keyboard/latin/utils/SubtypeUtils.kt +++ b/app/src/main/java/helium314/keyboard/latin/utils/SubtypeUtils.kt @@ -125,7 +125,7 @@ data class SettingsSubtype(val locale: Locale, val extraValues: String) { fun layoutName(type: LayoutType) = LayoutType.getLayoutMap(getExtraValueOf(KEYBOARD_LAYOUT_SET) ?: "")[type] - fun with(extraValueKey: String, extraValue: String?): SettingsSubtype { + fun with(extraValueKey: String, extraValue: String? = null): SettingsSubtype { val newList = extraValues.split(",") .filterNot { it.isBlank() || it.startsWith("$extraValueKey=") || it == extraValueKey } val newValue = if (extraValue == null) extraValueKey else "$extraValueKey=$extraValue" @@ -142,7 +142,9 @@ data class SettingsSubtype(val locale: Locale, val extraValues: String) { fun getExtraValueOf(extraValueKey: String): String? = extraValues.getExtraValueOf(extraValueKey) - fun withLayout(type: LayoutType, name: String): SettingsSubtype { + fun hasExtraValueOf(extraValueKey: String): Boolean = extraValues.hasExtraValueOf(extraValueKey) + + fun withLayout(type: LayoutType, name: String): SettingsSubtype { val map = LayoutType.getLayoutMap(getExtraValueOf(KEYBOARD_LAYOUT_SET) ?: "") map[type] = name return with(KEYBOARD_LAYOUT_SET, map.toExtraValue()) @@ -166,6 +168,9 @@ data class SettingsSubtype(val locale: Locale, val extraValues: String) { fun String.getExtraValueOf(extraValueKey: String) = split(",") .firstOrNull { it.startsWith("$extraValueKey=") }?.substringAfter("$extraValueKey=") + fun String.hasExtraValueOf(extraValueKey: String) = split(",") + .any { it.startsWith("$extraValueKey=") || it == extraValueKey } + /** Creates a SettingsSubtype from the given InputMethodSubtype. * Will strip some extra values that are set when creating the InputMethodSubtype from SettingsSubtype */ fun InputMethodSubtype.toSettingsSubtype(): SettingsSubtype { diff --git a/app/src/main/java/helium314/keyboard/settings/dialogs/SubtypeDialog.kt b/app/src/main/java/helium314/keyboard/settings/dialogs/SubtypeDialog.kt index 9dc468cbc..9aa4b9ce1 100644 --- a/app/src/main/java/helium314/keyboard/settings/dialogs/SubtypeDialog.kt +++ b/app/src/main/java/helium314/keyboard/settings/dialogs/SubtypeDialog.kt @@ -21,6 +21,7 @@ import androidx.compose.material3.Switch import androidx.compose.material3.Text import androidx.compose.material3.TextButton import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.collectAsState import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf @@ -55,7 +56,7 @@ import helium314.keyboard.latin.utils.LayoutType.Companion.displayNameId import helium314.keyboard.latin.utils.LayoutUtils import helium314.keyboard.latin.utils.LayoutUtilsCustom import helium314.keyboard.latin.utils.Log -import helium314.keyboard.latin.utils.ScriptUtils.SCRIPT_LATIN +import helium314.keyboard.latin.utils.ScriptUtils import helium314.keyboard.latin.utils.ScriptUtils.script import helium314.keyboard.latin.utils.SettingsSubtype import helium314.keyboard.latin.utils.SettingsSubtype.Companion.toSettingsSubtype @@ -90,6 +91,25 @@ fun SubtypeDialog( var currentSubtypeString by rememberSaveable { mutableStateOf(initialSubtype.toPref()) } val currentSubtype = currentSubtypeString.toSettingsSubtype() fun setCurrentSubtype(subtype: SettingsSubtype) { currentSubtypeString = subtype.toPref() } + LaunchedEffect(currentSubtypeString) { + if (ScriptUtils.scriptSupportsUppercase(currentSubtype.locale)) return@LaunchedEffect + // update the noShiftKey extra value + val mainLayout = currentSubtype.mainLayoutName() + val noShiftKey = if (mainLayout != null && LayoutUtilsCustom.isCustomLayout(mainLayout)) { + // determine from layout + val content = LayoutUtilsCustom.getLayoutFile(mainLayout, LayoutType.MAIN, ctx).readText() + !content.contains("\"shift_state_selector\"") + } else { + // determine from subtype with same layout + SubtypeSettings.getResourceSubtypesForLocale(currentSubtype.locale) + .firstOrNull { it.mainLayoutName() == mainLayout } + ?.containsExtraValueKey(ExtraValue.NO_SHIFT_KEY) ?: false + } + if (!noShiftKey && currentSubtype.hasExtraValueOf(ExtraValue.NO_SHIFT_KEY)) + setCurrentSubtype(currentSubtype.without(ExtraValue.NO_SHIFT_KEY)) + else if (noShiftKey && !currentSubtype.hasExtraValueOf(ExtraValue.NO_SHIFT_KEY)) + setCurrentSubtype(currentSubtype.with(ExtraValue.NO_SHIFT_KEY)) + } val availableLocalesForScript = getAvailableSecondaryLocales(ctx, currentSubtype.locale).sortedBy { it.toLanguageTag() } var showSecondaryLocaleDialog by remember { mutableStateOf(false) } @@ -141,7 +161,7 @@ fun SubtypeDialog( setCurrentSubtype(currentSubtype.without(ExtraValue.HINT_ORDER)) } } - if (currentSubtype.locale.script() == SCRIPT_LATIN) { + if (currentSubtype.locale.script() == ScriptUtils.SCRIPT_LATIN) { WithSmallTitle(stringResource(R.string.show_popup_keys_title)) { val explicitValue = currentSubtype.getExtraValueOf(ExtraValue.MORE_POPUPS) val value = explicitValue ?: prefs.getString(Settings.PREF_MORE_POPUP_KEYS, Defaults.PREF_MORE_POPUP_KEYS)!! @@ -155,7 +175,7 @@ fun SubtypeDialog( } } if (hasLocalizedNumberRow(currentSubtype.locale, ctx)) { - Row { + Row(verticalAlignment = Alignment.CenterVertically) { val checked = currentSubtype.getExtraValueOf(ExtraValue.LOCALIZED_NUMBER_ROW)?.toBoolean() Text(stringResource(R.string.localized_number_row), Modifier.weight(1f)) Switch( diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 2979f1185..02e35140c 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1003,6 +1003,6 @@ New dictionary: Really reset all customized icons? Really delete %s? - + Invalid name