From 84766971d52fcec2c8463d4c675a132b5f70e985 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=88=E6=80=9C?= Date: Sat, 3 Jun 2023 14:08:13 +0800 Subject: [PATCH 1/3] let the auto update theme can be canceled --- src/Wpf.Ui/Appearance/Watcher.cs | 95 +++++++++++++++++++------------- 1 file changed, 58 insertions(+), 37 deletions(-) diff --git a/src/Wpf.Ui/Appearance/Watcher.cs b/src/Wpf.Ui/Appearance/Watcher.cs index 02a57711e..7c1a09e1e 100644 --- a/src/Wpf.Ui/Appearance/Watcher.cs +++ b/src/Wpf.Ui/Appearance/Watcher.cs @@ -6,6 +6,7 @@ using System; using System.Windows; using System.Windows.Interop; + using Wpf.Ui.Controls.Window; namespace Wpf.Ui.Appearance; @@ -16,22 +17,32 @@ namespace Wpf.Ui.Appearance; /// Automatically updates the application background if the system theme or color is changed. /// settings work globally and cannot be changed for each . /// -public sealed class Watcher +public static class Watcher { /// - /// Gets or sets a value that indicates whether the uses custom . + /// Gets or sets the background effect for the window uses custom . + /// + public static WindowBackdropType BackgroundEffect { get; set; } = WindowBackdropType.None; + + /// + /// Gets or sets a value indicating whether to update the accent colors when the theme changes uses . /// - public WindowBackdropType BackgroundEffect { get; set; } = WindowBackdropType.None; + public static bool UpdateAccents { get; set; } = false; /// - /// Gets or sets a value that indicates whether the uses . + /// Gets or sets a value indicating whether to force the background effect even if it is not supported by the system. /// - public bool UpdateAccents { get; set; } = false; + public static bool ForceBackground { get; set; } = false; /// - /// Gets or sets a value that indicates whether the forces the background effect to be applied. + /// Gets or sets the HwndSource object that represents the window handle. /// - public bool ForceBackground { get; set; } = false; + public static HwndSource hWndSource { get; set; } = null!; + + /// + /// Gets or sets a value indicating whether the window has a hook to receive messages from the system. + /// + public static bool HasHook { get; set; } //public static void Register(Application app, WindowBackdropType backgroundEffect = WindowBackdropType.Mica, // bool updateAccents = true) @@ -40,9 +51,9 @@ public sealed class Watcher //} /// - /// Creates a new instance of and attaches the instance to the given . + /// Watches the and applies the background effect and theme according to the system theme. /// - /// The window that will be updated by . + /// The window that will be updated. /// Background effect to be applied when changing the theme. /// If , the accents will be updated when the change is detected. /// If , bypasses the app's theme compatibility check and tries to force the change of a background effect. @@ -52,6 +63,10 @@ public static void Watch(Window window, WindowBackdropType backgroundEffect = Wi if (window == null) return; + BackgroundEffect = backgroundEffect; + ForceBackground = forceBackground; + UpdateAccents = updateAccents; + if (window.IsLoaded) { // Get the handle from the window @@ -61,11 +76,7 @@ public static void Watch(Window window, WindowBackdropType backgroundEffect = Wi : hwnd; // Initialize a new instance with the window handle - var watcher = new Watcher(hwnd, backgroundEffect, updateAccents, forceBackground); - - // Updates themes on initialization if the current system theme is different from the app's. - var currentSystemTheme = SystemTheme.GetTheme(); - watcher.UpdateThemes(currentSystemTheme); + Watch(hwnd); return; } @@ -79,47 +90,57 @@ public static void Watch(Window window, WindowBackdropType backgroundEffect = Wi : hwnd; // Initialize a new instance with the window handle - var watcher = new Watcher(hwnd, backgroundEffect, updateAccents, forceBackground); - - // Updates themes on initialization if the current system theme is different from the app's. - var currentSystemTheme = SystemTheme.GetTheme(); - watcher.UpdateThemes(currentSystemTheme); + Watch(hwnd); }; } /// - /// Initializes a new instance of . + /// Unwatches the window and removes the hook to receive messages from the system. /// - /// Window handle - /// Background effect to be applied when changing the theme. - /// If , the accents will be updated when the change is detected. - /// If , bypasses the app's theme compatibility check and tries to force the change of a background effect. - public Watcher(IntPtr hWnd, WindowBackdropType backgroundEffect, bool updateAccents, bool forceBackground) + public static void UnWatch() { - var hWndSource = HwndSource.FromHwnd(hWnd); + if (HasHook) + { + hWndSource.RemoveHook(WndProc); + HasHook = false; + } + } - BackgroundEffect = backgroundEffect; - ForceBackground = forceBackground; - UpdateAccents = updateAccents; + /// + /// Watches the window handle and adds a hook to receive messages from the system. + /// + /// + private static void Watch(IntPtr hWnd) + { + if (!HasHook) + { + hWndSource = HwndSource.FromHwnd(hWnd); + hWndSource.AddHook(WndProc); + HasHook = true; + } - hWndSource?.AddHook(WndProc); + // Updates themes on initialization if the current system theme is different from the app's. + UpdateThemes(systemTheme: SystemTheme.GetTheme()); } /// /// Listens to system messages on the application windows. /// - private IntPtr WndProc(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) + private static IntPtr WndProc(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) { - if (msg != (int)Interop.User32.WM.WININICHANGE) - return IntPtr.Zero; - - var currentSystemTheme = SystemTheme.GetTheme(); - UpdateThemes(currentSystemTheme); + if (msg == (int)Interop.User32.WM.WININICHANGE) + { + UpdateThemes(systemTheme: SystemTheme.GetTheme()); + } return IntPtr.Zero; } - private void UpdateThemes(SystemThemeType systemTheme) + /// + /// Updates the themes according to the system theme and applies them to the window. + /// + /// + private static void UpdateThemes(SystemThemeType systemTheme) { AppearanceData.SystemTheme = systemTheme; From 397f82c6c7833cd1eafd9b221d2046f546261aae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=88=E6=80=9C?= Date: Sat, 3 Jun 2023 14:16:08 +0800 Subject: [PATCH 2/3] allow only background effects to be modified --- src/Wpf.Ui/Appearance/Theme.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Wpf.Ui/Appearance/Theme.cs b/src/Wpf.Ui/Appearance/Theme.cs index f35350f35..6665ad23a 100644 --- a/src/Wpf.Ui/Appearance/Theme.cs +++ b/src/Wpf.Ui/Appearance/Theme.cs @@ -5,6 +5,7 @@ using System; using System.Windows; + using Wpf.Ui.Controls.Window; using Wpf.Ui.Interop; @@ -49,7 +50,7 @@ public static void Apply(ThemeType themeType, WindowBackdropType backgroundEffec false ); - if (themeType == ThemeType.Unknown || themeType == AppearanceData.ApplicationTheme) + if (themeType == ThemeType.Unknown) return; var appDictionaries = new ResourceDictionaryManager(AppearanceData.LibraryNamespace); From 5b0c7dc7dd8ee8d7a86ad13122cc14e0a5e4517f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=88=E6=80=9C?= Date: Sat, 3 Jun 2023 14:44:28 +0800 Subject: [PATCH 3/3] change listviewitem trigger style --- src/Wpf.Ui/Styles/Controls/ListViewItem.xaml | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/Wpf.Ui/Styles/Controls/ListViewItem.xaml b/src/Wpf.Ui/Styles/Controls/ListViewItem.xaml index 515e82d33..a2b5af961 100644 --- a/src/Wpf.Ui/Styles/Controls/ListViewItem.xaml +++ b/src/Wpf.Ui/Styles/Controls/ListViewItem.xaml @@ -4,9 +4,7 @@ Copyright (C) Leszek Pomianowski and WPF UI Contributors. All Rights Reserved. --> - - -