diff options
| author | Jules Aguillon | 2022-03-05 18:15:36 +0100 |
|---|---|---|
| committer | Jules Aguillon | 2022-03-05 19:22:01 +0100 |
| commit | e92504ae92873ca7f0efa80421893bc69f98fa11 (patch) | |
| tree | 617658973d150e51bb398c1b9b937a5098a5e3fa | |
| parent | 4964bfb87f8d7dab50990864e274d4bc494f67b0 (diff) | |
| download | unexpected-keyboard-e92504ae92873ca7f0efa80421893bc69f98fa11.tar.gz unexpected-keyboard-e92504ae92873ca7f0efa80421893bc69f98fa11.zip | |
Improve the code for dynamic updates of the layout
| -rw-r--r-- | Makefile | 2 | ||||
| -rw-r--r-- | srcs/juloo.keyboard2/Config.java | 31 | ||||
| -rw-r--r-- | srcs/juloo.keyboard2/KeyValue.java | 1 | ||||
| -rw-r--r-- | srcs/juloo.keyboard2/Keyboard2View.java | 23 | ||||
| -rw-r--r-- | srcs/juloo.keyboard2/KeyboardData.java | 69 |
5 files changed, 37 insertions, 89 deletions
@@ -3,7 +3,7 @@ PACKAGE_NAME = juloo.keyboard2 ANDROID_PLATFORM_VERSION = android-30 -JAVA_VERSION = 1.7 +JAVA_VERSION = 1.8 SRC_DIR = srcs ASSETS_DIR = assets diff --git a/srcs/juloo.keyboard2/Config.java b/srcs/juloo.keyboard2/Config.java index cfd4fb7..b477c92 100644 --- a/srcs/juloo.keyboard2/Config.java +++ b/srcs/juloo.keyboard2/Config.java @@ -8,6 +8,7 @@ import android.os.Build; import android.preference.PreferenceManager; import android.util.DisplayMetrics; import android.util.TypedValue; +import android.view.KeyEvent; final class Config { @@ -118,6 +119,36 @@ final class Config theme = getThemeId(res, prefs.getString("theme", "")); } + /** Update the layout according to the configuration. + * - Remove the switching key if it isn't needed + * - Remove keys from other locales + * - Replace the action key to show the right label + * - Swap the enter and action keys + */ + public KeyboardData modify_layout(KeyboardData kw) + { + KeyValue action_key = (actionLabel == null) ? null : + new KeyValue(actionLabel, actionLabel, KeyValue.CHAR_NONE, + KeyValue.EVENT_ACTION, KeyValue.FLAG_NOREPEAT); + return kw.replaceKeys(key -> { + if (key == null) + return null; + switch (key.eventCode) + { + case KeyValue.EVENT_CHANGE_METHOD: + return shouldOfferSwitchingToNextInputMethod ? key : null; + case KeyEvent.KEYCODE_ENTER: + return (swapEnterActionKey && action_key != null) ? action_key : key; + case KeyValue.EVENT_ACTION: + return (swapEnterActionKey && action_key != null) ? + KeyValue.getKeyByName("enter") : action_key; + default: + if ((key.flags & key_flags_to_remove) != 0) + return null; + return key; + }}); + } + private float getDipPref(DisplayMetrics dm, SharedPreferences prefs, String pref_name, float def) { float value; diff --git a/srcs/juloo.keyboard2/KeyValue.java b/srcs/juloo.keyboard2/KeyValue.java index 9a73b83..ec29991 100644 --- a/srcs/juloo.keyboard2/KeyValue.java +++ b/srcs/juloo.keyboard2/KeyValue.java @@ -51,7 +51,6 @@ class KeyValue public static final int FLAG_ACCENT_MACRON = (1 << 27); public static final int FLAG_ACCENT_ORDINAL = (1 << 28); - public static final int FLAGS_ACCENTS = FLAG_ACCENT1 | FLAG_ACCENT2 | FLAG_ACCENT3 | FLAG_ACCENT4 | FLAG_ACCENT5 | FLAG_ACCENT6 | FLAG_ACCENT_CARON | FLAG_ACCENT_MACRON | FLAG_ACCENT_SUPERSCRIPT | diff --git a/srcs/juloo.keyboard2/Keyboard2View.java b/srcs/juloo.keyboard2/Keyboard2View.java index 92630ca..e282b0f 100644 --- a/srcs/juloo.keyboard2/Keyboard2View.java +++ b/srcs/juloo.keyboard2/Keyboard2View.java @@ -55,28 +55,7 @@ public class Keyboard2View extends View public void setKeyboard(KeyboardData kw) { - if (!_config.shouldOfferSwitchingToNextInputMethod) - kw = kw.replaceKeys( - new KeyboardData.ReplaceKeysByEvent(KeyValue.EVENT_CHANGE_METHOD, null)); - if (_config.key_flags_to_remove != 0) - kw = kw.replaceKeys( - new KeyboardData.ReplaceKeysByFlags(_config.key_flags_to_remove, null)); - // Replace the action key to show the right label. - KeyValue action_key = null; - if (_config.actionLabel != null) - { - action_key = new KeyValue(_config.actionLabel, _config.actionLabel, - KeyValue.CHAR_NONE, KeyValue.EVENT_ACTION, KeyValue.FLAG_NOREPEAT); - } - if (_config.swapEnterActionKey && action_key != null) - kw = kw.replaceKeys( - new KeyboardData.ReplaceKeysByEvent2(KeyEvent.KEYCODE_ENTER, - action_key, KeyValue.EVENT_ACTION, - KeyValue.getKeyByName("enter"))); - else - kw = kw.replaceKeys( - new KeyboardData.ReplaceKeysByEvent(KeyValue.EVENT_ACTION, action_key)); - _keyboard = kw; + _keyboard = _config.modify_layout(kw); reset(); } diff --git a/srcs/juloo.keyboard2/KeyboardData.java b/srcs/juloo.keyboard2/KeyboardData.java index 90085e5..81b6870 100644 --- a/srcs/juloo.keyboard2/KeyboardData.java +++ b/srcs/juloo.keyboard2/KeyboardData.java @@ -6,6 +6,7 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.function.Function; class KeyboardData { @@ -184,7 +185,8 @@ class KeyboardData public Key replaceKeys(MapKeys f) { - return new Key(f.map(key0), f.map(key1), f.map(key2), f.map(key3), f.map(key4), width, shift, edgekeys); + return new Key(f.apply(key0), f.apply(key1), f.apply(key2), + f.apply(key3), f.apply(key4), width, shift, edgekeys); } /** New key with the width multiplied by 's'. */ @@ -194,70 +196,7 @@ class KeyboardData } } - public static abstract interface MapKeys - { - public abstract KeyValue map(KeyValue k); - } - - public static class ReplaceKeysByFlags implements MapKeys - { - private final int _flags; - private final KeyValue _replacement; - - public ReplaceKeysByFlags(int flags, KeyValue r) - { - _flags = flags; - _replacement = r; - } - - public KeyValue map(KeyValue k) - { - return (k != null && (k.flags & _flags) != 0) ? _replacement : k; - } - } - - public static class ReplaceKeysByEvent implements MapKeys - { - private final int _eventCode; - private final KeyValue _replacement; - - public ReplaceKeysByEvent(int ev, KeyValue r) - { - _eventCode = ev; - _replacement = r; - } - - public KeyValue map(KeyValue k) - { - return (k != null && k.eventCode == _eventCode) ? _replacement : k; - } - } - - /* Replace two keys at the same time. Used for swaping keys. */ - public static class ReplaceKeysByEvent2 implements MapKeys - { - private final int _e1; - private final KeyValue _r1; - private final int _e2; - private final KeyValue _r2; - - public ReplaceKeysByEvent2(int e1, KeyValue r1, int e2, KeyValue r2) - { - _e1 = e1; - _r1 = r1; - _e2 = e2; - _r2 = r2; - } - - public KeyValue map(KeyValue k) - { - if (k == null) - return null; - if (k.eventCode == _e1) return _r1; - if (k.eventCode == _e2) return _r2; - return k; - } - } + public static abstract interface MapKeys extends Function<KeyValue, KeyValue> { } /** Parsing utils */ |
