[Settings]React on OS theme change fix (#29944)

* [Settings] React on OS theme change fix

* Fix new OobeWindow call after merge
This commit is contained in:
Stefan Markovic
2023-11-23 18:19:57 +01:00
committed by GitHub
parent 5439f9499a
commit faea17b612
6 changed files with 52 additions and 78 deletions

View File

@@ -106,7 +106,6 @@ namespace Microsoft.PowerToys.Settings.UI
protected override void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs args) protected override void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs args)
{ {
var cmdArgs = Environment.GetCommandLineArgs(); var cmdArgs = Environment.GetCommandLineArgs();
var isDark = IsDarkTheme();
if (cmdArgs != null && cmdArgs.Length >= RequiredArgumentsQty) if (cmdArgs != null && cmdArgs.Length >= RequiredArgumentsQty)
{ {
@@ -163,7 +162,7 @@ namespace Microsoft.PowerToys.Settings.UI
if (!ShowOobe && !ShowScoobe && !ShowFlyout) if (!ShowOobe && !ShowScoobe && !ShowFlyout)
{ {
settingsWindow = new MainWindow(isDark); settingsWindow = new MainWindow();
settingsWindow.Activate(); settingsWindow.Activate();
settingsWindow.ExtendsContentIntoTitleBar = true; settingsWindow.ExtendsContentIntoTitleBar = true;
settingsWindow.NavigateToSection(StartupPage); settingsWindow.NavigateToSection(StartupPage);
@@ -177,12 +176,12 @@ namespace Microsoft.PowerToys.Settings.UI
// Create the Settings window hidden so that it's fully initialized and // Create the Settings window hidden so that it's fully initialized and
// it will be ready to receive the notification if the user opens // it will be ready to receive the notification if the user opens
// the Settings from the tray icon. // the Settings from the tray icon.
settingsWindow = new MainWindow(isDark, true); settingsWindow = new MainWindow(true);
if (ShowOobe) if (ShowOobe)
{ {
PowerToysTelemetry.Log.WriteEvent(new OobeStartedEvent()); PowerToysTelemetry.Log.WriteEvent(new OobeStartedEvent());
OobeWindow oobeWindow = new OobeWindow(OOBE.Enums.PowerToysModules.Overview, isDark); OobeWindow oobeWindow = new OobeWindow(OOBE.Enums.PowerToysModules.Overview);
oobeWindow.Activate(); oobeWindow.Activate();
oobeWindow.ExtendsContentIntoTitleBar = true; oobeWindow.ExtendsContentIntoTitleBar = true;
SetOobeWindow(oobeWindow); SetOobeWindow(oobeWindow);
@@ -190,7 +189,7 @@ namespace Microsoft.PowerToys.Settings.UI
else if (ShowScoobe) else if (ShowScoobe)
{ {
PowerToysTelemetry.Log.WriteEvent(new ScoobeStartedEvent()); PowerToysTelemetry.Log.WriteEvent(new ScoobeStartedEvent());
OobeWindow scoobeWindow = new OobeWindow(OOBE.Enums.PowerToysModules.WhatsNew, isDark); OobeWindow scoobeWindow = new OobeWindow(OOBE.Enums.PowerToysModules.WhatsNew);
scoobeWindow.Activate(); scoobeWindow.Activate();
scoobeWindow.ExtendsContentIntoTitleBar = true; scoobeWindow.ExtendsContentIntoTitleBar = true;
SetOobeWindow(scoobeWindow); SetOobeWindow(scoobeWindow);
@@ -206,13 +205,19 @@ namespace Microsoft.PowerToys.Settings.UI
ShellPage.OpenFlyoutCallback(p); ShellPage.OpenFlyoutCallback(p);
} }
} }
if (SelectedTheme() == ElementTheme.Default)
{
themeListener = new ThemeListener();
themeListener.ThemeChanged += (_) => HandleThemeChange();
}
} }
else else
{ {
#if DEBUG #if DEBUG
// For debugging purposes // For debugging purposes
// Window is also needed to show MessageDialog // Window is also needed to show MessageDialog
settingsWindow = new MainWindow(isDark); settingsWindow = new MainWindow();
settingsWindow.ExtendsContentIntoTitleBar = true; settingsWindow.ExtendsContentIntoTitleBar = true;
settingsWindow.Activate(); settingsWindow.Activate();
settingsWindow.NavigateToSection(StartupPage); settingsWindow.NavigateToSection(StartupPage);
@@ -281,17 +286,16 @@ namespace Microsoft.PowerToys.Settings.UI
{ {
var hWnd = WinRT.Interop.WindowNative.GetWindowHandle(settingsWindow); var hWnd = WinRT.Interop.WindowNative.GetWindowHandle(settingsWindow);
ThemeHelpers.SetImmersiveDarkMode(hWnd, isDark); ThemeHelpers.SetImmersiveDarkMode(hWnd, isDark);
SetContentTheme(isDark, settingsWindow);
} }
if (oobeWindow != null) if (oobeWindow != null)
{ {
var hWnd = WinRT.Interop.WindowNative.GetWindowHandle(oobeWindow); var hWnd = WinRT.Interop.WindowNative.GetWindowHandle(oobeWindow);
ThemeHelpers.SetImmersiveDarkMode(hWnd, isDark); ThemeHelpers.SetImmersiveDarkMode(hWnd, isDark);
oobeWindow.SetTheme(isDark);
SetContentTheme(isDark, oobeWindow);
} }
SetContentTheme(isDark);
if (SelectedTheme() == ElementTheme.Default) if (SelectedTheme() == ElementTheme.Default)
{ {
themeListener = new ThemeListener(); themeListener = new ThemeListener();
@@ -313,19 +317,40 @@ namespace Microsoft.PowerToys.Settings.UI
} }
} }
public static void SetContentTheme(bool isDark, WindowEx window) public static int UpdateUIThemeMethod(string themeName)
{ {
var rootGrid = (FrameworkElement)window.Content; switch (themeName?.ToUpperInvariant())
if (rootGrid != null) {
case "LIGHT":
// OobeShellPage.OobeShellHandler.RequestedTheme = ElementTheme.Light;
ShellPage.ShellHandler.RequestedTheme = ElementTheme.Light;
break;
case "DARK":
// OobeShellPage.OobeShellHandler.RequestedTheme = ElementTheme.Dark;
ShellPage.ShellHandler.RequestedTheme = ElementTheme.Dark;
break;
case "SYSTEM":
// OobeShellPage.OobeShellHandler.RequestedTheme = ElementTheme.Default;
ShellPage.ShellHandler.RequestedTheme = ElementTheme.Default;
break;
default:
Logger.LogError($"Unexpected theme name: {themeName}");
break;
}
HandleThemeChange();
return 0;
}
public static void SetContentTheme(bool isDark)
{ {
if (isDark) if (isDark)
{ {
rootGrid.RequestedTheme = ElementTheme.Dark; App.Current.RequestedTheme = ApplicationTheme.Dark;
} }
else else
{ {
rootGrid.RequestedTheme = ElementTheme.Light; App.Current.RequestedTheme = ApplicationTheme.Light;
}
} }
} }

View File

@@ -23,7 +23,7 @@ namespace Microsoft.PowerToys.Settings.UI
/// </summary> /// </summary>
public sealed partial class MainWindow : WindowEx public sealed partial class MainWindow : WindowEx
{ {
public MainWindow(bool isDark, bool createHidden = false) public MainWindow(bool createHidden = false)
{ {
var bootTime = new System.Diagnostics.Stopwatch(); var bootTime = new System.Diagnostics.Stopwatch();
bootTime.Start(); bootTime.Start();
@@ -37,12 +37,6 @@ namespace Microsoft.PowerToys.Settings.UI
AppWindow appWindow = AppWindow.GetFromWindowId(windowId); AppWindow appWindow = AppWindow.GetFromWindowId(windowId);
appWindow.SetIcon("Assets\\Settings\\icon.ico"); appWindow.SetIcon("Assets\\Settings\\icon.ico");
// Passed by parameter, as it needs to be evaluated ASAP, otherwise there is a white flash
if (isDark)
{
ThemeHelpers.SetImmersiveDarkMode(hWnd, isDark);
}
var placement = Utils.DeserializePlacementOrDefault(hWnd); var placement = Utils.DeserializePlacementOrDefault(hWnd);
if (createHidden) if (createHidden)
{ {
@@ -107,7 +101,7 @@ namespace Microsoft.PowerToys.Settings.UI
{ {
if (App.GetOobeWindow() == null) if (App.GetOobeWindow() == null)
{ {
App.SetOobeWindow(new OobeWindow(Microsoft.PowerToys.Settings.UI.OOBE.Enums.PowerToysModules.Overview, App.IsDarkTheme())); App.SetOobeWindow(new OobeWindow(Microsoft.PowerToys.Settings.UI.OOBE.Enums.PowerToysModules.Overview));
} }
App.GetOobeWindow().Activate(); App.GetOobeWindow().Activate();
@@ -118,7 +112,7 @@ namespace Microsoft.PowerToys.Settings.UI
{ {
if (App.GetOobeWindow() == null) if (App.GetOobeWindow() == null)
{ {
App.SetOobeWindow(new OobeWindow(Microsoft.PowerToys.Settings.UI.OOBE.Enums.PowerToysModules.WhatsNew, App.IsDarkTheme())); App.SetOobeWindow(new OobeWindow(Microsoft.PowerToys.Settings.UI.OOBE.Enums.PowerToysModules.WhatsNew));
} }
else else
{ {
@@ -164,8 +158,6 @@ namespace Microsoft.PowerToys.Settings.UI
this.InitializeComponent(); this.InitializeComponent();
SetTheme(isDark);
// receive IPC Message // receive IPC Message
App.IPCMessageReceivedCallback = (string msg) => App.IPCMessageReceivedCallback = (string msg) =>
{ {
@@ -225,10 +217,5 @@ namespace Microsoft.PowerToys.Settings.UI
{ {
ShellPage.EnsurePageIsSelected(); ShellPage.EnsurePageIsSelected();
} }
private void SetTheme(bool isDark)
{
shellPage.RequestedTheme = isDark ? ElementTheme.Dark : ElementTheme.Light;
}
} }
} }

View File

@@ -32,7 +32,7 @@ namespace Microsoft.PowerToys.Settings.UI
private IntPtr _hWnd; private IntPtr _hWnd;
private AppWindow _appWindow; private AppWindow _appWindow;
public OobeWindow(PowerToysModules initialModule, bool isDark) public OobeWindow(PowerToysModules initialModule)
{ {
this.InitializeComponent(); this.InitializeComponent();
@@ -42,14 +42,6 @@ namespace Microsoft.PowerToys.Settings.UI
_appWindow = AppWindow.GetFromWindowId(_windowId); _appWindow = AppWindow.GetFromWindowId(_windowId);
_appWindow.SetIcon("Assets\\Settings\\icon.ico"); _appWindow.SetIcon("Assets\\Settings\\icon.ico");
// Passed by parameter, as it needs to be evaluated ASAP, otherwise there is a white flash
if (isDark)
{
ThemeHelpers.SetImmersiveDarkMode(_hWnd, isDark);
}
SetTheme(isDark);
OverlappedPresenter presenter = _appWindow.Presenter as OverlappedPresenter; OverlappedPresenter presenter = _appWindow.Presenter as OverlappedPresenter;
presenter.IsMinimizable = false; presenter.IsMinimizable = false;
presenter.IsMaximizable = false; presenter.IsMaximizable = false;
@@ -140,10 +132,5 @@ namespace Microsoft.PowerToys.Settings.UI
mainWindow.CloseHiddenWindow(); mainWindow.CloseHiddenWindow();
} }
} }
public void SetTheme(bool isDark)
{
shellPage.RequestedTheme = isDark ? ElementTheme.Dark : ElementTheme.Light;
}
} }
} }

View File

@@ -73,7 +73,7 @@ namespace Microsoft.PowerToys.Settings.UI.Views
loader.GetString("GeneralSettings_RunningAsUserText"), loader.GetString("GeneralSettings_RunningAsUserText"),
ShellPage.IsElevated, ShellPage.IsElevated,
ShellPage.IsUserAnAdmin, ShellPage.IsUserAnAdmin,
UpdateUIThemeMethod, App.UpdateUIThemeMethod,
ShellPage.SendDefaultIPCMessage, ShellPage.SendDefaultIPCMessage,
ShellPage.SendRestartAdminIPCMessage, ShellPage.SendRestartAdminIPCMessage,
ShellPage.SendCheckForUpdatesIPCMessage, ShellPage.SendCheckForUpdatesIPCMessage,
@@ -89,31 +89,6 @@ namespace Microsoft.PowerToys.Settings.UI.Views
doRefreshBackupRestoreStatus(100); doRefreshBackupRestoreStatus(100);
} }
public static int UpdateUIThemeMethod(string themeName)
{
switch (themeName?.ToUpperInvariant())
{
case "LIGHT":
// OobeShellPage.OobeShellHandler.RequestedTheme = ElementTheme.Light;
ShellPage.ShellHandler.RequestedTheme = ElementTheme.Light;
break;
case "DARK":
// OobeShellPage.OobeShellHandler.RequestedTheme = ElementTheme.Dark;
ShellPage.ShellHandler.RequestedTheme = ElementTheme.Dark;
break;
case "SYSTEM":
// OobeShellPage.OobeShellHandler.RequestedTheme = ElementTheme.Default;
ShellPage.ShellHandler.RequestedTheme = ElementTheme.Default;
break;
default:
Logger.LogError($"Unexpected theme name: {themeName}");
break;
}
App.HandleThemeChange();
return 0;
}
private void OpenColorsSettings_Click(object sender, RoutedEventArgs e) private void OpenColorsSettings_Click(object sender, RoutedEventArgs e)
{ {
try try

View File

@@ -65,6 +65,8 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
UpdatingSettings updatingSettingsConfig = UpdatingSettings.LoadSettings(); UpdatingSettings updatingSettingsConfig = UpdatingSettings.LoadSettings();
UpdateAvailable = updatingSettingsConfig != null && (updatingSettingsConfig.State == UpdatingSettings.UpdatingState.ReadyToInstall || updatingSettingsConfig.State == UpdatingSettings.UpdatingState.ReadyToDownload); UpdateAvailable = updatingSettingsConfig != null && (updatingSettingsConfig.State == UpdatingSettings.UpdatingState.ReadyToInstall || updatingSettingsConfig.State == UpdatingSettings.UpdatingState.ReadyToDownload);
App.UpdateUIThemeMethod(generalSettingsConfig.Theme);
} }
private void AddDashboardListItem(ModuleType moduleType) private void AddDashboardListItem(ModuleType moduleType)

View File

@@ -100,8 +100,6 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
// set the callback function value to update the UI theme. // set the callback function value to update the UI theme.
UpdateUIThemeCallBack = updateTheme; UpdateUIThemeCallBack = updateTheme;
UpdateUIThemeCallBack(GeneralSettingsConfig.Theme);
// Update Settings file folder: // Update Settings file folder:
_settingsConfigFileFolder = configFileSubfolder; _settingsConfigFileFolder = configFileSubfolder;