From e3f9341ed1ad35000cb14f92eb26687a429b548d Mon Sep 17 00:00:00 2001 From: Jules Aguillon Date: Sun, 12 Jan 2025 23:24:04 +0100 Subject: [PATCH] Refactor: KeyValue: Simplify StringWithSymbol This removes the Complex key kind and class by making StringWithSymbol a new kind of key. --- srcs/juloo.keyboard2/KeyEventHandler.java | 12 +--- srcs/juloo.keyboard2/KeyValue.java | 83 +++++++---------------- 2 files changed, 24 insertions(+), 71 deletions(-) diff --git a/srcs/juloo.keyboard2/KeyEventHandler.java b/srcs/juloo.keyboard2/KeyEventHandler.java index 471fad00a..eba488a8e 100644 --- a/srcs/juloo.keyboard2/KeyEventHandler.java +++ b/srcs/juloo.keyboard2/KeyEventHandler.java @@ -100,7 +100,7 @@ public void key_up(KeyValue key, Pointers.Modifiers mods) _recv.set_compose_pending(true); break; case Slider: handle_slider(key.getSlider(), key.getSliderRepeat()); break; - case Complex: send_complex_key(key.getComplexKind(), key.getComplex()); break; + case StringWithSymbol: send_text(key.getStringWithSymbol()); break; } update_meta_state(old_mods); } @@ -219,16 +219,6 @@ void send_context_menu_action(int id) conn.performContextMenuAction(id); } - void send_complex_key(KeyValue.Complex.Kind kind, KeyValue.Complex val) - { - switch (kind) - { - case StringWithSymbol: - send_text(((KeyValue.Complex.StringWithSymbol)val).str); - break; - } - } - @SuppressLint("InlinedApi") void handle_editing_key(KeyValue.Editing ev) { diff --git a/srcs/juloo.keyboard2/KeyValue.java b/srcs/juloo.keyboard2/KeyValue.java index 359d1d3bf..fea03faa3 100644 --- a/srcs/juloo.keyboard2/KeyValue.java +++ b/srcs/juloo.keyboard2/KeyValue.java @@ -90,10 +90,11 @@ public static enum Placeholder public static enum Kind { - Char, String, Keyevent, Event, Compose_pending, Hangul_initial, - Hangul_medial, Modifier, Editing, Placeholder, + Char, Keyevent, Event, Compose_pending, Hangul_initial, Hangul_medial, + Modifier, Editing, Placeholder, + String, // [_payload] is also the string to output, value is unused. Slider, // [_payload] is a [KeyValue.Slider], value is slider repeatition. - Complex, // [_payload] is a [KeyValue.Complex], value is [Complex.Kind]. + StringWithSymbol, // [_payload] is a [KeyValue.StringWithSymbol], value is unused. } private static final int FLAGS_OFFSET = 19; @@ -131,10 +132,7 @@ public static enum Kind check((((Kind.values().length - 1) << KIND_OFFSET) & ~KIND_BITS) == 0); } - /** - * [_payload.toString()] is the symbol that is rendered on the keyboard. - * For keys of kind [String], this is also the string to output. - */ + /** [_payload.toString()] is the symbol that is rendered on the keyboard. */ private final Comparable _payload; /** This field encodes three things: Kind (KIND_BITS), flags (FLAGS_BITS) and @@ -225,16 +223,10 @@ public int getSliderRepeat() return ((int)(short)(_code & VALUE_BITS)); } - /** Defined only when [getKind() == Kind.Complex]. */ - public Complex getComplex() - { - return (Complex)_payload; - } - - /** Defined only when [getKind() == Kind.Complex]. */ - public Complex.Kind getComplexKind() + /** Defined only when [getKind() == Kind.StringWithSymbol]. */ + public String getStringWithSymbol() { - return Complex.Kind.values()[(_code & VALUE_BITS)]; + return ((StringWithSymbol)_payload).str; } /* Update the char and the symbol. */ @@ -303,11 +295,6 @@ private KeyValue(Comparable p, int kind, int value, int flags) _code = (kind & KIND_BITS) | (flags & FLAGS_BITS) | (value & VALUE_BITS); } - private KeyValue(Complex p, Complex.Kind value, int flags) - { - this(p, Kind.Complex, value.ordinal(), flags); - } - public KeyValue(Comparable p, Kind k, int v, int f) { this(p, (k.ordinal() << KIND_OFFSET), v, f); @@ -463,8 +450,8 @@ public static KeyValue makeStringKey(String str, int flags) public static KeyValue makeStringKeyWithSymbol(String str, String symbol, int flags) { - return new KeyValue(new Complex.StringWithSymbol(str, symbol), - Complex.Kind.StringWithSymbol, flags); + return new KeyValue(new StringWithSymbol(str, symbol), + Kind.StringWithSymbol, 0, flags); } /** Make a modifier key for passing to [KeyModifier]. */ @@ -753,50 +740,26 @@ private static void check(boolean b) throw new RuntimeException("Assertion failure"); } - public static abstract class Complex implements Comparable + public static final class StringWithSymbol implements Comparable { - public abstract String getSymbol(); + public final String str; + final String _symbol; - /** [compareTo] can assume that [snd] is an instance of the same class. */ - @Override - public abstract int compareTo(Complex snd); - - @Override - public String toString() + public StringWithSymbol(String _str, String _sym) { - return getSymbol(); + str = _str; + _symbol = _sym; } - /** [hashCode] will be called on this class. */ - - /** The kind is stored in the [value] field of the key. */ - public static enum Kind - { - StringWithSymbol, - } + @Override + public String toString() { return _symbol; } - public static final class StringWithSymbol extends Complex + @Override + public int compareTo(StringWithSymbol snd) { - public final String str; - private final String _symbol; - - public StringWithSymbol(String _str, String _sym) - { - str = _str; - _symbol = _sym; - } - - @Override - public String getSymbol() { return _symbol; } - - @Override - public int compareTo(Complex _snd) - { - StringWithSymbol snd = (StringWithSymbol)_snd; - int d = str.compareTo(snd.str); - if (d != 0) return d; - return _symbol.compareTo(snd._symbol); - } + int d = str.compareTo(snd.str); + if (d != 0) return d; + return _symbol.compareTo(snd._symbol); } };