Skip to content

Commit 01c9be3

Browse files
committed
Use Command in ViewModel to control data change (Add MVVM Toolkit to generate RelayCommand)
1 parent e67e4fd commit 01c9be3

File tree

4 files changed

+98
-90
lines changed

4 files changed

+98
-90
lines changed

Flow.Launcher/Flow.Launcher.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
</ItemGroup>
8484

8585
<ItemGroup>
86+
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.0.0" />
8687
<PackageReference Include="Fody" Version="6.5.4">
8788
<PrivateAssets>all</PrivateAssets>
8889
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>

Flow.Launcher/MainWindow.xaml

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@
3232
Visibility="{Binding MainWindowVisibility, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
3333
WindowStartupLocation="Manual"
3434
WindowStyle="None"
35-
mc:Ignorable="d">
35+
mc:Ignorable="d"
36+
Left="{Binding Left, Mode=TwoWay}"
37+
Top="{Binding Top, Mode=TwoWay}">
3638
<Window.Resources>
3739
<converters:QuerySuggestionBoxConverter x:Key="QuerySuggestionBoxConverter" />
3840
<converters:BorderClipConverter x:Key="BorderClipConverter" />
@@ -84,6 +86,22 @@
8486
Modifiers="Ctrl" />
8587
<KeyBinding Key="Right" Command="{Binding LoadContextMenuCommand}" />
8688
<KeyBinding Key="Left" Command="{Binding EscCommand}" />
89+
<KeyBinding
90+
Key="OemCloseBrackets"
91+
Modifiers="Control"
92+
Command="{Binding IncreaseWidthCommand}"/>
93+
<KeyBinding
94+
Key="OemOpenBrackets"
95+
Modifiers="Control"
96+
Command="{Binding DecreaseWidthCommand}"/>
97+
<KeyBinding
98+
Key="OemPlus"
99+
Modifiers="Control"
100+
Command="{Binding IncreaseMaxResultCommand}"/>
101+
<KeyBinding
102+
Key="OemMinus"
103+
Modifiers="Control"
104+
Command="{Binding DecreaseMaxResultCommand}"/>
87105
<KeyBinding
88106
Key="H"
89107
Command="{Binding LoadHistoryCommand}"

Flow.Launcher/MainWindow.xaml.cs

Lines changed: 10 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ public MainWindow(Settings settings, MainViewModel mainVM)
4444
_viewModel = mainVM;
4545
_settings = settings;
4646
InitializeComponent();
47-
InitializePosition();
4847
animationSound.Open(new Uri(AppDomain.CurrentDomain.BaseDirectory + "Resources\\open.wav"));
4948
}
5049

@@ -89,6 +88,7 @@ private void OnLoaded(object sender, RoutedEventArgs _)
8988
InitializeColorScheme();
9089
WindowsInteropHelper.DisableControlBox(this);
9190
InitProgressbarAnimation();
91+
InitializePosition();
9292
// since the default main window visibility is visible
9393
// so we need set focus during startup
9494
QueryTextBox.Focus();
@@ -180,20 +180,6 @@ private void OnLoaded(object sender, RoutedEventArgs _)
180180
};
181181
}
182182

183-
private void InitializePosition()
184-
{
185-
if (_settings.RememberLastLaunchLocation)
186-
{
187-
Top = _settings.WindowTop;
188-
Left = _settings.WindowLeft;
189-
}
190-
else
191-
{
192-
Left = WindowLeft();
193-
Top = WindowTop();
194-
}
195-
}
196-
197183
private void UpdateNotifyIconText()
198184
{
199185
var menu = contextMenu;
@@ -454,6 +440,15 @@ public void HideStartup()
454440
}
455441
}
456442

