process label for simple popup keys

now toolbar keys and defined key labels work in simple layout popup keys
This commit is contained in:
Helium314 2025-05-18 21:52:25 +02:00
parent e7ccf72fc5
commit e45f0660a2
2 changed files with 39 additions and 39 deletions

View file

@ -49,6 +49,6 @@ open class PopupSet<T : AbstractKeyData>(
} }
class SimplePopups(val popupKeys: Collection<String>?) : PopupSet<AbstractKeyData>() { class SimplePopups(val popupKeys: Collection<String>?) : PopupSet<AbstractKeyData>() {
override fun getPopupKeyLabels(params: KeyboardParams) = popupKeys override fun getPopupKeyLabels(params: KeyboardParams) = popupKeys?.map { KeyData.processLabel(it, params) }
override fun isEmpty(): Boolean = popupKeys.isNullOrEmpty() override fun isEmpty(): Boolean = popupKeys.isNullOrEmpty()
} }

View file

@ -12,6 +12,7 @@ import kotlinx.serialization.Transient
import helium314.keyboard.keyboard.Key import helium314.keyboard.keyboard.Key
import helium314.keyboard.keyboard.KeyboardId import helium314.keyboard.keyboard.KeyboardId
import helium314.keyboard.keyboard.KeyboardTheme import helium314.keyboard.keyboard.KeyboardTheme
import helium314.keyboard.keyboard.internal.KeyboardCodesSet
import helium314.keyboard.keyboard.internal.KeyboardIconsSet import helium314.keyboard.keyboard.internal.KeyboardIconsSet
import helium314.keyboard.keyboard.internal.KeyboardParams import helium314.keyboard.keyboard.internal.KeyboardParams
import helium314.keyboard.keyboard.internal.keyboard_parser.floris.KeyCode.checkAndConvertCode import helium314.keyboard.keyboard.internal.keyboard_parser.floris.KeyCode.checkAndConvertCode
@ -285,6 +286,34 @@ sealed interface KeyData : AbstractKeyData {
return getStringInLocale(id, params) 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 // 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_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" 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 // 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 // idea: directly create PopupKeySpec, but need to deal with needsToUpcase and popupKeysColumnAndFlags
fun getPopupLabel(params: KeyboardParams): String { fun getPopupLabel(params: KeyboardParams): String {
val newLabel = processLabel(params) val newLabel = processLabel(label, params)
if (code == KeyCode.UNSPECIFIED) { if (code == KeyCode.UNSPECIFIED) {
if (newLabel == label) return label if (newLabel == label || newLabel.contains(KeyboardCodesSet.PREFIX_CODE))
return newLabel
val newCode = processCode() val newCode = processCode()
if (newLabel.endsWith("|")) return "${newLabel}!code/$newCode" // for toolbar keys if (newLabel.endsWith("|")) return "${newLabel}${KeyboardCodesSet.PREFIX_CODE}$newCode" // maybe not used any more
return if (newCode == code) newLabel else "${newLabel}|!code/$newCode" return if (newCode == code) newLabel else "${newLabel}|${KeyboardCodesSet.PREFIX_CODE}$newCode"
} }
if (code >= 32) { if (code >= 32) {
if (newLabel.startsWith(KeyboardIconsSet.PREFIX_ICON)) { if (newLabel.startsWith(KeyboardIconsSet.PREFIX_ICON)) {
@ -324,12 +354,12 @@ sealed interface KeyData : AbstractKeyData {
val outputText = String(codePoints, 0, codePoints.size) val outputText = String(codePoints, 0, codePoints.size)
return "${newLabel}|$outputText" return "${newLabel}|$outputText"
} }
return if (newLabel.endsWith("|")) "$newLabel!code/${processCode()}" // for toolbar keys return if (newLabel.endsWith("|")) "$newLabel${KeyboardCodesSet.PREFIX_CODE}${processCode()}" // for toolbar keys
else "$newLabel|!code/${processCode()}" else "$newLabel|${KeyboardCodesSet.PREFIX_CODE}${processCode()}"
} }
fun getCurrencyLabel(params: KeyboardParams): String { fun getCurrencyLabel(params: KeyboardParams): String {
val newLabel = processLabel(params) val newLabel = processLabel(label, params)
return when (code) { return when (code) {
// consider currency codes for label // consider currency codes for label
KeyCode.CURRENCY_SLOT_1 -> "$newLabel|${params.mLocaleKeyboardInfos.currencyKey.first}" KeyCode.CURRENCY_SLOT_1 -> "$newLabel|${params.mLocaleKeyboardInfos.currencyKey.first}"
@ -388,7 +418,7 @@ sealed interface KeyData : AbstractKeyData {
newLabel = getCurrencyLabel(params) newLabel = getCurrencyLabel(params)
} else { } else {
newCode = processCode() newCode = processCode()
newLabel = processLabel(params) newLabel = processLabel(label, params)
} }
var newLabelFlags = labelFlags or additionalLabelFlags or getAdditionalLabelFlags(params) var newLabelFlags = labelFlags or additionalLabelFlags or getAdditionalLabelFlags(params)
val newPopupKeys = popup.merge(getAdditionalPopupKeys(params)) val newPopupKeys = popup.merge(getAdditionalPopupKeys(params))
@ -474,36 +504,6 @@ sealed interface KeyData : AbstractKeyData {
else params.mDefaultKeyWidth 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 { private fun processCode(): Int {
if (code != KeyCode.UNSPECIFIED) return code if (code != KeyCode.UNSPECIFIED) return code
return when (label) { return when (label) {