Make key symbol smaller when 2 characters long or more

This was the case for string keys but not for macros or keys with custom
symbols.
This commit is contained in:
Jules Aguillon 2025-02-28 00:35:43 +01:00
parent b7d1508d7b
commit 55cb5f5315
3 changed files with 19 additions and 6 deletions

View File

@ -233,7 +233,8 @@ public final class KeyValue implements Comparable<KeyValue>
/* Update the char and the symbol. */ /* Update the char and the symbol. */
public KeyValue withChar(char c) public KeyValue withChar(char c)
{ {
return new KeyValue(String.valueOf(c), Kind.Char, c, getFlags()); return new KeyValue(String.valueOf(c), Kind.Char, c,
getFlags() & ~(FLAG_KEY_FONT | FLAG_SMALLER_FONT));
} }
public KeyValue withKeyevent(int code) public KeyValue withKeyevent(int code)
@ -248,6 +249,7 @@ public final class KeyValue implements Comparable<KeyValue>
public KeyValue withSymbol(String symbol) public KeyValue withSymbol(String symbol)
{ {
int flags = getFlags() & ~(FLAG_KEY_FONT | FLAG_SMALLER_FONT);
switch (getKind()) switch (getKind())
{ {
case Char: case Char:
@ -259,11 +261,13 @@ public final class KeyValue implements Comparable<KeyValue>
case Modifier: case Modifier:
case Editing: case Editing:
case Placeholder: case Placeholder:
return new KeyValue(symbol, _code, _code, getFlags()); if (symbol.length() > 1)
flags |= FLAG_SMALLER_FONT;
return new KeyValue(symbol, _code, _code, flags);
case Macro: case Macro:
return makeMacro(symbol, getMacro(), 0); return makeMacro(symbol, getMacro(), flags);
default: default:
return makeMacro(symbol, new KeyValue[]{ this }, 0); return makeMacro(symbol, new KeyValue[]{ this }, flags);
} }
} }
@ -477,6 +481,8 @@ public final class KeyValue implements Comparable<KeyValue>
public static KeyValue makeMacro(String symbol, KeyValue[] keys, int flags) public static KeyValue makeMacro(String symbol, KeyValue[] keys, int flags)
{ {
if (symbol.length() > 1)
flags |= FLAG_SMALLER_FONT;
return new KeyValue(new Macro(keys, symbol), Kind.Macro, 0, flags); return new KeyValue(new Macro(keys, symbol), Kind.Macro, 0, flags);
} }

View File

@ -93,7 +93,7 @@ public class KeyValueParserTest
public void parse_key_event() throws Exception public void parse_key_event() throws Exception
{ {
Utils.parse("a:keyevent:85", KeyValue.keyeventKey("a", 85, 0)); Utils.parse("a:keyevent:85", KeyValue.keyeventKey("a", 85, 0));
Utils.parse("symbol:keyevent:85", KeyValue.keyeventKey("symbol", 85, 0)); Utils.parse("symbol:keyevent:85", KeyValue.keyeventKey("symbol", 85, KeyValue.FLAG_SMALLER_FONT));
Utils.parse("macro:keyevent:85,abc", KeyValue.makeMacro("macro", new KeyValue[]{ Utils.parse("macro:keyevent:85,abc", KeyValue.makeMacro("macro", new KeyValue[]{
KeyValue.keyeventKey("", 85, 0), KeyValue.keyeventKey("", 85, 0),
KeyValue.makeStringKey("abc") KeyValue.makeStringKey("abc")
@ -112,7 +112,7 @@ public class KeyValueParserTest
Utils.parse(":str:'Foo'", KeyValue.makeStringKey("Foo")); Utils.parse(":str:'Foo'", KeyValue.makeStringKey("Foo"));
Utils.parse(":str flags='dim':'Foo'", KeyValue.makeStringKey("Foo", KeyValue.FLAG_SECONDARY)); Utils.parse(":str flags='dim':'Foo'", KeyValue.makeStringKey("Foo", KeyValue.FLAG_SECONDARY));
Utils.parse(":str symbol='Symbol':'Foo'", KeyValue.makeStringKey("Foo").withSymbol("Symbol")); Utils.parse(":str symbol='Symbol':'Foo'", KeyValue.makeStringKey("Foo").withSymbol("Symbol"));
Utils.parse(":str symbol='Symbol' flags='dim':'f'", KeyValue.makeStringKey("f").withSymbol("Symbol").withFlags(KeyValue.FLAG_SECONDARY)); Utils.parse(":str symbol='Symbol' flags='dim':'f'", KeyValue.makeStringKey("f").withSymbol("Symbol").withFlags(KeyValue.FLAG_SECONDARY | KeyValue.FLAG_SMALLER_FONT));
Utils.parse(":str flags='dim,small':'Foo'", KeyValue.makeStringKey("Foo", KeyValue.FLAG_SECONDARY | KeyValue.FLAG_SMALLER_FONT)); Utils.parse(":str flags='dim,small':'Foo'", KeyValue.makeStringKey("Foo", KeyValue.FLAG_SECONDARY | KeyValue.FLAG_SMALLER_FONT));
Utils.parse(":str flags=',,':'Foo'", KeyValue.makeStringKey("Foo")); // Unintentional Utils.parse(":str flags=',,':'Foo'", KeyValue.makeStringKey("Foo")); // Unintentional
Utils.expect_error(":unknown:Foo"); // Unknown kind Utils.expect_error(":unknown:Foo"); // Unknown kind

View File

@ -1,5 +1,6 @@
package juloo.keyboard2; package juloo.keyboard2;
import android.view.KeyEvent;
import juloo.keyboard2.KeyValue; import juloo.keyboard2.KeyValue;
import org.junit.Test; import org.junit.Test;
import static org.junit.Assert.*; import static org.junit.Assert.*;
@ -13,6 +14,12 @@ public class KeyValueTest
{ {
assertEquals(KeyValue.makeStringKey("Foo").withSymbol("Symbol"), assertEquals(KeyValue.makeStringKey("Foo").withSymbol("Symbol"),
KeyValue.makeMacro("Symbol", new KeyValue[] { KeyValue.makeStringKey("Foo") }, 0)); KeyValue.makeMacro("Symbol", new KeyValue[] { KeyValue.makeStringKey("Foo") }, 0));
assertEquals(KeyValue.getSpecialKeyByName("tab"),
KeyValue.keyeventKey(0xE00F, KeyEvent.KEYCODE_TAB, KeyValue.FLAG_KEY_FONT | KeyValue.FLAG_SMALLER_FONT));
assertEquals(KeyValue.getSpecialKeyByName("tab").withSymbol("t"),
KeyValue.keyeventKey("t", KeyEvent.KEYCODE_TAB, 0));
assertEquals(KeyValue.getSpecialKeyByName("tab").withSymbol("tab"),
KeyValue.keyeventKey("tab", KeyEvent.KEYCODE_TAB, KeyValue.FLAG_SMALLER_FONT));
} }
@Test @Test