fix wrong day/night theme showing in ColorsSettingsFragment

and run the keyboard reload in background to avoid visual glitches
This commit is contained in:
Helium314 2023-09-12 14:43:50 +02:00
parent b18b7dc820
commit 2ade6b3b09
2 changed files with 19 additions and 20 deletions

View file

@ -46,17 +46,18 @@ open class ColorsSettingsFragment : Fragment(R.layout.color_settings) {
val actionBar = activity.supportActionBar ?: return
actionBar.setTitle(titleResId)
}
if (isNight != ResourceUtils.isNight(requireContext().resources))
if (isNight != ResourceUtils.isNight(requireContext().resources)) {
// reload to get the right configuration
// todo: this does not work, keyboard also reloading with some other context
prefs.edit { putBoolean(Settings.PREF_FORCE_OPPOSITE_THEME, true) }
reloadKeyboard(false)
}
}
override fun onPause() {
super.onPause()
prefs.edit { putBoolean(Settings.PREF_FORCE_OPPOSITE_THEME, false) }
if (isNight != ResourceUtils.isNight(requireContext().resources))
// reload again so the correct configuration is applied
// todo: this does not work, keyboard also reloading with some other context
KeyboardSwitcher.getInstance().forceUpdateKeyboardTheme(requireContext())
}
@ -92,7 +93,7 @@ open class ColorsSettingsFragment : Fragment(R.layout.color_settings) {
val hidden = RichInputMethodManager.getInstance().inputMethodManager.hideSoftInputFromWindow(binding.dummyText.windowToken, 0)
val b = ColorPickerDialog.Builder(requireContext())
.setTitle(colorPrefNames[index])
// todo: later it should be activated, but currently setting alpha leads to glitches,
// todo: later alphy bar should be activated, but currently setting alpha leads to glitches,
// e.g. when setting alpha on key text it's not applied for key icons, but for emojis
.attachAlphaSlideBar(false)
.setPositiveButton(android.R.string.ok, ColorEnvelopeListener { envelope, _ ->
@ -142,25 +143,23 @@ open class ColorsSettingsFragment : Fragment(R.layout.color_settings) {
// only reloading main keyboard view is necessary...
// or get an actual (live) preview instead of the full keyboard?
// or accelerate keyboard inflate, a big here issue is emojiCategory creating many keyboards
KeyboardSwitcher.getInstance().forceUpdateKeyboardTheme(requireContext())
// todo: this does not work, keyboard also reloading with some other context
// KeyboardSwitcher.getInstance().forceUpdateKeyboardTheme(Settings.getDayNightContext(requireContext(), isNight))
if (!show) return
Thread.sleep(100) // some pause is necessary to avoid visual glitches
RichInputMethodManager.getInstance().inputMethodManager.showSoftInput(binding.dummyText, 0)
return
// for some reason showing again does not work when running with executor
// but when running without it's noticeably slow, and sometimes produces glitches
// todo: decider whether to just hide, or have some slowdown and show again
// KeyboardSwitcher.getInstance().forceUpdateKeyboardTheme(requireContext())
// if (!show) return
// Thread.sleep(100) // some pause is necessary to avoid visual glitches
// RichInputMethodManager.getInstance().inputMethodManager.showSoftInput(binding.dummyText, 0)
// return
// todo: fix slowdowns and sometimes showing glitches with above, then move away from executor
ExecutorUtils.getBackgroundExecutor(ExecutorUtils.KEYBOARD).execute {
KeyboardSwitcher.getInstance().forceUpdateKeyboardTheme(requireContext())
if (!show) return@execute
// for some reason showing again does not work when running with executor
// but when running without it's noticeably slow, and sometimes produces glitches
Thread.sleep(100)
RichInputMethodManager.getInstance().inputMethodManager.showSoftInput(binding.dummyText, 0)
}
}
}
class ColorsNightSettingsFragment : ColorsSettingsFragment() {

View file

@ -133,10 +133,6 @@ public final class Settings implements SharedPreferences.OnSharedPreferenceChang
public static final String PREF_DONT_SHOW_MISSING_DICTIONARY_DIALOG = "pref_dont_show_missing_dict_dialog";
// This preference key is deprecated. Use {@link #PREF_SHOW_LANGUAGE_SWITCH_KEY} instead.
// This is being used only for the backward compatibility.
private static final String PREF_SUPPRESS_LANGUAGE_SWITCH_KEY =
"pref_suppress_language_switch_key";
private static final String PREF_LAST_USED_PERSONALIZATION_TOKEN =
"pref_last_used_personalization_token";
@ -151,6 +147,9 @@ public final class Settings implements SharedPreferences.OnSharedPreferenceChang
public static final String PREF_LAST_SHOWN_EMOJI_CATEGORY_ID = "last_shown_emoji_category_id";
public static final String PREF_LAST_SHOWN_EMOJI_CATEGORY_PAGE_ID = "last_shown_emoji_category_page_id";
// used as a workaround against keyboard not showing edited theme in ColorsSettingsFragment
public static final String PREF_FORCE_OPPOSITE_THEME = "force_opposite_theme";
private static final float UNDEFINED_PREFERENCE_VALUE_FLOAT = -1.0f;
private static final int UNDEFINED_PREFERENCE_VALUE_INT = -1;
@ -535,7 +534,8 @@ public final class Settings implements SharedPreferences.OnSharedPreferenceChang
}
public static Colors getColorsForCurrentTheme(final Context context, final SharedPreferences prefs) {
final boolean isNight = ResourceUtils.isNight(context.getResources());
boolean isNight = ResourceUtils.isNight(context.getResources());
if (prefs.getBoolean(PREF_FORCE_OPPOSITE_THEME, false)) isNight = !isNight;
final String themeColors = (isNight && prefs.getBoolean(PREF_THEME_DAY_NIGHT, context.getResources().getBoolean(R.bool.day_night_default)))
? prefs.getString(Settings.PREF_THEME_VARIANT_NIGHT, KeyboardTheme.THEME_DARKER)
: prefs.getString(Settings.PREF_THEME_VARIANT, KeyboardTheme.THEME_LIGHT);