abouttreesummaryrefslogcommitdiff
path: root/srcs/juloo.keyboard2/ComposeKey.java
diff options
context:
space:
mode:
authorJules Aguillon2025-02-23 18:00:44 +0100
committerJules Aguillon2025-02-23 18:00:44 +0100
commitca25cc55f6bc2c7b3100da2f7bf18a078a23f55e (patch)
treee3e94c2fa073e44063b66d86f8be4789620714ad /srcs/juloo.keyboard2/ComposeKey.java
parent68be82a4f92f47300b9960cf9cf65040c96f17ed (diff)
downloadunexpected-keyboard-ca25cc55f6bc2c7b3100da2f7bf18a078a23f55e.tar.gz
unexpected-keyboard-ca25cc55f6bc2c7b3100da2f7bf18a078a23f55e.zip
Apply compose sequences to String keys
This is mostly useful for characters that do not fit on a single 16-bit char. Shift sequences for 𝕨𝕩𝕗𝕘𝕤 are added for illustration.
Diffstat (limited to 'srcs/juloo.keyboard2/ComposeKey.java')
-rw-r--r--srcs/juloo.keyboard2/ComposeKey.java42
1 files changed, 25 insertions, 17 deletions
diff --git a/srcs/juloo.keyboard2/ComposeKey.java b/srcs/juloo.keyboard2/ComposeKey.java
index f2169b1..57b2a4e 100644
--- a/srcs/juloo.keyboard2/ComposeKey.java
+++ b/srcs/juloo.keyboard2/ComposeKey.java
@@ -4,31 +4,22 @@ import java.util.Arrays;
public final class ComposeKey
{
- /** Apply the pending compose sequence to [kv]. */
+ /** Apply the pending compose sequence to [kv]. Returns [null] if no sequence
+ matched. */
public static KeyValue apply(int state, KeyValue kv)
{
switch (kv.getKind())
{
case Char:
- KeyValue res = apply(state, kv.getChar());
- // Grey-out characters not part of any sequence.
- if (res == null)
- return kv.withFlags(kv.getFlags() | KeyValue.FLAG_GREYED);
- return res;
- /* Tapping compose again exits the pending sequence. */
- case Compose_pending:
- return KeyValue.getKeyByName("compose_cancel");
- /* These keys are not greyed. */
- case Event:
- case Modifier:
- return kv;
- /* Other keys cannot be part of sequences. */
- default:
- return kv.withFlags(kv.getFlags() | KeyValue.FLAG_GREYED);
+ return apply(state, kv.getChar());
+ case String:
+ return apply(state, kv.getString());
}
+ return null;
}
- /** Apply the pending compose sequence to char [c]. */
+ /** Apply the pending compose sequence to char [c]. Returns [null] if no
+ sequence matched. */
public static KeyValue apply(int prev, char c)
{
char[] states = ComposeKeyData.states;
@@ -51,6 +42,23 @@ public final class ComposeKey
return KeyValue.makeCharKey((char)next_header);
}
+ /** Apply each char of a string to a sequence. Returns [null] if no sequence
+ matched. */
+ public static KeyValue apply(int prev, String s)
+ {
+ int i = 0;
+ while (true)
+ {
+ KeyValue k = apply(prev, s.charAt(i));
+ i++;
+ if (k == null) return null;
+ if (i >= s.length()) return k;
+ if (k.getKind() != KeyValue.Kind.Compose_pending)
+ return null; // Found a final state before the end of [s].
+ prev = k.getPendingCompose();
+ }
+ }
+
/** The state machine is comprised of two arrays.
The [states] array represents the different states and the associated