[Run] ViewModels Cleanup (#12209)

* view models cleanup

* removed unused commands
This commit is contained in:
Davide Giacometti
2021-07-15 16:34:27 +02:00
committed by GitHub
parent cdfb566137
commit 55923f2790
7 changed files with 57 additions and 193 deletions

View File

@@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation // Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license. // The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information. // See the LICENSE file in the project root for more information.
@@ -13,52 +13,13 @@ namespace PowerLauncher.ViewModel
{ {
private ICommand _command; private ICommand _command;
public string PluginName { get; set; } public string PluginName { get; }
private string _title; public string Title { get; }
public string Title public string Glyph { get; }
{
get => _title;
set
{
if (_title != value)
{
_title = value;
OnPropertyChanged(nameof(Title));
}
}
}
private string _glyph; public string FontFamily { get; }
public string Glyph
{
get => _glyph;
set
{
if (_glyph != value)
{
_glyph = value;
OnPropertyChanged(nameof(Glyph));
}
}
}
private string _fontFamily;
public string FontFamily
{
get => _fontFamily;
set
{
if (_fontFamily != value)
{
_fontFamily = value;
OnPropertyChanged(nameof(FontFamily));
}
}
}
public ICommand Command public ICommand Command
{ {
@@ -78,12 +39,23 @@ namespace PowerLauncher.ViewModel
} }
} }
public Key AcceleratorKey { get; set; } public Key AcceleratorKey { get; }
public ModifierKeys AcceleratorModifiers { get; set; } public ModifierKeys AcceleratorModifiers { get; }
public bool IsAcceleratorKeyEnabled { get; set; } public bool IsAcceleratorKeyEnabled { get; set; }
public ContextMenuItemViewModel(string pluginName, string title, string glyph, string fontFamily, Key acceleratorKey, ModifierKeys acceleratorModifiers, ICommand command)
{
PluginName = pluginName;
Title = title;
Glyph = glyph;
FontFamily = fontFamily;
Command = command;
AcceleratorKey = acceleratorKey;
AcceleratorModifiers = acceleratorModifiers;
}
public void SendTelemetryEvent(LauncherResultActionEvent.TriggerType triggerType) public void SendTelemetryEvent(LauncherResultActionEvent.TriggerType triggerType)
{ {
var eventData = new LauncherResultActionEvent() var eventData = new LauncherResultActionEvent()

View File

@@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation // Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license. // The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information. // See the LICENSE file in the project root for more information.
@@ -11,7 +11,6 @@ using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows; using System.Windows;
using System.Windows.Input; using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Threading; using System.Windows.Threading;
using interop; using interop;
using Microsoft.PowerLauncher.Telemetry; using Microsoft.PowerLauncher.Telemetry;
@@ -51,7 +50,7 @@ namespace PowerLauncher.ViewModel
private bool _saved; private bool _saved;
private ushort _hotkeyHandle; private ushort _hotkeyHandle;
internal HotkeyManager HotkeyManager { get; set; } internal HotkeyManager HotkeyManager { get; private set; }
public MainViewModel(PowerToysRunSettings settings) public MainViewModel(PowerToysRunSettings settings)
{ {
@@ -257,8 +256,6 @@ namespace PowerLauncher.ViewModel
SelectedResults.SelectPrevPage(); SelectedResults.SelectPrevPage();
}); });
SelectFirstResultCommand = new RelayCommand(_ => SelectedResults.SelectFirstResult());
OpenResultWithKeyboardCommand = new RelayCommand(index => OpenResultWithKeyboardCommand = new RelayCommand(index =>
{ {
OpenResultsEvent(index, false); OpenResultsEvent(index, false);
@@ -269,31 +266,6 @@ namespace PowerLauncher.ViewModel
OpenResultsEvent(index, true); OpenResultsEvent(index, true);
}); });
LoadContextMenuCommand = new RelayCommand(_ =>
{
if (SelectedIsFromQueryResults())
{
SelectedResults = ContextMenu;
}
else
{
SelectedResults = Results;
}
});
LoadHistoryCommand = new RelayCommand(_ =>
{
if (SelectedIsFromQueryResults())
{
SelectedResults = History;
History.SelectedIndex = _history.Items.Count - 1;
}
else
{
SelectedResults = Results;
}
});
ClearQueryCommand = new RelayCommand(_ => ClearQueryCommand = new RelayCommand(_ =>
{ {
if (!string.IsNullOrEmpty(QueryText)) if (!string.IsNullOrEmpty(QueryText))
@@ -306,10 +278,6 @@ namespace PowerLauncher.ViewModel
}); });
} }
public Brush MainWindowBackground { get; set; }
public Brush MainWindowBorderBrush { get; set; }
private ResultsViewModel _results; private ResultsViewModel _results;
public ResultsViewModel Results public ResultsViewModel Results
@@ -402,27 +370,12 @@ namespace PowerLauncher.ViewModel
{ {
Results.Visibility = Visibility.Hidden; Results.Visibility = Visibility.Hidden;
_queryTextBeforeLeaveResults = QueryText; _queryTextBeforeLeaveResults = QueryText;
// Because of Fody's optimization
// setter won't be called when property value is not changed.
// so we need manually call Query()
// http://stackoverflow.com/posts/25895769/revisions
if (string.IsNullOrEmpty(QueryText))
{
Query();
}
else
{
QueryText = string.Empty;
}
} }
_selectedResults.Visibility = Visibility.Visible; _selectedResults.Visibility = Visibility.Visible;
} }
} }
public Visibility ProgressBarVisibility { get; set; }
private Visibility _visibility; private Visibility _visibility;
public Visibility MainWindowVisibility public Visibility MainWindowVisibility
@@ -451,37 +404,31 @@ namespace PowerLauncher.ViewModel
} }
} }
public ICommand IgnoreCommand { get; set; } public ICommand IgnoreCommand { get; private set; }
public ICommand EscCommand { get; set; } public ICommand EscCommand { get; private set; }
public ICommand SelectNextItemCommand { get; set; } public ICommand SelectNextItemCommand { get; private set; }
public ICommand SelectPrevItemCommand { get; set; } public ICommand SelectPrevItemCommand { get; private set; }
public ICommand SelectNextContextMenuItemCommand { get; set; } public ICommand SelectNextContextMenuItemCommand { get; private set; }
public ICommand SelectPreviousContextMenuItemCommand { get; set; } public ICommand SelectPreviousContextMenuItemCommand { get; private set; }
public ICommand SelectNextTabItemCommand { get; set; } public ICommand SelectNextTabItemCommand { get; private set; }
public ICommand SelectPrevTabItemCommand { get; set; } public ICommand SelectPrevTabItemCommand { get; private set; }
public ICommand SelectNextPageCommand { get; set; } public ICommand SelectNextPageCommand { get; private set; }
public ICommand SelectPrevPageCommand { get; set; } public ICommand SelectPrevPageCommand { get; private set; }
public ICommand SelectFirstResultCommand { get; set; } public ICommand OpenResultWithKeyboardCommand { get; private set; }
public ICommand LoadContextMenuCommand { get; set; } public ICommand OpenResultWithMouseCommand { get; private set; }
public ICommand LoadHistoryCommand { get; set; } public ICommand ClearQueryCommand { get; private set; }
public ICommand OpenResultWithKeyboardCommand { get; set; }
public ICommand OpenResultWithMouseCommand { get; set; }
public ICommand ClearQueryCommand { get; set; }
public void Query() public void Query()
{ {

View File

@@ -24,17 +24,13 @@ namespace PowerLauncher.ViewModel
public ObservableCollection<ContextMenuItemViewModel> ContextMenuItems { get; } = new ObservableCollection<ContextMenuItemViewModel>(); public ObservableCollection<ContextMenuItemViewModel> ContextMenuItems { get; } = new ObservableCollection<ContextMenuItemViewModel>();
public ICommand ActivateContextButtonsHoverCommand { get; set; } public ICommand ActivateContextButtonsHoverCommand { get; }
public ICommand ActivateContextButtonsSelectionCommand { get; set; } public ICommand DeactivateContextButtonsHoverCommand { get; }
public ICommand DeactivateContextButtonsHoverCommand { get; set; } public bool IsSelected { get; private set; }
public ICommand DeactivateContextButtonsSelectionCommand { get; set; } public bool IsHovered { get; private set; }
public bool IsSelected { get; set; }
public bool IsHovered { get; set; }
private bool _areContextButtonsActive; private bool _areContextButtonsActive;
@@ -79,9 +75,7 @@ namespace PowerLauncher.ViewModel
LoadContextMenu(); LoadContextMenu();
ActivateContextButtonsHoverCommand = new RelayCommand(ActivateContextButtonsHoverAction); ActivateContextButtonsHoverCommand = new RelayCommand(ActivateContextButtonsHoverAction);
ActivateContextButtonsSelectionCommand = new RelayCommand(ActivateContextButtonsSelectionAction);
DeactivateContextButtonsHoverCommand = new RelayCommand(DeactivateContextButtonsHoverAction); DeactivateContextButtonsHoverCommand = new RelayCommand(DeactivateContextButtonsHoverAction);
DeactivateContextButtonsSelectionCommand = new RelayCommand(DeactivateContextButtonsSelectionAction);
} }
private void ActivateContextButtonsHoverAction(object sender) private void ActivateContextButtonsHoverAction(object sender)
@@ -89,11 +83,6 @@ namespace PowerLauncher.ViewModel
ActivateContextButtons(ActivationType.Hover); ActivateContextButtons(ActivationType.Hover);
} }
private void ActivateContextButtonsSelectionAction(object sender)
{
ActivateContextButtons(ActivationType.Selection);
}
public void ActivateContextButtons(ActivationType activationType) public void ActivateContextButtons(ActivationType activationType)
{ {
// Result does not contain any context menu items - we don't need to show the context menu ListView at all. // Result does not contain any context menu items - we don't need to show the context menu ListView at all.
@@ -122,11 +111,6 @@ namespace PowerLauncher.ViewModel
DeactivateContextButtons(ActivationType.Hover); DeactivateContextButtons(ActivationType.Hover);
} }
private void DeactivateContextButtonsSelectionAction(object sender)
{
DeactivateContextButtons(ActivationType.Selection);
}
public void DeactivateContextButtons(ActivationType activationType) public void DeactivateContextButtons(ActivationType activationType)
{ {
if (activationType == ActivationType.Selection) if (activationType == ActivationType.Selection)
@@ -156,15 +140,14 @@ namespace PowerLauncher.ViewModel
ContextMenuItems.Clear(); ContextMenuItems.Clear();
foreach (var r in results) foreach (var r in results)
{ {
ContextMenuItems.Add(new ContextMenuItemViewModel() ContextMenuItems.Add(new ContextMenuItemViewModel(
{ r.PluginName,
PluginName = r.PluginName, r.Title,
Title = r.Title, r.Glyph,
Glyph = r.Glyph, r.FontFamily,
FontFamily = r.FontFamily, r.AcceleratorKey,
AcceleratorKey = r.AcceleratorKey, r.AcceleratorModifiers,
AcceleratorModifiers = r.AcceleratorModifiers, new RelayCommand(_ =>
Command = new RelayCommand(_ =>
{ {
bool hideWindow = bool hideWindow =
r.Action != null && r.Action != null &&
@@ -179,8 +162,7 @@ namespace PowerLauncher.ViewModel
// TODO - Do we hide the window // TODO - Do we hide the window
// MainWindowVisibility = Visibility.Collapsed; // MainWindowVisibility = Visibility.Collapsed;
} }
}), })));
});
} }
} }

