Skip to content

Commit ae1a2ca

Browse files
committed
option for navbar to follow theme
fixes #4
1 parent 9907597 commit ae1a2ca

File tree

7 files changed

+88
-11
lines changed

7 files changed

+88
-11
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,12 @@ Changes:
2525
* based on wordmage's work https://github.com/openboard-team/openboard/tree/57d33791d7674e3fe0600eddb72f6b4317b5df00
2626
* tested with Google libraries and [others](https://github.com/openboard-team/openboard/issues/3#issuecomment-1200456262) (when building with the [rename](https://github.com/openboard-team/openboard/tree/57d33791d7674e3fe0600eddb72f6b4317b5df00))
2727
* Allow adjusting keyboard colors, https://github.com/openboard-team/openboard/issues/124
28+
* Optionally make the navigation bar follow current theme, https://github.com/Helium314/openboard/issues/4
2829
* Remove suggestions by long pressing on suggestion strip while the more suggestions popup is open, https://github.com/openboard-team/openboard/issues/106
2930
* suggestions get re-added if they are entered again
3031
* Optionally add typed words to system personal dictionary
3132
* Allow using contacts for suggestions (enable in spell checker settings), https://github.com/openboard-team/openboard/issues/374
33+
* Re-arranged comma-long-press-menu, https://github.com/Helium314/openboard/pull/7
3234

3335
Plan / to do:
3436
* ~upgrade dependencies~

app/src/main/java/org/dslul/openboard/inputmethod/latin/LatinIME.java

+61-8
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
138138
private static final String SCHEME_PACKAGE = "package";
139139

140140
final Settings mSettings;
141+
private int mOriginalNavBarColor = 0;
142+
private int mOriginalNavBarFlags = 0;
141143
private final DictionaryFacilitator mDictionaryFacilitator =
142144
DictionaryFacilitatorProvider.getDictionaryFacilitator(
143145
false /* isNeededForSpellChecking */);
@@ -1067,7 +1069,8 @@ void onStartInputViewInternal(final EditorInfo editorInfo, final boolean restart
10671069
@Override
10681070
public void onWindowShown() {
10691071
super.onWindowShown();
1070-
setNavigationBarVisibility(isInputViewShown());
1072+
if (isInputViewShown())
1073+
setNavigationBarColor();
10711074
}
10721075

10731076
@Override
@@ -1077,7 +1080,7 @@ public void onWindowHidden() {
10771080
if (mainKeyboardView != null) {
10781081
mainKeyboardView.closing();
10791082
}
1080-
setNavigationBarVisibility(false);
1083+
clearNavigationBarColor();
10811084
}
10821085

10831086
void onFinishInputInternal() {
@@ -2014,12 +2017,62 @@ public boolean shouldShowLanguageSwitchKey() {
20142017
return mSettings.getCurrent().isLanguageSwitchKeyEnabled();
20152018
}
20162019

2017-
private void setNavigationBarVisibility(final boolean visible) {
2018-
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
2019-
// For N and later, IMEs can specify Color.TRANSPARENT to make the navigation bar
2020-
// transparent. For other colors the system uses the default color.
2021-
getWindow().getWindow().setNavigationBarColor(
2022-
visible ? Color.BLACK : Color.TRANSPARENT);
2020+
// slightly modified from Simple Keyboard: https://github.com/rkkr/simple-keyboard/blob/master/app/src/main/java/rkr/simplekeyboard/inputmethod/latin/LatinIME.java
2021+
private void setNavigationBarColor() {
2022+
final SettingsValues settingsValues = mSettings.getCurrent();
2023+
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP || !settingsValues.mNavBarColor)
2024+
return;
2025+
final int color;
2026+
if (settingsValues.mUserTheme) {
2027+
final int c = settingsValues.mBackgroundColor;
2028+
// slightly adjust so color is same as keyboard background
2029+
color = Color.rgb((int) (Color.red(c) * 0.925), (int) (Color.green(c) * 0.9379), (int) (Color.blue(c) * 0.945));
2030+
} else
2031+
color = settingsValues.mBackgroundColor;
2032+
final Window window = getWindow().getWindow();
2033+
if (window == null)
2034+
return;
2035+
mOriginalNavBarColor = window.getNavigationBarColor();
2036+
window.setNavigationBarColor(color);
2037+
2038+
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
2039+
return;
2040+
final View view = window.getDecorView();
2041+
mOriginalNavBarFlags = view.getSystemUiVisibility();
2042+
if (isBrightColor(color)) {
2043+
view.setSystemUiVisibility(mOriginalNavBarFlags | View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR);
2044+
} else {
2045+
view.setSystemUiVisibility(mOriginalNavBarFlags & ~View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR);
2046+
}
2047+
}
2048+
2049+
private void clearNavigationBarColor() {
2050+
final SettingsValues settingsValues = mSettings.getCurrent();
2051+
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP || !settingsValues.mNavBarColor)
2052+
return;
2053+
final Window window = getWindow().getWindow();
2054+
if (window == null) {
2055+
return;
2056+
}
2057+
window.setNavigationBarColor(mOriginalNavBarColor);
2058+
2059+
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
2060+
return;
2061+
final View view = window.getDecorView();
2062+
view.setSystemUiVisibility(mOriginalNavBarFlags);
2063+
}
2064+
2065+
private static boolean isBrightColor(int color) {
2066+
if (android.R.color.transparent == color) {
2067+
return true;
2068+
}
2069+
// See http://www.nbdtech.com/Blog/archive/2008/04/27/Calculating-the-Perceived-Brightness-of-a-Color.aspx
2070+
boolean bright = false;
2071+
int[] rgb = {Color.red(color), Color.green(color), Color.blue(color)};
2072+
int brightness = (int) Math.sqrt(rgb[0] * rgb[0] * .241 + rgb[1] * rgb[1] * .691 + rgb[2] * rgb[2] * .068);
2073+
if (brightness >= 210) {
2074+
bright = true;
20232075
}
2076+
return bright;
20242077
}
20252078
}

app/src/main/java/org/dslul/openboard/inputmethod/latin/settings/AppearanceSettingsFragment.kt

+3
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ class AppearanceSettingsFragment : SubScreenFragment(), Preference.OnPreferenceC
5151
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
5252
removePreference(Settings.PREF_THEME_DAY_NIGHT)
5353
}
54+
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
55+
removePreference(Settings.PREF_NAVBAR_COLOR)
56+
}
5457
setupTheme()
5558

