Remove the explicit hashmap in KeyValue

Refactoring. Predefined keys are represented by a big switch statement
rather than added into a hashmap.
This commit is contained in:
Jules Aguillon 2023-01-29 19:38:54 +01:00
parent 7bc93c470e
commit fecc4dd70a

View File

@ -193,8 +193,6 @@ final class KeyValue
return _symbol.hashCode() + _code; return _symbol.hashCode() + _code;
} }
private static HashMap<String, KeyValue> keys = new HashMap<String, KeyValue>();
public KeyValue(String s, int kind, int value, int flags) public KeyValue(String s, int kind, int value, int flags)
{ {
check((kind & ~KIND_BITS) == 0); check((kind & ~KIND_BITS) == 0);
@ -209,69 +207,53 @@ final class KeyValue
this(s, (k.ordinal() << 29), v, f); this(s, (k.ordinal() << 29), v, f);
} }
public static KeyValue getKeyByName(String name) private static KeyValue charKey(String symbol, char c, int flags)
{ {
KeyValue kv = keys.get(name); return new KeyValue(symbol, Kind.Char, c, flags);
if (kv != null)
return kv;
if (name.length() == 1)
return new KeyValue(name, Kind.Char, name.charAt(0), 0);
else
return new KeyValue(name, Kind.String, 0, 0);
} }
private static void addKey(String name, String symbol, Kind kind, int code, int flags) private static KeyValue modifierKey(String symbol, Modifier m, int flags)
{
keys.put(name, new KeyValue(symbol, kind, code, flags));
}
private static void addCharKey(String name, String symbol, char c, int flags)
{
addKey(name, symbol, Kind.Char, c, flags);
}
private static void addModifierKey(String name, String symbol, Modifier m, int flags)
{ {
if (symbol.length() > 1) if (symbol.length() > 1)
flags |= FLAG_SMALLER_FONT; flags |= FLAG_SMALLER_FONT;
addKey(name, symbol, Kind.Modifier, m.ordinal(), return new KeyValue(symbol, Kind.Modifier, m.ordinal(),
FLAG_LATCH | FLAG_SPECIAL | FLAG_SECONDARY | flags); FLAG_LATCH | FLAG_SPECIAL | FLAG_SECONDARY | flags);
} }
private static void addModifierKey(String name, int symbol, Modifier m, int flags) private static KeyValue modifierKey(int symbol, Modifier m, int flags)
{ {
addModifierKey(name, String.valueOf((char)symbol), m, flags | FLAG_KEY_FONT); return modifierKey(String.valueOf((char)symbol), m, flags | FLAG_KEY_FONT);
} }
private static void addDiacritic(String name, int symbol, Modifier m) private static KeyValue diacritic(int symbol, Modifier m)
{ {
addKey(name, String.valueOf((char)symbol), Kind.Modifier, m.ordinal(), return new KeyValue(String.valueOf((char)symbol), Kind.Modifier, m.ordinal(),
FLAG_LATCH | FLAG_SPECIAL | FLAG_KEY_FONT); FLAG_LATCH | FLAG_SPECIAL | FLAG_KEY_FONT);
} }
private static void addEventKey(String name, String symbol, Event e, int flags) private static KeyValue eventKey(String symbol, Event e, int flags)
{ {
addKey(name, symbol, Kind.Event, e.ordinal(), flags | FLAG_SPECIAL | FLAG_SECONDARY); return new KeyValue(symbol, Kind.Event, e.ordinal(), flags | FLAG_SPECIAL | FLAG_SECONDARY);
} }
private static void addEventKey(String name, int symbol, Event e, int flags) private static KeyValue eventKey(int symbol, Event e, int flags)
{ {
addEventKey(name, String.valueOf((char)symbol), e, flags | FLAG_KEY_FONT); return eventKey(String.valueOf((char)symbol), e, flags | FLAG_KEY_FONT);
} }
private static void addKeyeventKey(String name, String symbol, int code, int flags) private static KeyValue keyeventKey(String symbol, int code, int flags)
{ {
addKey(name, symbol, Kind.Keyevent, code, flags | FLAG_SECONDARY); return new KeyValue(symbol, Kind.Keyevent, code, flags | FLAG_SECONDARY);
} }
private static void addKeyeventKey(String name, int symbol, int code, int flags) private static KeyValue keyeventKey(int symbol, int code, int flags)
{ {
addKeyeventKey(name, String.valueOf((char)symbol), code, flags | FLAG_KEY_FONT); return keyeventKey(String.valueOf((char)symbol), code, flags | FLAG_KEY_FONT);
} }
private static void addEditingKey(String name, String symbol, Editing action) private static KeyValue editingKey(String symbol, Editing action)
{ {
addKey(name, symbol, Kind.Editing, action.ordinal(), return new KeyValue(symbol, Kind.Editing, action.ordinal(),
FLAG_SPECIAL | FLAG_SECONDARY | FLAG_SMALLER_FONT); FLAG_SPECIAL | FLAG_SECONDARY | FLAG_SMALLER_FONT);
} }
@ -280,98 +262,110 @@ final class KeyValue
/** Use a unique id as the value because the symbol is shared between every /** Use a unique id as the value because the symbol is shared between every
placeholders (it is the empty string). */ placeholders (it is the empty string). */
private static void addPlaceholderKey(String name) private static KeyValue placeholderKey()
{ {
addKey(name, "", Kind.String, placeholder_unique_id++, 0); return new KeyValue("", Kind.String, placeholder_unique_id++, 0);
} }
static private static KeyValue fallbackMakeKey(String name)
{ {
addModifierKey("shift", 0x0A, Modifier.SHIFT, 0); if (name.length() == 1)
addModifierKey("ctrl", "Ctrl", Modifier.CTRL, 0); return new KeyValue(name, Kind.Char, name.charAt(0), 0);
addModifierKey("alt", "Alt", Modifier.ALT, 0); else
addDiacritic("accent_aigu", 0x50, Modifier.AIGU); return new KeyValue(name, Kind.String, 0, 0);
addDiacritic("accent_caron", 0x51, Modifier.CARON); }
addDiacritic("accent_cedille", 0x52, Modifier.CEDILLE);
addDiacritic("accent_circonflexe", 0x53, Modifier.CIRCONFLEXE);
addDiacritic("accent_grave", 0x54, Modifier.GRAVE);
addDiacritic("accent_macron", 0x55, Modifier.MACRON);
addDiacritic("accent_ring", 0x56, Modifier.RING);
addDiacritic("accent_tilde", 0x57, Modifier.TILDE);
addDiacritic("accent_trema", 0x58, Modifier.TREMA);
addDiacritic("accent_ogonek", 0x59, Modifier.OGONEK);
addDiacritic("accent_dot_above", 0x5A, Modifier.DOT_ABOVE);
addDiacritic("accent_double_aigu", 0x5B, Modifier.DOUBLE_AIGU);
addDiacritic("accent_slash", 0x5C, Modifier.SLASH);
addDiacritic("accent_arrow_right", 0x5D, Modifier.ARROW_RIGHT);
addDiacritic("accent_breve", 0x5E, Modifier.BREVE);
addDiacritic("accent_bar", 0x5F, Modifier.BAR);
addModifierKey("superscript", "Sup", Modifier.SUPERSCRIPT, 0);
addModifierKey("subscript", "Sub", Modifier.SUBSCRIPT, 0);
addModifierKey("ordinal", "Ord", Modifier.ORDINAL, 0);
addModifierKey("arrows", "Arr", Modifier.ARROWS, 0);
addModifierKey("box", "Box", Modifier.BOX, 0);
addModifierKey("fn", "Fn", Modifier.FN, 0);
addModifierKey("meta", "Meta", Modifier.META, 0);
addEventKey("config", 0x04, Event.CONFIG, FLAG_SMALLER_FONT); public static KeyValue getKeyByName(String name)
addEventKey("switch_text", "ABC", Event.SWITCH_TEXT, FLAG_SMALLER_FONT); {
addEventKey("switch_numeric", "123+", Event.SWITCH_NUMERIC, FLAG_SMALLER_FONT); switch (name)
addEventKey("switch_emoji", 0x01, Event.SWITCH_EMOJI, FLAG_SMALLER_FONT); {
addEventKey("switch_back_emoji", "ABC", Event.SWITCH_BACK_EMOJI, 0); case "shift": return modifierKey(0x0A, Modifier.SHIFT, 0);
addEventKey("switch_second", 0x13, Event.SWITCH_SECOND, FLAG_SMALLER_FONT); case "ctrl": return modifierKey("Ctrl", Modifier.CTRL, 0);
addEventKey("switch_second_back", 0x14, Event.SWITCH_SECOND_BACK, FLAG_SMALLER_FONT); case "alt": return modifierKey("Alt", Modifier.ALT, 0);
addEventKey("switch_greekmath", "πλ∇¬", Event.SWITCH_GREEKMATH, FLAG_SMALLER_FONT); case "accent_aigu": return diacritic(0x50, Modifier.AIGU);
addEventKey("change_method", 0x09, Event.CHANGE_METHOD, FLAG_SMALLER_FONT); case "accent_caron": return diacritic(0x51, Modifier.CARON);
addEventKey("action", "Action", Event.ACTION, FLAG_SMALLER_FONT); // Will always be replaced case "accent_cedille": return diacritic(0x52, Modifier.CEDILLE);
addEventKey("capslock", 0x12, Event.CAPS_LOCK, 0); case "accent_circonflexe": return diacritic(0x53, Modifier.CIRCONFLEXE);
case "accent_grave": return diacritic(0x54, Modifier.GRAVE);
case "accent_macron": return diacritic(0x55, Modifier.MACRON);
case "accent_ring": return diacritic(0x56, Modifier.RING);
case "accent_tilde": return diacritic(0x57, Modifier.TILDE);
case "accent_trema": return diacritic(0x58, Modifier.TREMA);
case "accent_ogonek": return diacritic(0x59, Modifier.OGONEK);
case "accent_dot_above": return diacritic(0x5A, Modifier.DOT_ABOVE);
case "accent_double_aigu": return diacritic(0x5B, Modifier.DOUBLE_AIGU);
case "accent_slash": return diacritic(0x5C, Modifier.SLASH);
case "accent_arrow_right": return diacritic(0x5D, Modifier.ARROW_RIGHT);
case "accent_breve": return diacritic(0x5E, Modifier.BREVE);
case "accent_bar": return diacritic(0x5F, Modifier.BAR);
case "superscript": return modifierKey("Sup", Modifier.SUPERSCRIPT, 0);
case "subscript": return modifierKey("Sub", Modifier.SUBSCRIPT, 0);
case "ordinal": return modifierKey("Ord", Modifier.ORDINAL, 0);
case "arrows": return modifierKey("Arr", Modifier.ARROWS, 0);
case "box": return modifierKey("Box", Modifier.BOX, 0);
case "fn": return modifierKey("Fn", Modifier.FN, 0);
case "meta": return modifierKey("Meta", Modifier.META, 0);
addKeyeventKey("esc", "Esc", KeyEvent.KEYCODE_ESCAPE, FLAG_SMALLER_FONT); case "config": return eventKey(0x04, Event.CONFIG, FLAG_SMALLER_FONT);
addKeyeventKey("enter", 0x0E, KeyEvent.KEYCODE_ENTER, 0); case "switch_text": return eventKey("ABC", Event.SWITCH_TEXT, FLAG_SMALLER_FONT);
addKeyeventKey("up", 0x05, KeyEvent.KEYCODE_DPAD_UP, 0); case "switch_numeric": return eventKey("123+", Event.SWITCH_NUMERIC, FLAG_SMALLER_FONT);
addKeyeventKey("right", 0x06, KeyEvent.KEYCODE_DPAD_RIGHT, 0); case "switch_emoji": return eventKey(0x01, Event.SWITCH_EMOJI, FLAG_SMALLER_FONT);
addKeyeventKey("down", 0x07, KeyEvent.KEYCODE_DPAD_DOWN, 0); case "switch_back_emoji": return eventKey("ABC", Event.SWITCH_BACK_EMOJI, 0);
addKeyeventKey("left", 0x08, KeyEvent.KEYCODE_DPAD_LEFT, 0); case "switch_second": return eventKey(0x13, Event.SWITCH_SECOND, FLAG_SMALLER_FONT);
addKeyeventKey("page_up", 0x02, KeyEvent.KEYCODE_PAGE_UP, 0); case "switch_second_back": return eventKey(0x14, Event.SWITCH_SECOND_BACK, FLAG_SMALLER_FONT);
addKeyeventKey("page_down", 0x03, KeyEvent.KEYCODE_PAGE_DOWN, 0); case "switch_greekmath": return eventKey("πλ∇¬", Event.SWITCH_GREEKMATH, FLAG_SMALLER_FONT);
addKeyeventKey("home", 0x0B, KeyEvent.KEYCODE_MOVE_HOME, 0); case "change_method": return eventKey(0x09, Event.CHANGE_METHOD, FLAG_SMALLER_FONT);
addKeyeventKey("end", 0x0C, KeyEvent.KEYCODE_MOVE_END, 0); case "action": return eventKey("Action", Event.ACTION, FLAG_SMALLER_FONT); // Will always be replaced
addKeyeventKey("backspace", 0x11, KeyEvent.KEYCODE_DEL, 0); case "capslock": return eventKey(0x12, Event.CAPS_LOCK, 0);
addKeyeventKey("delete", 0x10, KeyEvent.KEYCODE_FORWARD_DEL, 0);
addKeyeventKey("insert", "Ins", KeyEvent.KEYCODE_INSERT, FLAG_SMALLER_FONT);
addKeyeventKey("f1", "F1", KeyEvent.KEYCODE_F1, 0);
addKeyeventKey("f2", "F2", KeyEvent.KEYCODE_F2, 0);
addKeyeventKey("f3", "F3", KeyEvent.KEYCODE_F3, 0);
addKeyeventKey("f4", "F4", KeyEvent.KEYCODE_F4, 0);
addKeyeventKey("f5", "F5", KeyEvent.KEYCODE_F5, 0);
addKeyeventKey("f6", "F6", KeyEvent.KEYCODE_F6, 0);
addKeyeventKey("f7", "F7", KeyEvent.KEYCODE_F7, 0);
addKeyeventKey("f8", "F8", KeyEvent.KEYCODE_F8, 0);
addKeyeventKey("f9", "F9", KeyEvent.KEYCODE_F9, 0);
addKeyeventKey("f10", "F10", KeyEvent.KEYCODE_F10, 0);
addKeyeventKey("f11", "F11", KeyEvent.KEYCODE_F11, FLAG_SMALLER_FONT);
addKeyeventKey("f12", "F12", KeyEvent.KEYCODE_F12, FLAG_SMALLER_FONT);
addKeyeventKey("tab", 0x0F, KeyEvent.KEYCODE_TAB, FLAG_SMALLER_FONT);
addCharKey("\\t", "\\t", '\t', 0); // Send the tab character case "esc": return keyeventKey("Esc", KeyEvent.KEYCODE_ESCAPE, FLAG_SMALLER_FONT);
addCharKey("space", "\r", ' ', FLAG_KEY_FONT | FLAG_SECONDARY); case "enter": return keyeventKey(0x0E, KeyEvent.KEYCODE_ENTER, 0);
addCharKey("nbsp", "\u237d", '\u00a0', FLAG_SMALLER_FONT); case "up": return keyeventKey(0x05, KeyEvent.KEYCODE_DPAD_UP, 0);
case "right": return keyeventKey(0x06, KeyEvent.KEYCODE_DPAD_RIGHT, 0);
case "down": return keyeventKey(0x07, KeyEvent.KEYCODE_DPAD_DOWN, 0);
case "left": return keyeventKey(0x08, KeyEvent.KEYCODE_DPAD_LEFT, 0);
case "page_up": return keyeventKey(0x02, KeyEvent.KEYCODE_PAGE_UP, 0);
case "page_down": return keyeventKey(0x03, KeyEvent.KEYCODE_PAGE_DOWN, 0);
case "home": return keyeventKey(0x0B, KeyEvent.KEYCODE_MOVE_HOME, 0);
case "end": return keyeventKey(0x0C, KeyEvent.KEYCODE_MOVE_END, 0);
case "backspace": return keyeventKey(0x11, KeyEvent.KEYCODE_DEL, 0);
case "delete": return keyeventKey(0x10, KeyEvent.KEYCODE_FORWARD_DEL, 0);
case "insert": return keyeventKey("Ins", KeyEvent.KEYCODE_INSERT, FLAG_SMALLER_FONT);
case "f1": return keyeventKey("F1", KeyEvent.KEYCODE_F1, 0);
case "f2": return keyeventKey("F2", KeyEvent.KEYCODE_F2, 0);
case "f3": return keyeventKey("F3", KeyEvent.KEYCODE_F3, 0);
case "f4": return keyeventKey("F4", KeyEvent.KEYCODE_F4, 0);
case "f5": return keyeventKey("F5", KeyEvent.KEYCODE_F5, 0);
case "f6": return keyeventKey("F6", KeyEvent.KEYCODE_F6, 0);
case "f7": return keyeventKey("F7", KeyEvent.KEYCODE_F7, 0);
case "f8": return keyeventKey("F8", KeyEvent.KEYCODE_F8, 0);
case "f9": return keyeventKey("F9", KeyEvent.KEYCODE_F9, 0);
case "f10": return keyeventKey("F10", KeyEvent.KEYCODE_F10, 0);
case "f11": return keyeventKey("F11", KeyEvent.KEYCODE_F11, FLAG_SMALLER_FONT);
case "f12": return keyeventKey("F12", KeyEvent.KEYCODE_F12, FLAG_SMALLER_FONT);
case "tab": return keyeventKey(0x0F, KeyEvent.KEYCODE_TAB, FLAG_SMALLER_FONT);
addPlaceholderKey("removed"); case "\\t": return charKey("\\t", '\t', 0); // Send the tab character
addPlaceholderKey("f11_placeholder"); case "space": return charKey("\r", ' ', FLAG_KEY_FONT | FLAG_SECONDARY);
addPlaceholderKey("f12_placeholder"); case "nbsp": return charKey("\u237d", '\u00a0', FLAG_SMALLER_FONT);
addEditingKey("copy", "copy", Editing.COPY); case "removed": return placeholderKey();
addEditingKey("paste", "paste", Editing.PASTE); case "f11_placeholder": return placeholderKey();
addEditingKey("cut", "cut", Editing.CUT); case "f12_placeholder": return placeholderKey();
addEditingKey("selectAll", "s. all", Editing.SELECT_ALL);
addEditingKey("shareText", "share", Editing.SHARE); case "copy": return editingKey("copy", Editing.COPY);
addEditingKey("pasteAsPlainText", "<paste>", Editing.PASTE_PLAIN); case "paste": return editingKey("paste", Editing.PASTE);
addEditingKey("undo", "undo", Editing.UNDO); case "cut": return editingKey("cut", Editing.CUT);
addEditingKey("redo", "redo", Editing.REDO); case "selectAll": return editingKey("s. all", Editing.SELECT_ALL);
addEditingKey("replaceText", "repl.", Editing.REPLACE); case "shareText": return editingKey("share", Editing.SHARE);
addEditingKey("textAssist", "assist", Editing.ASSIST); case "pasteAsPlainText": return editingKey("<paste>", Editing.PASTE_PLAIN);
addEditingKey("autofill", "auto.", Editing.AUTOFILL); case "undo": return editingKey("undo", Editing.UNDO);
case "redo": return editingKey("redo", Editing.REDO);
case "replaceText": return editingKey("repl.", Editing.REPLACE);
case "textAssist": return editingKey("assist", Editing.ASSIST);
case "autofill": return editingKey("auto.", Editing.AUTOFILL);
default: return fallbackMakeKey(name);
}
} }
static final HashMap<String, String> keys_descr = new HashMap<String, String>(); static final HashMap<String, String> keys_descr = new HashMap<String, String>();