View File

@@ -95,8 +95,6 @@ namespace PowerLauncher.ViewModel
} }
} }
public Thickness Margin { get; set; }
private Visibility _visibility = Visibility.Hidden; private Visibility _visibility = Visibility.Hidden;
public Visibility Visibility public Visibility Visibility

View File

@@ -2,7 +2,6 @@
// The Microsoft Corporation licenses this file to you under the MIT license. // The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information. // See the LICENSE file in the project root for more information.
using System.Globalization;
using Wox.Infrastructure.Storage; using Wox.Infrastructure.Storage;
using Wox.Infrastructure.UserSettings; using Wox.Infrastructure.UserSettings;
using Wox.Plugin; using Wox.Plugin;
@@ -19,7 +18,7 @@ namespace PowerLauncher.ViewModel
Settings = _storage.Load(); Settings = _storage.Load();
} }
public PowerToysRunSettings Settings { get; set; } public PowerToysRunSettings Settings { get; }
public void Save() public void Save()
{ {

View File

@@ -62,14 +62,6 @@
<None Include="README.md" /> <None Include="README.md" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<None Remove="FodyWeavers.xml" />
</ItemGroup>
<ItemGroup>
<Content Include="FodyWeavers.xml" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="JetBrains.Annotations" Version="2020.1.0" /> <PackageReference Include="JetBrains.Annotations" Version="2020.1.0" />
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="3.3.0"> <PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="3.3.0">

View File

@@ -2,6 +2,7 @@
// The Microsoft Corporation licenses this file to you under the MIT license. // The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information. // See the LICENSE file in the project root for more information.
using System.Windows.Input;
using NUnit.Framework; using NUnit.Framework;
using PowerLauncher.ViewModel; using PowerLauncher.ViewModel;
using Wox.Plugin; using Wox.Plugin;
@@ -18,10 +19,7 @@ namespace Wox.Test
ResultsViewModel rvm = new ResultsViewModel(); ResultsViewModel rvm = new ResultsViewModel();
Result result = new Result(); Result result = new Result();
ResultViewModel selectedItem = new ResultViewModel(result); ResultViewModel selectedItem = new ResultViewModel(result);
selectedItem.ContextMenuItems.Add(new ContextMenuItemViewModel() selectedItem.ContextMenuItems.Add(new ContextMenuItemViewModel(null, null, null, null, Key.None, ModifierKeys.None, null));
{
Title = "Dummy Context Menu",
});
rvm.SelectedItem = selectedItem; rvm.SelectedItem = selectedItem;
// Assert // Assert
@@ -35,10 +33,7 @@ namespace Wox.Test
ResultsViewModel rvm = new ResultsViewModel(); ResultsViewModel rvm = new ResultsViewModel();
Result result = new Result(); Result result = new Result();
ResultViewModel selectedItem = new ResultViewModel(result); ResultViewModel selectedItem = new ResultViewModel(result);
selectedItem.ContextMenuItems.Add(new ContextMenuItemViewModel() selectedItem.ContextMenuItems.Add(new ContextMenuItemViewModel(null, null, null, null, Key.None, ModifierKeys.None, null));
{
Title = "Dummy Context Menu",
});
rvm.SelectedItem = selectedItem; rvm.SelectedItem = selectedItem;
// Act // Act
@@ -55,10 +50,7 @@ namespace Wox.Test
ResultsViewModel rvm = new ResultsViewModel(); ResultsViewModel rvm = new ResultsViewModel();
Result result = new Result(); Result result = new Result();
ResultViewModel selectedItem = new ResultViewModel(result); ResultViewModel selectedItem = new ResultViewModel(result);
selectedItem.ContextMenuItems.Add(new ContextMenuItemViewModel() selectedItem.ContextMenuItems.Add(new ContextMenuItemViewModel(null, null, null, null, Key.None, ModifierKeys.None, null));
{
Title = "Dummy Context Menu",
});
rvm.SelectedItem = selectedItem; rvm.SelectedItem = selectedItem;
// Act // Act
@@ -75,18 +67,9 @@ namespace Wox.Test
ResultsViewModel rvm = new ResultsViewModel(); ResultsViewModel rvm = new ResultsViewModel();
Result result = new Result(); Result result = new Result();
ResultViewModel selectedItem = new ResultViewModel(result); ResultViewModel selectedItem = new ResultViewModel(result);
selectedItem.ContextMenuItems.Add(new ContextMenuItemViewModel() selectedItem.ContextMenuItems.Add(new ContextMenuItemViewModel(null, null, null, null, Key.None, ModifierKeys.None, null));
{ selectedItem.ContextMenuItems.Add(new ContextMenuItemViewModel(null, null, null, null, Key.None, ModifierKeys.None, null));
Title = "Dummy Context Menu 1", selectedItem.ContextMenuItems.Add(new ContextMenuItemViewModel(null, null, null, null, Key.None, ModifierKeys.None, null));
});
selectedItem.ContextMenuItems.Add(new ContextMenuItemViewModel()
{
Title = "Dummy Context Menu 2",
});
selectedItem.ContextMenuItems.Add(new ContextMenuItemViewModel()
{
Title = "Dummy Context Menu 3",
});
rvm.SelectedItem = selectedItem; rvm.SelectedItem = selectedItem;
// Act // Act
@@ -106,10 +89,7 @@ namespace Wox.Test
ResultsViewModel rvm = new ResultsViewModel(); ResultsViewModel rvm = new ResultsViewModel();
Result result = new Result(); Result result = new Result();
ResultViewModel selectedItem = new ResultViewModel(result); ResultViewModel selectedItem = new ResultViewModel(result);
selectedItem.ContextMenuItems.Add(new ContextMenuItemViewModel() selectedItem.ContextMenuItems.Add(new ContextMenuItemViewModel(null, null, null, null, Key.None, ModifierKeys.None, null));
{
Title = "Dummy Context Menu",
});
rvm.SelectedItem = selectedItem; rvm.SelectedItem = selectedItem;
// Act // Act
@@ -127,10 +107,7 @@ namespace Wox.Test
ResultsViewModel rvm = new ResultsViewModel(); ResultsViewModel rvm = new ResultsViewModel();
Result result = new Result(); Result result = new Result();
ResultViewModel selectedItem = new ResultViewModel(result); ResultViewModel selectedItem = new ResultViewModel(result);
selectedItem.ContextMenuItems.Add(new ContextMenuItemViewModel() selectedItem.ContextMenuItems.Add(new ContextMenuItemViewModel(null, null, null, null, Key.None, ModifierKeys.None, null));
{
Title = "Dummy Context Menu",
});
rvm.SelectedItem = selectedItem; rvm.SelectedItem = selectedItem;
// Act // Act
@@ -148,10 +125,7 @@ namespace Wox.Test
ResultsViewModel rvm = new ResultsViewModel(); ResultsViewModel rvm = new ResultsViewModel();
Result result = new Result(); Result result = new Result();
ResultViewModel selectedItem = new ResultViewModel(result); ResultViewModel selectedItem = new ResultViewModel(result);
selectedItem.ContextMenuItems.Add(new ContextMenuItemViewModel() selectedItem.ContextMenuItems.Add(new ContextMenuItemViewModel(null, null, null, null, Key.None, ModifierKeys.None, null));
{
Title = "Dummy Context Menu",
});
rvm.SelectedItem = selectedItem; rvm.SelectedItem = selectedItem;
// Act // Act