5659
if (!ProductionFlags.IS_SPLIT_KEYBOARD_SUPPORTED ||

app/src/main/java/org/dslul/openboard/inputmethod/latin/settings/Settings.java

+1
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ public final class Settings implements SharedPreferences.OnSharedPreferenceChang
137137

138138
public static final String PREF_SECONDARY_LOCALES = "pref_secondary_locales";
139139
public static final String PREF_ADD_TO_PERSONAL_DICTIONARY = "add_to_personal_dictionary";
140+
public static final String PREF_NAVBAR_COLOR = "navbar_color";
140141

141142
// This preference key is deprecated. Use {@link #PREF_SHOW_LANGUAGE_SWITCH_KEY} instead.
142143
// This is being used only for the backward compatibility.

app/src/main/java/org/dslul/openboard/inputmethod/latin/settings/SettingsValues.java

+15-3
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ public class SettingsValues {
111111
public final int mScreenMetrics;
112112
public final boolean mAddToPersonalDictionary;
113113
public final boolean mUseContactsDictionary;
114+
public final boolean mNavBarColor;
114115

115116
// From the input box
116117
@Nonnull
@@ -263,13 +264,14 @@ public SettingsValues(final Context context, final SharedPreferences prefs, fina
263264
mOneHandedModeGravity = Settings.readOneHandedModeGravity(prefs);
264265
mSecondaryLocale = Settings.getSecondaryLocale(prefs, RichInputMethodManager.getInstance().getCurrentSubtypeLocale().toString());
265266

266-
mUserTheme = KeyboardTheme.getIsUser(KeyboardTheme.getThemeForParameters(
267+
final int keyboardThemeId = KeyboardTheme.getThemeForParameters(
267268
prefs.getString(Settings.PREF_THEME_FAMILY, ""),
268269
prefs.getString(Settings.PREF_THEME_VARIANT, ""),
269270
prefs.getBoolean(Settings.PREF_THEME_KEY_BORDERS, false),
270271
prefs.getBoolean(Settings.PREF_THEME_DAY_NIGHT, false),
271272
prefs.getBoolean(Settings.PREF_THEME_AMOLED_MODE, false)
272-
));
273+
);
274+
mUserTheme = KeyboardTheme.getIsUser(keyboardThemeId);
273275
mUserThemeColorAccent = prefs.getInt(Settings.PREF_THEME_USER_COLOR_ACCENT, Color.BLUE);
274276
final int keyBgColor;
275277
if (prefs.getBoolean(Settings.PREF_THEME_KEY_BORDERS, false))
@@ -280,11 +282,21 @@ public SettingsValues(final Context context, final SharedPreferences prefs, fina
280282
mHintTextColorFilter = BlendModeColorFilterCompat.createBlendModeColorFilterCompat(prefs.getInt(Settings.PREF_THEME_USER_COLOR_HINT_TEXT, Color.WHITE), BlendModeCompat.SRC_ATOP);
281283
mKeyTextColor = prefs.getInt(Settings.PREF_THEME_USER_COLOR_TEXT, Color.WHITE);
282284
mKeyTextColorFilter = BlendModeColorFilterCompat.createBlendModeColorFilterCompat(mKeyTextColor, BlendModeCompat.SRC_ATOP);
283-
mBackgroundColor = prefs.getInt(Settings.PREF_THEME_USER_COLOR_BACKGROUND, Color.DKGRAY);
285+
if (mUserTheme) {
286+
mBackgroundColor = prefs.getInt(Settings.PREF_THEME_USER_COLOR_BACKGROUND, Color.DKGRAY);
287+
} else if (KeyboardTheme.THEME_VARIANT_LIGHT.equals(KeyboardTheme.getThemeVariant(keyboardThemeId))) {
288+
mBackgroundColor = Color.rgb(236, 239, 241);
289+
} else if (keyboardThemeId == KeyboardTheme.THEME_ID_LXX_DARK) {
290+
mBackgroundColor = Color.rgb(38, 50, 56);
291+
} else {
292+
// dark border is 13/13/13, but that's ok
293+
mBackgroundColor = Color.BLACK;
294+
}
284295
mBackgroundColorFilter = BlendModeColorFilterCompat.createBlendModeColorFilterCompat(mBackgroundColor, BlendModeCompat.MODULATE);
285296

286297
mAddToPersonalDictionary = prefs.getBoolean(Settings.PREF_ADD_TO_PERSONAL_DICTIONARY, false);
287298
mUseContactsDictionary = prefs.getBoolean(AndroidSpellCheckerService.PREF_USE_CONTACTS_KEY, false);
299+
mNavBarColor = prefs.getBoolean(Settings.PREF_NAVBAR_COLOR, false);
288300
}
289301

290302
public boolean isMetricsLoggingEnabled() {

app/src/main/res/values/strings.xml

+2
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,8 @@ disposition rather than other common dispositions for Latin languages. [CHAR LIM
644644
<string name="amoled_mode">Deep black backgrounds</string>
645645
<!-- Description indicating amoled mode can lower power usage depending on the screen of the device. -->
646646
<string name="amoled_mode_summary">Can reduce power usage depending on the device’s screen technology</string>
647+
<!-- Option for setting navbar to follow keyboard color -->
648+
<string name="theme_navbar">Color navigation bar</string>
647649
<!-- Option for selecting custom theme colors -->
648650
<string name="select_user_colors">Adjust theme colors</string>
649651
<!-- Description for selection of user-defined colors. -->

app/src/main/res/xml/prefs_screen_appearance.xml

+4
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@
4545
android:title="@string/amoled_mode"
4646
android:summary="@string/amoled_mode_summary"/>
4747

48+
<CheckBoxPreference
49+
android:key="theme_key_borders"
50+
android:title="@string/theme_navbar"/>
51+
4852
<Preference
4953
android:key="theme_select_colors"
5054
android:title="@string/select_user_colors"

0 commit comments

Comments
 (0)