Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement proper theme changing logic #284

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions app/src/main/java/org/stratoemu/strato/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ import androidx.documentfile.provider.DocumentFile
import androidx.preference.PreferenceManager
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.repeatOnLifecycle
import com.google.android.material.snackbar.Snackbar
import dagger.hilt.android.AndroidEntryPoint
import org.stratoemu.strato.adapter.*
Expand All @@ -39,9 +42,11 @@ import org.stratoemu.strato.settings.EmulationSettings
import org.stratoemu.strato.settings.SettingsActivity
import org.stratoemu.strato.utils.GpuDriverHelper
import org.stratoemu.strato.utils.WindowInsetsHelper
import org.stratoemu.strato.di.getSettings
import javax.inject.Inject
import kotlin.math.ceil
import com.google.android.material.R as MaterialR
import kotlinx.coroutines.launch

@AndroidEntryPoint
class MainActivity : AppCompatActivity() {
Expand Down Expand Up @@ -100,6 +105,7 @@ class MainActivity : AppCompatActivity() {
else -> AppCompatDelegate.MODE_NIGHT_UNSPECIFIED
}
)
setTheme(if (getSettings().useMaterialYou) R.style.AppTheme_MaterialYou else R.style.AppTheme)
super.onCreate(savedInstanceState)

setContentView(binding.root)
Expand Down Expand Up @@ -152,6 +158,15 @@ class MainActivity : AppCompatActivity() {
window.decorView.findViewById<View>(android.R.id.content).viewTreeObserver.addOnTouchModeChangeListener { isInTouchMode ->
refreshIconVisible = !isInTouchMode
}

lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.CREATED) {
StratoApplication.themeChangeFlow.collect { themeId ->
// TODO(Ishan09811): Add more material color themes
recreate()
}
}
}
}

