Use Command in ViewModel to control data change (Add MVVM Toolkit to generate RelayCommand)

This commit is contained in:
Hongtao Zhang 2022-09-19 15:32:17 -05:00
parent e67e4fd99e
commit 01c9be37bf
No known key found for this signature in database
GPG key ID: 75F655B91C7AC9BB
4 changed files with 98 additions and 90 deletions

View file

@ -83,6 +83,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.0.0" />
<PackageReference Include="Fody" Version="6.5.4">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>

View file

@ -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}">
<Window.Resources>
<converters:QuerySuggestionBoxConverter x:Key="QuerySuggestionBoxConverter" />
<converters:BorderClipConverter x:Key="BorderClipConverter" />
@ -84,6 +86,22 @@
Modifiers="Ctrl" />
<KeyBinding Key="Right" Command="{Binding LoadContextMenuCommand}" />
<KeyBinding Key="Left" Command="{Binding EscCommand}" />
<KeyBinding
Key="OemCloseBrackets"
Modifiers="Control"
Command="{Binding IncreaseWidthCommand}"/>
<KeyBinding
Key="OemOpenBrackets"
Modifiers="Control"
Command="{Binding DecreaseWidthCommand}"/>
<KeyBinding
Key="OemPlus"
Modifiers="Control"
Command="{Binding IncreaseMaxResultCommand}"/>
<KeyBinding
Key="OemMinus"
Modifiers="Control"
Command="{Binding DecreaseMaxResultCommand}"/>
<KeyBinding
Key="H"
Command="{Binding LoadHistoryCommand}"

View file

@ -44,7 +44,6 @@ namespace Flow.Launcher
_viewModel = mainVM;
_settings = settings;
InitializeComponent();
InitializePosition();
animationSound.Open(new Uri(AppDomain.CurrentDomain.BaseDirectory + "Resources\\open.wav"));
}
@ -89,6 +88,7 @@ namespace Flow.Launcher
InitializeColorScheme();
WindowsInteropHelper.DisableControlBox(this);
InitProgressbarAnimation();
InitializePosition();
// since the default main window visibility is visible
// so we need set focus during startup
QueryTextBox.Focus();
@ -180,20 +180,6 @@ namespace Flow.Launcher
};
}
private void InitializePosition()
{
if (_settings.RememberLastLaunchLocation)
{
Top = _settings.WindowTop;
Left = _settings.WindowLeft;
}
else
{
Left = WindowLeft();
Top = WindowTop();
}
}
private void UpdateNotifyIconText()
{
var menu = contextMenu;
@ -454,6 +440,15 @@ namespace Flow.Launcher
}
}
private void InitializePosition()
{
if (!_settings.RememberLastLaunchLocation)
{
Left = WindowLeft();
Top = WindowTop();
}
}
public double WindowLeft()
{
var screen = Screen.FromPoint(System.Windows.Forms.Cursor.Position);
@ -505,12 +500,6 @@ namespace Flow.Launcher
_viewModel.LoadContextMenuCommand.Execute(null);
e.Handled = true;
}
if (specialKeyState.CtrlPressed)
{
_settings.WindowSize = _settings.WindowSize + 100;
Left = Left - 50;
}
break;
case Key.Left:
if (!_viewModel.SelectedIsFromQueryResults() && QueryTextBox.CaretIndex == 0)
@ -518,55 +507,6 @@ namespace Flow.Launcher
_viewModel.EscCommand.Execute(null);
e.Handled = true;
}
if (specialKeyState.CtrlPressed)
{
if (_settings.WindowSize < 400)
{
}
else
{
_settings.WindowSize = _settings.WindowSize - 100;
Left = Left + 50;
}
}
break;
case Key.OemOpenBrackets:
if (specialKeyState.CtrlPressed)
{
if (_settings.WindowSize < 400)
{
}
else
{
_settings.WindowSize = _settings.WindowSize - 100;
Left = Left + 50;
}
}
break;
case Key.OemCloseBrackets:
if (specialKeyState.CtrlPressed)
{
_settings.WindowSize = _settings.WindowSize + 100;
Left = Left - 50;
}
break;
case Key.OemMinus:
if (specialKeyState.CtrlPressed)
{
if (_settings.MaxResultsToShow < 2)
{
}
else
{
_settings.MaxResultsToShow = _settings.MaxResultsToShow - 1;
}
}
break;
case Key.OemPlus:
if (specialKeyState.CtrlPressed)
{
_settings.MaxResultsToShow = _settings.MaxResultsToShow + 1;
}
break;
case Key.F12:
if (specialKeyState.CtrlPressed)

View file

@ -21,10 +21,11 @@ using System.Threading.Channels;
using ISavable = Flow.Launcher.Plugin.ISavable;
using System.IO;
using System.Collections.Specialized;
using CommunityToolkit.Mvvm.Input;
namespace Flow.Launcher.ViewModel
{
public class MainViewModel : BaseModel, ISavable
public partial class MainViewModel : BaseModel, ISavable
{
#region Private Fields
@ -61,13 +62,6 @@ namespace Flow.Launcher.ViewModel
_lastQuery = new Query();
_settings = settings;
_settings.PropertyChanged += (_, args) =>
{
if (args.PropertyName == nameof(Settings.WindowSize))
{
OnPropertyChanged(nameof(MainWindowWidth));
}
};
_historyItemsStorage = new FlowLauncherJsonStorage<History>();
_userSelectedRecordStorage = new FlowLauncherJsonStorage<UserSelectedRecord>();
@ -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;
}
/// <summary>
/// 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"));
}
}