From c9152b8b1c21c99d7ce6b8209c904cab3f7958a3 Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Tue, 3 Mar 2026 15:53:53 +0800 Subject: [PATCH] Improve font and caret brush setter handling in styles Refactored style management to explicitly remove existing CaretBrush setters before adding new ones, preventing duplicates. Unified font property removal to consistently target Control's font properties, enhancing robustness and consistency for text control styling. --- Flow.Launcher.Core/Resource/Theme.cs | 32 ++++++++++++++++++---------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/Flow.Launcher.Core/Resource/Theme.cs b/Flow.Launcher.Core/Resource/Theme.cs index c3bb6190f..7d8db686d 100644 --- a/Flow.Launcher.Core/Resource/Theme.cs +++ b/Flow.Launcher.Core/Resource/Theme.cs @@ -194,7 +194,7 @@ namespace Flow.Launcher.Core.Resource // Remove existing font-related setters if (isTextBox) { - // First, find the setters to remove and store them in a list + // Find the setters to remove and store them in a list var settersToRemove = style.Setters .OfType() .Where(setter => @@ -204,7 +204,7 @@ namespace Flow.Launcher.Core.Resource setter.Property == Control.FontStretchProperty) .ToList(); - // Remove each found setter one by one + // Remove each found setter one by one foreach (var setter in settersToRemove) { style.Setters.Remove(setter); @@ -216,24 +216,30 @@ namespace Flow.Launcher.Core.Resource style.Setters.Add(new Setter(Control.FontWeightProperty, fontWeight)); style.Setters.Add(new Setter(Control.FontStretchProperty, fontStretch)); - // Set caret brush (retain existing logic) - var caretBrushPropertyValue = style.Setters.OfType().Any(x => x.Property.Name == "CaretBrush"); + // Set caret brush (retain existing logic) + var caretBrushProperty = style.Setters.OfType().Where(x => x.Property.Name == "CaretBrush")? + .FirstOrDefault(); var foregroundPropertyValue = style.Setters.OfType().Where(x => x.Property.Name == "Foreground") .Select(x => x.Value).FirstOrDefault(); - if (!caretBrushPropertyValue && foregroundPropertyValue != null) + if (caretBrushProperty != null && foregroundPropertyValue != null) + { + style.Setters.Remove(caretBrushProperty); style.Setters.Add(new Setter(TextBoxBase.CaretBrushProperty, foregroundPropertyValue)); + } } else { + // Find the setters to remove and store them in a list var settersToRemove = style.Setters .OfType() .Where(setter => - setter.Property == TextBlock.FontFamilyProperty || - setter.Property == TextBlock.FontStyleProperty || - setter.Property == TextBlock.FontWeightProperty || - setter.Property == TextBlock.FontStretchProperty) + setter.Property == Control.FontFamilyProperty || + setter.Property == Control.FontStyleProperty || + setter.Property == Control.FontWeightProperty || + setter.Property == Control.FontStretchProperty) .ToList(); + // Remove each found setter one by one foreach (var setter in settersToRemove) { style.Setters.Remove(setter); @@ -274,11 +280,15 @@ namespace Flow.Launcher.Core.Resource queryBoxStyle.Setters.Add(new Setter(Control.FontWeightProperty, fontWeight)); queryBoxStyle.Setters.Add(new Setter(Control.FontStretchProperty, fontStretch)); - var caretBrushPropertyValue = queryBoxStyle.Setters.OfType().Any(x => x.Property.Name == "CaretBrush"); + var caretBrushProperty = queryBoxStyle.Setters.OfType().Where(x => x.Property.Name == "CaretBrush")? + .FirstOrDefault(); var foregroundPropertyValue = queryBoxStyle.Setters.OfType().Where(x => x.Property.Name == "Foreground") .Select(x => x.Value).FirstOrDefault(); - if (!caretBrushPropertyValue && foregroundPropertyValue != null) //otherwise BaseQueryBoxStyle will handle styling + if (caretBrushProperty != null && foregroundPropertyValue != null) + { + queryBoxStyle.Setters.Remove(caretBrushProperty); queryBoxStyle.Setters.Add(new Setter(TextBoxBase.CaretBrushProperty, foregroundPropertyValue)); + } // Query suggestion box's font style is aligned with query box querySuggestionBoxStyle.Setters.Add(new Setter(Control.FontFamilyProperty, fontFamily));