443+
private void InitializePosition()
444+
{
445+
if (!_settings.RememberLastLaunchLocation)
446+
{
447+
Left = WindowLeft();
448+
Top = WindowTop();
449+
}
450+
}
451+
457452
public double WindowLeft()
458453
{
459454
var screen = Screen.FromPoint(System.Windows.Forms.Cursor.Position);
@@ -505,68 +500,13 @@ private void OnKeyDown(object sender, KeyEventArgs e)
505500
_viewModel.LoadContextMenuCommand.Execute(null);
506501
e.Handled = true;
507502
}
508-
if (specialKeyState.CtrlPressed)
509-
{
510-
511-
_settings.WindowSize = _settings.WindowSize + 100;
512-
Left = Left - 50;
513-
}
514503
break;
515504
case Key.Left:
516505
if (!_viewModel.SelectedIsFromQueryResults() && QueryTextBox.CaretIndex == 0)
517506
{
518507
_viewModel.EscCommand.Execute(null);
519508
e.Handled = true;
520509
}
521-
if (specialKeyState.CtrlPressed)
522-
{
523-
if (_settings.WindowSize < 400)
524-
{
525-
}
526-
else
527-
{
528-
_settings.WindowSize = _settings.WindowSize - 100;
529-
Left = Left + 50;
530-
}
531-
}
532-
break;
533-
case Key.OemOpenBrackets:
534-
if (specialKeyState.CtrlPressed)
535-
{
536-
if (_settings.WindowSize < 400)
537-
{
538-
}
539-
else
540-
{
541-
_settings.WindowSize = _settings.WindowSize - 100;
542-
Left = Left + 50;
543-
}
544-
}
545-
break;
546-
case Key.OemCloseBrackets:
547-
if (specialKeyState.CtrlPressed)
548-
{
549-
_settings.WindowSize = _settings.WindowSize + 100;
550-
Left = Left - 50;
551-
}
552-
break;
553-
case Key.OemMinus:
554-
if (specialKeyState.CtrlPressed)
555-
{
556-
if (_settings.MaxResultsToShow < 2)
557-
{
558-
}
559-
else
560-
{
561-
_settings.MaxResultsToShow = _settings.MaxResultsToShow - 1;
562-
}
563-
}
564-
break;
565-
case Key.OemPlus:
566-
if (specialKeyState.CtrlPressed)
567-
{
568-
_settings.MaxResultsToShow = _settings.MaxResultsToShow + 1;
569-
}
570510
break;
571511
case Key.F12:
572512
if (specialKeyState.CtrlPressed)

Flow.Launcher/ViewModel/MainViewModel.cs

Lines changed: 68 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@
2121
using ISavable = Flow.Launcher.Plugin.ISavable;
2222
using System.IO;
2323
using System.Collections.Specialized;
24+
using CommunityToolkit.Mvvm.Input;
2425

2526
namespace Flow.Launcher.ViewModel
2627
{
27-
public class MainViewModel : BaseModel, ISavable
28+
public partial class MainViewModel : BaseModel, ISavable
2829
{
2930
#region Private Fields
3031

@@ -61,13 +62,6 @@ public MainViewModel(Settings settings)
6162
_lastQuery = new Query();
6263

6364
_settings = settings;
64-
_settings.PropertyChanged += (_, args) =>
65-
{
66-
if (args.PropertyName == nameof(Settings.WindowSize))
67-
{
68-
OnPropertyChanged(nameof(MainWindowWidth));
69-
}
70-
};
7165

7266
_historyItemsStorage = new FlowLauncherJsonStorage<History>();
7367
_userSelectedRecordStorage = new FlowLauncherJsonStorage<UserSelectedRecord>();
@@ -82,6 +76,7 @@ public MainViewModel(Settings settings)
8276
_selectedResults = Results;
8377

8478
InitializeKeyCommands();
79+
8580
RegisterViewUpdate();
8681
RegisterResultsUpdatedEvent();
8782

@@ -154,6 +149,8 @@ private void RegisterResultsUpdatedEvent()
154149
}
155150
}
156151

