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.
This commit is contained in:
Jules Aguillon 2024-12-31 12:10:02 +01:00
parent f64a0be6fa
commit 42c23d3864
4 changed files with 25 additions and 31 deletions

View File

@ -101,8 +101,8 @@ public final class KeyValue implements Comparable<KeyValue>
// Behavior flags. // Behavior flags.
public static final int FLAG_LATCH = (1 << FLAGS_OFFSET << 0); public static final int FLAG_LATCH = (1 << FLAGS_OFFSET << 0);
// Key can be locked by typing twice // Key can be locked by typing twice when enabled in settings
public static final int FLAG_LOCK = (1 << FLAGS_OFFSET << 1); public static final int FLAG_DOUBLE_TAP_LOCK = (1 << FLAGS_OFFSET << 1);
// Special keys are not repeated and don't clear latched modifiers. // Special keys are not repeated and don't clear latched modifiers.
public static final int FLAG_SPECIAL = (1 << FLAGS_OFFSET << 2); public static final int FLAG_SPECIAL = (1 << FLAGS_OFFSET << 2);
// Whether the symbol should be greyed out. For example, keys that are not // Whether the symbol should be greyed out. For example, keys that are not
@ -118,8 +118,8 @@ public final class KeyValue implements Comparable<KeyValue>
// Ranges for the different components // Ranges for the different components
private static final int FLAGS_BITS = private static final int FLAGS_BITS =
FLAG_LATCH | FLAG_LOCK | FLAG_SPECIAL | FLAG_GREYED | FLAG_KEY_FONT | FLAG_LATCH | FLAG_DOUBLE_TAP_LOCK | FLAG_SPECIAL | FLAG_GREYED |
FLAG_SMALLER_FONT | FLAG_SECONDARY; FLAG_KEY_FONT | FLAG_SMALLER_FONT | FLAG_SECONDARY;
private static final int KIND_BITS = (0b1111 << KIND_OFFSET); // 4 bits wide 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 private static final int VALUE_BITS = ~(FLAGS_BITS | KIND_BITS); // 20 bits wide
@ -500,7 +500,7 @@ public final class KeyValue implements Comparable<KeyValue>
case "\\\\": return makeStringKey("\\"); case "\\\\": return makeStringKey("\\");
/* Modifiers and dead-keys */ /* 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 "ctrl": return modifierKey("Ctrl", Modifier.CTRL, 0);
case "alt": return modifierKey("Alt", Modifier.ALT, 0); case "alt": return modifierKey("Alt", Modifier.ALT, 0);
case "accent_aigu": return diacritic(0xE050, Modifier.AIGU); case "accent_aigu": return diacritic(0xE050, Modifier.AIGU);

View File

@ -104,11 +104,6 @@ public class Keyboard2View extends View
_keyboard = kw; _keyboard = kw;
_shift_kv = KeyValue.getKeyByName("shift"); _shift_kv = KeyValue.getKeyByName("shift");
_shift_key = _keyboard.findKeyWithValue(_shift_kv); _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_kv = KeyValue.getKeyByName("compose");
_compose_key = _keyboard.findKeyWithValue(_compose_kv); _compose_key = _keyboard.findKeyWithValue(_compose_kv);
KeyModifier.set_modmap(_keyboard.modmap); KeyModifier.set_modmap(_keyboard.modmap);

View File

@ -175,15 +175,6 @@ public final class LayoutModifier
break; break;
} }
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; return orig;
} }

View File

@ -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_LATCHABLE = 1;
public static final int FLAG_P_LATCHED = (1 << 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_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_LOCKED = (1 << 4);
public static final int FLAG_P_SLIDING = (1 << 5); public static final int FLAG_P_SLIDING = (1 << 5);
/** Clear latched (only if also FLAG_P_LATCHABLE set). */ /** 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 . */ /** The key must not be already latched . */
void add_fake_pointer(KeyboardData.Key key, KeyValue kv, boolean locked) void add_fake_pointer(KeyboardData.Key key, KeyValue kv, boolean locked)
{ {
Pointer ptr = new Pointer(-1, key, kv, 0.f, 0.f, Modifiers.EMPTY); int flags = pointer_flags_of_kv(kv) | FLAG_P_FAKE | FLAG_P_LATCHED;
ptr.flags = FLAG_P_FAKE | FLAG_P_LATCHED;
if (locked) 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); _ptrs.add(ptr);
_handler.onPointerFlagsChanged(false); _handler.onPointerFlagsChanged(false);
} }
@ -153,7 +153,7 @@ public final class Pointers implements Handler.Callback
if (latched != null) // Already latched if (latched != null) // Already latched
{ {
removePtr(ptr); // Remove dupplicate 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); lockPointer(latched, false);
else // Otherwise, unlatch 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. // The other key already "own" the latched modifiers and will clear them.
Modifiers mods = getModifiers(isOtherPointerDown()); Modifiers mods = getModifiers(isOtherPointerDown());
KeyValue value = _handler.modifyKey(key.keys[0], mods); 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); _ptrs.add(ptr);
startLongPress(ptr); startLongPress(ptr);
_handler.onPointerDown(value, false); _handler.onPointerDown(value, false);
@ -368,7 +368,7 @@ public final class Pointers implements Handler.Callback
/** Make a pointer into the locked state. */ /** Make a pointer into the locked state. */
private void lockPointer(Pointer ptr, boolean shouldVibrate) 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); _handler.onPointerFlagsChanged(shouldVibrate);
} }
@ -460,7 +460,7 @@ public final class Pointers implements Handler.Callback
} }
/** Return the [FLAG_P_*] flags that correspond to pressing [kv]. */ /** 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; int flags = 0;
if (kv.hasFlagsAny(KeyValue.FLAG_LATCH)) 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_CLEAR_LATCHED | FLAG_P_CANT_LOCK;
flags |= FLAG_P_LATCHABLE; flags |= FLAG_P_LATCHABLE;
} }
if (kv.hasFlagsAny(KeyValue.FLAG_LOCK)) if (_config.double_tap_lock_shift &&
flags |= FLAG_P_LOCKABLE; kv.hasFlagsAny(KeyValue.FLAG_DOUBLE_TAP_LOCK))
flags |= FLAG_P_DOUBLE_TAP_LOCK;
return flags; return flags;
} }
@ -512,6 +513,13 @@ public final class Pointers implements Handler.Callback
// Pointers // 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 private static final class Pointer
{ {
/** -1 when latched. */ /** -1 when latched. */
@ -533,7 +541,7 @@ public final class Pointers implements Handler.Callback
/** [null] when not in sliding mode. */ /** [null] when not in sliding mode. */
public Sliding sliding; 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; pointerId = p;
key = k; key = k;
@ -542,7 +550,7 @@ public final class Pointers implements Handler.Callback
downX = x; downX = x;
downY = y; downY = y;
modifiers = m; modifiers = m;
flags = (v == null) ? 0 : pointer_flags_of_kv(v); flags = f;
timeoutWhat = -1; timeoutWhat = -1;
sliding = null; sliding = null;
} }