From ec22f596d432a0a4dc39e2a2643827989e3f6bb2 Mon Sep 17 00:00:00 2001 From: DB p Date: Mon, 24 Mar 2025 16:26:07 +0900 Subject: [PATCH 1/6] Fix Non Resource situation when change theme --- Flow.Launcher.Core/Resource/Theme.cs | 41 ++++++++++++++++++++-------- 1 file changed, 30 insertions(+), 11 deletions(-) diff --git a/Flow.Launcher.Core/Resource/Theme.cs b/Flow.Launcher.Core/Resource/Theme.cs index fc5c2e4e947..25abda3a08b 100644 --- a/Flow.Launcher.Core/Resource/Theme.cs +++ b/Flow.Launcher.Core/Resource/Theme.cs @@ -17,6 +17,7 @@ using Flow.Launcher.Plugin; using Microsoft.Win32; using TextBox = System.Windows.Controls.TextBox; +using System.Diagnostics; namespace Flow.Launcher.Core.Resource { @@ -56,19 +57,24 @@ public Theme(IPublicAPI publicAPI, Settings settings) MakeSureThemeDirectoriesExist(); var dicts = Application.Current.Resources.MergedDictionaries; - _oldResource = dicts.First(d => + _oldResource = dicts.FirstOrDefault(d => { if (d.Source == null) return false; var p = d.Source.AbsolutePath; - var dir = Path.GetDirectoryName(p).NonNull(); - var info = new DirectoryInfo(dir); - var f = info.Name; - var e = Path.GetExtension(p); - var found = f == Folder && e == Extension; - return found; + return p.Contains(Folder) && Path.GetExtension(p) == Extension; }); + + if (_oldResource != null) + { + _oldTheme = Path.GetFileNameWithoutExtension(_oldResource.Source.AbsolutePath); + } + else + { + Log.Error("현재 테마 리소스를 찾을 수 없습니다. 기본 테마로 초기화합니다."); + _oldTheme = Constant.DefaultTheme; + }; _oldTheme = Path.GetFileNameWithoutExtension(_oldResource.Source.AbsolutePath); } @@ -98,11 +104,24 @@ private void MakeSureThemeDirectoriesExist() private void UpdateResourceDictionary(ResourceDictionary dictionaryToUpdate) { - var dicts = Application.Current.Resources.MergedDictionaries; - - dicts.Remove(_oldResource); - dicts.Add(dictionaryToUpdate); + // 새 테마 리소스를 먼저 추가하고 + if (!Application.Current.Resources.MergedDictionaries.Contains(dictionaryToUpdate)) + { + Application.Current.Resources.MergedDictionaries.Add(dictionaryToUpdate); + } + + // 그 다음 이전 테마 리소스 제거 + if (_oldResource != null && + _oldResource != dictionaryToUpdate && + Application.Current.Resources.MergedDictionaries.Contains(_oldResource)) + { + Application.Current.Resources.MergedDictionaries.Remove(_oldResource); + } + _oldResource = dictionaryToUpdate; + + // 리소스 변경 후 문제가 없는지 검증 + Debug.WriteLine($"테마 변경 후 리소스 딕셔너리 수: {Application.Current.Resources.MergedDictionaries.Count}"); } private ResourceDictionary GetThemeResourceDictionary(string theme) From a078777608281192c3a2835178a895d822972d42 Mon Sep 17 00:00:00 2001 From: DB p Date: Mon, 24 Mar 2025 18:10:12 +0900 Subject: [PATCH 2/6] - Fixed an issue where font settings were not applied when using the Blur theme - Removed bold style from highlight and delegated it to individual themes --- Flow.Launcher.Core/Resource/Theme.cs | 156 ++++++++++++++++-- .../ViewModels/SettingsPaneThemeViewModel.cs | 12 +- Flow.Launcher/Themes/Base.xaml | 5 - Flow.Launcher/Themes/BlurBlack Darker.xaml | 4 +- Flow.Launcher/Themes/BlurBlack.xaml | 4 +- Flow.Launcher/Themes/BlurWhite.xaml | 4 +- 6 files changed, 155 insertions(+), 30 deletions(-) diff --git a/Flow.Launcher.Core/Resource/Theme.cs b/Flow.Launcher.Core/Resource/Theme.cs index 25abda3a08b..5e2e336aad9 100644 --- a/Flow.Launcher.Core/Resource/Theme.cs +++ b/Flow.Launcher.Core/Resource/Theme.cs @@ -72,7 +72,7 @@ public Theme(IPublicAPI publicAPI, Settings settings) } else { - Log.Error("현재 테마 리소스를 찾을 수 없습니다. 기본 테마로 초기화합니다."); + Log.Error("Current theme resource not found. Initializing with default theme."); _oldTheme = Constant.DefaultTheme; }; _oldTheme = Path.GetFileNameWithoutExtension(_oldResource.Source.AbsolutePath); @@ -104,26 +104,149 @@ private void MakeSureThemeDirectoriesExist() private void UpdateResourceDictionary(ResourceDictionary dictionaryToUpdate) { - // 새 테마 리소스를 먼저 추가하고 + // Add the new theme resource first if (!Application.Current.Resources.MergedDictionaries.Contains(dictionaryToUpdate)) { Application.Current.Resources.MergedDictionaries.Add(dictionaryToUpdate); } - // 그 다음 이전 테마 리소스 제거 + // Then remove the old theme resource if (_oldResource != null && _oldResource != dictionaryToUpdate && Application.Current.Resources.MergedDictionaries.Contains(_oldResource)) { Application.Current.Resources.MergedDictionaries.Remove(_oldResource); } - _oldResource = dictionaryToUpdate; - - // 리소스 변경 후 문제가 없는지 검증 - Debug.WriteLine($"테마 변경 후 리소스 딕셔너리 수: {Application.Current.Resources.MergedDictionaries.Count}"); } + /// + /// Updates only the font settings and refreshes the UI. + /// + public void UpdateFonts() + { + try + { + // Loads a ResourceDictionary for the specified theme. + var themeName = GetCurrentTheme(); + var dict = GetThemeResourceDictionary(themeName); + + // Applies font settings to the theme resource. + ApplyFontSettings(dict); + UpdateResourceDictionary(dict); + _ = RefreshFrameAsync(); + } + catch (Exception e) + { + Log.Exception("Error occurred while updating theme fonts", e); + } + } + + /// + /// Loads and applies font settings to the theme resource. + /// + private void ApplyFontSettings(ResourceDictionary dict) + { + if (dict["QueryBoxStyle"] is Style queryBoxStyle && + dict["QuerySuggestionBoxStyle"] is Style querySuggestionBoxStyle) + { + var fontFamily = new FontFamily(_settings.QueryBoxFont); + var fontStyle = FontHelper.GetFontStyleFromInvariantStringOrNormal(_settings.QueryBoxFontStyle); + var fontWeight = FontHelper.GetFontWeightFromInvariantStringOrNormal(_settings.QueryBoxFontWeight); + var fontStretch = FontHelper.GetFontStretchFromInvariantStringOrNormal(_settings.QueryBoxFontStretch); + + SetFontProperties(queryBoxStyle, fontFamily, fontStyle, fontWeight, fontStretch, true); + SetFontProperties(querySuggestionBoxStyle, fontFamily, fontStyle, fontWeight, fontStretch, false); + } + + if (dict["ItemTitleStyle"] is Style resultItemStyle && + dict["ItemTitleSelectedStyle"] is Style resultItemSelectedStyle && + dict["ItemHotkeyStyle"] is Style resultHotkeyItemStyle && + dict["ItemHotkeySelectedStyle"] is Style resultHotkeyItemSelectedStyle) + { + var fontFamily = new FontFamily(_settings.ResultFont); + var fontStyle = FontHelper.GetFontStyleFromInvariantStringOrNormal(_settings.ResultFontStyle); + var fontWeight = FontHelper.GetFontWeightFromInvariantStringOrNormal(_settings.ResultFontWeight); + var fontStretch = FontHelper.GetFontStretchFromInvariantStringOrNormal(_settings.ResultFontStretch); + + SetFontProperties(resultItemStyle, fontFamily, fontStyle, fontWeight, fontStretch, false); + SetFontProperties(resultItemSelectedStyle, fontFamily, fontStyle, fontWeight, fontStretch, false); + SetFontProperties(resultHotkeyItemStyle, fontFamily, fontStyle, fontWeight, fontStretch, false); + SetFontProperties(resultHotkeyItemSelectedStyle, fontFamily, fontStyle, fontWeight, fontStretch, false); + } + + if (dict["ItemSubTitleStyle"] is Style resultSubItemStyle && + dict["ItemSubTitleSelectedStyle"] is Style resultSubItemSelectedStyle) + { + var fontFamily = new FontFamily(_settings.ResultSubFont); + var fontStyle = FontHelper.GetFontStyleFromInvariantStringOrNormal(_settings.ResultSubFontStyle); + var fontWeight = FontHelper.GetFontWeightFromInvariantStringOrNormal(_settings.ResultSubFontWeight); + var fontStretch = FontHelper.GetFontStretchFromInvariantStringOrNormal(_settings.ResultSubFontStretch); + + SetFontProperties(resultSubItemStyle, fontFamily, fontStyle, fontWeight, fontStretch, false); + SetFontProperties(resultSubItemSelectedStyle, fontFamily, fontStyle, fontWeight, fontStretch, false); + } + } + + /// + /// Applies font properties to a Style. + /// + private void SetFontProperties(Style style, FontFamily fontFamily, FontStyle fontStyle, FontWeight fontWeight, FontStretch fontStretch, bool isTextBox) + { + // Remove existing font-related setters + if (isTextBox) + { + // First, find the setters to remove and store them in a list + var settersToRemove = style.Setters + .OfType() + .Where(setter => + setter.Property == TextBox.FontFamilyProperty || + setter.Property == TextBox.FontStyleProperty || + setter.Property == TextBox.FontWeightProperty || + setter.Property == TextBox.FontStretchProperty) + .ToList(); + + // Remove each found setter one by one + foreach (var setter in settersToRemove) + { + style.Setters.Remove(setter); + } + + // 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)); + + // 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)); + } + else + { + var settersToRemove = style.Setters + .OfType() + .Where(setter => + setter.Property == TextBlock.FontFamilyProperty || + setter.Property == TextBlock.FontStyleProperty || + setter.Property == TextBlock.FontWeightProperty || + setter.Property == TextBlock.FontStretchProperty) + .ToList(); + + foreach (var setter in settersToRemove) + { + style.Setters.Remove(setter); + } + + style.Setters.Add(new Setter(TextBlock.FontFamilyProperty, fontFamily)); + style.Setters.Add(new Setter(TextBlock.FontStyleProperty, fontStyle)); + style.Setters.Add(new Setter(TextBlock.FontWeightProperty, fontWeight)); + style.Setters.Add(new Setter(TextBlock.FontStretchProperty, fontStretch)); + } + } private ResourceDictionary GetThemeResourceDictionary(string theme) { var uri = GetThemePath(theme); @@ -286,9 +409,10 @@ public bool ChangeTheme(string theme = null) if (string.IsNullOrEmpty(path)) throw new DirectoryNotFoundException("Theme path can't be found <{path}>"); - // reload all resources even if the theme itself hasn't changed in order to pickup changes - // to things like fonts - UpdateResourceDictionary(GetResourceDictionary(theme)); + // Retrieve theme resource – always use the resource with font settings applied. + var resourceDict = GetResourceDictionary(theme); + + UpdateResourceDictionary(resourceDict); _settings.Theme = theme; @@ -299,10 +423,11 @@ public bool ChangeTheme(string theme = null) } BlurEnabled = IsBlurTheme(); - //if (_settings.UseDropShadowEffect) - // AddDropShadowEffectToCurrentTheme(); - //Win32Helper.SetBlurForWindow(Application.Current.MainWindow, BlurEnabled); - _ = SetBlurForWindowAsync(); + + // 블러 및 그림자 효과 적용을 위한 비동기 처리 + _ = RefreshFrameAsync(); + + return true; } catch (DirectoryNotFoundException) { @@ -324,7 +449,6 @@ public bool ChangeTheme(string theme = null) } return false; } - return true; } #endregion @@ -500,7 +624,7 @@ await Application.Current.Dispatcher.InvokeAsync(() => private void SetBlurForWindow(string theme, BackdropTypes backdropType) { - var dict = GetThemeResourceDictionary(theme); + var dict = GetResourceDictionary(theme); // GetThemeResourceDictionary 대신 GetResourceDictionary 사용 if (dict == null) return; diff --git a/Flow.Launcher/SettingPages/ViewModels/SettingsPaneThemeViewModel.cs b/Flow.Launcher/SettingPages/ViewModels/SettingsPaneThemeViewModel.cs index 61d365b649c..48a0aa1d181 100644 --- a/Flow.Launcher/SettingPages/ViewModels/SettingsPaneThemeViewModel.cs +++ b/Flow.Launcher/SettingPages/ViewModels/SettingsPaneThemeViewModel.cs @@ -342,7 +342,7 @@ public FontFamily SelectedQueryBoxFont set { Settings.QueryBoxFont = value.ToString(); - _theme.ChangeTheme(); + _theme.UpdateFonts(); } } @@ -364,7 +364,7 @@ public FamilyTypeface SelectedQueryBoxFontFaces Settings.QueryBoxFontStretch = value.Stretch.ToString(); Settings.QueryBoxFontWeight = value.Weight.ToString(); Settings.QueryBoxFontStyle = value.Style.ToString(); - _theme.ChangeTheme(); + _theme.UpdateFonts(); } } @@ -386,7 +386,7 @@ public FontFamily SelectedResultFont set { Settings.ResultFont = value.ToString(); - _theme.ChangeTheme(); + _theme.UpdateFonts(); } } @@ -408,7 +408,7 @@ public FamilyTypeface SelectedResultFontFaces Settings.ResultFontStretch = value.Stretch.ToString(); Settings.ResultFontWeight = value.Weight.ToString(); Settings.ResultFontStyle = value.Style.ToString(); - _theme.ChangeTheme(); + _theme.UpdateFonts(); } } @@ -432,7 +432,7 @@ public FontFamily SelectedResultSubFont set { Settings.ResultSubFont = value.ToString(); - _theme.ChangeTheme(); + _theme.UpdateFonts(); } } @@ -453,7 +453,7 @@ public FamilyTypeface SelectedResultSubFontFaces Settings.ResultSubFontStretch = value.Stretch.ToString(); Settings.ResultSubFontWeight = value.Weight.ToString(); Settings.ResultSubFontStyle = value.Style.ToString(); - _theme.ChangeTheme(); + _theme.UpdateFonts(); } } diff --git a/Flow.Launcher/Themes/Base.xaml b/Flow.Launcher/Themes/Base.xaml index 907ded363dd..4772e7768b2 100644 --- a/Flow.Launcher/Themes/Base.xaml +++ b/Flow.Launcher/Themes/Base.xaml @@ -27,7 +27,6 @@ - + - + - + - - + - +