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

Fix: Fixed an issue where changing the theme mode could cause a crash #15391

Merged
merged 3 commits into from
May 13, 2024
Merged
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
62 changes: 35 additions & 27 deletions src/Files.App/Services/AppThemeModeService.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) 2024 Files Community
// Copyright (c) 2024 Files Community
// Licensed under the MIT License. See the LICENSE.

using Microsoft.Extensions.Logging;
using Microsoft.UI;
using Microsoft.UI.Windowing;
using Microsoft.UI.Xaml;
Expand Down Expand Up @@ -62,37 +63,44 @@ public void ApplyResources()
/// <inheritdoc/>
public void SetAppThemeMode(Window? window = null, AppWindowTitleBar? titleBar = null, ElementTheme? rootTheme = null, bool callThemeModeChangedEvent = true)
{
window ??= MainWindow.Instance;
titleBar ??= MainWindow.Instance.AppWindow.TitleBar;
rootTheme ??= AppThemeMode;

if (window.Content is FrameworkElement rootElement)
rootElement.RequestedTheme = (ElementTheme)rootTheme;

if (titleBar is not null)
try
{
titleBar.ButtonBackgroundColor = Colors.Transparent;
titleBar.ButtonInactiveBackgroundColor = Colors.Transparent;
window ??= MainWindow.Instance;
titleBar ??= MainWindow.Instance.AppWindow.TitleBar;
rootTheme ??= AppThemeMode;

switch (rootTheme)
if (window.Content is FrameworkElement rootElement)
rootElement.RequestedTheme = (ElementTheme)rootTheme;

if (titleBar is not null)
{
case ElementTheme.Default:
titleBar.ButtonHoverBackgroundColor = (Color)Application.Current.Resources["SystemBaseLowColor"];
titleBar.ButtonForegroundColor = (Color)Application.Current.Resources["SystemBaseHighColor"];
break;
case ElementTheme.Light:
titleBar.ButtonHoverBackgroundColor = Color.FromArgb(51, 0, 0, 0);
titleBar.ButtonForegroundColor = Colors.Black;
break;
case ElementTheme.Dark:
titleBar.ButtonHoverBackgroundColor = Color.FromArgb(51, 255, 255, 255);
titleBar.ButtonForegroundColor = Colors.White;
break;
titleBar.ButtonBackgroundColor = Colors.Transparent;
titleBar.ButtonInactiveBackgroundColor = Colors.Transparent;

switch (rootTheme)
{
case ElementTheme.Default:
titleBar.ButtonHoverBackgroundColor = (Color)Application.Current.Resources["SystemBaseLowColor"];
titleBar.ButtonForegroundColor = (Color)Application.Current.Resources["SystemBaseHighColor"];
break;
case ElementTheme.Light:
titleBar.ButtonHoverBackgroundColor = Color.FromArgb(51, 0, 0, 0);
titleBar.ButtonForegroundColor = Colors.Black;
break;
case ElementTheme.Dark:
titleBar.ButtonHoverBackgroundColor = Color.FromArgb(51, 255, 255, 255);
titleBar.ButtonForegroundColor = Colors.White;
break;
}
}
}

if (callThemeModeChangedEvent)
AppThemeModeChanged?.Invoke(null, EventArgs.Empty);
if (callThemeModeChangedEvent)
AppThemeModeChanged?.Invoke(null, EventArgs.Empty);
}
catch (Exception ex)
{
App.Logger.LogWarning(ex, "Failed to change theme mode of the app.");
}
}

private async void UISettings_ColorValuesChanged(UISettings sender, object args)
Expand Down
Loading