Skip to content

Commit

Permalink
Add themes
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Julow committed Dec 29, 2021
1 parent 40b1ec6 commit 093a00c
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 22 deletions.
2 changes: 1 addition & 1 deletion res/layout/emoji_pane.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:hardwareAccelerated="false" android:theme="@style/Dark">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:hardwareAccelerated="false">
<juloo.keyboard2.EmojiGroupButtonsBar android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content"/>
<juloo.keyboard2.EmojiGridView android:id="@+id/emoji_grid" android:background="?attr/colorKeyboard" android:layout_width="fill_parent" android:layout_height="@dimen/emoji_grid_height"/>
<LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content">
Expand Down
2 changes: 1 addition & 1 deletion res/layout/keyboard.xml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
<?xml version="1.0" encoding="utf-8"?>
<juloo.keyboard2.Keyboard2View xmlns:android="http://schemas.android.com/apk/res/android" android:hardwareAccelerated="false" android:theme="@style/Dark" android:background="?attr/colorKeyboard"/>
<juloo.keyboard2.Keyboard2View xmlns:android="http://schemas.android.com/apk/res/android" android:hardwareAccelerated="false" android:background="?attr/colorKeyboard"/>
9 changes: 9 additions & 0 deletions res/values/arrays.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,13 @@
<item>3</item>
<item>4</item>
</string-array>
<string-array name="pref_theme_entries">
<item>@string/pref_theme_e_dark</item>
<item>@string/pref_theme_e_light</item>
</string-array>
<string name="pref_theme_default">dark</string>
<string-array name="pref_theme_values">
<item>dark</item>
<item>light</item>
</string-array>
</resources>
4 changes: 4 additions & 0 deletions res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,8 @@
<string name="pref_horizontal_margin_summary">%sdp</string>
<string name="pref_character_size_title">Label size</string>
<string name="pref_character_size_summary">Size of characters displayed on the keyboard (%.2fx)</string>
<string name="pref_theme">Theme</string>
<string name="pref_theme_summary">%s</string>
<string name="pref_theme_e_dark">Dark</string>
<string name="pref_theme_e_light">Light</string>
</resources>
13 changes: 13 additions & 0 deletions res/values/themes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,17 @@
<item name="emoji_key_bg" type="color">?attr/emoji_button_bg</item>
<item name="emoji_key_text" type="color">?attr/colorLabel</item>
</style>
<style name="Light">
<item name="colorKeyboard">#e3e3e3</item>
<item name="colorKey">#cfcfcf</item>
<item name="colorKeyActivated">#e3e3e3</item>
<item name="colorLabel">#000000</item>
<item name="colorLabelActivated">#64afdd</item>
<item name="colorLabelLocked">#64dd76</item>
<item name="colorSubLabel">#5e5e5e</item>
<item name="emoji_button_bg" type="color">#dedede</item>
<item name="emoji_color" type="color">#000000</item>
<item name="emoji_key_bg" type="color">?attr/emoji_button_bg</item>
<item name="emoji_key_text" type="color">?attr/colorLabel</item>
</style>
</resources>
2 changes: 1 addition & 1 deletion res/xml/settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="@string/pref_category_layout">
<ListPreference android:key="layout" android:title="@string/pref_layout_title" android:summary="@string/pref_layout_summary" android:defaultValue="system" android:entries="@array/pref_layout_entries" android:entryValues="@array/pref_layout_values"/>
/&gt;
<ListPreference android:key="accents" android:title="@string/pref_accents_title" android:summary="@string/pref_accents_summary" android:defaultValue="@string/pref_accents_default" android:entries="@array/pref_accents_entries" android:entryValues="@array/pref_accents_values"/>
</PreferenceCategory>
<PreferenceCategory android:title="@string/pref_category_typing">
Expand All @@ -16,6 +15,7 @@
<juloo.common.IntSlideBarPreference android:key="vibrate_duration" android:title="@string/pref_vibrate_duration_title" android:summary="@string/pref_vibrate_duration_summary" android:defaultValue="20" min="5" max="50"/>
</PreferenceCategory>
<PreferenceCategory android:title="@string/pref_category_style">
<ListPreference android:key="theme" android:title="@string/pref_theme" android:summary="@string/pref_theme_summary" android:defaultValue="@string/pref_theme_default" android:entries="@array/pref_theme_entries" android:entryValues="@array/pref_theme_values"/>
<juloo.common.IntSlideBarPreference android:key="margin_bottom" android:title="@string/pref_margin_bottom_title" android:summary="@string/pref_margin_bottom_summary" android:defaultValue="5" min="0" max="100"/>
<juloo.common.IntSlideBarPreference android:key="key_height" android:title="@string/pref_key_height_title" android:summary="@string/pref_key_height_summary" android:defaultValue="50" min="30" max="90"/>
<juloo.common.IntSlideBarPreference android:key="horizontal_margin" android:title="@string/pref_horizontal_margin_title" android:summary="@string/pref_horizontal_margin_summary" android:defaultValue="3" min="0" max="20"/>
Expand Down
11 changes: 11 additions & 0 deletions srcs/juloo.keyboard2/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -85,6 +86,7 @@ public void refresh(Context context)
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)
Expand Down Expand Up @@ -121,6 +123,15 @@ public static int accentFlag_of_name(String name)
}
}

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)
Expand Down
34 changes: 16 additions & 18 deletions srcs/juloo.keyboard2/Keyboard2.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
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;
Expand Down Expand Up @@ -55,7 +56,7 @@ public void onCreate()
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();
}

Expand Down Expand Up @@ -138,16 +139,6 @@ private void refreshSubtypeImm()
}
}

@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)
{
Expand All @@ -157,6 +148,7 @@ public void onStartInputView(EditorInfo info, boolean restarting)
else
_keyboardView.setKeyboard(getLayout(_currentTextLayout));
_keyboardView.reset(); // Layout might need to change due to rotation
setInputView(_keyboardView);
}

@Override
Expand All @@ -176,15 +168,16 @@ public void onFinishInputView(boolean finishingInput)
@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 */
Expand All @@ -199,7 +192,7 @@ public void switchToNextInputMethod()
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);
}

Expand Down Expand Up @@ -247,4 +240,9 @@ private IBinder getConnectionToken()
{
return getWindow().getWindow().getAttributes().token;
}

private View inflate_view(int layout)
{
return View.inflate(new ContextThemeWrapper(this, _config.theme), layout, null);
}
}
2 changes: 1 addition & 1 deletion srcs/juloo.keyboard2/Theme.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 093a00c

Please sign in to comment.