From 81007f79aee6ca7671880afaa8e329e99bacb9f4 Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Tue, 15 Apr 2025 13:18:06 +0800 Subject: [PATCH] Remove mvvm command for code quality --- Flow.Launcher/ViewModel/RelayCommand.cs | 29 ---------- .../Flow.Launcher.Plugin.Explorer.csproj | 1 + .../ViewModels/RelayCommand.cs | 27 --------- .../ViewModels/SettingsViewModel.cs | 55 +++++++------------ .../Views/ExplorerSettings.xaml | 6 +- 5 files changed, 24 insertions(+), 94 deletions(-) delete mode 100644 Flow.Launcher/ViewModel/RelayCommand.cs delete mode 100644 Plugins/Flow.Launcher.Plugin.Explorer/ViewModels/RelayCommand.cs diff --git a/Flow.Launcher/ViewModel/RelayCommand.cs b/Flow.Launcher/ViewModel/RelayCommand.cs deleted file mode 100644 index e8d4af8b5..000000000 --- a/Flow.Launcher/ViewModel/RelayCommand.cs +++ /dev/null @@ -1,29 +0,0 @@ -using System; -using System.Windows.Input; - -namespace Flow.Launcher.ViewModel -{ - public class RelayCommand : ICommand - { - private readonly Action _action; - - public RelayCommand(Action action) - { - _action = action; - } - - public virtual bool CanExecute(object parameter) - { - return true; - } - -#pragma warning disable CS0067 // the event is never used - public event EventHandler CanExecuteChanged; -#pragma warning restore CS0067 - - public virtual void Execute(object parameter) - { - _action?.Invoke(parameter); - } - } -} diff --git a/Plugins/Flow.Launcher.Plugin.Explorer/Flow.Launcher.Plugin.Explorer.csproj b/Plugins/Flow.Launcher.Plugin.Explorer/Flow.Launcher.Plugin.Explorer.csproj index 549217027..f13460d3f 100644 --- a/Plugins/Flow.Launcher.Plugin.Explorer/Flow.Launcher.Plugin.Explorer.csproj +++ b/Plugins/Flow.Launcher.Plugin.Explorer/Flow.Launcher.Plugin.Explorer.csproj @@ -46,6 +46,7 @@ + diff --git a/Plugins/Flow.Launcher.Plugin.Explorer/ViewModels/RelayCommand.cs b/Plugins/Flow.Launcher.Plugin.Explorer/ViewModels/RelayCommand.cs deleted file mode 100644 index ff704b679..000000000 --- a/Plugins/Flow.Launcher.Plugin.Explorer/ViewModels/RelayCommand.cs +++ /dev/null @@ -1,27 +0,0 @@ -using System; -using System.Windows.Input; - -namespace Flow.Launcher.Plugin.Explorer.ViewModels -{ - internal class RelayCommand : ICommand - { - private Action _action; - - public RelayCommand(Action action) - { - _action = action; - } - - public virtual bool CanExecute(object parameter) - { - return true; - } - - public event EventHandler CanExecuteChanged; - - public virtual void Execute(object parameter) - { - _action?.Invoke(parameter); - } - } -} diff --git a/Plugins/Flow.Launcher.Plugin.Explorer/ViewModels/SettingsViewModel.cs b/Plugins/Flow.Launcher.Plugin.Explorer/ViewModels/SettingsViewModel.cs index 407300924..cf9ebd33f 100644 --- a/Plugins/Flow.Launcher.Plugin.Explorer/ViewModels/SettingsViewModel.cs +++ b/Plugins/Flow.Launcher.Plugin.Explorer/ViewModels/SettingsViewModel.cs @@ -1,9 +1,4 @@ #nullable enable -using Flow.Launcher.Plugin.Explorer.Search; -using Flow.Launcher.Plugin.Explorer.Search.Everything; -using Flow.Launcher.Plugin.Explorer.Search.Everything.Exceptions; -using Flow.Launcher.Plugin.Explorer.Search.QuickAccessLinks; -using Flow.Launcher.Plugin.Explorer.Views; using System; using System.Collections.Generic; using System.Diagnostics; @@ -13,11 +8,16 @@ using System.IO; using System.Linq; using System.Windows; using System.Windows.Forms; -using System.Windows.Input; +using CommunityToolkit.Mvvm.Input; +using Flow.Launcher.Plugin.Explorer.Search; +using Flow.Launcher.Plugin.Explorer.Search.Everything; +using Flow.Launcher.Plugin.Explorer.Search.Everything.Exceptions; +using Flow.Launcher.Plugin.Explorer.Search.QuickAccessLinks; +using Flow.Launcher.Plugin.Explorer.Views; namespace Flow.Launcher.Plugin.Explorer.ViewModels { - public class SettingsViewModel : BaseModel + public partial class SettingsViewModel : BaseModel { public Settings Settings { get; set; } @@ -36,7 +36,6 @@ namespace Flow.Launcher.Plugin.Explorer.ViewModels InitializeActionKeywordModels(); } - public void Save() { Context.API.SaveSettingJsonStorage(); @@ -48,7 +47,6 @@ namespace Flow.Launcher.Plugin.Explorer.ViewModels private EnumBindingModel _selectedContentSearchEngine; private EnumBindingModel _selectedPathEnumerationEngine; - public EnumBindingModel SelectedIndexSearchEngine { get => _selectedIndexSearchEngine; @@ -261,8 +259,7 @@ namespace Flow.Launcher.Plugin.Explorer.ViewModels public ActionKeywordModel? SelectedActionKeyword { get; set; } - public ICommand EditActionKeywordCommand => new RelayCommand(EditActionKeyword); - + [RelayCommand] private void EditActionKeyword(object obj) { if (SelectedActionKeyword is not { } actionKeyword) @@ -307,12 +304,6 @@ namespace Flow.Launcher.Plugin.Explorer.ViewModels public AccessLink? SelectedQuickAccessLink { get; set; } public AccessLink? SelectedIndexSearchExcludedPath { get; set; } - - - public ICommand RemoveLinkCommand => new RelayCommand(RemoveLink); - public ICommand EditLinkCommand => new RelayCommand(EditLink); - public ICommand AddLinkCommand => new RelayCommand(AddLink); - public void AppendLink(string containerName, AccessLink link) { var container = containerName switch @@ -324,6 +315,7 @@ namespace Flow.Launcher.Plugin.Explorer.ViewModels container.Add(link); } + [RelayCommand] private void EditLink(object commandParameter) { var (selectedLink, collection) = commandParameter switch @@ -360,7 +352,7 @@ namespace Flow.Launcher.Plugin.Explorer.ViewModels Context.API.ShowMsgBox(warning); } - + [RelayCommand] private void AddLink(object commandParameter) { var container = commandParameter switch @@ -385,6 +377,7 @@ namespace Flow.Launcher.Plugin.Explorer.ViewModels container.Add(newAccessLink); } + [RelayCommand] private void RemoveLink(object obj) { if (obj is not string container) return; @@ -435,7 +428,6 @@ namespace Flow.Launcher.Plugin.Explorer.ViewModels return path; } - internal static void OpenWindowsIndexingOptions() { var psi = new ProcessStartInfo @@ -448,39 +440,35 @@ namespace Flow.Launcher.Plugin.Explorer.ViewModels Process.Start(psi); } - private ICommand? _openFileEditorPathCommand; - - public ICommand OpenFileEditorPath => _openFileEditorPathCommand ??= new RelayCommand(_ => + [RelayCommand] + private void OpenFileEditorPath() { var path = PromptUserSelectPath(ResultType.File, Settings.EditorPath != null ? Path.GetDirectoryName(Settings.EditorPath) : null); if (path is null) return; FileEditorPath = path; - }); + } - private ICommand? _openFolderEditorPathCommand; - - public ICommand OpenFolderEditorPath => _openFolderEditorPathCommand ??= new RelayCommand(_ => + [RelayCommand] + private void OpenFolderEditorPath() { var path = PromptUserSelectPath(ResultType.File, Settings.FolderEditorPath != null ? Path.GetDirectoryName(Settings.FolderEditorPath) : null); if (path is null) return; FolderEditorPath = path; - }); + } - private ICommand? _openShellPathCommand; - - public ICommand OpenShellPath => _openShellPathCommand ??= new RelayCommand(_ => + [RelayCommand] + private void OpenShellPath() { var path = PromptUserSelectPath(ResultType.File, Settings.EditorPath != null ? Path.GetDirectoryName(Settings.EditorPath) : null); if (path is null) return; ShellPath = path; - }); - + } public string FileEditorPath { @@ -537,7 +525,6 @@ namespace Flow.Launcher.Plugin.Explorer.ViewModels } } - #region Everything FastSortWarning public Visibility FastSortWarningVisibility @@ -593,7 +580,5 @@ namespace Flow.Launcher.Plugin.Explorer.ViewModels } #endregion - - } } diff --git a/Plugins/Flow.Launcher.Plugin.Explorer/Views/ExplorerSettings.xaml b/Plugins/Flow.Launcher.Plugin.Explorer/Views/ExplorerSettings.xaml index 6ca7be84d..e5999da41 100644 --- a/Plugins/Flow.Launcher.Plugin.Explorer/Views/ExplorerSettings.xaml +++ b/Plugins/Flow.Launcher.Plugin.Explorer/Views/ExplorerSettings.xaml @@ -245,7 +245,7 @@ Margin="{StaticResource SettingPanelItemLeftMargin}" HorizontalAlignment="Left" VerticalAlignment="Center" - Command="{Binding OpenFileEditorPath}" + Command="{Binding OpenFileEditorPathCommand}" Content="{DynamicResource select}" /> @@ -272,7 +272,7 @@ Margin="{StaticResource SettingPanelItemLeftMargin}" HorizontalAlignment="Left" VerticalAlignment="Center" - Command="{Binding OpenFolderEditorPath}" + Command="{Binding OpenFolderEditorPathCommand}" Content="{DynamicResource select}" /> @@ -299,7 +299,7 @@ Margin="{StaticResource SettingPanelItemLeftMargin}" HorizontalAlignment="Left" VerticalAlignment="Center" - Command="{Binding OpenShellPath}" + Command="{Binding OpenShellPathCommand}" Content="{DynamicResource select}" />