[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
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
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.
// See the LICENSE file in the project root for more information.
@ -13,52 +13,13 @@ namespace PowerLauncher.ViewModel
{
private ICommand _command;
public string PluginName { get; set; }
public string PluginName { get; }
private string _title;
public string Title { get; }
public string Title
{
get => _title;
set
{
if (_title != value)
{
_title = value;
OnPropertyChanged(nameof(Title));
}
}
}
public string Glyph { get; }
private string _glyph;
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 string FontFamily { get; }
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 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)
{
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.
// See the LICENSE file in the project root for more information.
@ -11,7 +11,6 @@ using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Threading;
using interop;
using Microsoft.PowerLauncher.Telemetry;
@ -51,7 +50,7 @@ namespace PowerLauncher.ViewModel
private bool _saved;
private ushort _hotkeyHandle;
internal HotkeyManager HotkeyManager { get; set; }
internal HotkeyManager HotkeyManager { get; private set; }
public MainViewModel(PowerToysRunSettings settings)
{
@ -257,8 +256,6 @@ namespace PowerLauncher.ViewModel
SelectedResults.SelectPrevPage();
});
SelectFirstResultCommand = new RelayCommand(_ => SelectedResults.SelectFirstResult());
OpenResultWithKeyboardCommand = new RelayCommand(index =>
{
OpenResultsEvent(index, false);
@ -269,31 +266,6 @@ namespace PowerLauncher.ViewModel
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(_ =>
{
if (!string.IsNullOrEmpty(QueryText))
@ -306,10 +278,6 @@ namespace PowerLauncher.ViewModel
});
}
public Brush MainWindowBackground { get; set; }
public Brush MainWindowBorderBrush { get; set; }
private ResultsViewModel _results;
public ResultsViewModel Results
@ -402,27 +370,12 @@ namespace PowerLauncher.ViewModel
{
Results.Visibility = Visibility.Hidden;
_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;
}
}
public Visibility ProgressBarVisibility { get; set; }
private Visibility _visibility;
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 OpenResultWithKeyboardCommand { get; set; }
public ICommand OpenResultWithMouseCommand { get; set; }
public ICommand ClearQueryCommand { get; set; }
public ICommand ClearQueryCommand { get; private set; }
public void Query()
{

View file

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

View file

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

View file

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

View file

@ -62,14 +62,6 @@
<None Include="README.md" />
</ItemGroup>
<ItemGroup>
<None Remove="FodyWeavers.xml" />
</ItemGroup>
<ItemGroup>
<Content Include="FodyWeavers.xml" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="JetBrains.Annotations" Version="2020.1.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.
// See the LICENSE file in the project root for more information.
using System.Windows.Input;
using NUnit.Framework;
using PowerLauncher.ViewModel;
using Wox.Plugin;
@ -18,10 +19,7 @@ namespace Wox.Test
ResultsViewModel rvm = new ResultsViewModel();
Result result = new Result();
ResultViewModel selectedItem = new ResultViewModel(result);
selectedItem.ContextMenuItems.Add(new ContextMenuItemViewModel()
{
Title = "Dummy Context Menu",
});
selectedItem.ContextMenuItems.Add(new ContextMenuItemViewModel(null, null, null, null, Key.None, ModifierKeys.None, null));
rvm.SelectedItem = selectedItem;
// Assert
@ -35,10 +33,7 @@ namespace Wox.Test
ResultsViewModel rvm = new ResultsViewModel();
Result result = new Result();
ResultViewModel selectedItem = new ResultViewModel(result);
selectedItem.ContextMenuItems.Add(new ContextMenuItemViewModel()
{
Title = "Dummy Context Menu",
});
selectedItem.ContextMenuItems.Add(new ContextMenuItemViewModel(null, null, null, null, Key.None, ModifierKeys.None, null));
rvm.SelectedItem = selectedItem;
// Act
@ -55,10 +50,7 @@ namespace Wox.Test
ResultsViewModel rvm = new ResultsViewModel();
Result result = new Result();
ResultViewModel selectedItem = new ResultViewModel(result);
selectedItem.ContextMenuItems.Add(new ContextMenuItemViewModel()
{
Title = "Dummy Context Menu",
});
selectedItem.ContextMenuItems.Add(new ContextMenuItemViewModel(null, null, null, null, Key.None, ModifierKeys.None, null));
rvm.SelectedItem = selectedItem;
// Act
@ -75,18 +67,9 @@ namespace Wox.Test
ResultsViewModel rvm = new ResultsViewModel();
Result result = new Result();
ResultViewModel selectedItem = new ResultViewModel(result);
selectedItem.ContextMenuItems.Add(new ContextMenuItemViewModel()
{
Title = "Dummy Context Menu 1",
});
selectedItem.ContextMenuItems.Add(new ContextMenuItemViewModel()
{
Title = "Dummy Context Menu 2",
});
selectedItem.ContextMenuItems.Add(new ContextMenuItemViewModel()
{
Title = "Dummy Context Menu 3",
});
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));
selectedItem.ContextMenuItems.Add(new ContextMenuItemViewModel(null, null, null, null, Key.None, ModifierKeys.None, null));
rvm.SelectedItem = selectedItem;
// Act
@ -106,10 +89,7 @@ namespace Wox.Test
ResultsViewModel rvm = new ResultsViewModel();
Result result = new Result();
ResultViewModel selectedItem = new ResultViewModel(result);
selectedItem.ContextMenuItems.Add(new ContextMenuItemViewModel()
{
Title = "Dummy Context Menu",
});
selectedItem.ContextMenuItems.Add(new ContextMenuItemViewModel(null, null, null, null, Key.None, ModifierKeys.None, null));
rvm.SelectedItem = selectedItem;
// Act
@ -127,10 +107,7 @@ namespace Wox.Test
ResultsViewModel rvm = new ResultsViewModel();
Result result = new Result();
ResultViewModel selectedItem = new ResultViewModel(result);
selectedItem.ContextMenuItems.Add(new ContextMenuItemViewModel()
{
Title = "Dummy Context Menu",
});
selectedItem.ContextMenuItems.Add(new ContextMenuItemViewModel(null, null, null, null, Key.None, ModifierKeys.None, null));
rvm.SelectedItem = selectedItem;
// Act
@ -148,10 +125,7 @@ namespace Wox.Test
ResultsViewModel rvm = new ResultsViewModel();
Result result = new Result();
ResultViewModel selectedItem = new ResultViewModel(result);
selectedItem.ContextMenuItems.Add(new ContextMenuItemViewModel()
{
Title = "Dummy Context Menu",
});
selectedItem.ContextMenuItems.Add(new ContextMenuItemViewModel(null, null, null, null, Key.None, ModifierKeys.None, null));
rvm.SelectedItem = selectedItem;
// Act