From d72838e659f4b98d88408fba1621aba6ab0994f0 Mon Sep 17 00:00:00 2001 From: Aleksandras Kostarevas Date: Sat, 26 Aug 2023 20:26:26 +0300 Subject: [PATCH] Update dynamic themes when colors change --- .../org/futo/inputmethod/latin/LatinIME.kt | 18 +++++- .../latin/uix/InlineSuggestionView.kt | 1 - .../org/futo/inputmethod/latin/uix/Utils.kt | 16 ++++++ .../latin/uix/theme/ThemeOptions.kt | 1 + .../uix/theme/presets/AMOLEDDarkPurple.kt | 2 +- .../uix/theme/presets/ClassicMaterialDark.kt | 2 +- .../latin/uix/theme/presets/DynamicThemes.kt | 56 ++++++++++++------- .../uix/theme/presets/VoiceInputTheme.kt | 2 +- 8 files changed, 74 insertions(+), 24 deletions(-) create mode 100644 java/src/org/futo/inputmethod/latin/uix/Utils.kt diff --git a/java/src/org/futo/inputmethod/latin/LatinIME.kt b/java/src/org/futo/inputmethod/latin/LatinIME.kt index 8a11663c1..2b01aa3c7 100644 --- a/java/src/org/futo/inputmethod/latin/LatinIME.kt +++ b/java/src/org/futo/inputmethod/latin/LatinIME.kt @@ -63,11 +63,13 @@ import org.futo.inputmethod.latin.uix.THEME_KEY import org.futo.inputmethod.latin.uix.createInlineSuggestionsRequest import org.futo.inputmethod.latin.uix.deferGetSetting import org.futo.inputmethod.latin.uix.deferSetSetting +import org.futo.inputmethod.latin.uix.differsFrom import org.futo.inputmethod.latin.uix.theme.DarkColorScheme import org.futo.inputmethod.latin.uix.theme.ThemeOption import org.futo.inputmethod.latin.uix.theme.ThemeOptions import org.futo.inputmethod.latin.uix.theme.Typography import org.futo.inputmethod.latin.uix.theme.UixThemeWrapper +import org.futo.inputmethod.latin.uix.theme.presets.ClassicMaterialDark import org.futo.inputmethod.latin.uix.theme.presets.DynamicSystemTheme import org.futo.inputmethod.latin.uix.theme.presets.VoiceInputTheme @@ -110,6 +112,7 @@ class LatinIME : InputMethodService(), LifecycleOwner, ViewModelStoreOwner, Save this as LatinIMELegacy.SuggestionStripController ) + private var activeThemeOption: ThemeOption? = null private var activeColorScheme = DarkColorScheme private var colorSchemeLoaderJob: Job? = null @@ -153,6 +156,17 @@ class LatinIME : InputMethodService(), LifecycleOwner, ViewModelStoreOwner, Save return drawableProvider!! } + private fun updateColorsIfDynamicChanged() { + if(activeThemeOption?.dynamic == true) { + val currColors = activeColorScheme + val nextColors = activeThemeOption!!.obtainColors(this) + + if(currColors.differsFrom(nextColors)) { + updateDrawableProvider(nextColors) + } + } + } + override fun onCreate() { super.onCreate() @@ -164,6 +178,7 @@ class LatinIME : InputMethodService(), LifecycleOwner, ViewModelStoreOwner, Save themeOption = ThemeOptions[themeKey]!! } + activeThemeOption = themeOption activeColorScheme = themeOption.obtainColors(this@LatinIME) } @@ -379,7 +394,7 @@ class LatinIME : InputMethodService(), LifecycleOwner, ViewModelStoreOwner, Save super.onWindowShown() latinIMELegacy.onWindowShown() - // TODO: Check here if the dynamic color scheme has changed, reset and rebuild if so + updateColorsIfDynamicChanged() } override fun onWindowHidden() { @@ -518,6 +533,7 @@ class LatinIME : InputMethodService(), LifecycleOwner, ViewModelStoreOwner, Save override fun updateTheme(newTheme: ThemeOption) { assert(newTheme.available(this)) + activeThemeOption = newTheme updateDrawableProvider(newTheme.obtainColors(this)) deferSetSetting(THEME_KEY, newTheme.key) diff --git a/java/src/org/futo/inputmethod/latin/uix/InlineSuggestionView.kt b/java/src/org/futo/inputmethod/latin/uix/InlineSuggestionView.kt index ff7b1c22f..88121fb8a 100644 --- a/java/src/org/futo/inputmethod/latin/uix/InlineSuggestionView.kt +++ b/java/src/org/futo/inputmethod/latin/uix/InlineSuggestionView.kt @@ -17,7 +17,6 @@ import androidx.autofill.inline.common.TextViewStyle import androidx.autofill.inline.common.ViewStyle import androidx.autofill.inline.v1.InlineSuggestionUi import androidx.compose.foundation.layout.RowScope -import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.padding import androidx.compose.foundation.lazy.LazyRow import androidx.compose.material3.ColorScheme diff --git a/java/src/org/futo/inputmethod/latin/uix/Utils.kt b/java/src/org/futo/inputmethod/latin/uix/Utils.kt new file mode 100644 index 000000000..c22dd5ed6 --- /dev/null +++ b/java/src/org/futo/inputmethod/latin/uix/Utils.kt @@ -0,0 +1,16 @@ +package org.futo.inputmethod.latin.uix + +import androidx.compose.material3.ColorScheme + +// Not exhaustive +fun ColorScheme.differsFrom(other: ColorScheme): Boolean { + return this.background != other.background + || this.surface != other.surface + || this.primary != other.primary + || this.secondary != other.secondary + || this.primaryContainer != other.primaryContainer + || this.secondaryContainer != other.secondaryContainer + || this.onSurface != other.onSurface + || this.onBackground != other.onBackground + || this.onPrimary != other.onPrimary +} \ No newline at end of file diff --git a/java/src/org/futo/inputmethod/latin/uix/theme/ThemeOptions.kt b/java/src/org/futo/inputmethod/latin/uix/theme/ThemeOptions.kt index 63b7db877..b2915a36f 100644 --- a/java/src/org/futo/inputmethod/latin/uix/theme/ThemeOptions.kt +++ b/java/src/org/futo/inputmethod/latin/uix/theme/ThemeOptions.kt @@ -10,6 +10,7 @@ import org.futo.inputmethod.latin.uix.theme.presets.DynamicSystemTheme import org.futo.inputmethod.latin.uix.theme.presets.VoiceInputTheme data class ThemeOption( + val dynamic: Boolean, val key: String, val name: String, // TODO: @StringRes Int val available: (Context) -> Boolean, diff --git a/java/src/org/futo/inputmethod/latin/uix/theme/presets/AMOLEDDarkPurple.kt b/java/src/org/futo/inputmethod/latin/uix/theme/presets/AMOLEDDarkPurple.kt index 78c9c3530..9d31063a5 100644 --- a/java/src/org/futo/inputmethod/latin/uix/theme/presets/AMOLEDDarkPurple.kt +++ b/java/src/org/futo/inputmethod/latin/uix/theme/presets/AMOLEDDarkPurple.kt @@ -67,6 +67,6 @@ private val colorScheme = darkColorScheme( scrim = md_theme_dark_scrim, ) -val AMOLEDDarkPurple = ThemeOption("AMOLEDDarkPurple", "AMOLED Dark Purple", { true }) { +val AMOLEDDarkPurple = ThemeOption(false, "AMOLEDDarkPurple", "AMOLED Dark Purple", { true }) { colorScheme } \ No newline at end of file diff --git a/java/src/org/futo/inputmethod/latin/uix/theme/presets/ClassicMaterialDark.kt b/java/src/org/futo/inputmethod/latin/uix/theme/presets/ClassicMaterialDark.kt index 54d852316..4eb6c3588 100644 --- a/java/src/org/futo/inputmethod/latin/uix/theme/presets/ClassicMaterialDark.kt +++ b/java/src/org/futo/inputmethod/latin/uix/theme/presets/ClassicMaterialDark.kt @@ -84,7 +84,7 @@ private val colorScheme = darkColorScheme( scrim = md_theme_dark_scrim, ) -val ClassicMaterialDark = ThemeOption("ClassicMaterialDark", "AOSP Material Dark", { true }) { +val ClassicMaterialDark = ThemeOption(false, "ClassicMaterialDark", "AOSP Material Dark", { true }) { colorScheme } diff --git a/java/src/org/futo/inputmethod/latin/uix/theme/presets/DynamicThemes.kt b/java/src/org/futo/inputmethod/latin/uix/theme/presets/DynamicThemes.kt index c842ee93b..7bf154857 100644 --- a/java/src/org/futo/inputmethod/latin/uix/theme/presets/DynamicThemes.kt +++ b/java/src/org/futo/inputmethod/latin/uix/theme/presets/DynamicThemes.kt @@ -8,27 +8,45 @@ import androidx.compose.material3.dynamicDarkColorScheme import androidx.compose.material3.dynamicLightColorScheme import org.futo.inputmethod.latin.uix.theme.ThemeOption -val DynamicSystemTheme = ThemeOption("DynamicSystem", "Dynamic System", { Build.VERSION.SDK_INT >= Build.VERSION_CODES.S }) { - val uiModeManager = it.getSystemService(Context.UI_MODE_SERVICE) as UiModeManager - when (uiModeManager.nightMode) { - UiModeManager.MODE_NIGHT_YES -> dynamicDarkColorScheme(it) - UiModeManager.MODE_NIGHT_NO -> dynamicLightColorScheme(it) - UiModeManager.MODE_NIGHT_AUTO -> { - val currentNightMode = it.resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK - if(currentNightMode == Configuration.UI_MODE_NIGHT_NO) { - dynamicLightColorScheme(it) - } else { - dynamicDarkColorScheme(it) +val DynamicSystemTheme = ThemeOption( + dynamic = true, + key = "DynamicSystem", + name = "Dynamic System", + available = { Build.VERSION.SDK_INT >= Build.VERSION_CODES.S }, + obtainColors = { + val uiModeManager = it.getSystemService(Context.UI_MODE_SERVICE) as UiModeManager + when (uiModeManager.nightMode) { + UiModeManager.MODE_NIGHT_YES -> dynamicDarkColorScheme(it) + UiModeManager.MODE_NIGHT_NO -> dynamicLightColorScheme(it) + UiModeManager.MODE_NIGHT_AUTO -> { + val currentNightMode = it.resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK + if(currentNightMode == Configuration.UI_MODE_NIGHT_NO) { + dynamicLightColorScheme(it) + } else { + dynamicDarkColorScheme(it) + } } + else -> dynamicDarkColorScheme(it) } - else -> dynamicDarkColorScheme(it) } -} +) -val DynamicDarkTheme = ThemeOption("DynamicDark", "Dynamic Dark", { Build.VERSION.SDK_INT >= Build.VERSION_CODES.S }) { - dynamicDarkColorScheme(it) -} +val DynamicDarkTheme = ThemeOption( + dynamic = true, + key = "DynamicDark", + name = "Dynamic Dark", + available = { Build.VERSION.SDK_INT >= Build.VERSION_CODES.S }, + obtainColors = { + dynamicDarkColorScheme(it) + } +) -val DynamicLightTheme = ThemeOption("DynamicLight", "Dynamic Light", { Build.VERSION.SDK_INT >= Build.VERSION_CODES.S }) { - dynamicLightColorScheme(it) -} +val DynamicLightTheme = ThemeOption( + dynamic = true, + key = "DynamicLight", + name = "Dynamic Light", + available = { Build.VERSION.SDK_INT >= Build.VERSION_CODES.S }, + obtainColors = { + dynamicLightColorScheme(it) + } +) diff --git a/java/src/org/futo/inputmethod/latin/uix/theme/presets/VoiceInputTheme.kt b/java/src/org/futo/inputmethod/latin/uix/theme/presets/VoiceInputTheme.kt index 71c6a0765..a8bb43173 100644 --- a/java/src/org/futo/inputmethod/latin/uix/theme/presets/VoiceInputTheme.kt +++ b/java/src/org/futo/inputmethod/latin/uix/theme/presets/VoiceInputTheme.kt @@ -3,6 +3,6 @@ package org.futo.inputmethod.latin.uix.theme.presets import org.futo.inputmethod.latin.uix.theme.DarkColorScheme import org.futo.inputmethod.latin.uix.theme.ThemeOption -val VoiceInputTheme = ThemeOption("VoiceInputTheme", "Voice Input Theme", { true }) { +val VoiceInputTheme = ThemeOption(false, "VoiceInputTheme", "Voice Input Theme", { true }) { DarkColorScheme } \ No newline at end of file