Skip to content

Commit 0ddcc5c

Browse files
authored
Merge pull request #4319 from Flow-Launcher/FreezeResources1
Freeze SolidColorBrush to improve performance
2 parents 5ef6a2b + 7766d70 commit 0ddcc5c

File tree

2 files changed

+39
-24
lines changed

2 files changed

+39
-24
lines changed

Flow.Launcher.Core/Resource/Theme.cs

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -673,13 +673,13 @@ private void SetBlurForWindow(string theme, BackdropTypes backdropType)
673673
// If the BackdropType is Mica or MicaAlt, set the windowborderstyle's background to transparent
674674
if (backdropType == BackdropTypes.Mica || backdropType == BackdropTypes.MicaAlt)
675675
{
676-
windowBorderStyle.Setters.Remove(windowBorderStyle.Setters.OfType<Setter>().FirstOrDefault(x => x.Property.Name == "Background"));
677-
windowBorderStyle.Setters.Add(new Setter(Border.BackgroundProperty, new SolidColorBrush(Color.FromArgb(1, 0, 0, 0))));
676+
windowBorderStyle.Setters.Remove(windowBorderStyle.Setters.OfType<Setter>().FirstOrDefault(x => x.Property == Control.BackgroundProperty));
677+
windowBorderStyle.Setters.Add(new Setter(Border.BackgroundProperty, ThemeHelper.GetFrozenSolidColorBrush(Color.FromArgb(1, 0, 0, 0))));
678678
}
679679
else if (backdropType == BackdropTypes.Acrylic)
680680
{
681-
windowBorderStyle.Setters.Remove(windowBorderStyle.Setters.OfType<Setter>().FirstOrDefault(x => x.Property.Name == "Background"));
682-
windowBorderStyle.Setters.Add(new Setter(Border.BackgroundProperty, new SolidColorBrush(Colors.Transparent)));
681+
windowBorderStyle.Setters.Remove(windowBorderStyle.Setters.OfType<Setter>().FirstOrDefault(x => x.Property == Control.BackgroundProperty));
682+
windowBorderStyle.Setters.Add(new Setter(Border.BackgroundProperty, Brushes.Transparent));
683683
}
684684

685685
// For themes with blur enabled, the window border is rendered by the system, so it's treated as a simple rectangle regardless of thickness.
@@ -798,12 +798,12 @@ private void ApplyPreviewBackground(Color? bgColor = null)
798798
Application.Current.Resources["WindowBorderStyle"] is Style originalStyle)
799799
{
800800
// Copy the original style, including the base style if it exists
801-
CopyStyle(originalStyle, previewStyle);
801+
ThemeHelper.CopyStyle(originalStyle, previewStyle);
802802
}
803803

804804
// Apply background color (remove transparency in color)
805-
Color backgroundColor = Color.FromRgb(bgColor.Value.R, bgColor.Value.G, bgColor.Value.B);
806-
previewStyle.Setters.Add(new Setter(Border.BackgroundProperty, new SolidColorBrush(backgroundColor)));
805+
var backgroundColor = Color.FromRgb(bgColor.Value.R, bgColor.Value.G, bgColor.Value.B);
806+
previewStyle.Setters.Add(new Setter(Border.BackgroundProperty, ThemeHelper.GetFrozenSolidColorBrush(backgroundColor)));
807807

808808
// The blur theme keeps the corner round fixed (applying DWM code to modify it causes rendering issues).
809809
// The non-blur theme retains the previously set WindowBorderStyle.
@@ -817,21 +817,6 @@ private void ApplyPreviewBackground(Color? bgColor = null)
817817
Application.Current.Resources["PreviewWindowBorderStyle"] = previewStyle;
818818
}
819819

820-
private void CopyStyle(Style originalStyle, Style targetStyle)
821-
{
822-
// If the style is based on another style, copy the base style first
823-
if (originalStyle.BasedOn != null)
824-
{
825-
CopyStyle(originalStyle.BasedOn, targetStyle);
826-
}
827-
828-
// Copy the setters from the original style
829-
foreach (var setter in originalStyle.Setters.OfType<Setter>())
830-
{
831-
targetStyle.Setters.Add(new Setter(setter.Property, setter.Value));
832-
}
833-
}
834-
835820
private void ColorizeWindow(string theme, BackdropTypes backdropType)
836821
{
837822
var dict = GetThemeResourceDictionary(theme);
@@ -917,11 +902,11 @@ private void ColorizeWindow(string theme, BackdropTypes backdropType)
917902
// Only set the background to transparent if the theme supports blur
918903
if (backdropType == BackdropTypes.Mica || backdropType == BackdropTypes.MicaAlt)
919904
{
920-
mainWindow.Background = new SolidColorBrush(Color.FromArgb(1, 0, 0, 0));
905+
mainWindow.Background = ThemeHelper.GetFrozenSolidColorBrush(Color.FromArgb(1, 0, 0, 0));
921906
}
922907
else
923908
{
924-
mainWindow.Background = new SolidColorBrush(selectedBG);
909+
mainWindow.Background = ThemeHelper.GetFrozenSolidColorBrush(selectedBG);
925910
}
926911
}
927912
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System.Linq;
2+
using System.Windows;
3+
using System.Windows.Media;
4+
5+
namespace Flow.Launcher.Core.Resource;
6+
7+
public static class ThemeHelper
8+
{
9+
public static void CopyStyle(Style originalStyle, Style targetStyle)
10+
{
11+
// If the style is based on another style, copy the base style first
12+
if (originalStyle.BasedOn != null)
13+
{
14+
CopyStyle(originalStyle.BasedOn, targetStyle);
15+
}
16+
17+
// Copy the setters from the original style
18+
foreach (var setter in originalStyle.Setters.OfType<Setter>())
19+
{
20+
targetStyle.Setters.Add(new Setter(setter.Property, setter.Value));
21+
}
22+
}
23+
24+
public static SolidColorBrush GetFrozenSolidColorBrush(Color color)
25+
{
26+
var brush = new SolidColorBrush(color);
27+
brush.Freeze();
28+
return brush;
29+
}
30+
}

0 commit comments

Comments
 (0)