Add "Follow OS theme" option

This commit is contained in:
yell0wsuit 2024-04-06 22:27:14 +07:00
parent 66b1d59c66
commit 08004909b2
No known key found for this signature in database
GPG key ID: 5B4F198A9800F6F4
5 changed files with 70 additions and 4 deletions

View file

@ -84,7 +84,7 @@ namespace Ryujinx.Ava
ApplyConfiguredTheme();
}
private void ApplyConfiguredTheme()
public void ApplyConfiguredTheme()
{
try
{
@ -92,13 +92,16 @@ namespace Ryujinx.Ava
if (string.IsNullOrWhiteSpace(baseStyle))
{
ConfigurationState.Instance.UI.BaseStyle.Value = "Dark";
ConfigurationState.Instance.UI.BaseStyle.Value = "Auto";
baseStyle = ConfigurationState.Instance.UI.BaseStyle;
}
ThemeVariant systemTheme = DetectSystemTheme();
RequestedThemeVariant = baseStyle switch
{
"Auto" => systemTheme,
"Light" => ThemeVariant.Light,
"Dark" => ThemeVariant.Dark,
_ => ThemeVariant.Default,
@ -111,5 +114,29 @@ namespace Ryujinx.Ava
ShowRestartDialog();
}
}
// Convert Avalonia.Platform.PlatformThemeVariant to the expected ThemeVariant type
private Avalonia.Styling.ThemeVariant ConvertThemeVariant(Avalonia.Platform.PlatformThemeVariant platformThemeVariant)
{
switch (platformThemeVariant)
{
case Avalonia.Platform.PlatformThemeVariant.Dark:
return Avalonia.Styling.ThemeVariant.Dark;
case Avalonia.Platform.PlatformThemeVariant.Light:
return Avalonia.Styling.ThemeVariant.Light;
default:
return Avalonia.Styling.ThemeVariant.Default;
}
}
private ThemeVariant DetectSystemTheme()
{
var platformSettings = this.PlatformSettings;
var colorValues = platformSettings.GetColorValues();
return ConvertThemeVariant(colorValues.ThemeVariant);
}
}
}

View file

@ -298,6 +298,7 @@
"GameListContextMenuToggleFavorite": "Toggle Favorite",
"GameListContextMenuToggleFavoriteToolTip": "Toggle Favorite status of Game",
"SettingsTabGeneralTheme": "Theme:",
"SettingsTabGeneralThemeAuto": "Follow OS theme",
"SettingsTabGeneralThemeDark": "Dark",
"SettingsTabGeneralThemeLight": "Light",
"ControllerSettingsConfigureGeneral": "Configure",

View file

@ -405,7 +405,13 @@ namespace Ryujinx.Ava.UI.ViewModels
GameDirectories.Clear();
GameDirectories.AddRange(config.UI.GameDirs.Value);
BaseStyleIndex = config.UI.BaseStyle == "Light" ? 0 : 1;
BaseStyleIndex = config.UI.BaseStyle.Value switch
{
"Auto" => 0,
"Light" => 1,
"Dark" => 2,
_ => 0
};
// Input
EnableDockedMode = config.System.EnableDockedMode;
@ -492,7 +498,13 @@ namespace Ryujinx.Ava.UI.ViewModels
config.UI.GameDirs.Value = gameDirs;
}
config.UI.BaseStyle.Value = BaseStyleIndex == 0 ? "Light" : "Dark";
config.UI.BaseStyle.Value = BaseStyleIndex switch
{
0 => "Auto",
1 => "Light",
2 => "Dark",
_ => "Auto"
};
// Input
config.System.EnableDockedMode.Value = EnableDockedMode;

View file

@ -62,6 +62,9 @@
<ComboBox SelectedIndex="{Binding BaseStyleIndex}"
HorizontalContentAlignment="Left"
MinWidth="100">
<ComboBoxItem>
<TextBlock Text="{locale:Locale SettingsTabGeneralThemeAuto}" />
</ComboBoxItem>
<ComboBoxItem>
<TextBlock Text="{locale:Locale SettingsTabGeneralThemeLight}" />
</ComboBoxItem>

View file

@ -2,6 +2,7 @@ using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Primitives;
using Avalonia.Interactivity;
using Avalonia.Platform;
using Avalonia.Threading;
using FluentAvalonia.UI.Controls;
using Ryujinx.Ava.Common;
@ -87,6 +88,25 @@ namespace Ryujinx.Ava.UI.Windows
}
}
// Event handler for detecting OS theme change when using "Follow OS theme" option
private void OnPlatformColorValuesChanged(object? sender, PlatformColorValues e)
{
if (Application.Current is App app)
{
app.ApplyConfiguredTheme();
}
}
protected override void OnClosed(EventArgs e)
{
base.OnClosed(e);
if (this.PlatformSettings != null)
{
// Unsubscribe to the ColorValuesChanged event
this.PlatformSettings.ColorValuesChanged -= OnPlatformColorValuesChanged;
}
}
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
{
base.OnApplyTemplate(e);
@ -370,6 +390,9 @@ namespace Ryujinx.Ava.UI.Windows
Initialize();
// Subscribe to the ColorValuesChanged event
this.PlatformSettings.ColorValuesChanged += OnPlatformColorValuesChanged;
ViewModel.Initialize(
ContentManager,
StorageProvider,