[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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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)
{
var cmdArgs = Environment.GetCommandLineArgs();
var isDark = IsDarkTheme();
if (cmdArgs != null && cmdArgs.Length >= RequiredArgumentsQty)
{
@ -163,7 +162,7 @@ namespace Microsoft.PowerToys.Settings.UI
if (!ShowOobe && !ShowScoobe && !ShowFlyout)
{
settingsWindow = new MainWindow(isDark);
settingsWindow = new MainWindow();
settingsWindow.Activate();
settingsWindow.ExtendsContentIntoTitleBar = true;
settingsWindow.NavigateToSection(StartupPage);
@ -177,12 +176,12 @@ namespace Microsoft.PowerToys.Settings.UI
// Create the Settings window hidden so that it's fully initialized and
// it will be ready to receive the notification if the user opens
// the Settings from the tray icon.
settingsWindow = new MainWindow(isDark, true);
settingsWindow = new MainWindow(true);
if (ShowOobe)
{
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.ExtendsContentIntoTitleBar = true;
SetOobeWindow(oobeWindow);
@ -190,7 +189,7 @@ namespace Microsoft.PowerToys.Settings.UI
else if (ShowScoobe)
{
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.ExtendsContentIntoTitleBar = true;
SetOobeWindow(scoobeWindow);
@ -206,13 +205,19 @@ namespace Microsoft.PowerToys.Settings.UI
ShellPage.OpenFlyoutCallback(p);
}
}
if (SelectedTheme() == ElementTheme.Default)
{
themeListener = new ThemeListener();
themeListener.ThemeChanged += (_) => HandleThemeChange();
}
}
else
{
#if DEBUG
// For debugging purposes
// Window is also needed to show MessageDialog
settingsWindow = new MainWindow(isDark);
settingsWindow = new MainWindow();
settingsWindow.ExtendsContentIntoTitleBar = true;
settingsWindow.Activate();
settingsWindow.NavigateToSection(StartupPage);
@ -281,17 +286,16 @@ namespace Microsoft.PowerToys.Settings.UI
{
var hWnd = WinRT.Interop.WindowNative.GetWindowHandle(settingsWindow);
ThemeHelpers.SetImmersiveDarkMode(hWnd, isDark);
SetContentTheme(isDark, settingsWindow);
}
if (oobeWindow != null)
{
var hWnd = WinRT.Interop.WindowNative.GetWindowHandle(oobeWindow);
ThemeHelpers.SetImmersiveDarkMode(hWnd, isDark);
oobeWindow.SetTheme(isDark);
SetContentTheme(isDark, oobeWindow);
}
SetContentTheme(isDark);
if (SelectedTheme() == ElementTheme.Default)
{
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;
if (rootGrid != null)
switch (themeName?.ToUpperInvariant())
{
if (isDark)
{
rootGrid.RequestedTheme = ElementTheme.Dark;
}
else
{
rootGrid.RequestedTheme = ElementTheme.Light;
}
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)
{
App.Current.RequestedTheme = ApplicationTheme.Dark;
}
else
{
App.Current.RequestedTheme = ApplicationTheme.Light;
}
}

View File

@ -23,7 +23,7 @@ namespace Microsoft.PowerToys.Settings.UI
/// </summary>
public sealed partial class MainWindow : WindowEx
{
public MainWindow(bool isDark, bool createHidden = false)
public MainWindow(bool createHidden = false)
{
var bootTime = new System.Diagnostics.Stopwatch();
bootTime.Start();
@ -37,12 +37,6 @@ namespace Microsoft.PowerToys.Settings.UI
AppWindow appWindow = AppWindow.GetFromWindowId(windowId);
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);
if (createHidden)
{
@ -107,7 +101,7 @@ namespace Microsoft.PowerToys.Settings.UI
{
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();
@ -118,7 +112,7 @@ namespace Microsoft.PowerToys.Settings.UI
{
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
{
@ -164,8 +158,6 @@ namespace Microsoft.PowerToys.Settings.UI
this.InitializeComponent();
SetTheme(isDark);
// receive IPC Message
App.IPCMessageReceivedCallback = (string msg) =>
{
@ -225,10 +217,5 @@ namespace Microsoft.PowerToys.Settings.UI
{
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 AppWindow _appWindow;
public OobeWindow(PowerToysModules initialModule, bool isDark)
public OobeWindow(PowerToysModules initialModule)
{
this.InitializeComponent();
@ -42,14 +42,6 @@ namespace Microsoft.PowerToys.Settings.UI
_appWindow = AppWindow.GetFromWindowId(_windowId);
_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;
presenter.IsMinimizable = false;
presenter.IsMaximizable = false;
@ -140,10 +132,5 @@ namespace Microsoft.PowerToys.Settings.UI
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"),
ShellPage.IsElevated,
ShellPage.IsUserAnAdmin,
UpdateUIThemeMethod,
App.UpdateUIThemeMethod,
ShellPage.SendDefaultIPCMessage,
ShellPage.SendRestartAdminIPCMessage,
ShellPage.SendCheckForUpdatesIPCMessage,
@ -89,31 +89,6 @@ namespace Microsoft.PowerToys.Settings.UI.Views
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)
{
try

View File

@ -65,6 +65,8 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
UpdatingSettings updatingSettingsConfig = UpdatingSettings.LoadSettings();
UpdateAvailable = updatingSettingsConfig != null && (updatingSettingsConfig.State == UpdatingSettings.UpdatingState.ReadyToInstall || updatingSettingsConfig.State == UpdatingSettings.UpdatingState.ReadyToDownload);
App.UpdateUIThemeMethod(generalSettingsConfig.Theme);
}
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.
UpdateUIThemeCallBack = updateTheme;
UpdateUIThemeCallBack(GeneralSettingsConfig.Theme);
// Update Settings file folder:
_settingsConfigFileFolder = configFileSubfolder;