From 01c9be37bf795ad7087c6b649ee3b53c3529a4f1 Mon Sep 17 00:00:00 2001 From: Hongtao Zhang Date: Mon, 19 Sep 2022 15:32:17 -0500 Subject: [PATCH] Use Command in ViewModel to control data change (Add MVVM Toolkit to generate RelayCommand) --- Flow.Launcher/Flow.Launcher.csproj | 1 + Flow.Launcher/MainWindow.xaml | 20 +++++- Flow.Launcher/MainWindow.xaml.cs | 80 +++------------------- Flow.Launcher/ViewModel/MainViewModel.cs | 87 ++++++++++++++++++------ 4 files changed, 98 insertions(+), 90 deletions(-) diff --git a/Flow.Launcher/Flow.Launcher.csproj b/Flow.Launcher/Flow.Launcher.csproj index 66cc911ee..0db1915c5 100644 --- a/Flow.Launcher/Flow.Launcher.csproj +++ b/Flow.Launcher/Flow.Launcher.csproj @@ -83,6 +83,7 @@ + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/Flow.Launcher/MainWindow.xaml b/Flow.Launcher/MainWindow.xaml index 714fcc53f..7730c5658 100644 --- a/Flow.Launcher/MainWindow.xaml +++ b/Flow.Launcher/MainWindow.xaml @@ -32,7 +32,9 @@ Visibility="{Binding MainWindowVisibility, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" WindowStartupLocation="Manual" WindowStyle="None" - mc:Ignorable="d"> + mc:Ignorable="d" + Left="{Binding Left, Mode=TwoWay}" + Top="{Binding Top, Mode=TwoWay}"> @@ -84,6 +86,22 @@ Modifiers="Ctrl" /> + + + + - { - if (args.PropertyName == nameof(Settings.WindowSize)) - { - OnPropertyChanged(nameof(MainWindowWidth)); - } - }; _historyItemsStorage = new FlowLauncherJsonStorage(); _userSelectedRecordStorage = new FlowLauncherJsonStorage(); @@ -82,6 +76,7 @@ namespace Flow.Launcher.ViewModel _selectedResults = Results; InitializeKeyCommands(); + RegisterViewUpdate(); RegisterResultsUpdatedEvent(); @@ -154,6 +149,8 @@ namespace Flow.Launcher.ViewModel } } + + private void InitializeKeyCommands() { EscCommand = new RelayCommand(_ => @@ -307,7 +304,7 @@ namespace Flow.Launcher.ViewModel Notification.Show( InternationalizationManager.Instance.GetTranslation("success"), InternationalizationManager.Instance.GetTranslation("completedSuccessfully") - ); + ); }), TaskScheduler.Default) .ConfigureAwait(false); }); @@ -318,9 +315,9 @@ namespace Flow.Launcher.ViewModel #region ViewModel Properties public ResultsViewModel Results { get; private set; } - + public ResultsViewModel ContextMenu { get; private set; } - + public ResultsViewModel History { get; private set; } public bool GameModeStatus { get; set; } @@ -336,6 +333,54 @@ namespace Flow.Launcher.ViewModel } } + + public double Top + { + get => _settings.WindowTop; + set + { + _settings.WindowTop = value; + OnPropertyChanged(); + } + } + public double Left + { + get => _settings.WindowLeft; + set + { + _settings.WindowLeft = value; + OnPropertyChanged(); + } + } + + [RelayCommand] + private void IncreaseWidth() + { + MainWindowWidth += 100; + Left -= 50; + } + + [RelayCommand] + private void DecreaseWidth() + { + if (MainWindowWidth < 400) return; + Left += 50; + MainWindowWidth -= 100; + } + + [RelayCommand] + private void IncreaseMaxResult() + { + _settings.MaxResultsToShow += 1; + } + + [RelayCommand] + private void DecreaseMaxResult() + { + if (_settings.MaxResultsToShow < 2) return; + _settings.MaxResultsToShow -= 1; + } + /// /// we need move cursor to end when we manually changed query /// but we don't want to move cursor to end when query is updated from TextBox @@ -411,7 +456,11 @@ namespace Flow.Launcher.ViewModel public Visibility SearchIconVisibility { get; set; } - public double MainWindowWidth => _settings.WindowSize; + public double MainWindowWidth + { + get => _settings.WindowSize; + set => _settings.WindowSize = value; + } public string PluginIconPath { get; set; } = null; @@ -592,7 +641,7 @@ namespace Flow.Launcher.ViewModel PluginIconPath = null; SearchIconVisibility = Visibility.Visible; } - + if (query.ActionKeyword == Plugin.Query.GlobalPluginWildcardSign) { @@ -903,18 +952,18 @@ namespace Flow.Launcher.ViewModel Clipboard.SetFileDropList(paths); App.API.ShowMsg( - App.API.GetTranslation("copy") - +" " - + (isFile? App.API.GetTranslation("fileTitle") : App.API.GetTranslation("folderTitle")), + App.API.GetTranslation("copy") + + " " + + (isFile ? App.API.GetTranslation("fileTitle") : App.API.GetTranslation("folderTitle")), App.API.GetTranslation("completedSuccessfully")); } else { Clipboard.SetDataObject(copyText.ToString()); App.API.ShowMsg( - App.API.GetTranslation("copy") - + " " - + App.API.GetTranslation("textTitle"), + App.API.GetTranslation("copy") + + " " + + App.API.GetTranslation("textTitle"), App.API.GetTranslation("completedSuccessfully")); } }