A Swiss Army knife for developers.
C# blazorcross-platformcsharpdesktopdesktop-appdesktop-applicationdeveloper-toolsextensiblefluentfluent-designlinuxmacosmicasyntax-highlightingtypescriptwindowswindows-10windows-11winui
Added theme manager in WASDK and WASM app.
Etienne Baudoux3c25c1d5ebc6
diff --git a/src/app/dev/DevToys.Api/Core/Theme/ApplicationTheme.cs b/src/app/dev/DevToys.Api/Core/Theme/ApplicationTheme.csnew file mode 100644index 000000000..ca2e86464--- /dev/null+++ b/src/app/dev/DevToys.Api/Core/Theme/ApplicationTheme.cs@@ -0,0 +1,17 @@+namespace DevToys.Api.Core.Theme;++/// <summary>+/// Specifies a UI theme that should be used for individual UIElement parts of an app UI.+/// </summary>+public enum ApplicationTheme+{+ /// <summary>+ /// Use the **Light** default theme.+ /// </summary>+ Light,++ /// <summary>+ /// Use the **Dark** default theme.+ /// </summary>+ Dark+}diff --git a/src/app/dev/DevToys.Core/AppTheme.cs b/src/app/dev/DevToys.Api/Core/Theme/AvailableApplicationTheme.cssimilarity index 85%rename from src/app/dev/DevToys.Core/AppTheme.csrename to src/app/dev/DevToys.Api/Core/Theme/AvailableApplicationTheme.csindex 129841786..a8ec7b6ed 100644--- a/src/app/dev/DevToys.Core/AppTheme.cs+++ b/src/app/dev/DevToys.Api/Core/Theme/AvailableApplicationTheme.cs@@ -1,9 +1,9 @@-namespace DevToys.Core;+namespace DevToys.Api.Core.Theme;/// <summary>/// Specifies a UI theme that should be used for individual UIElement parts of an app UI./// </summary>-public enum AppTheme+public enum AvailableApplicationTheme{/// <summary>/// Use the Application.RequestedTheme value for the element. This is the default.diff --git a/src/app/dev/DevToys.Api/Core/Theme/IThemeListener.cs b/src/app/dev/DevToys.Api/Core/Theme/IThemeListener.csnew file mode 100644index 000000000..91d6916bf--- /dev/null+++ b/src/app/dev/DevToys.Api/Core/Theme/IThemeListener.cs@@ -0,0 +1,42 @@+namespace DevToys.Api.Core.Theme;++/// <summary>+/// Provides information about the application theme.+/// </summary>+public interface IThemeListener+{+ /// <summary>+ /// Gets the current operating system theme.+ /// </summary>+ AvailableApplicationTheme CurrentSystemTheme { get; }++ /// <summary>+ /// Gets the current app theme defined in the app settings.+ /// </summary>+ AvailableApplicationTheme CurrentAppTheme { get; }++ /// <summary>+ /// Gets the actual theme applied in the app.+ /// </summary>+ ApplicationTheme ActualAppTheme { get; }++ /// <summary>+ /// Gets or sets a value indicating whether the current theme is high contrast.+ /// </summary>+ bool IsHighContrast { get; }++ /// <summary>+ /// Raised when the theme has changed.+ /// </summary>+ event EventHandler? ThemeChanged;++ /// <summary>+ /// Change the color theme of the app based on <see cref="CurrentSystemTheme"/> and <see cref="CurrentAppTheme"/>.+ /// </summary>+ void ApplyDesiredColorTheme();++ /// <summary>+ /// Evaluates whether the theme should automatically be updated or not, based on app and operating system settings.+ /// </summary>+ void UpdateThemeIfNeeded();+}diff --git a/src/app/dev/DevToys.Api/IsExternalInit.cs b/src/app/dev/DevToys.Api/IsExternalInit.csdeleted file mode 100644index 4351fad11..000000000--- a/src/app/dev/DevToys.Api/IsExternalInit.cs+++ /dev/null@@ -1,3 +0,0 @@-namespace System.Runtime.CompilerServices;--internal static class IsExternalInit { }diff --git a/src/app/dev/DevToys.Core/Settings/PredefinedSettings.cs b/src/app/dev/DevToys.Core/Settings/PredefinedSettings.csindex e31ee1371..397843613 100644--- a/src/app/dev/DevToys.Core/Settings/PredefinedSettings.cs+++ b/src/app/dev/DevToys.Core/Settings/PredefinedSettings.cs@@ -1,4 +1,5 @@using DevToys.Api;+using DevToys.Api.Core.Theme;namespace DevToys.Core.Settings;@@ -16,9 +17,9 @@ public static class PredefinedSettings/// <summary>/// The color theme of the application./// </summary>- public static readonly SettingDefinition<AppTheme> Theme+ public static readonly SettingDefinition<AvailableApplicationTheme> Theme= new(name: nameof(Theme),isRoaming: false,- defaultValue: AppTheme.Default);+ defaultValue: AvailableApplicationTheme.Default);}diff --git a/src/app/dev/DevToys.UI.Framework/Controls/BackdropPage.cs b/src/app/dev/DevToys.UI.Framework/Controls/BackdropPage.csindex 7c7104e04..29267efc6 100644--- a/src/app/dev/DevToys.UI.Framework/Controls/BackdropPage.cs+++ b/src/app/dev/DevToys.UI.Framework/Controls/BackdropPage.cs@@ -1,4 +1,5 @@-using Microsoft.UI.Xaml;+using DevToys.Api.Core.Theme;+using Microsoft.UI.Xaml;using Microsoft.UI.Xaml.Controls;using Windows.Foundation;#if HAS_UNO@@ -10,6 +11,37 @@ namespace DevToys.UI.Framework.Controls;public abstract class BackdropPage : Page{+ private readonly IThemeListener _themeListener;++ protected BackdropPage(BackdropWindow window, IThemeListener themeListener)+ {+ Guard.IsNotNull(themeListener);+ Guard.IsNotNull(window);++ _themeListener = themeListener;+ _themeListener.ThemeChanged += ThemeListener_ThemeChanged;++ Window = window;+#if HAS_UNO+ Window.Window.Content = this;+ Window.Window.Activated += Window_Activated;+ Window.Window.VisibilityChanged += Window_VisibilityChanged;+ Window.Window.Closed += Window_Closed;+#else+ Window.Content = this;+ Window.Activated += Window_Activated;+ Window.VisibilityChanged += Window_VisibilityChanged;+ Window.Closed += Window_Closed;+#endif+ Window.Closing += Window_Closing;+ Window.Shown += Window_Shown;+ }++ private void ThemeListener_ThemeChanged(object? sender, EventArgs e)+ {+ this.RequestedTheme = _themeListener.ActualAppTheme == Api.Core.Theme.ApplicationTheme.Dark ? ElementTheme.Dark : ElementTheme.Light;+ }+internal BackdropWindow Window { get; }/// <summary>@@ -41,25 +73,6 @@ public abstract class BackdropPage : Page/// </summary>public event TypedEventHandler<BackdropWindow, WindowActivatedEventArgs>? Activated;- protected BackdropPage(BackdropWindow window)- {- Guard.IsNotNull(window);- Window = window;-#if HAS_UNO- Window.Window.Content = this;- Window.Window.Activated += Window_Activated;- Window.Window.VisibilityChanged += Window_VisibilityChanged;- Window.Window.Closed += Window_Closed;-#else- Window.Content = this;- Window.Activated += Window_Activated;- Window.VisibilityChanged += Window_VisibilityChanged;- Window.Closed += Window_Closed;-#endif- Window.Closing += Window_Closing;- Window.Shown += Window_Shown;- }-/// <summary>/// Changes the size of the window./// </summary>@@ -107,6 +120,7 @@ public abstract class BackdropPage : Page#endifWindow.Closing -= Window_Closing;Window.Shown -= Window_Shown;+ _themeListener.ThemeChanged -= ThemeListener_ThemeChanged;}#if HAS_UNOdiff --git a/src/app/dev/DevToys.UI.Framework/DevToys.UI.Framework.csproj b/src/app/dev/DevToys.UI.Framework/DevToys.UI.Framework.csprojindex 215fc943a..7a8ca5b4d 100644--- a/src/app/dev/DevToys.UI.Framework/DevToys.UI.Framework.csproj+++ b/src/app/dev/DevToys.UI.Framework/DevToys.UI.Framework.csproj@@ -77,6 +77,10 @@<PackageReference Include="Uno.UI.Adapter.Microsoft.Extensions.Logging" /></ItemGroup>+ <ItemGroup>+ <ProjectReference Include="..\DevToys.Api\DevToys.Api.csproj" />+ </ItemGroup>+<ItemGroup><UpToDateCheckInput Include="**\*.xaml" /><Page Include="**\*.xaml">diff --git a/src/app/dev/DevToys.UI/Views/MainWindow.xaml.cs b/src/app/dev/DevToys.UI/Views/MainWindow.xaml.csindex 3c251ccda..9e81a9a91 100644--- a/src/app/dev/DevToys.UI/Views/MainWindow.xaml.cs+++ b/src/app/dev/DevToys.UI/Views/MainWindow.xaml.cs@@ -1,4 +1,5 @@-using DevToys.UI.Framework.Controls;+using DevToys.Api.Core.Theme;+using DevToys.UI.Framework.Controls;namespace DevToys.UI.Views;@@ -12,8 +13,8 @@ public sealed partial class MainWindow : BackdropPageprivate const string NavigationViewCompactStateName = "NavigationViewCompact";private const string NavigationViewMinimalStateName = "NavigationViewMinimal";- public MainWindow(BackdropWindow backdropWindow)- : base(backdropWindow)+ public MainWindow(BackdropWindow backdropWindow, IThemeListener themeListener)+ : base(backdropWindow, themeListener){this.InitializeComponent();this.WindowStartupLocation = WindowStartupLocation.CenterScreen;diff --git a/src/app/dev/platforms/desktop/macos/DevToys.MacOS/DevToys.MacOS.csproj b/src/app/dev/platforms/desktop/macos/DevToys.MacOS/DevToys.MacOS.csprojindex e0d14d7d8..05a094024 100644--- a/src/app/dev/platforms/desktop/macos/DevToys.MacOS/DevToys.MacOS.csproj+++ b/src/app/dev/platforms/desktop/macos/DevToys.MacOS/DevToys.MacOS.csproj@@ -27,7 +27,7 @@<DefineConstants>TRACE;__MACCATALYST__</DefineConstants></PropertyGroup><ItemGroup>- <Compile Include="..\..\..\desktop\windows\DevToys.Wasdk\Core\Settings\SettingsStorage.cs" Link="Core\Settings\SettingsStorage.cs" />+ <Compile Include="..\..\..\desktop\windows\DevToys.Wasdk\Core\**\*" Link="Core\%(RecursiveDir)/%(Filename)%(Extension)" /></ItemGroup><ItemGroup><PackageReference Include="Uno.WinUI"/>diff --git a/src/app/dev/platforms/desktop/windows/DevToys.Wasdk/Core/Theme/ThemeListener.cs b/src/app/dev/platforms/desktop/windows/DevToys.Wasdk/Core/Theme/ThemeListener.csnew file mode 100644index 000000000..e4008b8e8--- /dev/null+++ b/src/app/dev/platforms/desktop/windows/DevToys.Wasdk/Core/Theme/ThemeListener.cs@@ -0,0 +1,123 @@+using DevToys.Api;+using DevToys.Api.Core.Theme;+using DevToys.Core.Settings;+using Microsoft.UI.Xaml;+using Windows.UI.ViewManagement;++namespace DevToys.Wasdk.Core.Theme;++[Export(typeof(IThemeListener))]+internal sealed class ThemeListener : IThemeListener+{+ private readonly AccessibilitySettings _accessible = new();+ private readonly ISettingsProvider _settingsProvider;++ [ImportingConstructor]+ public ThemeListener(ISettingsProvider settingsProvider)+ {+ _settingsProvider = settingsProvider;++ _settingsProvider.SettingChanged += SettingsProvider_SettingChanged;++ if (Application.Current.RequestedTheme == Microsoft.UI.Xaml.ApplicationTheme.Dark)+ {+ CurrentSystemTheme = AvailableApplicationTheme.Dark;+ }+ else+ {+ CurrentSystemTheme = AvailableApplicationTheme.Light;+ }++ IsHighContrast = _accessible.HighContrast;+ }++ public AvailableApplicationTheme CurrentSystemTheme { get; private set; }++ public AvailableApplicationTheme CurrentAppTheme => _settingsProvider.GetSetting(PredefinedSettings.Theme);++ public Api.Core.Theme.ApplicationTheme ActualAppTheme { get; private set; }++ public bool IsHighContrast { get; private set; }++ public event EventHandler? ThemeChanged;++ public void UpdateThemeIfNeeded()+ {+ AvailableApplicationTheme currentAppTheme;+ if (Application.Current.RequestedTheme == Microsoft.UI.Xaml.ApplicationTheme.Dark)+ {+ currentAppTheme = AvailableApplicationTheme.Dark;+ }+ else+ {+ currentAppTheme = AvailableApplicationTheme.Light;+ }++ if (CurrentSystemTheme != currentAppTheme || IsHighContrast != _accessible.HighContrast)+ {+ UpdateProperties();+ }+ }++ public void ApplyDesiredColorTheme()+ {+ AvailableApplicationTheme theme = CurrentAppTheme;++ if (theme == AvailableApplicationTheme.Default)+ {+ theme = CurrentSystemTheme;+ }++ // Set theme for window root.+ if (theme == AvailableApplicationTheme.Dark)+ {+ ActualAppTheme = Api.Core.Theme.ApplicationTheme.Dark;+ }+ else+ {+ ActualAppTheme = Api.Core.Theme.ApplicationTheme.Light;+ }+ }++ private void SettingsProvider_SettingChanged(object? sender, SettingChangedEventArgs e)+ {+ if (string.Equals(PredefinedSettings.Theme.Name, e.SettingName, StringComparison.Ordinal))+ {+ ApplyDesiredColorTheme();+ ThemeChanged?.Invoke(this, EventArgs.Empty);+ }+ }++ /// <summary>+ /// Set our current properties and fire a change notification.+ /// </summary>+ private void UpdateProperties()+ {+ // TODO: Not sure if HighContrastScheme names are localized?+ if (_accessible.HighContrast && _accessible.HighContrastScheme.IndexOf("white", StringComparison.OrdinalIgnoreCase) != -1)+ {+ IsHighContrast = false;+ CurrentSystemTheme = AvailableApplicationTheme.Light;+ }+ else+ {+ // Otherwise, we just set to what's in the system as we'd expect.+ IsHighContrast = _accessible.HighContrast;++ AvailableApplicationTheme currentAppTheme;+ if (Application.Current.RequestedTheme == Microsoft.UI.Xaml.ApplicationTheme.Dark)+ {+ currentAppTheme = AvailableApplicationTheme.Dark;+ }+ else+ {+ currentAppTheme = AvailableApplicationTheme.Light;+ }++ CurrentSystemTheme = currentAppTheme;+ }++ ApplyDesiredColorTheme();+ ThemeChanged?.Invoke(this, EventArgs.Empty);+ }+}diff --git a/src/app/dev/platforms/web/DevToys.Wasm/DevToys.Wasm.csproj b/src/app/dev/platforms/web/DevToys.Wasm/DevToys.Wasm.csprojindex 21cc04014..810538429 100644--- a/src/app/dev/platforms/web/DevToys.Wasm/DevToys.Wasm.csproj+++ b/src/app/dev/platforms/web/DevToys.Wasm/DevToys.Wasm.csproj@@ -26,7 +26,7 @@<UpToDateCheckInput Include="..\..\..\DevToys.UI\**\*.xaml" /></ItemGroup><ItemGroup>- <Compile Include="..\..\desktop\windows\DevToys.Wasdk\Core\Settings\SettingsStorage.cs" Link="Core\Settings\SettingsStorage.cs" />+ <Compile Include="..\..\desktop\windows\DevToys.Wasdk\Core\**\*" Link="Core\%(RecursiveDir)/%(Filename)%(Extension)" /></ItemGroup><ItemGroup><EmbeddedResource Include="WasmCSS\Fonts.css" />diff --git a/src/app/dev/shared/App.xaml.cs b/src/app/dev/shared/App.xaml.csindex 48b33e52a..05c44b2ac 100644--- a/src/app/dev/shared/App.xaml.cs+++ b/src/app/dev/shared/App.xaml.cs@@ -1,4 +1,5 @@using DevToys.Api;+using DevToys.Api.Core.Theme;using DevToys.Api.Core.Threading;using DevToys.Core.Mef;using DevToys.Core.Settings;@@ -26,8 +27,7 @@ public sealed partial class App : Applicationprivate readonly Task<IMefProvider> _mefProvider;private readonly AsyncLazy<ISettingsProvider> _settingsProvider;-- private MainWindow? _mainWindow;+ private readonly AsyncLazy<IThemeListener> _themeListener;/// <summary>/// Initializes the singleton application object. This is the first line of authored code@@ -45,14 +45,24 @@ public sealed partial class App : Applicationthis.Suspending += OnSuspending;#endif- _mefComposer = Task.Run(() => new MefComposer());- _mefProvider = Task.Run(async () =>- {- return (await _mefComposer).Provider;- });+ _mefComposer+ = Task.Run(+ () => new MefComposer(+ new[]+ {+ typeof(MainWindow).Assembly+ }));++ _mefProvider+ = Task.Run(+ async () => (await _mefComposer).Provider);+_settingsProvider = new AsyncLazy<ISettingsProvider>(async () => (await _mefProvider).Import<ISettingsProvider>());+ _themeListener = new AsyncLazy<IThemeListener>(async () => (await _mefProvider).Import<IThemeListener>());}+ internal MainWindow? MainWindow { get; private set; }+/// <summary>/// Invoked when the application is launched normally by the end user. Other entry points/// will be used such as when the application is launched to open a specific file.@@ -74,22 +84,30 @@ public sealed partial class App : Application?? LanguageManager.Instance.AvailableLanguages[0];LanguageManager.Instance.SetCurrentCulture(languageDefinition);+ IThemeListener themeListener = await _themeListener.GetValueAsync();+#if __WINDOWS__// On Windows 10 version 1607 or later, this code signals that this app wants to participate in prelaunchCoreApplication.EnablePrelaunch(true);#endif#if __WINDOWS__- _mainWindow = new MainWindow(new BackdropWindow());- _mainWindow.Show();+ MainWindow = new MainWindow(new BackdropWindow(), themeListener);#elif __MACCATALYST__// Important! Keep the full name `Microsoft.UI.Xaml.Window.Current` otherwise the Mac app won't build.// See https://blog.mzikmund.com/2020/04/resolving-uno-platform-uiwindow-does-not-contain-a-definition-for-current-issue/- _mainWindow = new MainWindow(new BackdropWindow(Microsoft.UI.Xaml.Window.Current));+ _mainWindow = new MainWindow(new BackdropWindow(Microsoft.UI.Xaml.Window.Current), themeListener);#else- _mainWindow = new MainWindow(new BackdropWindow(Window.Current));+ MainWindow = new MainWindow(new BackdropWindow(Window.Current), themeListener);#endif+ // Apply the app color theme.+ themeListener.ApplyDesiredColorTheme();++ MainWindow.Activated += MainWindow_Activated;++ MainWindow.Show();+Windows.ApplicationModel.Activation.LaunchActivatedEventArgs uwpArgs = args.UWPLaunchActivatedEventArgs;#if !(NET6_0_OR_GREATER && WINDOWS)@@ -97,10 +115,15 @@ public sealed partial class App : Application#endif{// Ensure the current window is active- _mainWindow.Activate();+ MainWindow.Activate();}}+ private async void MainWindow_Activated(BackdropWindow sender, WindowActivatedEventArgs args)+ {+ (await _themeListener.GetValueAsync()).UpdateThemeIfNeeded();+ }+/// <summary>/// Invoked when application execution is being suspended. Application state is saved/// without knowing whether the application will be terminated or resumed with the contents