From 5c025bec864673c8a0cdeeda7255607f4c1cf3e0 Mon Sep 17 00:00:00 2001 From: Aleksandras Kostarevas Date: Wed, 24 Jul 2024 21:33:30 +0300 Subject: [PATCH] Automatically add morekeys_misc and actions based on morekeys and qwertysyms --- .../latin/settings/LongPressKeySettings.kt | 35 ++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/java/src/org/futo/inputmethod/latin/settings/LongPressKeySettings.kt b/java/src/org/futo/inputmethod/latin/settings/LongPressKeySettings.kt index b08fd4242..e09feeb15 100644 --- a/java/src/org/futo/inputmethod/latin/settings/LongPressKeySettings.kt +++ b/java/src/org/futo/inputmethod/latin/settings/LongPressKeySettings.kt @@ -51,6 +51,36 @@ private fun getKind(moreKey: String): LongPressKey? { } } +val alphabet = "abcdefghijklmnopqrstuvwxyz".toSet() + +private fun getBestMatchForMisc(moreKeys: List): String? { + if(moreKeys.any { getKind(it) == LongPressKey.MiscLetters }) return null + + return moreKeys.firstOrNull { getKind(it) == LongPressKey.LanguageKeys }?.let { + val k = it.replace("!text/morekeys_", "") + + if(k.length == 1 && alphabet.contains(k[0])) { + "!text/morekeys_misc_$k" + } else { + null + } + } +} + +private fun getBestMatchForAction(moreKeys: List): String? { + if(moreKeys.any { getKind(it) == LongPressKey.QuickActions }) return null + + return moreKeys.firstOrNull { getKind(it) == LongPressKey.Symbols }?.let { + val k = it.replace("!text/qwertysyms_", "") + + if(k.length == 1 && alphabet.contains(k[0])) { + "!text/actions_$k" + } else { + null + } + } +} + val LongPressKeyLayoutSetting = SettingsKey( stringPreferencesKey("longPressKeyOrdering"), "${LongPressKey.Numbers.ordinal},${LongPressKey.LanguageKeys.ordinal},${LongPressKey.Symbols.ordinal},${LongPressKey.QuickActions.ordinal},${LongPressKey.MiscLetters.ordinal}" @@ -74,7 +104,10 @@ class LongPressKeySettings(val context: Context) { private val currentSetting = context.getSettingBlocking(LongPressKeyLayoutSetting).toLongPressKeyLayoutItems() fun reorderMoreKeys(moreKeys: String): String { - val keys = MoreKeySpec.splitKeySpecs(moreKeys)?.toList() ?: listOf(moreKeys) + val keys = (MoreKeySpec.splitKeySpecs(moreKeys)?.toList() ?: listOf(moreKeys)).toMutableList() + + getBestMatchForMisc(keys)?.let { keys.add(it) } + getBestMatchForAction(keys)?.let { keys.add(it) } val finalKeys = mutableListOf()