From 612657811179eefbb425698fb9a29311a9ee9945 Mon Sep 17 00:00:00 2001 From: Jules Aguillon Date: Sun, 15 Jan 2023 23:19:09 +0100 Subject: [PATCH] Avoid switching back from secondary layout automatically Stay on the secondary layout after a config refresh or onStartInputView. The information is kept until the keyboard is restarted. Additionally, move tweaking the secondary layout to the Config class now that physical equality is not needed. --- srcs/juloo.keyboard2/Config.java | 19 ++++++- srcs/juloo.keyboard2/KeyEventHandler.java | 5 +- srcs/juloo.keyboard2/Keyboard2.java | 67 +++++++++++++++-------- 3 files changed, 64 insertions(+), 27 deletions(-) diff --git a/srcs/juloo.keyboard2/Config.java b/srcs/juloo.keyboard2/Config.java index 3a22118..fbb542a 100644 --- a/srcs/juloo.keyboard2/Config.java +++ b/srcs/juloo.keyboard2/Config.java @@ -108,7 +108,7 @@ final class Config keyboardHeightPercent = _prefs.getInt("keyboard_height", 35); } layout = layout_of_string(res, _prefs.getString("layout", "none")); - second_layout = layout_of_string(res, _prefs.getString("second_layout", "none")); + second_layout = tweak_secondary_layout(layout_of_string(res, _prefs.getString("second_layout", "none"))); custom_layout = KeyboardData.load_string(_prefs.getString("custom_layout", "")); inverse_numpad = _prefs.getString("numpad_layout", "default").equals("low_first"); // The baseline for the swipe distance correspond to approximately the @@ -237,6 +237,23 @@ final class Config return kw; } + /** Modify a layout to turn it into a secondary layout by changing the + "switch_second" key. */ + KeyboardData tweak_secondary_layout(KeyboardData layout) + { + if (layout == null) + return null; + return layout.mapKeys(new KeyboardData.MapKeyValues() { + public KeyValue apply(KeyValue key, boolean localized) + { + if (key.getKind() == KeyValue.Kind.Event + && key.getEvent() == KeyValue.Event.SWITCH_SECOND) + return KeyValue.getKeyByName("switch_second_back"); + return key; + } + }); + } + private float get_dip_pref(DisplayMetrics dm, String pref_name, float def) { float value; diff --git a/srcs/juloo.keyboard2/KeyEventHandler.java b/srcs/juloo.keyboard2/KeyEventHandler.java index d6afb46..3031972 100644 --- a/srcs/juloo.keyboard2/KeyEventHandler.java +++ b/srcs/juloo.keyboard2/KeyEventHandler.java @@ -44,8 +44,7 @@ class KeyEventHandler implements Config.IKeyEventHandler switch (key.getEvent()) { case CONFIG: _recv.showKeyboardConfig(); break; - case SWITCH_TEXT: - case SWITCH_SECOND_BACK: _recv.switch_text(); break; + case SWITCH_TEXT: _recv.switch_text(); break; case SWITCH_NUMERIC: _recv.switch_layout(R.xml.numeric); break; case SWITCH_EMOJI: _recv.setPane_emoji(); break; case SWITCH_BACK_EMOJI: _recv.setPane_normal(); break; @@ -56,6 +55,7 @@ class KeyEventHandler implements Config.IKeyEventHandler conn.performEditorAction(actionId); break; case SWITCH_SECOND: _recv.switch_second(); break; + case SWITCH_SECOND_BACK: _recv.switch_primary(); break; case SWITCH_GREEKMATH: _recv.switch_layout(R.xml.greekmath); break; case CAPS_LOCK: _recv.set_shift_state(true, true); break; } @@ -176,6 +176,7 @@ class KeyEventHandler implements Config.IKeyEventHandler public void showKeyboardConfig(); public void switch_text(); + public void switch_primary(); public void switch_second(); public void switch_layout(int layout_id); diff --git a/srcs/juloo.keyboard2/Keyboard2.java b/srcs/juloo.keyboard2/Keyboard2.java index cdec907..6558539 100644 --- a/srcs/juloo.keyboard2/Keyboard2.java +++ b/srcs/juloo.keyboard2/Keyboard2.java @@ -33,7 +33,8 @@ public class Keyboard2 extends InputMethodService private KeyEventHandler _keyeventhandler; // If not 'null', the layout to use instead of [_currentTextLayout]. private KeyboardData _currentSpecialLayout; - private KeyboardData _currentTextLayout; + private Current_text_layout _currentTextLayout; + // Layout associated with the currently selected locale. private KeyboardData _localeTextLayout; private ViewGroup _emojiPane = null; @@ -41,9 +42,21 @@ public class Keyboard2 extends InputMethodService private boolean _debug_logs = false; - KeyboardData main_layout() + /** Layout currently visible. */ + KeyboardData current_layout() { - return (_currentSpecialLayout != null) ? _currentSpecialLayout : _currentTextLayout; + if (_currentSpecialLayout != null) + return _currentSpecialLayout; + if (_currentTextLayout == Current_text_layout.SECONDARY) + return _config.second_layout; + return (_config.layout == null) ? _localeTextLayout : _config.layout; + } + + void setTextLayout(Current_text_layout layout) + { + _currentTextLayout = layout; + _currentSpecialLayout = null; + _keyboardView.setKeyboard(current_layout()); } @Override @@ -148,10 +161,15 @@ public class Keyboard2 extends InputMethodService refreshAccentsOption(imm, subtype); } } - _config.shouldOfferSwitchingToSecond = - _config.second_layout != null && - _currentTextLayout != _config.second_layout; - _currentTextLayout = (_config.layout == null) ? _localeTextLayout : _config.layout; + if (_config.second_layout == null) + { + _config.shouldOfferSwitchingToSecond = false; + _currentTextLayout = Current_text_layout.PRIMARY; + } + else + { + _config.shouldOfferSwitchingToSecond = true; + } } private String actionLabel_of_imeAction(int action) @@ -239,7 +257,7 @@ public class Keyboard2 extends InputMethodService refresh_config(); refresh_action_label(info); refresh_special_layout(info); - _keyboardView.setKeyboard(main_layout()); + _keyboardView.setKeyboard(current_layout()); _keyeventhandler.started(info); setInputView(_keyboardView); if (_debug_logs) @@ -259,7 +277,7 @@ public class Keyboard2 extends InputMethodService public void onCurrentInputMethodSubtypeChanged(InputMethodSubtype subtype) { refreshSubtypeImm(); - _keyboardView.setKeyboard(main_layout()); + _keyboardView.setKeyboard(current_layout()); } @Override @@ -281,7 +299,7 @@ public class Keyboard2 extends InputMethodService { refresh_config(); setInputView(_keyboardView); - _keyboardView.setKeyboard(main_layout()); + _keyboardView.setKeyboard(current_layout()); } @Override @@ -321,7 +339,8 @@ public class Keyboard2 extends InputMethodService public void switch_text() { - _keyboardView.setKeyboard(_currentTextLayout); + _currentSpecialLayout = null; + _keyboardView.setKeyboard(current_layout()); } public void switch_layout(int layout_id) @@ -331,19 +350,13 @@ public class Keyboard2 extends InputMethodService public void switch_second() { - if (_config.second_layout == null) - return; - KeyboardData layout = - _config.second_layout.mapKeys(new KeyboardData.MapKeyValues() { - public KeyValue apply(KeyValue key, boolean localized) - { - if (key.getKind() == KeyValue.Kind.Event - && key.getEvent() == KeyValue.Event.SWITCH_SECOND) - return KeyValue.getKeyByName("switch_second_back"); - return key; - } - }); - _keyboardView.setKeyboard(layout); + if (_config.second_layout != null) + setTextLayout(Current_text_layout.SECONDARY); + } + + public void switch_primary() + { + setTextLayout(Current_text_layout.PRIMARY); } public void showKeyboardConfig() @@ -368,4 +381,10 @@ public class Keyboard2 extends InputMethodService { return View.inflate(new ContextThemeWrapper(this, _config.theme), layout, null); } + + private static enum Current_text_layout + { + PRIMARY, + SECONDARY + } }