From e45f0660a269be82ad3dc68f23691bd19e42b305 Mon Sep 17 00:00:00 2001 From: Helium314 Date: Sun, 18 May 2025 21:52:25 +0200 Subject: [PATCH] process label for simple popup keys now toolbar keys and defined key labels work in simple layout popup keys --- .../keyboard_parser/floris/PopupSet.kt | 2 +- .../keyboard_parser/floris/TextKeyData.kt | 76 +++++++++---------- 2 files changed, 39 insertions(+), 39 deletions(-) diff --git a/app/src/main/java/helium314/keyboard/keyboard/internal/keyboard_parser/floris/PopupSet.kt b/app/src/main/java/helium314/keyboard/keyboard/internal/keyboard_parser/floris/PopupSet.kt index f0cb2cb40..8d696ddaf 100644 --- a/app/src/main/java/helium314/keyboard/keyboard/internal/keyboard_parser/floris/PopupSet.kt +++ b/app/src/main/java/helium314/keyboard/keyboard/internal/keyboard_parser/floris/PopupSet.kt @@ -49,6 +49,6 @@ open class PopupSet( } class SimplePopups(val popupKeys: Collection?) : PopupSet() { - override fun getPopupKeyLabels(params: KeyboardParams) = popupKeys + override fun getPopupKeyLabels(params: KeyboardParams) = popupKeys?.map { KeyData.processLabel(it, params) } override fun isEmpty(): Boolean = popupKeys.isNullOrEmpty() } diff --git a/app/src/main/java/helium314/keyboard/keyboard/internal/keyboard_parser/floris/TextKeyData.kt b/app/src/main/java/helium314/keyboard/keyboard/internal/keyboard_parser/floris/TextKeyData.kt index dcb50e802..d3abc77d0 100644 --- a/app/src/main/java/helium314/keyboard/keyboard/internal/keyboard_parser/floris/TextKeyData.kt +++ b/app/src/main/java/helium314/keyboard/keyboard/internal/keyboard_parser/floris/TextKeyData.kt @@ -12,6 +12,7 @@ import kotlinx.serialization.Transient import helium314.keyboard.keyboard.Key import helium314.keyboard.keyboard.KeyboardId import helium314.keyboard.keyboard.KeyboardTheme +import helium314.keyboard.keyboard.internal.KeyboardCodesSet import helium314.keyboard.keyboard.internal.KeyboardIconsSet import helium314.keyboard.keyboard.internal.KeyboardParams import helium314.keyboard.keyboard.internal.keyboard_parser.floris.KeyCode.checkAndConvertCode @@ -285,6 +286,34 @@ sealed interface KeyData : AbstractKeyData { return getStringInLocale(id, params) } + fun processLabel(label: String, params: KeyboardParams): String = when (label) { + KeyLabel.SYMBOL_ALPHA -> if (params.mId.isAlphabetKeyboard) params.mLocaleKeyboardInfos.labelSymbol else params.mLocaleKeyboardInfos.labelAlphabet + KeyLabel.SYMBOL -> params.mLocaleKeyboardInfos.labelSymbol + KeyLabel.ALPHA -> params.mLocaleKeyboardInfos.labelAlphabet + KeyLabel.COMMA -> params.mLocaleKeyboardInfos.labelComma + KeyLabel.PERIOD -> getPeriodLabel(params) + KeyLabel.SPACE -> getSpaceLabel(params) + KeyLabel.ACTION -> "${getActionKeyLabel(params)}|${getActionKeyCode(params)}" + KeyLabel.DELETE -> "!icon/delete_key|!code/key_delete" + KeyLabel.SHIFT -> "${getShiftLabel(params)}|!code/key_shift" + KeyLabel.COM -> params.mLocaleKeyboardInfos.tlds.first() + KeyLabel.LANGUAGE_SWITCH -> "!icon/language_switch_key|!code/key_language_switch" + KeyLabel.ZWNJ -> "!icon/zwnj_key|\u200C" + KeyLabel.CURRENCY -> params.mLocaleKeyboardInfos.currencyKey.first + KeyLabel.CURRENCY1 -> params.mLocaleKeyboardInfos.currencyKey.second[0] + KeyLabel.CURRENCY2 -> params.mLocaleKeyboardInfos.currencyKey.second[1] + KeyLabel.CURRENCY3 -> params.mLocaleKeyboardInfos.currencyKey.second[2] + KeyLabel.CURRENCY4 -> params.mLocaleKeyboardInfos.currencyKey.second[3] + KeyLabel.CURRENCY5 -> params.mLocaleKeyboardInfos.currencyKey.second[4] + KeyLabel.CTRL, KeyLabel.ALT, KeyLabel.FN, KeyLabel.META , KeyLabel.ESCAPE -> label.uppercase(Locale.US) + KeyLabel.TAB -> "!icon/tab_key|!code/${KeyCode.TAB}" + else -> { + if (label in toolbarKeyStrings.values) { + "!icon/$label|!code/${getCodeForToolbarKey(ToolbarKey.valueOf(label.uppercase(Locale.US)))}" + } else label + } + } + // could make arrays right away, but they need to be copied anyway as popupKeys arrays are modified when creating KeyParams private const val POPUP_EYS_NAVIGATE_PREVIOUS = "!icon/previous_key|!code/key_action_previous,!icon/clipboard_action_key|!code/key_clipboard" private const val POPUP_EYS_NAVIGATE_NEXT = "!icon/clipboard_action_key|!code/key_clipboard,!icon/next_key|!code/key_action_next" @@ -301,12 +330,13 @@ sealed interface KeyData : AbstractKeyData { // so better only do it in case the popup stuff needs more improvements // idea: directly create PopupKeySpec, but need to deal with needsToUpcase and popupKeysColumnAndFlags fun getPopupLabel(params: KeyboardParams): String { - val newLabel = processLabel(params) + val newLabel = processLabel(label, params) if (code == KeyCode.UNSPECIFIED) { - if (newLabel == label) return label + if (newLabel == label || newLabel.contains(KeyboardCodesSet.PREFIX_CODE)) + return newLabel val newCode = processCode() - if (newLabel.endsWith("|")) return "${newLabel}!code/$newCode" // for toolbar keys - return if (newCode == code) newLabel else "${newLabel}|!code/$newCode" + if (newLabel.endsWith("|")) return "${newLabel}${KeyboardCodesSet.PREFIX_CODE}$newCode" // maybe not used any more + return if (newCode == code) newLabel else "${newLabel}|${KeyboardCodesSet.PREFIX_CODE}$newCode" } if (code >= 32) { if (newLabel.startsWith(KeyboardIconsSet.PREFIX_ICON)) { @@ -324,12 +354,12 @@ sealed interface KeyData : AbstractKeyData { val outputText = String(codePoints, 0, codePoints.size) return "${newLabel}|$outputText" } - return if (newLabel.endsWith("|")) "$newLabel!code/${processCode()}" // for toolbar keys - else "$newLabel|!code/${processCode()}" + return if (newLabel.endsWith("|")) "$newLabel${KeyboardCodesSet.PREFIX_CODE}${processCode()}" // for toolbar keys + else "$newLabel|${KeyboardCodesSet.PREFIX_CODE}${processCode()}" } fun getCurrencyLabel(params: KeyboardParams): String { - val newLabel = processLabel(params) + val newLabel = processLabel(label, params) return when (code) { // consider currency codes for label KeyCode.CURRENCY_SLOT_1 -> "$newLabel|${params.mLocaleKeyboardInfos.currencyKey.first}" @@ -388,7 +418,7 @@ sealed interface KeyData : AbstractKeyData { newLabel = getCurrencyLabel(params) } else { newCode = processCode() - newLabel = processLabel(params) + newLabel = processLabel(label, params) } var newLabelFlags = labelFlags or additionalLabelFlags or getAdditionalLabelFlags(params) val newPopupKeys = popup.merge(getAdditionalPopupKeys(params)) @@ -474,36 +504,6 @@ sealed interface KeyData : AbstractKeyData { else params.mDefaultKeyWidth } - // todo (later): encoding the code in the label should be avoided, because we know it already - private fun processLabel(params: KeyboardParams): String = when (label) { - KeyLabel.SYMBOL_ALPHA -> if (params.mId.isAlphabetKeyboard) params.mLocaleKeyboardInfos.labelSymbol else params.mLocaleKeyboardInfos.labelAlphabet - KeyLabel.SYMBOL -> params.mLocaleKeyboardInfos.labelSymbol - KeyLabel.ALPHA -> params.mLocaleKeyboardInfos.labelAlphabet - KeyLabel.COMMA -> params.mLocaleKeyboardInfos.labelComma - KeyLabel.PERIOD -> getPeriodLabel(params) - KeyLabel.SPACE -> getSpaceLabel(params) - KeyLabel.ACTION -> "${getActionKeyLabel(params)}|${getActionKeyCode(params)}" - KeyLabel.DELETE -> "!icon/delete_key|!code/key_delete" - KeyLabel.SHIFT -> "${getShiftLabel(params)}|!code/key_shift" -// KeyLabel.EMOJI -> "!icon/emoji_normal_key|!code/key_emoji" - KeyLabel.COM -> params.mLocaleKeyboardInfos.tlds.first() - KeyLabel.LANGUAGE_SWITCH -> "!icon/language_switch_key|!code/key_language_switch" - KeyLabel.ZWNJ -> "!icon/zwnj_key|\u200C" - KeyLabel.CURRENCY -> params.mLocaleKeyboardInfos.currencyKey.first - KeyLabel.CURRENCY1 -> params.mLocaleKeyboardInfos.currencyKey.second[0] - KeyLabel.CURRENCY2 -> params.mLocaleKeyboardInfos.currencyKey.second[1] - KeyLabel.CURRENCY3 -> params.mLocaleKeyboardInfos.currencyKey.second[2] - KeyLabel.CURRENCY4 -> params.mLocaleKeyboardInfos.currencyKey.second[3] - KeyLabel.CURRENCY5 -> params.mLocaleKeyboardInfos.currencyKey.second[4] - KeyLabel.CTRL, KeyLabel.ALT, KeyLabel.FN, KeyLabel.META , KeyLabel.ESCAPE -> label.uppercase(Locale.US) - KeyLabel.TAB -> "!icon/tab_key|" - else -> { - if (label in toolbarKeyStrings.values) { - "!icon/$label|" - } else label - } - } - private fun processCode(): Int { if (code != KeyCode.UNSPECIFIED) return code return when (label) {