From 2af033b316d6b23c3c5ec672ad2dcc41f48290b8 Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Mon, 24 Mar 2025 22:07:19 +0800 Subject: [PATCH] Cleanup codes --- Flow.Launcher.Core/Resource/Theme.cs | 72 +++++++++---------- .../ViewModels/SettingsPaneThemeViewModel.cs | 12 ++-- 2 files changed, 40 insertions(+), 44 deletions(-) diff --git a/Flow.Launcher.Core/Resource/Theme.cs b/Flow.Launcher.Core/Resource/Theme.cs index ecbd89f35..9d490ef81 100644 --- a/Flow.Launcher.Core/Resource/Theme.cs +++ b/Flow.Launcher.Core/Resource/Theme.cs @@ -6,6 +6,7 @@ using System.Xml; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; +using System.Windows.Controls.Primitives; using System.Windows.Markup; using System.Windows.Media; using System.Windows.Media.Effects; @@ -16,8 +17,6 @@ using Flow.Launcher.Infrastructure.Logger; using Flow.Launcher.Infrastructure.UserSettings; using Flow.Launcher.Plugin; using Microsoft.Win32; -using TextBox = System.Windows.Controls.TextBox; -using System.Diagnostics; namespace Flow.Launcher.Core.Resource { @@ -59,8 +58,7 @@ namespace Flow.Launcher.Core.Resource var dicts = Application.Current.Resources.MergedDictionaries; _oldResource = dicts.FirstOrDefault(d => { - if (d.Source == null) - return false; + if (d.Source == null) return false; var p = d.Source.AbsolutePath; return p.Contains(Folder) && Path.GetExtension(p) == Extension; @@ -103,19 +101,19 @@ namespace Flow.Launcher.Core.Resource private void UpdateResourceDictionary(ResourceDictionary dictionaryToUpdate) { - // Add the new theme resource first + // Add new resources if (!Application.Current.Resources.MergedDictionaries.Contains(dictionaryToUpdate)) { Application.Current.Resources.MergedDictionaries.Add(dictionaryToUpdate); } - - // Then remove the old theme resource - if (_oldResource != null && - _oldResource != dictionaryToUpdate && + + // Remove old resources + if (_oldResource != null && _oldResource != dictionaryToUpdate && Application.Current.Resources.MergedDictionaries.Contains(_oldResource)) { Application.Current.Resources.MergedDictionaries.Remove(_oldResource); } + _oldResource = dictionaryToUpdate; } @@ -190,7 +188,7 @@ namespace Flow.Launcher.Core.Resource /// /// Applies font properties to a Style. /// - private void SetFontProperties(Style style, FontFamily fontFamily, FontStyle fontStyle, FontWeight fontWeight, FontStretch fontStretch, bool isTextBox) + private static void SetFontProperties(Style style, FontFamily fontFamily, FontStyle fontStyle, FontWeight fontWeight, FontStretch fontStretch, bool isTextBox) { // Remove existing font-related setters if (isTextBox) @@ -199,10 +197,10 @@ namespace Flow.Launcher.Core.Resource var settersToRemove = style.Setters .OfType() .Where(setter => - setter.Property == TextBox.FontFamilyProperty || - setter.Property == TextBox.FontStyleProperty || - setter.Property == TextBox.FontWeightProperty || - setter.Property == TextBox.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 @@ -212,17 +210,17 @@ namespace Flow.Launcher.Core.Resource } // Add New font setter - style.Setters.Add(new Setter(TextBox.FontFamilyProperty, fontFamily)); - style.Setters.Add(new Setter(TextBox.FontStyleProperty, fontStyle)); - style.Setters.Add(new Setter(TextBox.FontWeightProperty, fontWeight)); - style.Setters.Add(new Setter(TextBox.FontStretchProperty, fontStretch)); + style.Setters.Add(new Setter(Control.FontFamilyProperty, fontFamily)); + style.Setters.Add(new Setter(Control.FontStyleProperty, fontStyle)); + 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"); var foregroundPropertyValue = style.Setters.OfType().Where(x => x.Property.Name == "Foreground") .Select(x => x.Value).FirstOrDefault(); if (!caretBrushPropertyValue && foregroundPropertyValue != null) - style.Setters.Add(new Setter(TextBox.CaretBrushProperty, foregroundPropertyValue)); + style.Setters.Add(new Setter(TextBoxBase.CaretBrushProperty, foregroundPropertyValue)); } else { @@ -246,6 +244,7 @@ namespace Flow.Launcher.Core.Resource style.Setters.Add(new Setter(TextBlock.FontStretchProperty, fontStretch)); } } + private ResourceDictionary GetThemeResourceDictionary(string theme) { var uri = GetThemePath(theme); @@ -269,22 +268,22 @@ namespace Flow.Launcher.Core.Resource var fontWeight = FontHelper.GetFontWeightFromInvariantStringOrNormal(_settings.QueryBoxFontWeight); var fontStretch = FontHelper.GetFontStretchFromInvariantStringOrNormal(_settings.QueryBoxFontStretch); - queryBoxStyle.Setters.Add(new Setter(TextBox.FontFamilyProperty, fontFamily)); - queryBoxStyle.Setters.Add(new Setter(TextBox.FontStyleProperty, fontStyle)); - queryBoxStyle.Setters.Add(new Setter(TextBox.FontWeightProperty, fontWeight)); - queryBoxStyle.Setters.Add(new Setter(TextBox.FontStretchProperty, fontStretch)); + queryBoxStyle.Setters.Add(new Setter(Control.FontFamilyProperty, fontFamily)); + queryBoxStyle.Setters.Add(new Setter(Control.FontStyleProperty, fontStyle)); + 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 foregroundPropertyValue = queryBoxStyle.Setters.OfType().Where(x => x.Property.Name == "Foreground") .Select(x => x.Value).FirstOrDefault(); if (!caretBrushPropertyValue && foregroundPropertyValue != null) //otherwise BaseQueryBoxStyle will handle styling - queryBoxStyle.Setters.Add(new Setter(TextBox.CaretBrushProperty, foregroundPropertyValue)); + queryBoxStyle.Setters.Add(new Setter(TextBoxBase.CaretBrushProperty, foregroundPropertyValue)); // Query suggestion box's font style is aligned with query box - querySuggestionBoxStyle.Setters.Add(new Setter(TextBox.FontFamilyProperty, fontFamily)); - querySuggestionBoxStyle.Setters.Add(new Setter(TextBox.FontStyleProperty, fontStyle)); - querySuggestionBoxStyle.Setters.Add(new Setter(TextBox.FontWeightProperty, fontWeight)); - querySuggestionBoxStyle.Setters.Add(new Setter(TextBox.FontStretchProperty, fontStretch)); + querySuggestionBoxStyle.Setters.Add(new Setter(Control.FontFamilyProperty, fontFamily)); + querySuggestionBoxStyle.Setters.Add(new Setter(Control.FontStyleProperty, fontStyle)); + querySuggestionBoxStyle.Setters.Add(new Setter(Control.FontWeightProperty, fontWeight)); + querySuggestionBoxStyle.Setters.Add(new Setter(Control.FontStretchProperty, fontStretch)); } if (dict["ItemTitleStyle"] is Style resultItemStyle && @@ -321,7 +320,7 @@ namespace Flow.Launcher.Core.Resource /* Ignore Theme Window Width and use setting */ var windowStyle = dict["WindowStyle"] as Style; var width = _settings.WindowSize; - windowStyle.Setters.Add(new Setter(Window.WidthProperty, width)); + windowStyle.Setters.Add(new Setter(FrameworkElement.WidthProperty, width)); return dict; } @@ -423,7 +422,7 @@ namespace Flow.Launcher.Core.Resource BlurEnabled = IsBlurTheme(); - // 블러 및 그림자 효과 적용을 위한 비동기 처리 + // Apply blur and drop shadow effects _ = RefreshFrameAsync(); return true; @@ -623,17 +622,14 @@ namespace Flow.Launcher.Core.Resource private void SetBlurForWindow(string theme, BackdropTypes backdropType) { - var dict = GetResourceDictionary(theme); // GetThemeResourceDictionary 대신 GetResourceDictionary 사용 - if (dict == null) - return; + var dict = GetResourceDictionary(theme); + if (dict == null) return; var windowBorderStyle = dict.Contains("WindowBorderStyle") ? dict["WindowBorderStyle"] as Style : null; - if (windowBorderStyle == null) - return; + if (windowBorderStyle == null) return; - Window mainWindow = Application.Current.MainWindow; - if (mainWindow == null) - return; + var mainWindow = Application.Current.MainWindow; + if (mainWindow == null) return; // Check if the theme supports blur bool hasBlur = dict.Contains("ThemeBlurEnabled") && dict["ThemeBlurEnabled"] is bool b && b; diff --git a/Flow.Launcher/SettingPages/ViewModels/SettingsPaneThemeViewModel.cs b/Flow.Launcher/SettingPages/ViewModels/SettingsPaneThemeViewModel.cs index 48a0aa1d1..733996425 100644 --- a/Flow.Launcher/SettingPages/ViewModels/SettingsPaneThemeViewModel.cs +++ b/Flow.Launcher/SettingPages/ViewModels/SettingsPaneThemeViewModel.cs @@ -342,7 +342,7 @@ public partial class SettingsPaneThemeViewModel : BaseModel set { Settings.QueryBoxFont = value.ToString(); - _theme.UpdateFonts(); + _theme.UpdateFonts(); } } @@ -364,7 +364,7 @@ public partial class SettingsPaneThemeViewModel : BaseModel Settings.QueryBoxFontStretch = value.Stretch.ToString(); Settings.QueryBoxFontWeight = value.Weight.ToString(); Settings.QueryBoxFontStyle = value.Style.ToString(); - _theme.UpdateFonts(); + _theme.UpdateFonts(); } } @@ -386,7 +386,7 @@ public partial class SettingsPaneThemeViewModel : BaseModel set { Settings.ResultFont = value.ToString(); - _theme.UpdateFonts(); + _theme.UpdateFonts(); } } @@ -408,7 +408,7 @@ public partial class SettingsPaneThemeViewModel : BaseModel Settings.ResultFontStretch = value.Stretch.ToString(); Settings.ResultFontWeight = value.Weight.ToString(); Settings.ResultFontStyle = value.Style.ToString(); - _theme.UpdateFonts(); + _theme.UpdateFonts(); } } @@ -432,7 +432,7 @@ public partial class SettingsPaneThemeViewModel : BaseModel set { Settings.ResultSubFont = value.ToString(); - _theme.UpdateFonts(); + _theme.UpdateFonts(); } } @@ -453,7 +453,7 @@ public partial class SettingsPaneThemeViewModel : BaseModel Settings.ResultSubFontStretch = value.Stretch.ToString(); Settings.ResultSubFontWeight = value.Weight.ToString(); Settings.ResultSubFontStyle = value.Style.ToString(); - _theme.UpdateFonts(); + _theme.UpdateFonts(); } }