android: Add theme mode picker

This commit is contained in:
Charles Lombardo 2023-04-20 18:01:09 -04:00 committed by bunnei
parent f0ba58f5aa
commit d9684a2010
5 changed files with 76 additions and 11 deletions

View File

@ -136,6 +136,7 @@ class Settings {
const val PREF_FIRST_APP_LAUNCH = "FirstApplicationLaunch" const val PREF_FIRST_APP_LAUNCH = "FirstApplicationLaunch"
const val PREF_THEME = "Theme" const val PREF_THEME = "Theme"
const val PREF_THEME_MODE = "ThemeMode"
private val configFileSectionsMap: MutableMap<String, List<String>> = HashMap() private val configFileSectionsMap: MutableMap<String, List<String>> = HashMap()

View File

@ -16,6 +16,7 @@ import org.yuzu.yuzu_emu.features.settings.model.IntSetting
import org.yuzu.yuzu_emu.features.settings.model.Settings import org.yuzu.yuzu_emu.features.settings.model.Settings
import org.yuzu.yuzu_emu.features.settings.model.view.* import org.yuzu.yuzu_emu.features.settings.model.view.*
import org.yuzu.yuzu_emu.features.settings.utils.SettingsFile import org.yuzu.yuzu_emu.features.settings.utils.SettingsFile
import org.yuzu.yuzu_emu.utils.ThemeHelper
class SettingsFragmentPresenter(private val fragmentView: SettingsFragmentView) { class SettingsFragmentPresenter(private val fragmentView: SettingsFragmentView) {
private var menuTag: String? = null private var menuTag: String? = null
@ -355,6 +356,30 @@ class SettingsFragmentPresenter(private val fragmentView: SettingsFragmentView)
) )
) )
} }
val themeMode: AbstractIntSetting = object : AbstractIntSetting {
override var int: Int
get() = preferences.getInt(Settings.PREF_THEME_MODE, -1)
set(value) {
preferences.edit().putInt(Settings.PREF_THEME_MODE, value).apply()
ThemeHelper.setThemeMode(settingsActivity)
}
override val key: String? = null
override val section: String? = null
override val isRuntimeEditable: Boolean = true
override val valueAsString: String
get() = preferences.getInt(Settings.PREF_THEME_MODE, -1).toString()
}
add(
SingleChoiceSetting(
themeMode,
R.string.change_theme_mode,
0,
R.array.themeModeEntries,
R.array.themeModeValues
)
)
} }
} }
} }

View File

@ -8,8 +8,10 @@ import android.content.res.Configuration
import android.graphics.Color import android.graphics.Color
import androidx.annotation.ColorInt import androidx.annotation.ColorInt
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.app.AppCompatDelegate
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import androidx.core.view.WindowCompat import androidx.core.view.WindowCompat
import androidx.core.view.WindowInsetsControllerCompat
import androidx.preference.PreferenceManager import androidx.preference.PreferenceManager
import org.yuzu.yuzu_emu.R import org.yuzu.yuzu_emu.R
import org.yuzu.yuzu_emu.YuzuApplication import org.yuzu.yuzu_emu.YuzuApplication
@ -26,21 +28,11 @@ object ThemeHelper {
@JvmStatic @JvmStatic
fun setTheme(activity: AppCompatActivity) { fun setTheme(activity: AppCompatActivity) {
val preferences = PreferenceManager.getDefaultSharedPreferences(YuzuApplication.appContext) val preferences = PreferenceManager.getDefaultSharedPreferences(YuzuApplication.appContext)
setThemeMode(activity)
when (preferences.getInt(Settings.PREF_THEME, 0)) { when (preferences.getInt(Settings.PREF_THEME, 0)) {
DEFAULT -> activity.setTheme(R.style.Theme_Yuzu_Main) DEFAULT -> activity.setTheme(R.style.Theme_Yuzu_Main)
MATERIAL_YOU -> activity.setTheme(R.style.Theme_Yuzu_Main_MaterialYou) MATERIAL_YOU -> activity.setTheme(R.style.Theme_Yuzu_Main_MaterialYou)
} }
val windowController = WindowCompat.getInsetsController(
activity.window,
activity.window.decorView
)
val isLightMode =
(activity.resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK) == Configuration.UI_MODE_NIGHT_NO
windowController.isAppearanceLightStatusBars = isLightMode
windowController.isAppearanceLightNavigationBars = isLightMode
activity.window.statusBarColor = ContextCompat.getColor(activity, android.R.color.transparent)
} }
@JvmStatic @JvmStatic
@ -80,4 +72,34 @@ object ThemeHelper {
activity.recreate() activity.recreate()
} }
} }
fun setThemeMode(activity: AppCompatActivity) {
val themeMode = PreferenceManager.getDefaultSharedPreferences(activity.applicationContext)
.getInt(Settings.PREF_THEME_MODE, AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
activity.delegate.localNightMode = themeMode
val windowController = WindowCompat.getInsetsController(
activity.window,
activity.window.decorView
)
val systemReportedThemeMode =
activity.resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK
when (themeMode) {
AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM -> when (systemReportedThemeMode) {
Configuration.UI_MODE_NIGHT_NO -> setLightModeSystemBars(windowController)
Configuration.UI_MODE_NIGHT_YES -> setDarkModeSystemBars(windowController)
}
AppCompatDelegate.MODE_NIGHT_NO -> setLightModeSystemBars(windowController)
AppCompatDelegate.MODE_NIGHT_YES -> setDarkModeSystemBars(windowController)
}
}
private fun setLightModeSystemBars(windowController: WindowInsetsControllerCompat) {
windowController.isAppearanceLightStatusBars = true
windowController.isAppearanceLightNavigationBars = true
}
private fun setDarkModeSystemBars(windowController: WindowInsetsControllerCompat) {
windowController.isAppearanceLightStatusBars = false
windowController.isAppearanceLightNavigationBars = false
}
} }

View File

@ -199,4 +199,15 @@
<item>1</item> <item>1</item>
</integer-array> </integer-array>
<string-array name="themeModeEntries">
<item>@string/theme_mode_follow_system</item>
<item>@string/theme_mode_light</item>
<item>@string/theme_mode_dark</item>
</string-array>
<integer-array name="themeModeValues">
<item>-1</item>
<item>1</item>
<item>2</item>
</integer-array>
</resources> </resources>

View File

@ -235,4 +235,10 @@
<string name="theme_default">Default</string> <string name="theme_default">Default</string>
<string name="theme_material_you">Material You</string> <string name="theme_material_you">Material You</string>
<!-- Theme Modes -->
<string name="change_theme_mode">Change Theme Mode</string>
<string name="theme_mode_follow_system">Follow System</string>
<string name="theme_mode_light">Light</string>
<string name="theme_mode_dark">Dark</string>
</resources> </resources>