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

[Settings] React on OS theme change fix #29944

Merged
merged 3 commits into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
65 changes: 45 additions & 20 deletions src/settings-ui/Settings.UI/SettingsXAML/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ public static void OpenSettingsWindow(Type type = null, bool ensurePageIsSelecte
protected override void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs args)
{
var cmdArgs = Environment.GetCommandLineArgs();
var isDark = IsDarkTheme();

if (cmdArgs != null && cmdArgs.Length >= RequiredArgumentsQty)
{
Expand Down Expand Up @@ -163,7 +162,7 @@ protected override void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs ar

if (!ShowOobe && !ShowScoobe && !ShowFlyout)
{
settingsWindow = new MainWindow(isDark);
settingsWindow = new MainWindow();
settingsWindow.Activate();
settingsWindow.ExtendsContentIntoTitleBar = true;
settingsWindow.NavigateToSection(StartupPage);
Expand All @@ -177,20 +176,20 @@ protected override void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs ar
// 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);
}
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);
Expand All @@ -206,13 +205,19 @@ protected override void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs ar
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);
Expand Down Expand Up @@ -281,17 +286,16 @@ public static void HandleThemeChange()
{
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();
Expand All @@ -313,19 +317,40 @@ public static void HandleThemeChange()
}
}

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;
}
}

Expand Down
17 changes: 2 additions & 15 deletions src/settings-ui/Settings.UI/SettingsXAML/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,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();
Expand All @@ -36,12 +36,6 @@ public MainWindow(bool isDark, bool createHidden = false)
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)
{
Expand Down Expand Up @@ -106,7 +100,7 @@ public MainWindow(bool isDark, bool createHidden = false)
{
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();
Expand Down Expand Up @@ -148,8 +142,6 @@ public MainWindow(bool isDark, bool createHidden = false)

this.InitializeComponent();

SetTheme(isDark);

// receive IPC Message
App.IPCMessageReceivedCallback = (string msg) =>
{
Expand Down Expand Up @@ -209,10 +201,5 @@ internal void EnsurePageIsSelected()
{
ShellPage.EnsurePageIsSelected();
}

private void SetTheme(bool isDark)
{
shellPage.RequestedTheme = isDark ? ElementTheme.Dark : ElementTheme.Light;
}
}
}
15 changes: 1 addition & 14 deletions src/settings-ui/Settings.UI/SettingsXAML/OobeWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public sealed partial class OobeWindow : WindowEx
private IntPtr _hWnd;
private AppWindow _appWindow;

public OobeWindow(PowerToysModules initialModule, bool isDark)
public OobeWindow(PowerToysModules initialModule)
{
this.InitializeComponent();

Expand All @@ -42,14 +42,6 @@ public OobeWindow(PowerToysModules initialModule, bool isDark)
_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;
Expand Down Expand Up @@ -132,10 +124,5 @@ private void Window_Closed(object sender, WindowEventArgs args)
mainWindow.CloseHiddenWindow();
}
}

public void SetTheme(bool isDark)
{
shellPage.RequestedTheme = isDark ? ElementTheme.Dark : ElementTheme.Light;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public GeneralPage()
loader.GetString("GeneralSettings_RunningAsUserText"),
ShellPage.IsElevated,
ShellPage.IsUserAnAdmin,
UpdateUIThemeMethod,
App.UpdateUIThemeMethod,
ShellPage.SendDefaultIPCMessage,
ShellPage.SendRestartAdminIPCMessage,
ShellPage.SendCheckForUpdatesIPCMessage,
Expand All @@ -89,31 +89,6 @@ public GeneralPage()
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
Expand Down
2 changes: 2 additions & 0 deletions src/settings-ui/Settings.UI/ViewModels/DashboardViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ public DashboardViewModel(ISettingsRepository<GeneralSettings> settingsRepositor

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)
Expand Down
2 changes: 0 additions & 2 deletions src/settings-ui/Settings.UI/ViewModels/GeneralViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,6 @@ public GeneralViewModel(ISettingsRepository<GeneralSettings> settingsRepository,
// set the callback function value to update the UI theme.
UpdateUIThemeCallBack = updateTheme;

UpdateUIThemeCallBack(GeneralSettingsConfig.Theme);

// Update Settings file folder:
_settingsConfigFileFolder = configFileSubfolder;

Expand Down
Loading