Fixed the system theme overriding a fixed color theme

This commit is contained in:
Thorsten Sommer committed 2026-09-22 22:15:21 +02:00
1 parent c9d3760435
commit 50c686aa9d
3 files changed
+18 -8

No files matched your search

@@ -102,4 +102,4 @@
</MudLayout>
</MudPaper>
<MudThemeProvider @ref="@this.themeProvider" Theme="@this.ColorTheme" IsDarkMode="@this.useDarkMode" />
<MudThemeProvider @ref="@this.themeProvider" Theme="@this.ColorTheme" IsDarkMode="@this.useDarkMode" ObserveSystemThemeChange="@this.FollowSystemTheme" />
@@ -144,7 +144,7 @@ public partial class MainLayout : LayoutComponentBase, IMessageBusReceiver, ILan
// Send a message to start the plugin system:
await this.MessageBus.SendMessage<bool>(this, Event.STARTUP_PLUGIN_SYSTEM);
await this.themeProvider.WatchSystemDarkModeAsync(this.SystemeThemeChanged);
await this.themeProvider.WatchSystemDarkModeAsync(this.SystemThemeChanged);
await this.UpdateThemeConfiguration();
this.LoadNavItems();
this.LoadEmbeddingItem();
@@ -564,19 +564,28 @@ public partial class MainLayout : LayoutComponentBase, IMessageBusReceiver, ILan
}
}
private async Task SystemeThemeChanged(bool isDark)
/// <summary>
/// True, when the user wants AI Studio to follow the light or dark mode of the operating system.
/// </summary>
/// <remarks>
/// This also decides whether the MudThemeProvider watches the operating system at all. On a system
/// change, the provider takes the new mode into its own state first and calls our handler only
/// afterward, so the handler cannot prevent it. Nor can a new render of this layout undo it: the
/// provider takes over its IsDarkMode parameter only when that value changes, and with a fixed theme,
/// it never does. Were the provider watching while the user chose a fixed theme, MudBlazor would show
/// the colors of the system from the next render on, while the rest of the app kept the chosen ones.
/// </remarks>
private bool FollowSystemTheme => this.SettingsManager.ConfigurationData.App.PreferredTheme is Themes.SYSTEM;
private async Task SystemThemeChanged(bool isDark)
{
this.Logger.LogInformation($"The system theme changed to {(isDark ? "dark" : "light")}.");
if (this.SettingsManager.ConfigurationData.App.PreferredTheme is not Themes.SYSTEM)
return;
await this.UpdateThemeConfiguration();
}
private async Task UpdateThemeConfiguration()
{
if (this.SettingsManager.ConfigurationData.App.PreferredTheme is Themes.SYSTEM)
if (this.FollowSystemTheme)
this.useDarkMode = await this.themeProvider.GetSystemDarkModeAsync();
else
this.useDarkMode = this.SettingsManager.ConfigurationData.App.PreferredTheme == Themes.DARK;