Skip to content

Commit a97fbbf

Browse files
authored
Fix: Fixed possible System.NullReferenceException in ThemeHelper (#12175)
1 parent 8c29e26 commit a97fbbf

File tree

2 files changed

+9
-15
lines changed

2 files changed

+9
-15
lines changed

src/Files.App/Helpers/ThemeHelper.cs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ namespace Files.App.Helpers
1717
public static class ThemeHelper
1818
{
1919
private const string selectedAppThemeKey = "theme";
20-
private static Window currentApplicationWindow;
21-
private static AppWindowTitleBar titleBar;
20+
private static Window? currentApplicationWindow;
21+
private static AppWindowTitleBar? titleBar;
2222

2323
// Keep reference so it does not get optimized/garbage collected
2424
public static UISettings UiSettings;
@@ -49,7 +49,8 @@ public static void Initialize()
4949
currentApplicationWindow = App.Window;
5050

5151
// Set TitleBar background color
52-
titleBar = App.GetAppWindow(currentApplicationWindow).TitleBar;
52+
if (currentApplicationWindow is not null)
53+
titleBar = App.GetAppWindow(currentApplicationWindow).TitleBar;
5354

5455
// Apply the desired theme based on what is set in the application settings
5556
ApplyTheme();
@@ -62,24 +63,19 @@ public static void Initialize()
6263
private static async void UiSettings_ColorValuesChanged(UISettings sender, object args)
6364
{
6465
// Make sure we have a reference to our window so we dispatch a UI change
65-
if (currentApplicationWindow is not null)
66-
{
67-
// Dispatch on UI thread so that we have a current appbar to access and change
68-
await currentApplicationWindow.DispatcherQueue.EnqueueAsync(() =>
69-
{
70-
ApplyTheme();
71-
});
72-
}
66+
if (currentApplicationWindow is null)
67+
return;
68+
69+
// Dispatch on UI thread so that we have a current appbar to access and change
70+
await currentApplicationWindow.DispatcherQueue.EnqueueAsync(ApplyTheme);
7371
}
7472

7573
private static void ApplyTheme()
7674
{
7775
var rootTheme = RootTheme;
7876

7977
if (App.Window.Content is FrameworkElement rootElement)
80-
{
8178
rootElement.RequestedTheme = rootTheme;
82-
}
8379

8480
if (titleBar is not null)
8581
{

src/Files.App/ViewModels/MainPageViewModel.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,9 +276,7 @@ public async void OnNavigatedTo(NavigationEventArgs e)
276276
return;
277277

278278
if (drivesViewModel.Drives.Count == 0)
279-
{
280279
await drivesViewModel.UpdateDrivesAsync();
281-
}
282280

283281
//Initialize the static theme helper to capture a reference to this window
284282
//to handle theme changes without restarting the app

0 commit comments

Comments
 (0)