From 42c23d386432df0b79b4835bc711e27724a91cbc Mon Sep 17 00:00:00 2001 From: Jules Aguillon Date: Tue, 31 Dec 2024 12:10:02 +0100 Subject: [PATCH] Refactor: Simplify double tap for capslock This doesn't fix a bug but remove some tricky code. The shift key is no longer different when the "double tap for capslock" option is on. The handling of the option is moved to Pointer instead and becomes simpler. --- srcs/juloo.keyboard2/KeyValue.java | 10 ++++---- srcs/juloo.keyboard2/Keyboard2View.java | 5 ---- srcs/juloo.keyboard2/LayoutModifier.java | 9 ------- srcs/juloo.keyboard2/Pointers.java | 32 +++++++++++++++--------- 4 files changed, 25 insertions(+), 31 deletions(-) diff --git a/srcs/juloo.keyboard2/KeyValue.java b/srcs/juloo.keyboard2/KeyValue.java index 57fe88d..c6778c9 100644 --- a/srcs/juloo.keyboard2/KeyValue.java +++ b/srcs/juloo.keyboard2/KeyValue.java @@ -101,8 +101,8 @@ public final class KeyValue implements Comparable // Behavior flags. public static final int FLAG_LATCH = (1 << FLAGS_OFFSET << 0); - // Key can be locked by typing twice - public static final int FLAG_LOCK = (1 << FLAGS_OFFSET << 1); + // Key can be locked by typing twice when enabled in settings + public static final int FLAG_DOUBLE_TAP_LOCK = (1 << FLAGS_OFFSET << 1); // Special keys are not repeated and don't clear latched modifiers. public static final int FLAG_SPECIAL = (1 << FLAGS_OFFSET << 2); // Whether the symbol should be greyed out. For example, keys that are not @@ -118,8 +118,8 @@ public final class KeyValue implements Comparable // Ranges for the different components private static final int FLAGS_BITS = - FLAG_LATCH | FLAG_LOCK | FLAG_SPECIAL | FLAG_GREYED | FLAG_KEY_FONT | - FLAG_SMALLER_FONT | FLAG_SECONDARY; + FLAG_LATCH | FLAG_DOUBLE_TAP_LOCK | FLAG_SPECIAL | FLAG_GREYED | + FLAG_KEY_FONT | FLAG_SMALLER_FONT | FLAG_SECONDARY; private static final int KIND_BITS = (0b1111 << KIND_OFFSET); // 4 bits wide private static final int VALUE_BITS = ~(FLAGS_BITS | KIND_BITS); // 20 bits wide @@ -500,7 +500,7 @@ public final class KeyValue implements Comparable case "\\\\": return makeStringKey("\\"); /* Modifiers and dead-keys */ - case "shift": return modifierKey(0xE00A, Modifier.SHIFT, 0); + case "shift": return modifierKey(0xE00A, Modifier.SHIFT, FLAG_DOUBLE_TAP_LOCK); case "ctrl": return modifierKey("Ctrl", Modifier.CTRL, 0); case "alt": return modifierKey("Alt", Modifier.ALT, 0); case "accent_aigu": return diacritic(0xE050, Modifier.AIGU); diff --git a/srcs/juloo.keyboard2/Keyboard2View.java b/srcs/juloo.keyboard2/Keyboard2View.java index f124b6b..6f6be2a 100644 --- a/srcs/juloo.keyboard2/Keyboard2View.java +++ b/srcs/juloo.keyboard2/Keyboard2View.java @@ -104,11 +104,6 @@ public class Keyboard2View extends View _keyboard = kw; _shift_kv = KeyValue.getKeyByName("shift"); _shift_key = _keyboard.findKeyWithValue(_shift_kv); - if (_shift_key == null) - { - _shift_kv = _shift_kv.withFlags(_shift_kv.getFlags() | KeyValue.FLAG_LOCK); - _shift_key = _keyboard.findKeyWithValue(_shift_kv); - } _compose_kv = KeyValue.getKeyByName("compose"); _compose_key = _keyboard.findKeyWithValue(_compose_kv); KeyModifier.set_modmap(_keyboard.modmap); diff --git a/srcs/juloo.keyboard2/LayoutModifier.java b/srcs/juloo.keyboard2/LayoutModifier.java index 31f5468..3ae98ca 100644 --- a/srcs/juloo.keyboard2/LayoutModifier.java +++ b/srcs/juloo.keyboard2/LayoutModifier.java @@ -175,15 +175,6 @@ public final class LayoutModifier break; } break; - case Modifier: - switch (orig.getModifier()) - { - case SHIFT: - if (globalConfig.double_tap_lock_shift) - return orig.withFlags(orig.getFlags() | KeyValue.FLAG_LOCK); - break; - } - break; } return orig; } diff --git a/srcs/juloo.keyboard2/Pointers.java b/srcs/juloo.keyboard2/Pointers.java index 5bcdcda..f13718f 100644 --- a/srcs/juloo.keyboard2/Pointers.java +++ b/srcs/juloo.keyboard2/Pointers.java @@ -16,7 +16,7 @@ public final class Pointers implements Handler.Callback public static final int FLAG_P_LATCHABLE = 1; public static final int FLAG_P_LATCHED = (1 << 1); public static final int FLAG_P_FAKE = (1 << 2); - public static final int FLAG_P_LOCKABLE = (1 << 3); + public static final int FLAG_P_DOUBLE_TAP_LOCK = (1 << 3); public static final int FLAG_P_LOCKED = (1 << 4); public static final int FLAG_P_SLIDING = (1 << 5); /** Clear latched (only if also FLAG_P_LATCHABLE set). */ @@ -86,10 +86,10 @@ public final class Pointers implements Handler.Callback /** The key must not be already latched . */ void add_fake_pointer(KeyboardData.Key key, KeyValue kv, boolean locked) { - Pointer ptr = new Pointer(-1, key, kv, 0.f, 0.f, Modifiers.EMPTY); - ptr.flags = FLAG_P_FAKE | FLAG_P_LATCHED; + int flags = pointer_flags_of_kv(kv) | FLAG_P_FAKE | FLAG_P_LATCHED; if (locked) - ptr.flags |= FLAG_P_LOCKED; + flags |= FLAG_P_LOCKED; + Pointer ptr = new Pointer(-1, key, kv, 0.f, 0.f, Modifiers.EMPTY, flags); _ptrs.add(ptr); _handler.onPointerFlagsChanged(false); } @@ -153,7 +153,7 @@ public final class Pointers implements Handler.Callback if (latched != null) // Already latched { removePtr(ptr); // Remove dupplicate - if ((latched.flags & FLAG_P_LOCKABLE) != 0) // Toggle lockable key + if ((latched.flags & FLAG_P_DOUBLE_TAP_LOCK) != 0) // Toggle lockable key lockPointer(latched, false); else // Otherwise, unlatch { @@ -204,7 +204,7 @@ public final class Pointers implements Handler.Callback // The other key already "own" the latched modifiers and will clear them. Modifiers mods = getModifiers(isOtherPointerDown()); KeyValue value = _handler.modifyKey(key.keys[0], mods); - Pointer ptr = new Pointer(pointerId, key, value, x, y, mods); + Pointer ptr = make_pointer(pointerId, key, value, x, y, mods); _ptrs.add(ptr); startLongPress(ptr); _handler.onPointerDown(value, false); @@ -368,7 +368,7 @@ public final class Pointers implements Handler.Callback /** Make a pointer into the locked state. */ private void lockPointer(Pointer ptr, boolean shouldVibrate) { - ptr.flags = (ptr.flags & ~FLAG_P_LOCKABLE) | FLAG_P_LOCKED; + ptr.flags = (ptr.flags & ~FLAG_P_DOUBLE_TAP_LOCK) | FLAG_P_LOCKED; _handler.onPointerFlagsChanged(shouldVibrate); } @@ -460,7 +460,7 @@ public final class Pointers implements Handler.Callback } /** Return the [FLAG_P_*] flags that correspond to pressing [kv]. */ - static int pointer_flags_of_kv(KeyValue kv) + int pointer_flags_of_kv(KeyValue kv) { int flags = 0; if (kv.hasFlagsAny(KeyValue.FLAG_LATCH)) @@ -470,8 +470,9 @@ public final class Pointers implements Handler.Callback flags |= FLAG_P_CLEAR_LATCHED | FLAG_P_CANT_LOCK; flags |= FLAG_P_LATCHABLE; } - if (kv.hasFlagsAny(KeyValue.FLAG_LOCK)) - flags |= FLAG_P_LOCKABLE; + if (_config.double_tap_lock_shift && + kv.hasFlagsAny(KeyValue.FLAG_DOUBLE_TAP_LOCK)) + flags |= FLAG_P_DOUBLE_TAP_LOCK; return flags; } @@ -512,6 +513,13 @@ public final class Pointers implements Handler.Callback // Pointers + Pointer make_pointer(int p, KeyboardData.Key k, KeyValue v, float x, float y, + Modifiers m) + { + int flags = (v == null) ? 0 : pointer_flags_of_kv(v); + return new Pointer(p, k, v, x, y, m, flags); + } + private static final class Pointer { /** -1 when latched. */ @@ -533,7 +541,7 @@ public final class Pointers implements Handler.Callback /** [null] when not in sliding mode. */ public Sliding sliding; - public Pointer(int p, KeyboardData.Key k, KeyValue v, float x, float y, Modifiers m) + public Pointer(int p, KeyboardData.Key k, KeyValue v, float x, float y, Modifiers m, int f) { pointerId = p; key = k; @@ -542,7 +550,7 @@ public final class Pointers implements Handler.Callback downX = x; downY = y; modifiers = m; - flags = (v == null) ? 0 : pointer_flags_of_kv(v); + flags = f; timeoutWhat = -1; sliding = null; }