private fun setAppListDecoration() {
Expand Down
18 changes: 13 additions & 5 deletions app/src/main/java/org/stratoemu/strato/StratoApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ package org.stratoemu.strato

import android.app.Application
import android.content.Context
import com.google.android.material.color.DynamicColors
import com.google.android.material.color.DynamicColorsOptions
import dagger.hilt.android.HiltAndroidApp
import org.stratoemu.strato.di.getSettings
import java.io.File
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

/**
* @return The optimal directory for putting public files inside, this may return a private directory if a public directory cannot be retrieved
Expand All @@ -29,14 +32,19 @@ class StratoApplication : Application() {
private set

val context : Context get() = instance.applicationContext

private val _themeChangeFlow = MutableSharedFlow<Int>()
val themeChangeFlow = _themeChangeFlow.asSharedFlow()

fun setTheme(newValue: Boolean) {
val newTheme = if (newValue) R.style.AppTheme_MaterialYou else R.style.AppTheme
CoroutineScope(Dispatchers.Main).launch { _themeChangeFlow.emit(newTheme) }
}
}

override fun onCreate() {
super.onCreate()
instance = this
System.loadLibrary("skyline")

val dynamicColorsOptions = DynamicColorsOptions.Builder().setPrecondition { _, _ -> getSettings().useMaterialYou }.build()
DynamicColors.applyToActivitiesIfAvailable(this, dynamicColorsOptions)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import org.stratoemu.strato.input.dialog.StickDialog
import org.stratoemu.strato.input.onscreen.OnScreenEditActivity
import org.stratoemu.strato.settings.AppSettings
import org.stratoemu.strato.utils.WindowInsetsHelper
import org.stratoemu.strato.di.getSettings
import javax.inject.Inject

/**
Expand Down Expand Up @@ -174,6 +175,7 @@ class ControllerActivity : AppCompatActivity() {
* This initializes all of the elements in the activity
*/
override fun onCreate(state : Bundle?) {
setTheme(if (getSettings().useMaterialYou) R.style.AppTheme_MaterialYou else R.style.AppTheme)
super.onCreate(state)

if (id < 0 || id > 7)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import org.stratoemu.strato.utils.GpuDriverHelper
import org.stratoemu.strato.utils.GpuDriverInstallResult
import org.stratoemu.strato.utils.WindowInsetsHelper
import org.stratoemu.strato.utils.serializable
import org.stratoemu.strato.di.getSettings
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
Expand Down Expand Up @@ -125,6 +126,7 @@ class GpuDriverActivity : AppCompatActivity() {
}

override fun onCreate(savedInstanceState : Bundle?) {
setTheme(if (getSettings().useMaterialYou) R.style.AppTheme_MaterialYou else R.style.AppTheme)
super.onCreate(savedInstanceState)

setContentView(binding.root)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package org.stratoemu.strato.settings

import android.content.Intent
import android.os.Bundle
import android.os.Build
import android.view.View
import androidx.preference.Preference
import androidx.preference.PreferenceCategory
Expand All @@ -20,6 +21,7 @@ import org.stratoemu.strato.MainActivity
import org.stratoemu.strato.R
import org.stratoemu.strato.utils.GpuDriverHelper
import org.stratoemu.strato.utils.WindowInsetsHelper
import org.stratoemu.strato.StratoApplication
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
Expand All @@ -45,9 +47,9 @@ class GlobalSettingsFragment : PreferenceFragmentCompat() {
addPreferencesFromResource(R.xml.credits_preferences)

// Re-launch the app if Material You is toggled
findPreference<Preference>("use_material_you")?.setOnPreferenceChangeListener { _, _ ->
requireActivity().finishAffinity()
startActivity(Intent(requireContext(), MainActivity::class.java))
findPreference<Preference>("use_material_you")?.setOnPreferenceChangeListener { _, newValue ->
val isMaterialYouEnabled = newValue as Boolean
StratoApplication.setTheme(isMaterialYouEnabled)
true
}

Expand All @@ -72,11 +74,23 @@ class GlobalSettingsFragment : PreferenceFragmentCompat() {
if (BuildConfig.BUILD_TYPE != "release")
findPreference<Preference>("validation_layer")?.isVisible = true

if (!GpuDriverHelper.supportsForceMaxGpuClocks()) {
val forceMaxGpuClocksPref = findPreference<TwoStatePreference>("force_max_gpu_clocks")!!
forceMaxGpuClocksPref.isSelectable = false
forceMaxGpuClocksPref.isChecked = false
forceMaxGpuClocksPref.summary = context!!.getString(R.string.force_max_gpu_clocks_desc_unsupported)
disablePreference("use_material_you", Build.VERSION.SDK_INT < Build.VERSION_CODES.S, null)
disablePreference("force_max_gpu_clocks", !GpuDriverHelper.supportsForceMaxGpuClocks(), context!!.getString(R.string.force_max_gpu_clocks_desc_unsupported))
}

private fun disablePreference(
preferenceId: String,
isDisabled: Boolean,
disabledSummary: String? = null
) {
val preference = findPreference<Preference>(preferenceId)!!
preference.isSelectable = !isDisabled
preference.isEnabled = !isDisabled
if (preference is TwoStatePreference && isDisabled) {
preference.isChecked = false
}
if (isDisabled && disabledSummary != null) {
preference.summary = disabledSummary
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ import androidx.preference.Preference
import androidx.preference.PreferenceCategory
import androidx.preference.PreferenceFragmentCompat
import androidx.preference.forEach
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.repeatOnLifecycle
import com.google.android.material.appbar.AppBarLayout
import com.google.android.material.internal.ToolbarUtils
import org.stratoemu.strato.BuildConfig
Expand All @@ -33,6 +36,9 @@ import org.stratoemu.strato.preference.dialog.EditTextPreferenceMaterialDialogFr
import org.stratoemu.strato.preference.dialog.IntegerListPreferenceMaterialDialogFragmentCompat
import org.stratoemu.strato.preference.dialog.ListPreferenceMaterialDialogFragmentCompat
import org.stratoemu.strato.utils.WindowInsetsHelper
import org.stratoemu.strato.StratoApplication
import org.stratoemu.strato.di.getSettings
import kotlinx.coroutines.launch

private const val PREFERENCE_DIALOG_FRAGMENT_TAG = "androidx.preference.PreferenceFragment.DIALOG"

Expand All @@ -59,6 +65,7 @@ class SettingsActivity : AppCompatActivity(), PreferenceFragmentCompat.OnPrefere
* This initializes all of the elements in the activity and displays the settings fragment
*/
override fun onCreate(savedInstanceState : Bundle?) {
setTheme(if (getSettings().useMaterialYou) R.style.AppTheme_MaterialYou else R.style.AppTheme)
super.onCreate(savedInstanceState)

setContentView(binding.root)
Expand Down Expand Up @@ -123,6 +130,15 @@ class SettingsActivity : AppCompatActivity(), PreferenceFragmentCompat.OnPrefere
.replace(R.id.settings, preferenceFragment)
.commit()
}

lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.CREATED) {
StratoApplication.themeChangeFlow.collect { themeId ->
// TODO(Ishan09811): Add more material color themes
recreate()
}
}
}
}

override fun onCreateOptionsMenu(menu : Menu?) : Boolean {
Expand Down
63 changes: 63 additions & 0 deletions app/src/main/res/values-night-v31/themes.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme.MaterialYou" parent="AppTheme">
<item name="colorPrimary">@color/m3_sys_color_dynamic_dark_primary</item>
<item name="colorOnPrimary">@color/m3_sys_color_dynamic_dark_on_primary</item>
<item name="colorPrimaryInverse">@color/m3_sys_color_dynamic_dark_inverse_primary</item>
<item name="colorPrimaryContainer">@color/m3_sys_color_dynamic_dark_primary_container</item>
<item name="colorOnPrimaryContainer">@color/m3_sys_color_dynamic_dark_on_primary_container</item>
<item name="colorPrimaryFixed">@color/m3_sys_color_dynamic_primary_fixed</item>
<item name="colorPrimaryFixedDim">@color/m3_sys_color_dynamic_primary_fixed_dim</item>
<item name="colorOnPrimaryFixed">@color/m3_sys_color_dynamic_on_primary_fixed</item>
<item name="colorOnPrimaryFixedVariant">@color/m3_sys_color_dynamic_on_primary_fixed_variant</item>
<item name="colorSecondary">@color/m3_sys_color_dynamic_dark_secondary</item>
<item name="colorOnSecondary">@color/m3_sys_color_dynamic_dark_on_secondary</item>
<item name="colorSecondaryContainer">@color/m3_sys_color_dynamic_dark_secondary_container</item>
<item name="colorOnSecondaryContainer">@color/m3_sys_color_dynamic_dark_on_secondary_container</item>
<item name="colorSecondaryFixed">@color/m3_sys_color_dynamic_secondary_fixed</item>
<item name="colorSecondaryFixedDim">@color/m3_sys_color_dynamic_secondary_fixed_dim</item>
<item name="colorOnSecondaryFixed">@color/m3_sys_color_dynamic_on_secondary_fixed</item>
<item name="colorOnSecondaryFixedVariant">@color/m3_sys_color_dynamic_on_secondary_fixed_variant</item>
<item name="colorTertiary">@color/m3_sys_color_dynamic_dark_tertiary</item>
<item name="colorOnTertiary">@color/m3_sys_color_dynamic_dark_on_tertiary</item>
<item name="colorTertiaryContainer">@color/m3_sys_color_dynamic_dark_tertiary_container</item>
<item name="colorOnTertiaryContainer">@color/m3_sys_color_dynamic_dark_on_tertiary_container</item>
<item name="colorTertiaryFixed">@color/m3_sys_color_dynamic_tertiary_fixed</item>
<item name="colorTertiaryFixedDim">@color/m3_sys_color_dynamic_tertiary_fixed_dim</item>
<item name="colorOnTertiaryFixed">@color/m3_sys_color_dynamic_on_tertiary_fixed</item>
<item name="colorOnTertiaryFixedVariant">@color/m3_sys_color_dynamic_on_tertiary_fixed_variant</item>
<item name="android:colorBackground">@color/m3_sys_color_dynamic_dark_background</item>
<item name="colorOnBackground">@color/m3_sys_color_dynamic_dark_on_background</item>
<item name="colorSurface">@color/m3_sys_color_dynamic_dark_surface</item>
<item name="colorOnSurface">@color/m3_sys_color_dynamic_dark_on_surface</item>
<item name="colorSurfaceVariant">@color/m3_sys_color_dynamic_dark_surface_variant</item>
<item name="colorOnSurfaceVariant">@color/m3_sys_color_dynamic_dark_on_surface_variant</item>
<item name="colorSurfaceInverse">@color/m3_sys_color_dynamic_dark_inverse_surface</item>
<item name="colorOnSurfaceInverse">@color/m3_sys_color_dynamic_dark_inverse_on_surface</item>
<item name="colorSurfaceBright">@color/m3_sys_color_dynamic_dark_surface_bright</item>
<item name="colorSurfaceDim">@color/m3_sys_color_dynamic_dark_surface_dim</item>
<item name="colorSurfaceContainer">@color/m3_sys_color_dynamic_dark_surface_container</item>
<item name="colorSurfaceContainerLow">@color/m3_sys_color_dynamic_dark_surface_container_low</item>
<item name="colorSurfaceContainerHigh">@color/m3_sys_color_dynamic_dark_surface_container_high</item>
<item name="colorSurfaceContainerLowest">@color/m3_sys_color_dynamic_dark_surface_container_lowest</item>
<item name="colorSurfaceContainerHighest">@color/m3_sys_color_dynamic_dark_surface_container_highest</item>
<item name="colorOutline">@color/m3_sys_color_dynamic_dark_outline</item>
<item name="colorError">@color/m3_sys_color_dark_error</item>
<item name="colorOnError">@color/m3_sys_color_dark_on_error</item>
<item name="colorErrorContainer">@color/m3_sys_color_dark_error_container</item>
<item name="colorOnErrorContainer">@color/m3_sys_color_dark_on_error_container</item>
<item name="android:textColorPrimary">@color/m3_dynamic_dark_default_color_primary_text</item>
<item name="android:textColorPrimaryInverse">@color/m3_dynamic_default_color_primary_text</item>
<item name="android:textColorSecondary">@color/m3_dynamic_dark_default_color_secondary_text</item>
<item name="android:textColorSecondaryInverse">@color/m3_dynamic_default_color_secondary_text</item>
<item name="android:textColorTertiary">@color/m3_dynamic_dark_default_color_secondary_text</item>
<item name="android:textColorTertiaryInverse">@color/m3_dynamic_default_color_secondary_text</item>
<item name="android:textColorPrimaryDisableOnly">@color/m3_dynamic_dark_primary_text_disable_only</item>
<item name="android:textColorPrimaryInverseDisableOnly">@color/m3_dynamic_primary_text_disable_only</item>
<item name="android:textColorHint">@color/m3_dynamic_dark_hint_foreground</item>
<item name="android:textColorHintInverse">@color/m3_dynamic_hint_foreground</item>
<item name="android:textColorHighlight">@color/m3_dynamic_dark_highlighted_text</item>
<item name="android:textColorHighlightInverse">@color/m3_dynamic_highlighted_text</item>
<item name="android:textColorAlertDialogListItem">@color/m3_dynamic_dark_default_color_primary_text</item>
</style>
</resources>
Loading