improve popup key handling and update tests

remove outdated part from layouts.md
fixes #883
This commit is contained in:
Helium314 2024-06-19 22:42:36 +02:00
parent 5b7f4dae4c
commit 3e74a29f2e
8 changed files with 291 additions and 136 deletions

View file

@ -1130,7 +1130,7 @@ public class Key implements Comparable<Key> {
: hintLabel;
}
String outputText = KeySpecParser.getOutputText(keySpec);
String outputText = KeySpecParser.getOutputText(keySpec, code);
if (needsToUpcase) {
outputText = StringUtils.toTitleCaseOfKeyLabel(outputText, localeForUpcasing);
}

View file

@ -150,7 +150,7 @@ public final class KeySpecParser {
}
@Nullable
public static String getOutputText(@Nullable final String keySpec) {
public static String getOutputText(@Nullable final String keySpec, final int code) {
if (keySpec == null) {
// TODO: Throw {@link KeySpecParserError} once Key.keyLabel attribute becomes mandatory.
return null;
@ -170,7 +170,9 @@ public final class KeySpecParser {
return outputText;
}
final String label = getLabel(keySpec);
if (label == null && DebugFlags.DEBUG_ENABLED) {
if (label == null) {
if (keySpec.startsWith(KeyboardIconsSet.PREFIX_ICON) && code != KeyCode.UNSPECIFIED && code != KeyCode.MULTIPLE_CODE_POINTS)
return null; // allow empty label in case of icon & actual code
throw new KeySpecParserError("Empty label: " + keySpec);
}
// Code is automatically generated for one letter label. See {@link getCode()}.

View file

@ -60,7 +60,7 @@ public final class PopupKeySpec {
mOutputText = mLabel;
} else {
mCode = code;
final String outputText = KeySpecParser.getOutputText(popupKeySpec);
final String outputText = KeySpecParser.getOutputText(popupKeySpec, code);
mOutputText = needsToUpperCase
? StringUtils.toTitleCaseOfKeyLabel(outputText, locale) : outputText;
}

View file

@ -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.KeySpecParser
import helium314.keyboard.keyboard.internal.KeyboardIconsSet
import helium314.keyboard.keyboard.internal.KeyboardParams
import helium314.keyboard.keyboard.internal.keyboard_parser.floris.KeyCode.checkAndConvertCode
@ -303,13 +304,21 @@ sealed interface KeyData : AbstractKeyData {
if (newLabel.endsWith("|")) return "${newLabel}!code/$newCode" // for toolbar keys
return if (newCode == code) newLabel else "${newLabel}|!code/$newCode"
}
if (code >= 32)
return "${newLabel}|${StringUtils.newSingleCodePointString(code)}"
if (code >= 32) {
if (newLabel.startsWith(KeyboardIconsSet.PREFIX_ICON)) {
// we ignore everything after the first |
// todo (later): for now this is fine, but it should rather be done when creating the popup key,
// and it should be consistent with other popups and also with normal keys
return "${newLabel.substringBefore("|")}|${StringUtils.newSingleCodePointString(code)}"
}
return "$newLabel|${StringUtils.newSingleCodePointString(code)}"
}
if (code in KeyCode.Spec.CURRENCY) {
return getCurrencyLabel(params)
}
return if (newLabel.endsWith("|")) "${newLabel}!code/${processCode()}" // for toolbar keys
else "${newLabel}|!code/${processCode()}"
return if (newLabel.endsWith("|")) "$newLabel!code/${processCode()}" // for toolbar keys
else "$newLabel|!code/${processCode()}"
}
fun getCurrencyLabel(params: KeyboardParams): String {

View file

@ -64,7 +64,7 @@ public final class PunctuationSuggestions extends SuggestedWords {
final String keySpec = super.getWord(index);
final int code = KeySpecParser.getCode(keySpec);
return (code == KeyCode.MULTIPLE_CODE_POINTS)
? KeySpecParser.getOutputText(keySpec)
? KeySpecParser.getOutputText(keySpec, code)
: StringUtils.newSingleCodePointString(code);
}

View file

@ -92,7 +92,7 @@ private fun checkLayout(layoutContent: String, context: Context): Boolean? {
return null
return false
} catch (e: Exception) { Log.w(TAG, "error parsing custom simple layout", e) }
if (layoutContent.startsWith("[")) {
if (layoutContent.trimStart().startsWith("[")) {
// layout can't be loaded, assume it's json -> load json layout again because the error message shown to the user is from the most recent error
try {
RawKeyboardParser.parseJsonString(layoutContent).map { row -> row.mapNotNull { it.compute(params)?.toKeyParams(params) } }
@ -101,7 +101,7 @@ private fun checkLayout(layoutContent: String, context: Context): Boolean? {
return null
}
private fun checkKeys(keys: List<List<Key.KeyParams>>): Boolean {
fun checkKeys(keys: List<List<Key.KeyParams>>): Boolean {
if (keys.isEmpty() || keys.any { it.isEmpty() }) {
Log.w(TAG, "empty rows")
return false