152+
153+
157154
private void InitializeKeyCommands()
158155
{
159156
EscCommand = new RelayCommand(_ =>
@@ -307,7 +304,7 @@ private void InitializeKeyCommands()
307304
Notification.Show(
308305
InternationalizationManager.Instance.GetTranslation("success"),
309306
InternationalizationManager.Instance.GetTranslation("completedSuccessfully")
310-
);
307+
);
311308
}), TaskScheduler.Default)
312309
.ConfigureAwait(false);
313310
});
@@ -318,9 +315,9 @@ private void InitializeKeyCommands()
318315
#region ViewModel Properties
319316

320317
public ResultsViewModel Results { get; private set; }
321-
318+
322319
public ResultsViewModel ContextMenu { get; private set; }
323-
320+
324321
public ResultsViewModel History { get; private set; }
325322

326323
public bool GameModeStatus { get; set; }
@@ -336,6 +333,54 @@ public string QueryText
336333
}
337334
}
338335

336+
337+
public double Top
338+
{
339+
get => _settings.WindowTop;
340+
set
341+
{
342+
_settings.WindowTop = value;
343+
OnPropertyChanged();
344+
}
345+
}
346+
public double Left
347+
{
348+
get => _settings.WindowLeft;
349+
set
350+
{
351+
_settings.WindowLeft = value;
352+
OnPropertyChanged();
353+
}
354+
}
355+
356+
[RelayCommand]
357+
private void IncreaseWidth()
358+
{
359+
MainWindowWidth += 100;
360+
Left -= 50;
361+
}
362+
363+
[RelayCommand]
364+
private void DecreaseWidth()
365+
{
366+
if (MainWindowWidth < 400) return;
367+
Left += 50;
368+
MainWindowWidth -= 100;
369+
}
370+
371+
[RelayCommand]
372+
private void IncreaseMaxResult()
373+
{
374+
_settings.MaxResultsToShow += 1;
375+
}
376+
377+
[RelayCommand]
378+
private void DecreaseMaxResult()
379+
{
380+
if (_settings.MaxResultsToShow < 2) return;
381+
_settings.MaxResultsToShow -= 1;
382+
}
383+
339384
/// <summary>
340385
/// we need move cursor to end when we manually changed query
341386
/// but we don't want to move cursor to end when query is updated from TextBox
@@ -411,7 +456,11 @@ private ResultsViewModel SelectedResults
411456

412457
public Visibility SearchIconVisibility { get; set; }
413458

414-
public double MainWindowWidth => _settings.WindowSize;
459+
public double MainWindowWidth
460+
{
461+
get => _settings.WindowSize;
462+
set => _settings.WindowSize = value;
463+
}
415464

416465
public string PluginIconPath { get; set; } = null;
417466

@@ -592,7 +641,7 @@ private async void QueryResults()
592641
PluginIconPath = null;
593642
SearchIconVisibility = Visibility.Visible;
594643
}
595-
644+
596645

597646
if (query.ActionKeyword == Plugin.Query.GlobalPluginWildcardSign)
598647
{
@@ -903,18 +952,18 @@ public void ResultCopy(string stringToCopy)
903952

904953
Clipboard.SetFileDropList(paths);
905954
App.API.ShowMsg(
906-
App.API.GetTranslation("copy")
907-
+" "
908-
+ (isFile? App.API.GetTranslation("fileTitle") : App.API.GetTranslation("folderTitle")),
955+
App.API.GetTranslation("copy")
956+
+ " "
957+
+ (isFile ? App.API.GetTranslation("fileTitle") : App.API.GetTranslation("folderTitle")),
909958
App.API.GetTranslation("completedSuccessfully"));
910959
}
911960
else
912961
{
913962
Clipboard.SetDataObject(copyText.ToString());
914963
App.API.ShowMsg(
915-
App.API.GetTranslation("copy")
916-
+ " "
917-
+ App.API.GetTranslation("textTitle"),
964+
App.API.GetTranslation("copy")
965+
+ " "
966+
+ App.API.GetTranslation("textTitle"),
918967
App.API.GetTranslation("completedSuccessfully"));
919968
}
920969
}

0 commit comments

Comments
 (0)