From 9abf136ea22427bc271326fcbaf533a3601607cf Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Wed, 4 Mar 2026 12:48:48 +0800 Subject: [PATCH 1/4] Improve SolidColorBrush usage and property checks in Theme Refactor background brush creation to assign, freeze, and reuse SolidColorBrush instances for better WPF performance. Replace string-based property checks with type-safe Control.BackgroundProperty comparisons for improved robustness. --- Flow.Launcher.Core/Resource/Theme.cs | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/Flow.Launcher.Core/Resource/Theme.cs b/Flow.Launcher.Core/Resource/Theme.cs index c3bb6190f01..62713b44132 100644 --- a/Flow.Launcher.Core/Resource/Theme.cs +++ b/Flow.Launcher.Core/Resource/Theme.cs @@ -673,13 +673,17 @@ private void SetBlurForWindow(string theme, BackdropTypes backdropType) // If the BackdropType is Mica or MicaAlt, set the windowborderstyle's background to transparent if (backdropType == BackdropTypes.Mica || backdropType == BackdropTypes.MicaAlt) { - windowBorderStyle.Setters.Remove(windowBorderStyle.Setters.OfType().FirstOrDefault(x => x.Property.Name == "Background")); - windowBorderStyle.Setters.Add(new Setter(Border.BackgroundProperty, new SolidColorBrush(Color.FromArgb(1, 0, 0, 0)))); + windowBorderStyle.Setters.Remove(windowBorderStyle.Setters.OfType().FirstOrDefault(x => x.Property == Control.BackgroundProperty)); + var brush = new SolidColorBrush(Color.FromArgb(1, 0, 0, 0)); + brush.Freeze(); + windowBorderStyle.Setters.Add(new Setter(Border.BackgroundProperty, brush)); } else if (backdropType == BackdropTypes.Acrylic) { - windowBorderStyle.Setters.Remove(windowBorderStyle.Setters.OfType().FirstOrDefault(x => x.Property.Name == "Background")); - windowBorderStyle.Setters.Add(new Setter(Border.BackgroundProperty, new SolidColorBrush(Colors.Transparent))); + windowBorderStyle.Setters.Remove(windowBorderStyle.Setters.OfType().FirstOrDefault(x => x.Property == Control.BackgroundProperty)); + var brush = new SolidColorBrush(Colors.Transparent); + brush.Freeze(); + windowBorderStyle.Setters.Add(new Setter(Border.BackgroundProperty, brush)); } // For themes with blur enabled, the window border is rendered by the system, so it's treated as a simple rectangle regardless of thickness. @@ -803,7 +807,9 @@ private void ApplyPreviewBackground(Color? bgColor = null) // Apply background color (remove transparency in color) Color backgroundColor = Color.FromRgb(bgColor.Value.R, bgColor.Value.G, bgColor.Value.B); - previewStyle.Setters.Add(new Setter(Border.BackgroundProperty, new SolidColorBrush(backgroundColor))); + var brush = new SolidColorBrush(backgroundColor); + brush.Freeze(); + previewStyle.Setters.Add(new Setter(Border.BackgroundProperty, brush)); // The blur theme keeps the corner round fixed (applying DWM code to modify it causes rendering issues). // The non-blur theme retains the previously set WindowBorderStyle. @@ -917,11 +923,15 @@ private void ColorizeWindow(string theme, BackdropTypes backdropType) // Only set the background to transparent if the theme supports blur if (backdropType == BackdropTypes.Mica || backdropType == BackdropTypes.MicaAlt) { - mainWindow.Background = new SolidColorBrush(Color.FromArgb(1, 0, 0, 0)); + var backgroundBrush = new SolidColorBrush(Color.FromArgb(1, 0, 0, 0)); + backgroundBrush.Freeze(); + mainWindow.Background = backgroundBrush; } else { - mainWindow.Background = new SolidColorBrush(selectedBG); + var backgroundBrush = new SolidColorBrush(selectedBG); + backgroundBrush.Freeze(); + mainWindow.Background = backgroundBrush; } } } From ed14c45fd241b37ba0e36ad91be5d2c516b8a833 Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Sun, 8 Mar 2026 23:46:08 +0800 Subject: [PATCH 2/4] Refactor brush creation with ThemeHelper utility Introduce ThemeHelper.GetFreezeSolidColorBrush to centralize and reuse SolidColorBrush creation and freezing logic. Refactor Theme.cs to use this helper, improving code clarity and maintainability. Also, use Brushes.Transparent directly where appropriate. --- Flow.Launcher.Core/Resource/Theme.cs | 22 ++++++---------------- Flow.Launcher.Core/Resource/ThemeHelper.cs | 13 +++++++++++++ 2 files changed, 19 insertions(+), 16 deletions(-) create mode 100644 Flow.Launcher.Core/Resource/ThemeHelper.cs diff --git a/Flow.Launcher.Core/Resource/Theme.cs b/Flow.Launcher.Core/Resource/Theme.cs index 62713b44132..5e0e2894ad6 100644 --- a/Flow.Launcher.Core/Resource/Theme.cs +++ b/Flow.Launcher.Core/Resource/Theme.cs @@ -674,16 +674,12 @@ private void SetBlurForWindow(string theme, BackdropTypes backdropType) if (backdropType == BackdropTypes.Mica || backdropType == BackdropTypes.MicaAlt) { windowBorderStyle.Setters.Remove(windowBorderStyle.Setters.OfType().FirstOrDefault(x => x.Property == Control.BackgroundProperty)); - var brush = new SolidColorBrush(Color.FromArgb(1, 0, 0, 0)); - brush.Freeze(); - windowBorderStyle.Setters.Add(new Setter(Border.BackgroundProperty, brush)); + windowBorderStyle.Setters.Add(new Setter(Border.BackgroundProperty, ThemeHelper.GetFreezeSolidColorBrush(Color.FromArgb(1, 0, 0, 0)))); } else if (backdropType == BackdropTypes.Acrylic) { windowBorderStyle.Setters.Remove(windowBorderStyle.Setters.OfType().FirstOrDefault(x => x.Property == Control.BackgroundProperty)); - var brush = new SolidColorBrush(Colors.Transparent); - brush.Freeze(); - windowBorderStyle.Setters.Add(new Setter(Border.BackgroundProperty, brush)); + windowBorderStyle.Setters.Add(new Setter(Border.BackgroundProperty, Brushes.Transparent)); } // For themes with blur enabled, the window border is rendered by the system, so it's treated as a simple rectangle regardless of thickness. @@ -806,10 +802,8 @@ private void ApplyPreviewBackground(Color? bgColor = null) } // Apply background color (remove transparency in color) - Color backgroundColor = Color.FromRgb(bgColor.Value.R, bgColor.Value.G, bgColor.Value.B); - var brush = new SolidColorBrush(backgroundColor); - brush.Freeze(); - previewStyle.Setters.Add(new Setter(Border.BackgroundProperty, brush)); + var backgroundColor = Color.FromRgb(bgColor.Value.R, bgColor.Value.G, bgColor.Value.B); + previewStyle.Setters.Add(new Setter(Border.BackgroundProperty, ThemeHelper.GetFreezeSolidColorBrush(backgroundColor))); // The blur theme keeps the corner round fixed (applying DWM code to modify it causes rendering issues). // The non-blur theme retains the previously set WindowBorderStyle. @@ -923,15 +917,11 @@ private void ColorizeWindow(string theme, BackdropTypes backdropType) // Only set the background to transparent if the theme supports blur if (backdropType == BackdropTypes.Mica || backdropType == BackdropTypes.MicaAlt) { - var backgroundBrush = new SolidColorBrush(Color.FromArgb(1, 0, 0, 0)); - backgroundBrush.Freeze(); - mainWindow.Background = backgroundBrush; + mainWindow.Background = ThemeHelper.GetFreezeSolidColorBrush(Color.FromArgb(1, 0, 0, 0)); } else { - var backgroundBrush = new SolidColorBrush(selectedBG); - backgroundBrush.Freeze(); - mainWindow.Background = backgroundBrush; + mainWindow.Background = ThemeHelper.GetFreezeSolidColorBrush(selectedBG); } } } diff --git a/Flow.Launcher.Core/Resource/ThemeHelper.cs b/Flow.Launcher.Core/Resource/ThemeHelper.cs new file mode 100644 index 00000000000..dd998e19ad3 --- /dev/null +++ b/Flow.Launcher.Core/Resource/ThemeHelper.cs @@ -0,0 +1,13 @@ +using System.Windows.Media; + +namespace Flow.Launcher.Core.Resource; + +public static class ThemeHelper +{ + public static SolidColorBrush GetFreezeSolidColorBrush(Color color) + { + var brush = new SolidColorBrush(color); + brush.Freeze(); + return brush; + } +} From 36e15d3809664e54539bb4bc0fe0ba96922cb9f9 Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Sun, 8 Mar 2026 23:48:10 +0800 Subject: [PATCH 3/4] Refactor: move CopyStyle to ThemeHelper class Moved the CopyStyle method from Theme to a new static ThemeHelper class for better code organization and reusability. Updated all references in Theme to use ThemeHelper.CopyStyle. Added ThemeHelper.cs and necessary using directives. --- Flow.Launcher.Core/Resource/Theme.cs | 17 +---------------- Flow.Launcher.Core/Resource/ThemeHelper.cs | 19 ++++++++++++++++++- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/Flow.Launcher.Core/Resource/Theme.cs b/Flow.Launcher.Core/Resource/Theme.cs index 5e0e2894ad6..8b678e0b964 100644 --- a/Flow.Launcher.Core/Resource/Theme.cs +++ b/Flow.Launcher.Core/Resource/Theme.cs @@ -798,7 +798,7 @@ private void ApplyPreviewBackground(Color? bgColor = null) Application.Current.Resources["WindowBorderStyle"] is Style originalStyle) { // Copy the original style, including the base style if it exists - CopyStyle(originalStyle, previewStyle); + ThemeHelper.CopyStyle(originalStyle, previewStyle); } // Apply background color (remove transparency in color) @@ -817,21 +817,6 @@ private void ApplyPreviewBackground(Color? bgColor = null) Application.Current.Resources["PreviewWindowBorderStyle"] = previewStyle; } - private void CopyStyle(Style originalStyle, Style targetStyle) - { - // If the style is based on another style, copy the base style first - if (originalStyle.BasedOn != null) - { - CopyStyle(originalStyle.BasedOn, targetStyle); - } - - // Copy the setters from the original style - foreach (var setter in originalStyle.Setters.OfType()) - { - targetStyle.Setters.Add(new Setter(setter.Property, setter.Value)); - } - } - private void ColorizeWindow(string theme, BackdropTypes backdropType) { var dict = GetThemeResourceDictionary(theme); diff --git a/Flow.Launcher.Core/Resource/ThemeHelper.cs b/Flow.Launcher.Core/Resource/ThemeHelper.cs index dd998e19ad3..ad7f1c9571c 100644 --- a/Flow.Launcher.Core/Resource/ThemeHelper.cs +++ b/Flow.Launcher.Core/Resource/ThemeHelper.cs @@ -1,9 +1,26 @@ -using System.Windows.Media; +using System.Linq; +using System.Windows; +using System.Windows.Media; namespace Flow.Launcher.Core.Resource; public static class ThemeHelper { + public static void CopyStyle(Style originalStyle, Style targetStyle) + { + // If the style is based on another style, copy the base style first + if (originalStyle.BasedOn != null) + { + CopyStyle(originalStyle.BasedOn, targetStyle); + } + + // Copy the setters from the original style + foreach (var setter in originalStyle.Setters.OfType()) + { + targetStyle.Setters.Add(new Setter(setter.Property, setter.Value)); + } + } + public static SolidColorBrush GetFreezeSolidColorBrush(Color color) { var brush = new SolidColorBrush(color); From 7766d7053da399ec1dabeb752566dd6a86066592 Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Sun, 8 Mar 2026 23:49:09 +0800 Subject: [PATCH 4/4] Rename GetFreezeSolidColorBrush to GetFrozenSolidColorBrush Renamed the method GetFreezeSolidColorBrush to GetFrozenSolidColorBrush in ThemeHelper.cs for improved naming consistency. Updated all references in Theme.cs accordingly. No changes to functionality. --- Flow.Launcher.Core/Resource/Theme.cs | 8 ++++---- Flow.Launcher.Core/Resource/ThemeHelper.cs | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Flow.Launcher.Core/Resource/Theme.cs b/Flow.Launcher.Core/Resource/Theme.cs index 8b678e0b964..4d15857e6fa 100644 --- a/Flow.Launcher.Core/Resource/Theme.cs +++ b/Flow.Launcher.Core/Resource/Theme.cs @@ -674,7 +674,7 @@ private void SetBlurForWindow(string theme, BackdropTypes backdropType) if (backdropType == BackdropTypes.Mica || backdropType == BackdropTypes.MicaAlt) { windowBorderStyle.Setters.Remove(windowBorderStyle.Setters.OfType().FirstOrDefault(x => x.Property == Control.BackgroundProperty)); - windowBorderStyle.Setters.Add(new Setter(Border.BackgroundProperty, ThemeHelper.GetFreezeSolidColorBrush(Color.FromArgb(1, 0, 0, 0)))); + windowBorderStyle.Setters.Add(new Setter(Border.BackgroundProperty, ThemeHelper.GetFrozenSolidColorBrush(Color.FromArgb(1, 0, 0, 0)))); } else if (backdropType == BackdropTypes.Acrylic) { @@ -803,7 +803,7 @@ private void ApplyPreviewBackground(Color? bgColor = null) // Apply background color (remove transparency in color) var backgroundColor = Color.FromRgb(bgColor.Value.R, bgColor.Value.G, bgColor.Value.B); - previewStyle.Setters.Add(new Setter(Border.BackgroundProperty, ThemeHelper.GetFreezeSolidColorBrush(backgroundColor))); + previewStyle.Setters.Add(new Setter(Border.BackgroundProperty, ThemeHelper.GetFrozenSolidColorBrush(backgroundColor))); // The blur theme keeps the corner round fixed (applying DWM code to modify it causes rendering issues). // The non-blur theme retains the previously set WindowBorderStyle. @@ -902,11 +902,11 @@ private void ColorizeWindow(string theme, BackdropTypes backdropType) // Only set the background to transparent if the theme supports blur if (backdropType == BackdropTypes.Mica || backdropType == BackdropTypes.MicaAlt) { - mainWindow.Background = ThemeHelper.GetFreezeSolidColorBrush(Color.FromArgb(1, 0, 0, 0)); + mainWindow.Background = ThemeHelper.GetFrozenSolidColorBrush(Color.FromArgb(1, 0, 0, 0)); } else { - mainWindow.Background = ThemeHelper.GetFreezeSolidColorBrush(selectedBG); + mainWindow.Background = ThemeHelper.GetFrozenSolidColorBrush(selectedBG); } } } diff --git a/Flow.Launcher.Core/Resource/ThemeHelper.cs b/Flow.Launcher.Core/Resource/ThemeHelper.cs index ad7f1c9571c..cd6b04c53b5 100644 --- a/Flow.Launcher.Core/Resource/ThemeHelper.cs +++ b/Flow.Launcher.Core/Resource/ThemeHelper.cs @@ -21,7 +21,7 @@ public static void CopyStyle(Style originalStyle, Style targetStyle) } } - public static SolidColorBrush GetFreezeSolidColorBrush(Color color) + public static SolidColorBrush GetFrozenSolidColorBrush(Color color) { var brush = new SolidColorBrush(color); brush.Freeze();