From ebf80415d8034d853bd79ba7cfb578db67862529 Mon Sep 17 00:00:00 2001 From: Jules Aguillon Date: Thu, 19 Feb 2026 00:35:14 +0100 Subject: Refactor: KeyValue constants (#1180) * Refactor: KeyValue constants Add constants for keys that are accessed from the app's code. This simplifies the fake pointer handling in Keyboard2View. * Refactor: Pre-compute action key replacement The action key and the enter swap are pre-computed in EditorConfig. This simplifies the code.--- srcs/juloo.keyboard2/Config.java | 6 +++--- srcs/juloo.keyboard2/EditorConfig.java | 27 ++++++++++++++++++++------- srcs/juloo.keyboard2/KeyModifier.java | 6 +++--- srcs/juloo.keyboard2/KeyValue.java | 32 ++++++++++++++++++++++---------- srcs/juloo.keyboard2/Keyboard2View.java | 14 +++++--------- srcs/juloo.keyboard2/LayoutModifier.java | 13 ++++--------- srcs/juloo.keyboard2/Logs.java | 3 --- 7 files changed, 57 insertions(+), 44 deletions(-) (limited to 'srcs') diff --git a/srcs/juloo.keyboard2/Config.java b/srcs/juloo.keyboard2/Config.java index 18e9fe7..2831254 100644 --- a/srcs/juloo.keyboard2/Config.java +++ b/srcs/juloo.keyboard2/Config.java @@ -281,10 +281,10 @@ public final class Config { switch (prefs.getString("change_method_key_replacement", "prev")) { - case "prev": return KeyValue.getKeyByName("change_method_prev"); - case "next": return KeyValue.getKeyByName("change_method_next"); + case "prev": return KeyValue.CHANGE_METHOD_PREV; + case "next": return KeyValue.CHANGE_METHOD_NEXT; default: - case "picker": return KeyValue.getKeyByName("change_method"); + case "picker": return KeyValue.CHANGE_METHOD; } } diff --git a/srcs/juloo.keyboard2/EditorConfig.java b/srcs/juloo.keyboard2/EditorConfig.java index 67b7799..a198775 100644 --- a/srcs/juloo.keyboard2/EditorConfig.java +++ b/srcs/juloo.keyboard2/EditorConfig.java @@ -9,10 +9,12 @@ import juloo.keyboard2.suggestions.CandidatesView; public final class EditorConfig { - public String actionLabel = null; // Might be [null] - /** Action performed by the Action key. Only when [actionLabel != null]. */ + /** Key that replaces the "ACTION" key. Might be [null] to remove that key. */ + public KeyValue action_key_replacement = null; + /** Key that replaces the "ENTER" key. Might be [null] to not replace the + enter key. */ + public KeyValue enter_key_replacement = null; public int actionId; - public boolean swapEnterActionKey = false; // Swap the "enter" and "action" keys /** Whether selection mode turns on automatically when text is selected. */ public boolean selection_mode_enabled = true; /** Whether the numeric layout should be shown by default. */ @@ -46,18 +48,29 @@ public final class EditorConfig /* Selection mode. Editors with [TYPE_NULL] are for example Termux and Emacs. */ selection_mode_enabled = inputType != InputType.TYPE_NULL; + enter_key_replacement = null; /* Action key. Looks at [info.actionLabel] first. */ if (info.actionLabel != null) { - actionLabel = info.actionLabel.toString(); actionId = info.actionId; - swapEnterActionKey = false; + action_key_replacement = + KeyValue.makeActionKey(info.actionLabel.toString()); } else { actionId = options & EditorInfo.IME_MASK_ACTION; - actionLabel = actionLabel_of_imeAction(actionId, res); - swapEnterActionKey = (options & EditorInfo.IME_FLAG_NO_ENTER_ACTION) == 0; + String label = actionLabel_of_imeAction(actionId, res); + action_key_replacement = null; + if (label != null) + { + action_key_replacement = KeyValue.makeActionKey(label); + // Swap the enter and action keys + if ((options & EditorInfo.IME_FLAG_NO_ENTER_ACTION) == 0) + { + enter_key_replacement = action_key_replacement; + action_key_replacement = KeyValue.ENTER; + } + } } /* Numeric layout */ switch (inputType) diff --git a/srcs/juloo.keyboard2/KeyModifier.java b/srcs/juloo.keyboard2/KeyModifier.java index cf9af3b..c40c342 100644 --- a/srcs/juloo.keyboard2/KeyModifier.java +++ b/srcs/juloo.keyboard2/KeyModifier.java @@ -109,9 +109,9 @@ public final class KeyModifier { case CHANGE_METHOD_PREV: case CHANGE_METHOD_NEXT: - return KeyValue.getKeyByName("change_method"); + return KeyValue.CHANGE_METHOD; case SWITCH_VOICE_TYPING: - return KeyValue.getKeyByName("voice_typing_chooser"); + return KeyValue.VOICE_TYPING_CHOOSER; } break; } @@ -150,7 +150,7 @@ public final class KeyModifier return res; /* Tapping compose again exits the pending sequence. */ case Compose_pending: - return KeyValue.getKeyByName("compose_cancel"); + return KeyValue.COMPOSE_CANCEL; /* These keys are not greyed. */ case Event: case Modifier: diff --git a/srcs/juloo.keyboard2/KeyValue.java b/srcs/juloo.keyboard2/KeyValue.java index 7cb50a4..1fb84f4 100644 --- a/srcs/juloo.keyboard2/KeyValue.java +++ b/srcs/juloo.keyboard2/KeyValue.java @@ -523,6 +523,18 @@ public final class KeyValue implements Comparable } } + /** Keys constants. Keys which are accessed from the application's code. */ + public static final KeyValue ENTER = keyeventKey(0xE00E, KeyEvent.KEYCODE_ENTER, 0); + public static final KeyValue CONFIG = eventKey(0xE004, Event.CONFIG, FLAG_SMALLER_FONT); + public static final KeyValue SHIFT = modifierKey(0xE00A, Modifier.SHIFT, FLAG_DOUBLE_TAP_LOCK); + public static final KeyValue COMPOSE = makeComposePending(0xE016, ComposeKeyData.compose, FLAG_SECONDARY); + public static final KeyValue SELECTION_MODE = makeInternalModifier(Modifier.SELECTION_MODE); + public static final KeyValue CHANGE_METHOD = eventKey(0xE009, Event.CHANGE_METHOD_PICKER, FLAG_SMALLER_FONT); + public static final KeyValue CHANGE_METHOD_PREV = eventKey(0xE009, Event.CHANGE_METHOD_PREV, FLAG_SMALLER_FONT); + public static final KeyValue CHANGE_METHOD_NEXT = eventKey(0xE009, Event.CHANGE_METHOD_NEXT, FLAG_SMALLER_FONT); + public static final KeyValue VOICE_TYPING_CHOOSER = eventKey(0xE015, Event.SWITCH_VOICE_TYPING_CHOOSER, FLAG_SMALLER_FONT); + public static final KeyValue COMPOSE_CANCEL = placeholderKey(0xE01A, Placeholder.COMPOSE_CANCEL, FLAG_SECONDARY); + public static KeyValue getSpecialKeyByName(String name) { switch (name) @@ -536,7 +548,7 @@ public final class KeyValue implements Comparable case "\\\\": return makeStringKey("\\"); /* Modifiers and dead-keys */ - case "shift": return modifierKey(0xE00A, Modifier.SHIFT, FLAG_DOUBLE_TAP_LOCK); + case "shift": return SHIFT; case "ctrl": return modifierKey("Ctrl", Modifier.CTRL, 0); case "alt": return modifierKey("Alt", Modifier.ALT, 0); case "accent_aigu": return diacritic(0xE050, Modifier.AIGU); @@ -617,7 +629,7 @@ public final class KeyValue implements Comparable case "combining_palatalization": return makeCharKey(0xE223, '\u0484', 0); /* Special event keys */ - case "config": return eventKey(0xE004, Event.CONFIG, FLAG_SMALLER_FONT); + case "config": return CONFIG; case "switch_text": return eventKey("ABC", Event.SWITCH_TEXT, FLAG_SMALLER_FONT); case "switch_numeric": return eventKey("123+", Event.SWITCH_NUMERIC, FLAG_SMALLER_FONT); case "switch_emoji": return eventKey(0xE001, Event.SWITCH_EMOJI, FLAG_SMALLER_FONT); @@ -627,17 +639,17 @@ public final class KeyValue implements Comparable case "switch_forward": return eventKey(0xE013, Event.SWITCH_FORWARD, FLAG_SMALLER_FONT); case "switch_backward": return eventKey(0xE014, Event.SWITCH_BACKWARD, FLAG_SMALLER_FONT); case "switch_greekmath": return eventKey("πλ∇¬", Event.SWITCH_GREEKMATH, FLAG_SMALLER_FONT); - case "change_method": return eventKey(0xE009, Event.CHANGE_METHOD_PICKER, FLAG_SMALLER_FONT); - case "change_method_prev": return eventKey(0xE009, Event.CHANGE_METHOD_PREV, FLAG_SMALLER_FONT); - case "change_method_next": return eventKey(0xE009, Event.CHANGE_METHOD_NEXT, FLAG_SMALLER_FONT); + case "change_method": return CHANGE_METHOD; + case "change_method_prev": return CHANGE_METHOD_PREV; + case "change_method_next": return CHANGE_METHOD_NEXT; case "action": return eventKey("Action", Event.ACTION, FLAG_SMALLER_FONT); // Will always be replaced case "capslock": return eventKey(0xE012, Event.CAPS_LOCK, 0); case "voice_typing": return eventKey(0xE015, Event.SWITCH_VOICE_TYPING, FLAG_SMALLER_FONT); - case "voice_typing_chooser": return eventKey(0xE015, Event.SWITCH_VOICE_TYPING_CHOOSER, FLAG_SMALLER_FONT); + case "voice_typing_chooser": return VOICE_TYPING_CHOOSER; /* Key events */ case "esc": return keyeventKey("Esc", KeyEvent.KEYCODE_ESCAPE, FLAG_SMALLER_FONT); - case "enter": return keyeventKey(0xE00E, KeyEvent.KEYCODE_ENTER, 0); + case "enter": return ENTER; case "up": return keyeventKey(0xE005, KeyEvent.KEYCODE_DPAD_UP, 0); case "right": return keyeventKey(0xE006, KeyEvent.KEYCODE_DPAD_RIGHT, FLAG_SMALLER_FONT); case "down": return keyeventKey(0xE007, KeyEvent.KEYCODE_DPAD_DOWN, 0); @@ -740,8 +752,8 @@ public final class KeyValue implements Comparable case "autofill": return editingKey("auto", Editing.AUTOFILL); /* The compose key */ - case "compose": return makeComposePending(0xE016, ComposeKeyData.compose, FLAG_SECONDARY); - case "compose_cancel": return placeholderKey(0xE01A, Placeholder.COMPOSE_CANCEL, FLAG_SECONDARY); + case "compose": return COMPOSE; + case "compose_cancel": return COMPOSE_CANCEL; /* Placeholder keys */ case "removed": return placeholderKey(Placeholder.REMOVED); @@ -816,7 +828,7 @@ public final class KeyValue implements Comparable return makeStringKey(name, FLAG_SMALLER_FONT); /* Internal keys */ - case "selection_mode": return makeInternalModifier(Modifier.SELECTION_MODE); + case "selection_mode": return SELECTION_MODE; default: return null; } diff --git a/srcs/juloo.keyboard2/Keyboard2View.java b/srcs/juloo.keyboard2/Keyboard2View.java index f0fd879..d93c8bb 100644 --- a/srcs/juloo.keyboard2/Keyboard2View.java +++ b/srcs/juloo.keyboard2/Keyboard2View.java @@ -27,11 +27,9 @@ public class Keyboard2View extends View /** The key holding the shift key is used to set shift state from autocapitalisation. */ - private KeyValue _shift_kv; private KeyboardData.Key _shift_key; /** Used to add fake pointers. */ - private KeyValue _compose_kv; private KeyboardData.Key _compose_key; private Pointers _pointers; @@ -109,10 +107,8 @@ public class Keyboard2View extends View public void setKeyboard(KeyboardData kw) { _keyboard = kw; - _shift_kv = KeyValue.getKeyByName("shift"); - _shift_key = _keyboard.findKeyWithValue(_shift_kv); - _compose_kv = KeyValue.getKeyByName("compose"); - _compose_key = _keyboard.findKeyWithValue(_compose_kv); + _shift_key = _keyboard.findKeyWithValue(KeyValue.SHIFT); + _compose_key = _keyboard.findKeyWithValue(KeyValue.COMPOSE); KeyModifier.set_modmap(_keyboard.modmap); reset(); } @@ -136,13 +132,13 @@ public class Keyboard2View extends View /** Called by auto-capitalisation. */ public void set_shift_state(boolean latched, boolean lock) { - set_fake_ptr_latched(_shift_key, _shift_kv, latched, lock); + set_fake_ptr_latched(_shift_key, KeyValue.SHIFT, latched, lock); } /** Called from [KeyEventHandler]. */ public void set_compose_pending(boolean pending) { - set_fake_ptr_latched(_compose_key, _compose_kv, pending, false); + set_fake_ptr_latched(_compose_key, KeyValue.COMPOSE, pending, false); } /** Called from [Keybard2.onUpdateSelection]. */ @@ -150,7 +146,7 @@ public class Keyboard2View extends View { if (_config.editor_config.selection_mode_enabled) set_fake_ptr_latched(KeyboardData.Key.EMPTY, - KeyValue.getKeyByName("selection_mode"), selection_state, true); + KeyValue.SELECTION_MODE, selection_state, true); } public KeyValue modifyKey(KeyValue k, Pointers.Modifiers mods) diff --git a/srcs/juloo.keyboard2/LayoutModifier.java b/srcs/juloo.keyboard2/LayoutModifier.java index 4a79314..163ff4e 100644 --- a/srcs/juloo.keyboard2/LayoutModifier.java +++ b/srcs/juloo.keyboard2/LayoutModifier.java @@ -31,7 +31,7 @@ public final class LayoutModifier final Set remove_keys = new HashSet(); // Make sure the config key is accessible to avoid being locked in a custom // layout. - extra_keys.put(KeyValue.getKeyByName("config"), KeyboardData.PreferredPos.ANYWHERE); + extra_keys.put(KeyValue.CONFIG, KeyboardData.PreferredPos.ANYWHERE); extra_keys.putAll(globalConfig.extra_keys_param); extra_keys.putAll(globalConfig.extra_keys_custom); // Number row and numpads are added after the modification pass to allow @@ -158,12 +158,7 @@ public final class LayoutModifier case CHANGE_METHOD_PICKER: return globalConfig.change_method_key_replacement; case ACTION: - String action_label = ec.actionLabel; - if (action_label == null) - return null; // Remove the action key - if (ec.swapEnterActionKey) - return KeyValue.getKeyByName("enter"); - return KeyValue.makeActionKey(action_label); + return ec.action_key_replacement; case SWITCH_FORWARD: return (globalConfig.layouts.size() > 1) ? orig : null; case SWITCH_BACKWARD: @@ -177,8 +172,8 @@ public final class LayoutModifier switch (orig.getKeyevent()) { case KeyEvent.KEYCODE_ENTER: - if (ec.swapEnterActionKey && ec.actionLabel != null) - return KeyValue.makeActionKey(ec.actionLabel); + if (ec.enter_key_replacement != null) + return ec.enter_key_replacement; break; } break; diff --git a/srcs/juloo.keyboard2/Logs.java b/srcs/juloo.keyboard2/Logs.java index 1bef51c..72813b9 100644 --- a/srcs/juloo.keyboard2/Logs.java +++ b/srcs/juloo.keyboard2/Logs.java @@ -23,9 +23,6 @@ public final class Logs info.dump(_debug_logs, ""); if (info.extras != null) _debug_logs.println("extras: "+info.extras.toString()); - _debug_logs.println("swapEnterActionKey: " - +conf.editor_config.swapEnterActionKey); - _debug_logs.println("actionLabel: "+conf.editor_config.actionLabel); } public static void debug_config_migration(int from_version, int to_version) -- cgit v1.2.3