From da30e2e5958fecc9ae64edb1e7241f22117525d2 Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Mon, 17 Mar 2025 09:37:34 +0800 Subject: [PATCH] Improve code quality --- Flow.Launcher.Core/Resource/Theme.cs | 52 +++++++------ .../ViewModels/SettingsPaneThemeViewModel.cs | 78 +++++++++---------- .../Flow.Launcher.Plugin.Sys/ThemeSelector.cs | 2 +- 3 files changed, 66 insertions(+), 66 deletions(-) diff --git a/Flow.Launcher.Core/Resource/Theme.cs b/Flow.Launcher.Core/Resource/Theme.cs index 17fb698b4..2027f2f32 100644 --- a/Flow.Launcher.Core/Resource/Theme.cs +++ b/Flow.Launcher.Core/Resource/Theme.cs @@ -24,8 +24,6 @@ namespace Flow.Launcher.Core.Resource { #region Properties & Fields - public string CurrentTheme => _settings.Theme; - public bool BlurEnabled { get; set; } private const string ThemeMetadataNamePrefix = "Name:"; @@ -78,6 +76,11 @@ namespace Flow.Launcher.Core.Resource #region Theme Resources + public string GetCurrentTheme() + { + return _settings.Theme; + } + private void MakeSureThemeDirectoriesExist() { foreach (var dir in _themeDirectories.Where(dir => !Directory.Exists(dir))) @@ -183,7 +186,7 @@ namespace Flow.Launcher.Core.Resource private ResourceDictionary GetCurrentResourceDictionary() { - return GetResourceDictionary(_settings.Theme); + return GetResourceDictionary(GetCurrentTheme()); } private ThemeData GetThemeDataFromPath(string path) @@ -253,9 +256,10 @@ namespace Flow.Launcher.Core.Resource return themes.OrderBy(o => o.Name).ToList(); } - public bool ChangeTheme(string theme) + public bool ChangeTheme(string theme = null) { - const string defaultTheme = Constant.DefaultTheme; + if (string.IsNullOrEmpty(theme)) + theme = GetCurrentTheme(); string path = GetThemePath(theme); try @@ -270,7 +274,7 @@ namespace Flow.Launcher.Core.Resource _settings.Theme = theme; //always allow re-loading default theme, in case of failure of switching to a new theme from default theme - if (_oldTheme != theme || theme == defaultTheme) + if (_oldTheme != theme || theme == Constant.DefaultTheme) { _oldTheme = Path.GetFileNameWithoutExtension(_oldResource.Source.AbsolutePath); } @@ -284,20 +288,20 @@ namespace Flow.Launcher.Core.Resource catch (DirectoryNotFoundException) { Log.Error($"|Theme.ChangeTheme|Theme <{theme}> path can't be found"); - if (theme != defaultTheme) + if (theme != Constant.DefaultTheme) { - _api.ShowMsgBox(string.Format(InternationalizationManager.Instance.GetTranslation("theme_load_failure_path_not_exists"), theme)); - ChangeTheme(defaultTheme); + _api.ShowMsgBox(string.Format(_api.GetTranslation("theme_load_failure_path_not_exists"), theme)); + ChangeTheme(Constant.DefaultTheme); } return false; } catch (XamlParseException) { Log.Error($"|Theme.ChangeTheme|Theme <{theme}> fail to parse"); - if (theme != defaultTheme) + if (theme != Constant.DefaultTheme) { - _api.ShowMsgBox(string.Format(InternationalizationManager.Instance.GetTranslation("theme_load_failure_parse_error"), theme)); - ChangeTheme(defaultTheme); + _api.ShowMsgBox(string.Format(_api.GetTranslation("theme_load_failure_parse_error"), theme)); + ChangeTheme(Constant.DefaultTheme); } return false; } @@ -429,7 +433,7 @@ namespace Flow.Launcher.Core.Resource { AutoDropShadow(useDropShadowEffect); } - SetBlurForWindow(backdropType); + SetBlurForWindow(GetCurrentTheme(), backdropType); if (!BlurEnabled) { @@ -448,7 +452,7 @@ namespace Flow.Launcher.Core.Resource // Get the actual backdrop type and drop shadow effect settings var (backdropType, _) = GetActualValue(); - SetBlurForWindow(backdropType); + SetBlurForWindow(GetCurrentTheme(), backdropType); }, DispatcherPriority.Normal); } @@ -475,9 +479,9 @@ namespace Flow.Launcher.Core.Resource return (backdropType, useDropShadowEffect); } - private void SetBlurForWindow(BackdropTypes backdropType) + private void SetBlurForWindow(string theme, BackdropTypes backdropType) { - var dict = GetThemeResourceDictionary(_settings.Theme); + var dict = GetThemeResourceDictionary(theme); if (dict == null) return; @@ -507,13 +511,13 @@ namespace Flow.Launcher.Core.Resource // Apply the blur effect Win32Helper.DWMSetBackdropForWindow(mainWindow, backdropType); - ColorizeWindow(backdropType); + ColorizeWindow(theme, backdropType); } else { // Apply default style when Blur is disabled Win32Helper.DWMSetBackdropForWindow(mainWindow, BackdropTypes.None); - ColorizeWindow(backdropType); + ColorizeWindow(theme, backdropType); } UpdateResourceDictionary(dict); @@ -559,9 +563,9 @@ namespace Flow.Launcher.Core.Resource // Get Background Color from WindowBorderStyle when there not color for BG. // for theme has not "LightBG" or "DarkBG" case. - private Color GetWindowBorderStyleBackground() + private Color GetWindowBorderStyleBackground(string theme) { - var Resources = GetThemeResourceDictionary(_settings.Theme); + var Resources = GetThemeResourceDictionary(theme); var windowBorderStyle = (Style)Resources["WindowBorderStyle"]; var backgroundSetter = windowBorderStyle.Setters @@ -634,9 +638,9 @@ namespace Flow.Launcher.Core.Resource Application.Current.Resources["PreviewWindowBorderStyle"] = previewStyle; } - private void ColorizeWindow(BackdropTypes backdropType) + private void ColorizeWindow(string theme, BackdropTypes backdropType) { - var dict = GetThemeResourceDictionary(_settings.Theme); + var dict = GetThemeResourceDictionary(theme); if (dict == null) return; var mainWindow = Application.Current.MainWindow; @@ -687,11 +691,11 @@ namespace Flow.Launcher.Core.Resource // Retrieve LightBG value (fallback to WindowBorderStyle background color if not found) try { - LightBG = dict.Contains("LightBG") ? (Color)dict["LightBG"] : GetWindowBorderStyleBackground(); + LightBG = dict.Contains("LightBG") ? (Color)dict["LightBG"] : GetWindowBorderStyleBackground(theme); } catch (Exception) { - LightBG = GetWindowBorderStyleBackground(); + LightBG = GetWindowBorderStyleBackground(theme); } // Retrieve DarkBG value (fallback to LightBG if not found) diff --git a/Flow.Launcher/SettingPages/ViewModels/SettingsPaneThemeViewModel.cs b/Flow.Launcher/SettingPages/ViewModels/SettingsPaneThemeViewModel.cs index f8c0fa642..61d365b64 100644 --- a/Flow.Launcher/SettingPages/ViewModels/SettingsPaneThemeViewModel.cs +++ b/Flow.Launcher/SettingPages/ViewModels/SettingsPaneThemeViewModel.cs @@ -5,6 +5,7 @@ using System.Globalization; using System.IO; using System.Linq; using System.Windows.Media; +using CommunityToolkit.Mvvm.DependencyInjection; using CommunityToolkit.Mvvm.Input; using Flow.Launcher.Core.Resource; using Flow.Launcher.Helper; @@ -13,7 +14,6 @@ using Flow.Launcher.Infrastructure.UserSettings; using Flow.Launcher.Plugin; using Flow.Launcher.ViewModel; using ModernWpf; -using ThemeManager = Flow.Launcher.Core.Resource.ThemeManager; using ThemeManagerForColorSchemeSwitch = ModernWpf.ThemeManager; namespace Flow.Launcher.SettingPages.ViewModels; @@ -22,18 +22,22 @@ public partial class SettingsPaneThemeViewModel : BaseModel { private const string DefaultFont = "Segoe UI"; public Settings Settings { get; } + private readonly Theme _theme = Ioc.Default.GetRequiredService(); public static string LinkHowToCreateTheme => @"https://flowlauncher.com/docs/#/how-to-create-a-theme"; public static string LinkThemeGallery => "https://github.com/Flow-Launcher/Flow.Launcher/discussions/1438"; + private List _themes; + public List Themes => _themes ??= _theme.LoadAvailableThemes(); + private Theme.ThemeData _selectedTheme; public Theme.ThemeData SelectedTheme { - get => _selectedTheme ??= Themes.Find(v => v.FileNameWithoutExtension == Settings.Theme); + get => _selectedTheme ??= Themes.Find(v => v.FileNameWithoutExtension == _theme.GetCurrentTheme()); set { _selectedTheme = value; - ThemeManager.Instance.ChangeTheme(value.FileNameWithoutExtension); + _theme.ChangeTheme(value.FileNameWithoutExtension); // Update UI state OnPropertyChanged(nameof(BackdropType)); @@ -41,7 +45,7 @@ public partial class SettingsPaneThemeViewModel : BaseModel OnPropertyChanged(nameof(IsDropShadowEnabled)); OnPropertyChanged(nameof(DropShadowEffect)); - _ = ThemeManager.Instance.RefreshFrameAsync(); + _ = _theme.RefreshFrameAsync(); } } @@ -54,14 +58,14 @@ public partial class SettingsPaneThemeViewModel : BaseModel } } - public bool IsDropShadowEnabled => !ThemeManager.Instance.BlurEnabled; + public bool IsDropShadowEnabled => !_theme.BlurEnabled; public bool DropShadowEffect { get => Settings.UseDropShadowEffect; set { - if (ThemeManager.Instance.BlurEnabled) + if (_theme.BlurEnabled) { // Always DropShadowEffect = true with blur theme Settings.UseDropShadowEffect = true; @@ -71,11 +75,11 @@ public partial class SettingsPaneThemeViewModel : BaseModel // User can change shadow with non-blur theme. if (value) { - ThemeManager.Instance.AddDropShadowEffectToCurrentTheme(); + _theme.AddDropShadowEffectToCurrentTheme(); } else { - ThemeManager.Instance.RemoveDropShadowEffectFromCurrentTheme(); + _theme.RemoveDropShadowEffectFromCurrentTheme(); } Settings.UseDropShadowEffect = value; @@ -113,9 +117,6 @@ public partial class SettingsPaneThemeViewModel : BaseModel set => Settings.ResultSubItemFontSize = value; } - private List _themes; - public List Themes => _themes ??= ThemeManager.Instance.LoadAvailableThemes(); - public class ColorSchemeData : DropdownDataGeneric { } public List ColorSchemes { get; } = DropdownDataGeneric.GetValues("ColorScheme"); @@ -132,7 +133,7 @@ public partial class SettingsPaneThemeViewModel : BaseModel _ => ThemeManagerForColorSchemeSwitch.Current.ApplicationTheme }; Settings.ColorScheme = value; - _ = ThemeManager.Instance.RefreshFrameAsync(); + _ = _theme.RefreshFrameAsync(); } } @@ -209,13 +210,7 @@ public partial class SettingsPaneThemeViewModel : BaseModel public class AnimationSpeedData : DropdownDataGeneric { } public List AnimationSpeeds { get; } = DropdownDataGeneric.GetValues("AnimationSpeed"); - public class BackdropTypeData : DropdownDataGeneric - { - public void ApplyBackdrop() - { - _ = ThemeManager.Instance.SetBlurForWindowAsync(); - } - } + public class BackdropTypeData : DropdownDataGeneric { } public List BackdropTypesList { get; } = DropdownDataGeneric.GetValues("BackdropTypes"); @@ -233,8 +228,9 @@ public partial class SettingsPaneThemeViewModel : BaseModel } Settings.BackdropType = value; - var backdropData = BackdropTypesList.FirstOrDefault(b => b.Value == value); - backdropData?.ApplyBackdrop(); + + _ = _theme.SetBlurForWindowAsync(); + OnPropertyChanged(nameof(IsDropShadowEnabled)); } } @@ -284,37 +280,37 @@ public partial class SettingsPaneThemeViewModel : BaseModel { var results = new List { - new Result + new() { - Title = InternationalizationManager.Instance.GetTranslation("SampleTitleExplorer"), - SubTitle = InternationalizationManager.Instance.GetTranslation("SampleSubTitleExplorer"), + Title = App.API.GetTranslation("SampleTitleExplorer"), + SubTitle = App.API.GetTranslation("SampleSubTitleExplorer"), IcoPath = Path.Combine( Constant.ProgramDirectory, @"Plugins\Flow.Launcher.Plugin.Explorer\Images\explorer.png" ) }, - new Result + new() { - Title = InternationalizationManager.Instance.GetTranslation("SampleTitleWebSearch"), - SubTitle = InternationalizationManager.Instance.GetTranslation("SampleSubTitleWebSearch"), + Title = App.API.GetTranslation("SampleTitleWebSearch"), + SubTitle = App.API.GetTranslation("SampleSubTitleWebSearch"), IcoPath = Path.Combine( Constant.ProgramDirectory, @"Plugins\Flow.Launcher.Plugin.WebSearch\Images\web_search.png" ) }, - new Result + new() { - Title = InternationalizationManager.Instance.GetTranslation("SampleTitleProgram"), - SubTitle = InternationalizationManager.Instance.GetTranslation("SampleSubTitleProgram"), + Title = App.API.GetTranslation("SampleTitleProgram"), + SubTitle = App.API.GetTranslation("SampleSubTitleProgram"), IcoPath = Path.Combine( Constant.ProgramDirectory, @"Plugins\Flow.Launcher.Plugin.Program\Images\program.png" ) }, - new Result + new() { - Title = InternationalizationManager.Instance.GetTranslation("SampleTitleProcessKiller"), - SubTitle = InternationalizationManager.Instance.GetTranslation("SampleSubTitleProcessKiller"), + Title = App.API.GetTranslation("SampleTitleProcessKiller"), + SubTitle = App.API.GetTranslation("SampleSubTitleProcessKiller"), IcoPath = Path.Combine( Constant.ProgramDirectory, @"Plugins\Flow.Launcher.Plugin.ProcessKiller\Images\app.png" @@ -346,7 +342,7 @@ public partial class SettingsPaneThemeViewModel : BaseModel set { Settings.QueryBoxFont = value.ToString(); - ThemeManager.Instance.ChangeTheme(Settings.Theme); + _theme.ChangeTheme(); } } @@ -368,7 +364,7 @@ public partial class SettingsPaneThemeViewModel : BaseModel Settings.QueryBoxFontStretch = value.Stretch.ToString(); Settings.QueryBoxFontWeight = value.Weight.ToString(); Settings.QueryBoxFontStyle = value.Style.ToString(); - ThemeManager.Instance.ChangeTheme(Settings.Theme); + _theme.ChangeTheme(); } } @@ -390,7 +386,7 @@ public partial class SettingsPaneThemeViewModel : BaseModel set { Settings.ResultFont = value.ToString(); - ThemeManager.Instance.ChangeTheme(Settings.Theme); + _theme.ChangeTheme(); } } @@ -412,7 +408,7 @@ public partial class SettingsPaneThemeViewModel : BaseModel Settings.ResultFontStretch = value.Stretch.ToString(); Settings.ResultFontWeight = value.Weight.ToString(); Settings.ResultFontStyle = value.Style.ToString(); - ThemeManager.Instance.ChangeTheme(Settings.Theme); + _theme.ChangeTheme(); } } @@ -420,9 +416,9 @@ public partial class SettingsPaneThemeViewModel : BaseModel { get { - if (Fonts.SystemFontFamilies.Count(o => + if (Fonts.SystemFontFamilies.Any(o => o.FamilyNames.Values != null && - o.FamilyNames.Values.Contains(Settings.ResultSubFont)) > 0) + o.FamilyNames.Values.Contains(Settings.ResultSubFont))) { var font = new FontFamily(Settings.ResultSubFont); return font; @@ -436,7 +432,7 @@ public partial class SettingsPaneThemeViewModel : BaseModel set { Settings.ResultSubFont = value.ToString(); - ThemeManager.Instance.ChangeTheme(Settings.Theme); + _theme.ChangeTheme(); } } @@ -457,7 +453,7 @@ public partial class SettingsPaneThemeViewModel : BaseModel Settings.ResultSubFontStretch = value.Stretch.ToString(); Settings.ResultSubFontWeight = value.Weight.ToString(); Settings.ResultSubFontStyle = value.Style.ToString(); - ThemeManager.Instance.ChangeTheme(Settings.Theme); + _theme.ChangeTheme(); } } diff --git a/Plugins/Flow.Launcher.Plugin.Sys/ThemeSelector.cs b/Plugins/Flow.Launcher.Plugin.Sys/ThemeSelector.cs index b467aefee..4467b94fb 100644 --- a/Plugins/Flow.Launcher.Plugin.Sys/ThemeSelector.cs +++ b/Plugins/Flow.Launcher.Plugin.Sys/ThemeSelector.cs @@ -19,7 +19,7 @@ namespace Flow.Launcher.Plugin.Sys private Theme.ThemeData _selectedTheme; public Theme.ThemeData SelectedTheme { - get => _selectedTheme ??= Themes.Find(v => v.FileNameWithoutExtension == _theme.CurrentTheme); + get => _selectedTheme ??= Themes.Find(v => v.FileNameWithoutExtension == _theme.GetCurrentTheme()); set { _selectedTheme = value;