[Settings]Theme override fix and cleanup (#32362)

* theme override fix and cleanup

* test fix
This commit is contained in:
Davide Giacometti
2024-04-17 16:39:19 +02:00
committed by GitHub
parent fba5f6f002
commit b14aa8276d
9 changed files with 89 additions and 129 deletions

View File

@@ -5,20 +5,16 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net.Http.Json;
using System.Runtime.InteropServices;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Common.UI;
using interop;
using ManagedCommon;
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.Services;
using Microsoft.PowerToys.Settings.UI.Views;
using Microsoft.PowerToys.Telemetry;
using Microsoft.UI.Xaml;
@@ -73,8 +69,6 @@ namespace Microsoft.PowerToys.Settings.UI
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
@@ -259,19 +253,6 @@ namespace Microsoft.PowerToys.Settings.UI
ShellPage.OpenFlyoutCallback(p);
}
}
if (SelectedTheme() == ElementTheme.Default)
{
try
{
themeListener = new ThemeListener();
themeListener.ThemeChanged += (_) => HandleThemeChange();
}
catch (Exception ex)
{
Logger.LogError($"HandleThemeChange exception. Please install .NET 4.", ex);
}
}
}
/// <summary>
@@ -311,7 +292,7 @@ namespace Microsoft.PowerToys.Settings.UI
ShowMessageDialog("The application is running in Debug mode.", "DEBUG");
#else
/* If we try to run Settings as a standalone app, it will start PowerToys.exe if not running and open Settings again through it in the Dashboard page. */
SettingsDeepLink.OpenSettings(SettingsDeepLink.SettingsWindow.Dashboard, true);
Common.UI.SettingsDeepLink.OpenSettings(Common.UI.SettingsDeepLink.SettingsWindow.Dashboard, true);
Exit();
#endif
}
@@ -347,92 +328,24 @@ namespace Microsoft.PowerToys.Settings.UI
return ipcmanager;
}
public static ElementTheme SelectedTheme()
{
switch (SettingsRepository<GeneralSettings>.GetInstance(settingsUtils).SettingsConfig.Theme.ToUpper(CultureInfo.InvariantCulture))
{
case "DARK": return ElementTheme.Dark;
case "LIGHT": return ElementTheme.Light;
default: return ElementTheme.Default;
}
}
public static bool IsDarkTheme()
{
var selectedTheme = SelectedTheme();
return selectedTheme == ElementTheme.Dark || (selectedTheme == ElementTheme.Default && ThemeHelpers.GetAppTheme() == AppTheme.Dark);
}
public static void HandleThemeChange()
{
try
{
bool 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 (SelectedTheme() == ElementTheme.Default)
{
themeListener = new ThemeListener();
themeListener.ThemeChanged += (_) => HandleThemeChange();
}
else if (themeListener != null)
{
themeListener.Dispose();
themeListener = null;
}
}
catch (Exception e)
{
if (!loggedImmersiveDarkException)
{
Logger.LogError($"HandleThemeChange exception. Please install .NET 4.", e);
loggedImmersiveDarkException = true;
}
}
return ThemeService.Theme == ElementTheme.Dark || (ThemeService.Theme == ElementTheme.Default && ThemeHelpers.GetAppTheme() == AppTheme.Dark);
}
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;
}
HandleThemeChange();
return 0;
}
private static ISettingsUtils settingsUtils = new SettingsUtils();
private static ThemeService themeService = new ThemeService(SettingsRepository<GeneralSettings>.GetInstance(settingsUtils));
public static ThemeService ThemeService => themeService;
private static MainWindow settingsWindow;
private static OobeWindow oobeWindow;
private static FlyoutWindow flyoutWindow;
private static ThemeListener themeListener;
public static void ClearSettingsWindow()
{