From 093a00c572a29d292bc6bbb09559611cb61e1331 Mon Sep 17 00:00:00 2001 From: Jules Aguillon Date: Thu, 30 Dec 2021 00:26:05 +0100 Subject: Add themes Add a "Theme" option to choose between a dark and light theme. The light theme uses the colors of the dark theme with the luminance inversed. The reloading after a configuration change is changed slightly: - Special handling is needed when the Theme is changed (recreate the views) - The default implementation of 'onConfigurationChanged' is used Which triggers more refresh (but don't recreate the views) - 'onCreateInputView' is no longer needed --- res/layout/emoji_pane.xml | 2 +- res/layout/keyboard.xml | 2 +- res/values/arrays.xml | 9 +++++++++ res/values/strings.xml | 4 ++++ res/values/themes.xml | 13 +++++++++++++ res/xml/settings.xml | 2 +- srcs/juloo.keyboard2/Config.java | 11 +++++++++++ srcs/juloo.keyboard2/Keyboard2.java | 34 ++++++++++++++++------------------ srcs/juloo.keyboard2/Theme.java | 2 +- 9 files changed, 57 insertions(+), 22 deletions(-) diff --git a/res/layout/emoji_pane.xml b/res/layout/emoji_pane.xml index 3c58330..2931282 100644 --- a/res/layout/emoji_pane.xml +++ b/res/layout/emoji_pane.xml @@ -1,5 +1,5 @@ - + diff --git a/res/layout/keyboard.xml b/res/layout/keyboard.xml index b15f6d0..8af048d 100644 --- a/res/layout/keyboard.xml +++ b/res/layout/keyboard.xml @@ -1,2 +1,2 @@ - + diff --git a/res/values/arrays.xml b/res/values/arrays.xml index 77be329..819b1e6 100644 --- a/res/values/arrays.xml +++ b/res/values/arrays.xml @@ -18,4 +18,13 @@ 3 4 + + @string/pref_theme_e_dark + @string/pref_theme_e_light + + dark + + dark + light + diff --git a/res/values/strings.xml b/res/values/strings.xml index ce753cf..6ca5e6d 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -40,4 +40,8 @@ %sdp Label size Size of characters displayed on the keyboard (%.2fx) + Theme + %s + Dark + Light diff --git a/res/values/themes.xml b/res/values/themes.xml index 02f97be..11689d5 100644 --- a/res/values/themes.xml +++ b/res/values/themes.xml @@ -32,4 +32,17 @@ ?attr/emoji_button_bg ?attr/colorLabel + diff --git a/res/xml/settings.xml b/res/xml/settings.xml index 568b6c1..8561717 100644 --- a/res/xml/settings.xml +++ b/res/xml/settings.xml @@ -2,7 +2,6 @@ - /> @@ -16,6 +15,7 @@ + diff --git a/srcs/juloo.keyboard2/Config.java b/srcs/juloo.keyboard2/Config.java index 73c43be..fdac104 100644 --- a/srcs/juloo.keyboard2/Config.java +++ b/srcs/juloo.keyboard2/Config.java @@ -29,6 +29,7 @@ final class Config public boolean preciseRepeat; public float characterSize; // Ratio public int accents; // Values are R.values.pref_accents_v_* + public int theme; // Values are R.style.* // Dynamically set public boolean shouldOfferSwitchingToNextInputMethod; @@ -85,6 +86,7 @@ final class Config preciseRepeat = prefs.getBoolean("precise_repeat", preciseRepeat); characterSize = prefs.getFloat("character_size", characterSize); accents = Integer.valueOf(prefs.getString("accents", "1")); + theme = themeId_of_string(prefs.getString("theme", "")); } private float getDipPref(DisplayMetrics dm, SharedPreferences prefs, String pref_name, float def) @@ -121,6 +123,15 @@ final class Config } } + public static int themeId_of_string(String name) + { + switch (name) + { + case "light": return R.style.Light; + default: case "dark": return R.style.Dark; + } + } + private static Config _globalConfig = null; public static void initGlobalConfig(Context context, IKeyEventHandler handler) diff --git a/srcs/juloo.keyboard2/Keyboard2.java b/srcs/juloo.keyboard2/Keyboard2.java index 17c9d4e..b820b98 100644 --- a/srcs/juloo.keyboard2/Keyboard2.java +++ b/srcs/juloo.keyboard2/Keyboard2.java @@ -16,6 +16,7 @@ import android.view.inputmethod.InputConnection; import android.view.inputmethod.InputMethodInfo; import android.view.inputmethod.InputMethodManager; import android.view.inputmethod.InputMethodSubtype; +import android.view.ContextThemeWrapper; import android.view.KeyEvent; import android.view.View; import android.view.ViewGroup; @@ -55,7 +56,7 @@ public class Keyboard2 extends InputMethodService PreferenceManager.getDefaultSharedPreferences(this).registerOnSharedPreferenceChangeListener(this); Config.initGlobalConfig(this, new KeyEventHandler(this.new Receiver())); _config = Config.globalConfig(); - _keyboardView = (Keyboard2View)getLayoutInflater().inflate(R.layout.keyboard, null); + _keyboardView = (Keyboard2View)inflate_view(R.layout.keyboard); _keyboardView.reset(); } @@ -138,16 +139,6 @@ public class Keyboard2 extends InputMethodService } } - @Override - public View onCreateInputView() - { - ViewGroup parent = (ViewGroup)_keyboardView.getParent(); - - if (parent != null) - parent.removeView(_keyboardView); - return (_keyboardView); - } - @Override public void onStartInputView(EditorInfo info, boolean restarting) { @@ -157,6 +148,7 @@ public class Keyboard2 extends InputMethodService else _keyboardView.setKeyboard(getLayout(_currentTextLayout)); _keyboardView.reset(); // Layout might need to change due to rotation + setInputView(_keyboardView); } @Override @@ -176,15 +168,16 @@ public class Keyboard2 extends InputMethodService @Override public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { + int prev_theme = _config.theme; _config.refresh(this); refreshSubtypeImm(); _keyboardView.refreshConfig(getLayout(_currentTextLayout)); - } - - @Override - public void onConfigurationChanged(Configuration newConfig) - { - _keyboardView.reset(); + // Refreshing the theme config requires re-creating the views + if (prev_theme != _config.theme) + { + _keyboardView = (Keyboard2View)inflate_view(R.layout.keyboard); + _emojiPane = null; + } } /** Not static */ @@ -199,7 +192,7 @@ public class Keyboard2 extends InputMethodService public void setPane_emoji() { if (_emojiPane == null) - _emojiPane = (ViewGroup)getLayoutInflater().inflate(R.layout.emoji_pane, null); + _emojiPane = (ViewGroup)inflate_view(R.layout.emoji_pane); setInputView(_emojiPane); } @@ -247,4 +240,9 @@ public class Keyboard2 extends InputMethodService { return getWindow().getWindow().getAttributes().token; } + + private View inflate_view(int layout) + { + return View.inflate(new ContextThemeWrapper(this, _config.theme), layout, null); + } } diff --git a/srcs/juloo.keyboard2/Theme.java b/srcs/juloo.keyboard2/Theme.java index e90074b..04f365f 100644 --- a/srcs/juloo.keyboard2/Theme.java +++ b/srcs/juloo.keyboard2/Theme.java @@ -25,7 +25,7 @@ public class Theme public Theme(Context context, AttributeSet attrs) { - TypedArray s = context.getTheme().obtainStyledAttributes(attrs, R.styleable.keyboard, 0, R.style.Dark); + TypedArray s = context.getTheme().obtainStyledAttributes(attrs, R.styleable.keyboard, 0, 0); keyBgPaint.setColor(s.getColor(R.styleable.keyboard_colorKey, 0)); keyDownBgPaint.setColor(s.getColor(R.styleable.keyboard_colorKeyActivated, 0)); // colorKeyboard = s.getColor(R.styleable.keyboard_colorKeyboard, 0); -- cgit v1.2.3