diff options
| author | Jules Aguillon | 2023-09-10 11:43:56 +0200 |
|---|---|---|
| committer | Jules Aguillon | 2023-09-10 11:43:56 +0200 |
| commit | d771e9d2c7de7d62f5514995f4a224e1e293a376 (patch) | |
| tree | 8593ae2b8ac9fa9040acb04ba42424a845684615 | |
| parent | 44e2e86f19bd9e7acddad50506dad0737fa032d9 (diff) | |
| download | unexpected-keyboard-d771e9d2c7de7d62f5514995f4a224e1e293a376.tar.gz unexpected-keyboard-d771e9d2c7de7d62f5514995f4a224e1e293a376.zip | |
Refactor: Compute key positions in layouts
`KeyboardData.getKeys()` now returns a map of the keys present on the
layout to their position. Positions are the row, column and swipe
direction.
The computed map is cached in the KeyboardData object as it might be
accessed later by `findKeyWithValue`, which now do less work.
| -rw-r--r-- | srcs/juloo.keyboard2/Config.java | 4 | ||||
| -rw-r--r-- | srcs/juloo.keyboard2/Keyboard2View.java | 3 | ||||
| -rw-r--r-- | srcs/juloo.keyboard2/KeyboardData.java | 66 |
3 files changed, 51 insertions, 22 deletions
diff --git a/srcs/juloo.keyboard2/Config.java b/srcs/juloo.keyboard2/Config.java index 30be772..da78f7f 100644 --- a/srcs/juloo.keyboard2/Config.java +++ b/srcs/juloo.keyboard2/Config.java @@ -181,7 +181,7 @@ final class Config if (extra_keys_subtype != null) { Set<KeyValue> present = new HashSet<KeyValue>(); - kw.getKeys(present); + present.addAll(kw.getKeys().keySet()); present.addAll(extra_keys_param); present.addAll(extra_keys_custom); extra_keys_subtype.compute(extra_keys, @@ -189,7 +189,7 @@ final class Config } boolean number_row = this.number_row && !show_numpad; if (number_row) - KeyboardData.number_row.getKeys(remove_keys); + remove_keys.addAll(KeyboardData.number_row.getKeys(0).keySet()); kw = kw.mapKeys(new KeyboardData.MapKeyValues() { public KeyValue apply(KeyValue key, boolean localized) { diff --git a/srcs/juloo.keyboard2/Keyboard2View.java b/srcs/juloo.keyboard2/Keyboard2View.java index 949cdab..3ef9957 100644 --- a/srcs/juloo.keyboard2/Keyboard2View.java +++ b/srcs/juloo.keyboard2/Keyboard2View.java @@ -19,6 +19,9 @@ public class Keyboard2View extends View implements View.OnTouchListener, Pointers.IPointerEventHandler { private KeyboardData _keyboard; + + /** The key holding the shift key is used to set shift state from + autocapitalisation. */ private KeyValue _shift_kv; private KeyboardData.Key _shift_key; diff --git a/srcs/juloo.keyboard2/KeyboardData.java b/srcs/juloo.keyboard2/KeyboardData.java index b5218e4..0083b38 100644 --- a/srcs/juloo.keyboard2/KeyboardData.java +++ b/srcs/juloo.keyboard2/KeyboardData.java @@ -25,6 +25,8 @@ class KeyboardData public final Modmap modmap; /** Might be null. */ public final String script; + /** Position of every keys on the layout, see [getKeys()]. */ + private Map<KeyValue, KeyPos> _key_pos = null; public KeyboardData mapKeys(MapKey f) { @@ -87,19 +89,22 @@ class KeyboardData public Key findKeyWithValue(KeyValue kv) { - for (Row r : rows) - { - Key k = r.findKeyWithValue(kv); - if (k != null) - return k; - } - return null; + KeyPos pos = getKeys().get(kv); + if (pos == null || pos.row >= rows.size()) + return null; + return rows.get(pos.row).get_key_at_pos(pos); } - public void getKeys(Set<KeyValue> dst) + /** This is computed once and cached. */ + public Map<KeyValue, KeyPos> getKeys() { - for (Row r : rows) - r.getKeys(dst); + if (_key_pos == null) + { + _key_pos = new HashMap<KeyValue, KeyPos>(); + for (int r = 0; r < rows.size(); r++) + rows.get(r).getKeys(_key_pos, r); + } + return _key_pos; } private static void addExtraKeys_to_row(ArrayList<Row> rows, final Iterator<KeyValue> extra_keys, int row_i, final int d) @@ -258,10 +263,17 @@ class KeyboardData return new Row(keys, h, shift); } - public void getKeys(Set<KeyValue> dst) + public void getKeys(Map<KeyValue, KeyPos> dst, int row) { - for (Key k : keys) - k.getKeys(dst); + for (int c = 0; c < keys.size(); c++) + keys.get(c).getKeys(dst, row, c); + } + + public Map<KeyValue, KeyPos> getKeys(int row) + { + Map<KeyValue, KeyPos> dst = new HashMap<KeyValue, KeyPos>(); + getKeys(dst, row); + return dst; } public Row mapKeys(MapKey f) @@ -281,12 +293,11 @@ class KeyboardData }); } - public Key findKeyWithValue(KeyValue kv) + public Key get_key_at_pos(KeyPos pos) { - for (Key k : keys) - if (k.hasValue(kv)) - return k; - return null; + if (pos.col >= keys.size()) + return null; + return keys.get(pos.col); } } @@ -384,11 +395,11 @@ class KeyboardData return new Key(keys, keysflags, width * s, shift, slider, indication); } - public void getKeys(Set<KeyValue> dst) + public void getKeys(Map<KeyValue, KeyPos> dst, int row, int col) { for (int i = 0; i < keys.length; i++) if (keys[i] != null) - dst.add(keys[i]); + dst.put(keys[i], new KeyPos(row, col, i)); } public KeyValue getKeyValue(int i) @@ -464,6 +475,21 @@ class KeyboardData } } + /** Position of a key on the layout. */ + public final static class KeyPos + { + public final int row; + public final int col; + public final int dir; + + public KeyPos(int r, int c, int d) + { + row = r; + col = c; + dir = d; + } + } + /** Parsing utils */ /** Returns [false] on [END_DOCUMENT] or [END_TAG], [true] otherwise. */ |
