Update dynamic themes when colors change

This commit is contained in:
Aleksandras Kostarevas 2023-08-26 20:26:26 +03:00
parent bd0368d89f
commit d72838e659
8 changed files with 74 additions and 24 deletions

View File

@ -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)

View File

@ -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

View File

@ -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
}

View File

@ -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,

View File

@ -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
}

View File

@ -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
}

View File

@ -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)
}
)

View File

@ -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
}