Skip to content

Commit

Permalink
[arm64][Settings]Handle the case when .NET load crash on System.Manag…
Browse files Browse the repository at this point in the history
…ement (#19176)

Prevent crashing with "System.Management requires native modules from the .NET Framework to operate." on creating ThemeListener.
  • Loading branch information
yuyoyuppe committed Jul 5, 2022
1 parent 32fc88a commit d4b62d8
Showing 1 changed file with 32 additions and 18 deletions.
50 changes: 32 additions & 18 deletions src/settings-ui/Settings.UI/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Microsoft.PowerToys.Settings.UI.Helpers;
using Microsoft.PowerToys.Settings.UI.Library;
using Microsoft.PowerToys.Settings.UI.Library.Telemetry.Events;
using Microsoft.PowerToys.Settings.UI.Library.Utilities;
using Microsoft.PowerToys.Telemetry;
using Microsoft.UI.Xaml;
using Windows.UI.Popups;
Expand Down Expand Up @@ -58,6 +59,8 @@ private enum Arguments

public static Action<string> IPCMessageReceivedCallback { get; set; }

private static bool loggedImmersiveDarkException;

/// <summary>
/// Initializes a new instance of the <see cref="App"/> class.
/// Initializes the singleton application object. This is the first line of authored code
Expand Down Expand Up @@ -216,29 +219,40 @@ public static bool IsDarkTheme()

public static void HandleThemeChange()
{
var isDark = IsDarkTheme();
if (settingsWindow != null)
try
{
var hWnd = WinRT.Interop.WindowNative.GetWindowHandle(settingsWindow);
ThemeHelpers.SetImmersiveDarkMode(hWnd, isDark);
}
var isDark = IsDarkTheme();
if (settingsWindow != null)
{
var hWnd = WinRT.Interop.WindowNative.GetWindowHandle(settingsWindow);
ThemeHelpers.SetImmersiveDarkMode(hWnd, isDark);
}

if (oobeWindow != null)
{
var hWnd = WinRT.Interop.WindowNative.GetWindowHandle(oobeWindow);
ThemeHelpers.SetImmersiveDarkMode(hWnd, isDark);
}
if (oobeWindow != null)
{
var hWnd = WinRT.Interop.WindowNative.GetWindowHandle(oobeWindow);
ThemeHelpers.SetImmersiveDarkMode(hWnd, isDark);
}

var selectedTheme = SelectedTheme();
if (selectedTheme == "SYSTEM")
{
themeListener = new ThemeListener();
themeListener.ThemeChanged += (_) => HandleThemeChange();
var selectedTheme = SelectedTheme();
if (selectedTheme == "SYSTEM")
{
themeListener = new ThemeListener();
themeListener.ThemeChanged += (_) => HandleThemeChange();
}
else if (themeListener != null)
{
themeListener.Dispose();
themeListener = null;
}
}
else if (themeListener != null)
catch (Exception e)
{
themeListener.Dispose();
themeListener = null;
if (!loggedImmersiveDarkException)
{
Logger.LogError($"HandleThemeChange exception. Please install .NET 4.", e);
loggedImmersiveDarkException = true;
}
}
}

Expand Down

0 comments on commit d4b62d8

Please sign in to comment.