Skip to content

Commit

Permalink
Refactor: KeyValue: Simplify StringWithSymbol
Browse files Browse the repository at this point in the history
This removes the Complex key kind and class by making StringWithSymbol a
new kind of key.
  • Loading branch information
Julow committed Jan 12, 2025
1 parent 3ea5c8d commit e3f9341
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 71 deletions.
12 changes: 1 addition & 11 deletions srcs/juloo.keyboard2/KeyEventHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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)
{
Expand Down
83 changes: 23 additions & 60 deletions srcs/juloo.keyboard2/KeyValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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. */
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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]. */
Expand Down Expand Up @@ -753,50 +740,26 @@ private static void check(boolean b)
throw new RuntimeException("Assertion failure");
}

public static abstract class Complex implements Comparable<Complex>
public static final class StringWithSymbol implements Comparable<StringWithSymbol>
{
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);
}
};

Expand Down

0 comments on commit e3f9341

Please sign in to comment.