Skip to content

Commit

Permalink
improve popup key handling and update tests
Browse files Browse the repository at this point in the history
remove outdated part from layouts.md
fixes #883
  • Loading branch information
Helium314 committed Jun 19, 2024
1 parent 5b7f4da commit 3e74a29
Show file tree
Hide file tree
Showing 8 changed files with 289 additions and 134 deletions.
2 changes: 1 addition & 1 deletion app/src/main/java/helium314/keyboard/keyboard/Key.java
Original file line number Diff line number Diff line change
Expand Up @@ -1130,7 +1130,7 @@ public KeyParams(
: hintLabel;
}

String outputText = KeySpecParser.getOutputText(keySpec);
String outputText = KeySpecParser.getOutputText(keySpec, code);
if (needsToUpcase) {
outputText = StringUtils.toTitleCaseOfKeyLabel(outputText, localeForUpcasing);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ private static String getOutputTextInternal(@NonNull final String keySpec, final
}

@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;
Expand All @@ -170,7 +170,9 @@ public static String getOutputText(@Nullable final String keySpec) {
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()}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public PopupKeySpec(@NonNull final String popupKeySpec, boolean needsToUpperCase
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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public String getWord(final int index) {
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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) } }
Expand All @@ -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
Expand Down
Loading

0 comments on commit 3e74a29

Please sign in to comment.