diff options
| author | Jules Aguillon | 2023-07-29 18:37:06 +0200 |
|---|---|---|
| committer | Jules Aguillon | 2023-07-30 21:44:14 +0200 |
| commit | dad5f57a036e3f0ad7278ccee5bd248192cbbad2 (patch) | |
| tree | 022b170f190edcf204dc453a49e83193821aeb80 /srcs | |
| parent | 818aa4c7d597116ed595e960d4415b57ed56d2ec (diff) | |
| download | unexpected-keyboard-dad5f57a036e3f0ad7278ccee5bd248192cbbad2.tar.gz unexpected-keyboard-dad5f57a036e3f0ad7278ccee5bd248192cbbad2.zip | |
Allow more than 2 layouts
The two layout selection options are replaced by a ListGroupPreference
that allow to enter an arbitrary amount of layouts.
The "switch_second" and "switch_second_back" keys are replaced by
"switch_forward" and "switch_backward", which allow to cycle through the
selected layouts in two directions.
Layouts are changed to place these two key on the space bar.
The backward key is not shown if there's only two layouts.
Diffstat (limited to 'srcs')
| -rw-r--r-- | srcs/juloo.keyboard2/Config.java | 36 | ||||
| -rw-r--r-- | srcs/juloo.keyboard2/KeyValue.java | 8 | ||||
| -rw-r--r-- | srcs/juloo.keyboard2/Keyboard2.java | 51 | ||||
| -rw-r--r-- | srcs/juloo.keyboard2/LayoutsPreference.java | 89 |
4 files changed, 126 insertions, 58 deletions
diff --git a/srcs/juloo.keyboard2/Config.java b/srcs/juloo.keyboard2/Config.java index 09db10d..b38604b 100644 --- a/srcs/juloo.keyboard2/Config.java +++ b/srcs/juloo.keyboard2/Config.java @@ -8,6 +8,7 @@ import android.os.Build; import android.util.DisplayMetrics; import android.util.TypedValue; import android.view.KeyEvent; +import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; import java.util.List; @@ -25,8 +26,8 @@ final class Config public final float sublabelTextSize; // From preferences - public KeyboardData layout; // Or 'null' for the system defaults - public KeyboardData second_layout; // Or 'null' for none + /** [null] represent the [system] layout. */ + public List<KeyboardData> layouts; public KeyboardData custom_layout; // Might be 'null' public boolean show_numpad = false; // From the 'numpad_layout' option, also apply to the numeric pane. @@ -56,7 +57,6 @@ final class Config // Dynamically set public boolean shouldOfferSwitchingToNextInputMethod; - public boolean shouldOfferSwitchingToSecond; public boolean shouldOfferVoiceTyping; public String actionLabel; // Might be 'null' public int actionId; // Meaningful only when 'actionLabel' isn't 'null' @@ -80,7 +80,6 @@ final class Config refresh(res); // initialized later shouldOfferSwitchingToNextInputMethod = false; - shouldOfferSwitchingToSecond = false; shouldOfferVoiceTyping = false; actionLabel = null; actionId = 0; @@ -116,8 +115,10 @@ final class Config { keyboardHeightPercent = _prefs.getInt("keyboard_height", 35); } - layout = layout_of_string(res, _prefs.getString("layout", "none")); - second_layout = tweak_secondary_layout(layout_of_string(res, _prefs.getString("second_layout", "none"))); + List<String> layout_names = LayoutsPreference.load_from_preferences(_prefs); + layouts = new ArrayList<KeyboardData>(); + for (String l : layout_names) + layouts.add(layout_of_string(res, l)); custom_layout = KeyboardData.load_string(_prefs.getString("custom_layout", "")); inverse_numpad = _prefs.getString("numpad_layout", "default").equals("low_first"); number_row = _prefs.getBoolean("number_row", false); @@ -214,8 +215,10 @@ final class Config case ACTION: return (swapEnterActionKey && action_key != null) ? KeyValue.getKeyByName("enter") : action_key; - case SWITCH_SECOND: - return shouldOfferSwitchingToSecond ? key : null; + case SWITCH_FORWARD: + return (layouts.size() > 1) ? key : null; + case SWITCH_BACKWARD: + return (layouts.size() > 2) ? key : null; case SWITCH_VOICE_TYPING: return shouldOfferVoiceTyping ? key : null; } @@ -287,23 +290,6 @@ final class Config }); } - /** 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/KeyValue.java b/srcs/juloo.keyboard2/KeyValue.java index b3008ca..84a214b 100644 --- a/srcs/juloo.keyboard2/KeyValue.java +++ b/srcs/juloo.keyboard2/KeyValue.java @@ -15,8 +15,8 @@ final class KeyValue CHANGE_METHOD, CHANGE_METHOD_PREV, ACTION, - SWITCH_SECOND, - SWITCH_SECOND_BACK, + SWITCH_FORWARD, + SWITCH_BACKWARD, SWITCH_GREEKMATH, CAPS_LOCK, SWITCH_VOICE_TYPING, @@ -346,8 +346,8 @@ final class KeyValue case "switch_numeric": return eventKey("123+", Event.SWITCH_NUMERIC, FLAG_SMALLER_FONT); case "switch_emoji": return eventKey(0x01, Event.SWITCH_EMOJI, FLAG_SMALLER_FONT); case "switch_back_emoji": return eventKey("ABC", Event.SWITCH_BACK_EMOJI, 0); - case "switch_second": return eventKey(0x13, Event.SWITCH_SECOND, FLAG_SMALLER_FONT); - case "switch_second_back": return eventKey(0x14, Event.SWITCH_SECOND_BACK, FLAG_SMALLER_FONT); + case "switch_forward": return eventKey(0x13, Event.SWITCH_FORWARD, FLAG_SMALLER_FONT); + case "switch_backward": return eventKey(0x14, Event.SWITCH_BACKWARD, FLAG_SMALLER_FONT); case "switch_greekmath": return eventKey("πλ∇¬", Event.SWITCH_GREEKMATH, FLAG_SMALLER_FONT); case "change_method": return eventKey(0x09, Event.CHANGE_METHOD, FLAG_SMALLER_FONT); case "change_method_prev": return eventKey(0x09, Event.CHANGE_METHOD_PREV, FLAG_SMALLER_FONT); diff --git a/srcs/juloo.keyboard2/Keyboard2.java b/srcs/juloo.keyboard2/Keyboard2.java index 3606213..1b59e5e 100644 --- a/srcs/juloo.keyboard2/Keyboard2.java +++ b/srcs/juloo.keyboard2/Keyboard2.java @@ -30,7 +30,8 @@ public class Keyboard2 extends InputMethodService private KeyEventHandler _keyeventhandler; // If not 'null', the layout to use instead of [_currentTextLayout]. private KeyboardData _currentSpecialLayout; - private Current_text_layout _currentTextLayout; + /** Current layout index in [Config.layouts]. */ + private int _currentTextLayout; // Layout associated with the currently selected locale. Not 'null'. private KeyboardData _localeTextLayout; private ViewGroup _emojiPane = null; @@ -43,23 +44,31 @@ public class Keyboard2 extends InputMethodService { if (_currentSpecialLayout != null) return _currentSpecialLayout; - KeyboardData layout; - if (_currentTextLayout == Current_text_layout.SECONDARY) - layout = _config.second_layout; - else if (_config.layout == null) + KeyboardData layout = null; + if (_currentTextLayout >= _config.layouts.size()) + _currentTextLayout = 0; + if (_currentTextLayout < _config.layouts.size()) + layout = _config.layouts.get(_currentTextLayout); + if (layout == null) layout = _localeTextLayout; - else - layout = _config.layout; return _config.modify_layout(layout); } - void setTextLayout(Current_text_layout layout) + void setTextLayout(int l) { - _currentTextLayout = layout; + if (l == _currentTextLayout) + return; + _currentTextLayout = l; _currentSpecialLayout = null; _keyboardView.setKeyboard(current_layout()); } + void incrTextLayout(int delta) + { + int s = _config.layouts.size(); + setTextLayout((_currentTextLayout + delta + s) % s); + } + void setSpecialLayout(KeyboardData l) { _currentSpecialLayout = l; @@ -162,15 +171,6 @@ public class Keyboard2 extends InputMethodService if (default_layout == null) default_layout = loadLayout(R.xml.latn_qwerty_us); _localeTextLayout = default_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) @@ -419,13 +419,12 @@ public class Keyboard2 extends InputMethodService conn.performEditorAction(actionId); break; - case SWITCH_SECOND: - if (_config.second_layout != null) - setTextLayout(Current_text_layout.SECONDARY); + case SWITCH_FORWARD: + incrTextLayout(1); break; - case SWITCH_SECOND_BACK: - setTextLayout(Current_text_layout.PRIMARY); + case SWITCH_BACKWARD: + incrTextLayout(-1); break; case SWITCH_GREEKMATH: @@ -469,10 +468,4 @@ public class Keyboard2 extends InputMethodService { return View.inflate(new ContextThemeWrapper(this, _config.theme), layout, null); } - - private static enum Current_text_layout - { - PRIMARY, - SECONDARY - } } diff --git a/srcs/juloo.keyboard2/LayoutsPreference.java b/srcs/juloo.keyboard2/LayoutsPreference.java new file mode 100644 index 0000000..5d1d566 --- /dev/null +++ b/srcs/juloo.keyboard2/LayoutsPreference.java @@ -0,0 +1,89 @@ +package juloo.keyboard2; + +import android.app.AlertDialog; +import android.content.Context; +import android.content.DialogInterface; +import android.content.SharedPreferences; +import android.content.res.Resources; +import android.util.AttributeSet; +import android.widget.ArrayAdapter; +import android.widget.EditText; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +public class LayoutsPreference extends ListGroupPreference +{ + static final String KEY = "layouts"; + static final List<String> DEFAULT = Collections.singletonList("system"); + + /** Layout names as stored in the preferences. */ + String[] _layout_names; + /** Text displayed for each layout in the dialog list. */ + String[] _layout_display_names; + + public LayoutsPreference(Context ctx, AttributeSet attrs) + { + super(ctx, attrs); + setKey(KEY); + Resources res = ctx.getResources(); + _layout_names = res.getStringArray(R.array.pref_layout_values); + _layout_display_names = res.getStringArray(R.array.pref_layout_entries); + } + + public static List<String> load_from_preferences(SharedPreferences prefs) + { + return load_from_preferences(KEY, prefs, DEFAULT); + } + + @Override + protected void onSetInitialValue(boolean restoreValue, Object defaultValue) + { + super.onSetInitialValue(restoreValue, defaultValue); + if (_values.size() == 0) + set_values(new ArrayList<String>(DEFAULT), false); + } + + @Override + String label_of_value(String value, int i) + { + return getContext().getString(R.string.pref_layouts_item, i + 1, value); + } + + @Override + AddButton on_attach_add_button(AddButton prev_btn) + { + if (prev_btn == null) + return new LayoutsAddButton(getContext()); + return prev_btn; + } + + @Override + boolean should_allow_remove_item() + { + return (_values.size() > 1); + } + + void select(final SelectionCallback callback) + { + ArrayAdapter layouts = new ArrayAdapter(getContext(), android.R.layout.simple_list_item_1, _layout_display_names); + new AlertDialog.Builder(getContext()) + .setView(R.layout.custom_extra_key_add_dialog) + .setAdapter(layouts, new DialogInterface.OnClickListener(){ + public void onClick(DialogInterface dialog, int which) + { + callback.select(_layout_names[which]); + } + }) + .show(); + } + + class LayoutsAddButton extends AddButton + { + public LayoutsAddButton(Context ctx) + { + super(ctx); + setLayoutResource(R.layout.pref_layouts_add_btn); + } + } +} |
