From cd95c589de20a2d296e0cd2816ab8b5dadeb1a89 Mon Sep 17 00:00:00 2001 From: Jules Aguillon Date: Sun, 6 Aug 2023 01:30:02 +0200 Subject: [PATCH] Extra keys alternatives For each extra key, a list of alternative can be specified. An extra key won't be added to the keyboard if all its alternatives are already present on it. This is useful to avoid having the dead key for an accent and the accented letters at the same time. --- srcs/juloo.keyboard2/Config.java | 9 ++- srcs/juloo.keyboard2/ExtraKeys.java | 93 ++++++++++++++++++++--------- srcs/juloo.keyboard2/Keyboard2.java | 5 +- 3 files changed, 75 insertions(+), 32 deletions(-) diff --git a/srcs/juloo.keyboard2/Config.java b/srcs/juloo.keyboard2/Config.java index b38604b..53bd2d2 100644 --- a/srcs/juloo.keyboard2/Config.java +++ b/srcs/juloo.keyboard2/Config.java @@ -185,7 +185,14 @@ final class Config final Set extra_keys = new HashSet(); final Set remove_keys = new HashSet(); if (extra_keys_subtype != null) - extra_keys_subtype.compute(extra_keys, kw.script); + { + Set present = new HashSet(); + kw.getKeys(present); + present.addAll(extra_keys_param); + present.addAll(extra_keys_custom); + extra_keys_subtype.compute(extra_keys, + new ExtraKeys.Query(kw.script, present)); + } extra_keys.addAll(extra_keys_param); extra_keys.addAll(extra_keys_custom); boolean number_row = this.number_row && !show_numpad; diff --git a/srcs/juloo.keyboard2/ExtraKeys.java b/srcs/juloo.keyboard2/ExtraKeys.java index 22187c1..36acaf1 100644 --- a/srcs/juloo.keyboard2/ExtraKeys.java +++ b/srcs/juloo.keyboard2/ExtraKeys.java @@ -1,6 +1,7 @@ package juloo.keyboard2; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -8,50 +9,86 @@ import java.util.Set; class ExtraKeys { - Map> _keys_per_script; + List _ks; public ExtraKeys() { - _keys_per_script = new HashMap>(); + _ks = new ArrayList(); } - public void add_keys_for_script(String script, List kvs) + public void parse_and_add_keys_for_script(String script, String extra_keys_str) { - List ks = _keys_per_script.get(script); - if (ks == null) ks = new ArrayList(); - ks.addAll(kvs); - _keys_per_script.put(script, ks); + _ks.addAll(parse_extra_keys(script, extra_keys_str)); } - /** Add the keys that should be added to the keyboard into [dst]. [null] is - a valid script. */ - public void compute(Set dst, String script) + /** Add the keys that should be added to the keyboard into [dst]. */ + public void compute(Set dst, Query q) { - if (script == null) + for (ExtraKey k : _ks) { - for (String sc : _keys_per_script.keySet()) - get_keys_of_script(dst, sc); - } - else - { - get_keys_of_script(dst, null); - get_keys_of_script(dst, script); + if (k.should_add(q)) + dst.add(k.kv); } } - void get_keys_of_script(Set dst, String script) + public static List parse_extra_keys(String script, String str) { - List ks = _keys_per_script.get(script); - if (ks != null) - dst.addAll(ks); - } - - public static List parse_extra_keys(String str) - { - List dst = new ArrayList(); + List dst = new ArrayList(); String[] ks = str.split("\\|"); for (int i = 0; i < ks.length; i++) - dst.add(KeyValue.getKeyByName(ks[i])); + dst.add(ExtraKey.parse(ks[i], script)); return dst; } + + final static class ExtraKey + { + /** The key to add. */ + final KeyValue kv; + /** The key will be added to layouts of the same script. If null, might be + added to layouts of any script. */ + final String script; + /** The key will not be added to layout that already contain all the + alternatives. */ + final List alternatives; + + ExtraKey(KeyValue kv_, String script_, List alts_) + { + kv = kv_; + script = script_; + alternatives = alts_; + } + + /** Whether the key should be added to the keyboard. */ + public boolean should_add(Query q) + { + return + (q.script == null || script == null || q.script.equals(script)) + && (alternatives.size() == 0 || !q.present.containsAll(alternatives)); + } + + /** Extra keys are of the form "key name" or "key name:alt 1:alt 2". */ + public static ExtraKey parse(String str, String script) + { + String[] strs = str.split(":"); + KeyValue kv = KeyValue.getKeyByName(strs[0]); + KeyValue[] alts = new KeyValue[strs.length-1]; + for (int i = 1; i < strs.length; i++) + alts[i-1] = KeyValue.getKeyByName(strs[i]); + return new ExtraKey(kv, script, Arrays.asList(alts)); + } + } + + public final static class Query + { + /** Script of the current layout. Might be null. */ + final String script; + /** Keys present on the layout. */ + final Set present; + + public Query(String script_, Set present_) + { + script = script_; + present = present_; + } + } } diff --git a/srcs/juloo.keyboard2/Keyboard2.java b/srcs/juloo.keyboard2/Keyboard2.java index 1b59e5e..77c2c15 100644 --- a/srcs/juloo.keyboard2/Keyboard2.java +++ b/srcs/juloo.keyboard2/Keyboard2.java @@ -114,9 +114,8 @@ public class Keyboard2 extends InputMethodService { String extra_keys = subtype.getExtraValueOf("extra_keys"); String script = subtype.getExtraValueOf("script"); - if (extra_keys == null) - return; - dst.add_keys_for_script(script, ExtraKeys.parse_extra_keys(extra_keys)); + if (extra_keys != null) + dst.parse_and_add_keys_for_script(script, extra_keys); } private void refreshAccentsOption(InputMethodManager imm, InputMethodSubtype subtype)