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 possible System.NullReferenceException in ThemeHelper #12175

Merged
merged 3 commits into from
Apr 23, 2023
Merged
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
22 changes: 9 additions & 13 deletions src/Files.App/Helpers/ThemeHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ namespace Files.App.Helpers
public static class ThemeHelper
{
private const string selectedAppThemeKey = "theme";
private static Window currentApplicationWindow;
private static AppWindowTitleBar titleBar;
private static Window? currentApplicationWindow;
private static AppWindowTitleBar? titleBar;

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

// Set TitleBar background color
titleBar = App.GetAppWindow(currentApplicationWindow).TitleBar;
if (currentApplicationWindow is not null)
titleBar = App.GetAppWindow(currentApplicationWindow).TitleBar;

// Apply the desired theme based on what is set in the application settings
ApplyTheme();
Expand All @@ -62,24 +63,19 @@ public static void Initialize()
private static async void UiSettings_ColorValuesChanged(UISettings sender, object args)
{
// Make sure we have a reference to our window so we dispatch a UI change
if (currentApplicationWindow is not null)
{
// Dispatch on UI thread so that we have a current appbar to access and change
await currentApplicationWindow.DispatcherQueue.EnqueueAsync(() =>
{
ApplyTheme();
});
}
if (currentApplicationWindow is null)
return;
Comment on lines +66 to +67
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If currentApplicationWindow is null here, shouldn't we try initialize again?

Copy link
Member Author

@yaira2 yaira2 Apr 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it get stuck in a loop?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should try one time every the event raised.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm curious what you have in mind, would you add a Boolean to keep track?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have like this in mind.

		private static async void UiSettings_ColorValuesChanged(UISettings sender, object args)
		{
			// Make sure we have a reference to our window so we dispatch a UI change
			if (currentApplicationWindow is null)
			{
				currentApplicationWindow = App.Window;

				if (currentApplicationWindow is not null)
					titleBar = App.GetAppWindow(currentApplicationWindow).TitleBar;
				else
					return;
			}

			// Dispatch on UI thread so that we have a current appbar to access and change
			await currentApplicationWindow.DispatcherQueue.EnqueueAsync(ApplyTheme);
		}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hishitetsu can you open a PR?


// Dispatch on UI thread so that we have a current appbar to access and change
await currentApplicationWindow.DispatcherQueue.EnqueueAsync(ApplyTheme);
}

private static void ApplyTheme()
{
var rootTheme = RootTheme;

if (App.Window.Content is FrameworkElement rootElement)
{
rootElement.RequestedTheme = rootTheme;
}

if (titleBar is not null)
{
Expand Down
2 changes: 0 additions & 2 deletions src/Files.App/ViewModels/MainPageViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -276,9 +276,7 @@ public async void OnNavigatedTo(NavigationEventArgs e)
return;

if (drivesViewModel.Drives.Count == 0)
{
await drivesViewModel.UpdateDrivesAsync();
}

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