Skip to content

Commit

Permalink
[Run] ViewModels Cleanup (#12209)
Browse files Browse the repository at this point in the history
* view models cleanup

* removed unused commands
  • Loading branch information
davidegiacometti committed Jul 15, 2021
1 parent cdfb566 commit 55923f2
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 193 deletions.
Original file line number Diff line number Diff line change
@@ -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.

Expand All @@ -13,52 +13,13 @@ public class ContextMenuItemViewModel : BaseModel
{
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
{
Expand All @@ -78,12 +39,23 @@ public ICommand Command
}
}

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()
Expand Down
83 changes: 15 additions & 68 deletions src/modules/launcher/PowerLauncher/ViewModel/MainViewModel.cs
Original file line number Diff line number Diff line change
@@ -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.

Expand All @@ -11,7 +11,6 @@
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;
Expand Down Expand Up @@ -51,7 +50,7 @@ public class MainViewModel : BaseModel, ISavable, IDisposable
private bool _saved;
private ushort _hotkeyHandle;

internal HotkeyManager HotkeyManager { get; set; }
internal HotkeyManager HotkeyManager { get; private set; }

public MainViewModel(PowerToysRunSettings settings)
{
Expand Down Expand Up @@ -257,8 +256,6 @@ private void InitializeKeyCommands()
SelectedResults.SelectPrevPage();
});

SelectFirstResultCommand = new RelayCommand(_ => SelectedResults.SelectFirstResult());

OpenResultWithKeyboardCommand = new RelayCommand(index =>
{
OpenResultsEvent(index, false);
Expand All @@ -269,31 +266,6 @@ private void InitializeKeyCommands()
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))
Expand All @@ -306,10 +278,6 @@ private void InitializeKeyCommands()
});
}

public Brush MainWindowBackground { get; set; }

public Brush MainWindowBorderBrush { get; set; }

private ResultsViewModel _results;

public ResultsViewModel Results
Expand Down Expand Up @@ -402,27 +370,12 @@ private ResultsViewModel SelectedResults
{
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
Expand Down Expand Up @@ -451,37 +404,31 @@ public Visibility MainWindowVisibility
}
}

public ICommand IgnoreCommand { get; set; }

public ICommand EscCommand { get; set; }

public ICommand SelectNextItemCommand { get; set; }

public ICommand SelectPrevItemCommand { get; set; }
public ICommand IgnoreCommand { get; private set; }

public ICommand SelectNextContextMenuItemCommand { get; set; }
public ICommand EscCommand { get; private set; }

public ICommand SelectPreviousContextMenuItemCommand { get; set; }
public ICommand SelectNextItemCommand { get; private set; }

public ICommand SelectNextTabItemCommand { get; set; }
public ICommand SelectPrevItemCommand { get; private set; }

public ICommand SelectPrevTabItemCommand { get; set; }
public ICommand SelectNextContextMenuItemCommand { get; private set; }

public ICommand SelectNextPageCommand { get; set; }
public ICommand SelectPreviousContextMenuItemCommand { get; private set; }

public ICommand SelectPrevPageCommand { get; set; }
public ICommand SelectNextTabItemCommand { get; private set; }

public ICommand SelectFirstResultCommand { get; set; }
public ICommand SelectPrevTabItemCommand { get; private set; }

public ICommand LoadContextMenuCommand { get; set; }
public ICommand SelectNextPageCommand { get; private set; }

public ICommand LoadHistoryCommand { get; set; }
public ICommand SelectPrevPageCommand { get; private set; }

public ICommand OpenResultWithKeyboardCommand { get; set; }
public ICommand OpenResultWithKeyboardCommand { get; private set; }

public ICommand OpenResultWithMouseCommand { get; set; }
public ICommand OpenResultWithMouseCommand { get; private set; }

public ICommand ClearQueryCommand { get; set; }
public ICommand ClearQueryCommand { get; private set; }

public void Query()
{
Expand Down
44 changes: 13 additions & 31 deletions src/modules/launcher/PowerLauncher/ViewModel/ResultViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,13 @@ public enum ActivationType

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;

Expand Down Expand Up @@ -79,21 +75,14 @@ public ResultViewModel(Result result)
LoadContextMenu();

ActivateContextButtonsHoverCommand = new RelayCommand(ActivateContextButtonsHoverAction);
ActivateContextButtonsSelectionCommand = new RelayCommand(ActivateContextButtonsSelectionAction);
DeactivateContextButtonsHoverCommand = new RelayCommand(DeactivateContextButtonsHoverAction);
DeactivateContextButtonsSelectionCommand = new RelayCommand(DeactivateContextButtonsSelectionAction);
}

private void ActivateContextButtonsHoverAction(object sender)
{
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.
Expand Down Expand Up @@ -122,11 +111,6 @@ private void DeactivateContextButtonsHoverAction(object sender)
DeactivateContextButtons(ActivationType.Hover);
}

private void DeactivateContextButtonsSelectionAction(object sender)
{
DeactivateContextButtons(ActivationType.Selection);
}

public void DeactivateContextButtons(ActivationType activationType)
{
if (activationType == ActivationType.Selection)
Expand Down Expand Up @@ -156,15 +140,14 @@ public void LoadContextMenu()
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 &&
Expand All @@ -179,8 +162,7 @@ public void LoadContextMenu()
// TODO - Do we hide the window
// MainWindowVisibility = Visibility.Collapsed;
}
}),
});
})));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,6 @@ public ResultViewModel SelectedItem
}
}

public Thickness Margin { get; set; }

private Visibility _visibility = Visibility.Hidden;

public Visibility Visibility
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -19,7 +18,7 @@ public SettingWindowViewModel()
Settings = _storage.Load();
}

public PowerToysRunSettings Settings { get; set; }
public PowerToysRunSettings Settings { get; }

public void Save()
{
Expand Down
8 changes: 0 additions & 8 deletions src/modules/launcher/Wox.Plugin/Wox.Plugin.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -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">
Expand Down

0 comments on commit 55923f2

Please sign in to comment.