improve popup key compatibility woith codes and labels

This commit is contained in:
Helium314 2024-06-06 21:53:23 +02:00
parent 4de8f770ef
commit bdc418cb70
4 changed files with 43 additions and 12 deletions

View file

@ -22,7 +22,13 @@ public final class KeyboardCodesSet {
public static int getCode(final String name) {
Integer id = sNameToIdMap.get(name);
if (id == null) throw new RuntimeException("Unknown key code: " + name);
if (id == null) {
try {
return KeyCode.INSTANCE.checkAndConvertCode(Integer.parseInt(name));
} catch (final Exception e) {
throw new RuntimeException("Unknown key code: " + name);
}
}
return DEFAULT[id];
}

View file

@ -45,14 +45,6 @@ interface AbstractKeyData {
* it is always required to check for the string's length before attempting to directly retrieve the first char.
*/
fun asString(isForDisplay: Boolean): String // todo: remove it? not used at all (better only later, maybe useful for getting display label in some languages)
/** get the label, but also considers code, which can't be set separately for popup keys and thus goes into the label */
fun getPopupLabel(params: KeyboardParams): String {
val keyData = if (this is KeyData) this else compute(params) ?: return ""
if (keyData.code == KeyCode.UNSPECIFIED || keyData.code < 0) // don't allow negative codes in popups
return keyData.label
return "${keyData.label}|${StringUtils.newSingleCodePointString(keyData.code)}"
}
}
/**

View file

@ -289,6 +289,26 @@ sealed interface KeyData : AbstractKeyData {
private const val POPUP_EYS_NAVIGATE_EMOJI_PREVIOUS_NEXT = "!fixedColumnOrder!4,!needsDividers!,!icon/previous_key|!code/key_action_previous,!icon/clipboard_action_key|!code/key_clipboard,!icon/emoji_action_key|!code/key_emoji,!icon/next_key|!code/key_action_next"
}
/** get the label, but also considers code, which can't be set separately for popup keys and thus goes into the label */
// this mashes the code into the popup label to make it work
// actually that's a bad approach, but at the same time doing things properly and with reasonable performance requires much more work
// 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)
if (code == KeyCode.UNSPECIFIED) {
return if (newLabel == label) label
else if (newLabel.endsWith("|")) "${newLabel}!code/${processCode()}" // for toolbar keys
else "${newLabel}|!code/${processCode()}"
}
if (code >= 32)
return "${newLabel}|${StringUtils.newSingleCodePointString(code)}"
if (code in KeyCode.Spec.CURRENCY)
return newLabel // should be treated correctly here, currently this is done in PopupKeysUtils
return if (newLabel.endsWith("|")) "${newLabel}!code/${processCode()}" // for toolbar keys
else "${newLabel}|!code/${processCode()}"
}
override fun compute(params: KeyboardParams): KeyData? {
require(groupId <= GROUP_ENTER) { "only groups up to GROUP_ENTER are supported" }
require(label.isNotEmpty() || type == KeyType.PLACEHOLDER || code != KeyCode.UNSPECIFIED) { "non-placeholder key has no code and no label" }