Skip to content

Commit

Permalink
Code Quality: Replaced Home and Sidebar commands with actions (#16484)
Browse files Browse the repository at this point in the history
  • Loading branch information
yaira2 authored Nov 19, 2024
1 parent 88a991e commit 415c6ba
Show file tree
Hide file tree
Showing 16 changed files with 74 additions and 144 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ public string Description

public virtual bool IsExecutable =>
ContentPageContext.SelectedItem is not null &&
ContentPageContext.SelectedItem.IsFolder &&
UserSettingsService.GeneralSettingsService.ShowOpenInNewPane;
ContentPageContext.SelectedItem.IsFolder;

public virtual bool IsAccessibleGlobally
=> true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ namespace Files.App.Actions
internal sealed class OpenInNewPaneFromHomeAction : BaseOpenInNewPaneAction
{
public override bool IsExecutable =>
UserSettingsService.GeneralSettingsService.ShowOpenInNewPane &&
HomePageContext.IsAnyItemRightClicked &&
HomePageContext.RightClickedItem is not null &&
(HomePageContext.RightClickedItem is WidgetFileTagCardItem fileTagItem
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ namespace Files.App.Actions
internal sealed class OpenInNewPaneFromSidebarAction : BaseOpenInNewPaneAction
{
public override bool IsExecutable =>
UserSettingsService.GeneralSettingsService.ShowOpenInNewPane &&
SidebarContext.IsItemRightClicked &&
SidebarContext.RightClickedItem is not null &&
SidebarContext.RightClickedItem.MenuOptions.IsLocationItem;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ ContentPageContext.ShellPage is not null &&
ContentPageContext.ShellPage.SlimContentPage is not null &&
ContentPageContext.SelectedItems.Count is not 0 &&
ContentPageContext.SelectedItems.Count <= 5 &&
ContentPageContext.SelectedItems.Count(x => x.IsFolder) == ContentPageContext.SelectedItems.Count &&
UserSettingsService.GeneralSettingsService.ShowOpenInNewTab;
ContentPageContext.SelectedItems.Count(x => x.IsFolder) == ContentPageContext.SelectedItems.Count;

public BaseOpenInNewTabAction()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ namespace Files.App.Actions
internal sealed class OpenInNewTabFromHomeAction : BaseOpenInNewTabAction
{
public override bool IsExecutable =>
UserSettingsService.GeneralSettingsService.ShowOpenInNewTab &&
HomePageContext.IsAnyItemRightClicked &&
HomePageContext.RightClickedItem is not null &&
(HomePageContext.RightClickedItem is WidgetFileTagCardItem fileTagItem
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ namespace Files.App.Actions
internal sealed class OpenInNewTabFromSidebarAction : BaseOpenInNewTabAction
{
public override bool IsExecutable =>
UserSettingsService.GeneralSettingsService.ShowOpenInNewTab &&
SidebarContext.IsItemRightClicked &&
SidebarContext.RightClickedItem is not null &&
SidebarContext.RightClickedItem.MenuOptions.IsLocationItem;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ ContentPageContext.ShellPage is not null &&
ContentPageContext.ShellPage.SlimContentPage is not null &&
ContentPageContext.SelectedItems.Count is not 0 &&
ContentPageContext.SelectedItems.Count <= 5 &&
ContentPageContext.SelectedItems.Count(x => x.IsFolder) == ContentPageContext.SelectedItems.Count &&
UserSettingsService.GeneralSettingsService.ShowOpenInNewWindow;
ContentPageContext.SelectedItems.Count(x => x.IsFolder) == ContentPageContext.SelectedItems.Count;

public BaseOpenInNewWindowAction()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ public override HotKey HotKey
=> HotKey.None;

public override bool IsExecutable =>
UserSettingsService.GeneralSettingsService.ShowOpenInNewWindow &&
HomePageContext.IsAnyItemRightClicked &&
HomePageContext.RightClickedItem is not null &&
(HomePageContext.RightClickedItem is WidgetFileTagCardItem fileTagItem
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ public override HotKey HotKey
=> HotKey.None;

public override bool IsExecutable =>
UserSettingsService.GeneralSettingsService.ShowOpenInNewWindow &&
SidebarContext.IsItemRightClicked &&
SidebarContext.RightClickedItem is not null &&
SidebarContext.RightClickedItem.MenuOptions.IsLocationItem;
Expand Down
15 changes: 12 additions & 3 deletions src/Files.App/Data/Factories/ContentPageContextFlyoutFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -407,9 +407,18 @@ public static List<ContextMenuFlyoutItemViewModel> GetBaseItemMenuItems(
ShowItem = itemsSelected && showOpenItemWith
},
new ContextMenuFlyoutItemViewModelBuilder(Commands.OpenFileLocation).Build(),
new ContextMenuFlyoutItemViewModelBuilder(Commands.OpenInNewTabAction).Build(),
new ContextMenuFlyoutItemViewModelBuilder(Commands.OpenInNewWindowAction).Build(),
new ContextMenuFlyoutItemViewModelBuilder(Commands.OpenInNewPaneAction).Build(),
new ContextMenuFlyoutItemViewModelBuilder(Commands.OpenInNewTabAction)
{
IsVisible = UserSettingsService.GeneralSettingsService.ShowOpenInNewTab
}.Build(),
new ContextMenuFlyoutItemViewModelBuilder(Commands.OpenInNewWindowAction)
{
IsVisible = UserSettingsService.GeneralSettingsService.ShowOpenInNewWindow
}.Build(),
new ContextMenuFlyoutItemViewModelBuilder(Commands.OpenInNewPaneAction)
{
IsVisible = UserSettingsService.GeneralSettingsService.ShowOpenInNewPane
}.Build(),
new ContextMenuFlyoutItemViewModel()
{
Text = "BaseLayoutItemContextFlyoutSetAs/Text".GetLocalizedResource(),
Expand Down
51 changes: 14 additions & 37 deletions src/Files.App/ViewModels/UserControls/SidebarViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -254,9 +254,6 @@ public SidebarViewModel()
HideSectionCommand = new RelayCommand(HideSection);
UnpinItemCommand = new RelayCommand(UnpinItem);
PinItemCommand = new RelayCommand(PinItem);
OpenInNewTabCommand = new AsyncRelayCommand(OpenInNewTabAsync);
OpenInNewWindowCommand = new AsyncRelayCommand(OpenInNewWindowAsync);
OpenInNewPaneCommand = new AsyncRelayCommand(OpenInNewPaneAsync);
EjectDeviceCommand = new RelayCommand(EjectDevice);
OpenPropertiesCommand = new RelayCommand<CommandBarFlyout>(OpenProperties);
ReorderItemsCommand = new AsyncRelayCommand(ReorderItemsAsync);
Expand Down Expand Up @@ -830,41 +827,12 @@ public async void HandleItemInvokedAsync(object item, PointerUpdateKind pointerU

private ICommand UnpinItemCommand { get; }

private ICommand OpenInNewTabCommand { get; }

private ICommand OpenInNewWindowCommand { get; }

private ICommand OpenInNewPaneCommand { get; }

private ICommand EjectDeviceCommand { get; }

private ICommand OpenPropertiesCommand { get; }

private ICommand ReorderItemsCommand { get; }

private async Task OpenInNewPaneAsync()
{
if (await DriveHelpers.CheckEmptyDrive(rightClickedItem.Path))
return;
PaneHolder.OpenSecondaryPane(rightClickedItem.Path);
}

private async Task OpenInNewTabAsync()
{
if (await DriveHelpers.CheckEmptyDrive(rightClickedItem.Path))
return;

await NavigationHelpers.OpenPathInNewTab(rightClickedItem.Path, false);
}

private async Task OpenInNewWindowAsync()
{
if (await DriveHelpers.CheckEmptyDrive(rightClickedItem.Path))
return;

await NavigationHelpers.OpenPathInNewWindowAsync(rightClickedItem.Path);
}

private void PinItem()
{
if (rightClickedItem is DriveItem)
Expand Down Expand Up @@ -971,14 +939,14 @@ private List<ContextMenuFlyoutItemViewModel> GetLocationItemMenuItems(INavigatio
{
new ContextMenuFlyoutItemViewModel()
{
Text = "SideBarCreateNewLibrary/Text".GetLocalizedResource(),
Text = Strings.SideBarCreateNewLibrary_Text.GetLocalizedResource(),
Glyph = "\uE710",
Command = CreateLibraryCommand,
ShowItem = options.IsLibrariesHeader
},
new ContextMenuFlyoutItemViewModel()
{
Text = "SideBarRestoreLibraries/Text".GetLocalizedResource(),
Text = Strings.SideBarRestoreLibraries_Text.GetLocalizedResource(),
Glyph = "\uE10E",
Command = RestoreLibrariesCommand,
ShowItem = options.IsLibrariesHeader
Expand All @@ -991,9 +959,18 @@ private List<ContextMenuFlyoutItemViewModel> GetLocationItemMenuItems(INavigatio
{
IsVisible = options.ShowEmptyRecycleBin,
}.Build(),
new ContextMenuFlyoutItemViewModelBuilder(Commands.OpenInNewTabFromSidebarAction).Build(),
new ContextMenuFlyoutItemViewModelBuilder(Commands.OpenInNewWindowFromSidebarAction).Build(),
new ContextMenuFlyoutItemViewModelBuilder(Commands.OpenInNewPaneFromSidebarAction).Build(),
new ContextMenuFlyoutItemViewModelBuilder(Commands.OpenInNewTabFromSidebarAction)
{
IsVisible = UserSettingsService.GeneralSettingsService.ShowOpenInNewTab
}.Build(),
new ContextMenuFlyoutItemViewModelBuilder(Commands.OpenInNewWindowFromSidebarAction)
{
IsVisible = UserSettingsService.GeneralSettingsService.ShowOpenInNewWindow
}.Build(),
new ContextMenuFlyoutItemViewModelBuilder(Commands.OpenInNewPaneFromSidebarAction)
{
IsVisible = UserSettingsService.GeneralSettingsService.ShowOpenInNewPane
}.Build(),
new ContextMenuFlyoutItemViewModel()
{
Text = "PinFolderToSidebar".GetLocalizedResource(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ public abstract class BaseWidgetViewModel : ObservableObject
protected ICommand RemoveRecentItemCommand { get; set; } = null!;
protected ICommand ClearAllItemsCommand { get; set; } = null!;
protected ICommand OpenFileLocationCommand { get; set; } = null!;
protected ICommand OpenInNewTabCommand { get; set; } = null!;
protected ICommand OpenInNewWindowCommand { get; set; } = null!;
protected ICommand OpenPropertiesCommand { get; set; } = null!;
protected ICommand PinToSidebarCommand { get; set; } = null!;
protected ICommand UnpinFromSidebarCommand { get; set; } = null!;
Expand Down Expand Up @@ -100,16 +98,6 @@ widgetCardItem.DataContext is not WidgetCardItem item ||

// Command methods

public async Task ExecuteOpenInNewTabCommand(WidgetCardItem? item)
{
await NavigationHelpers.OpenPathInNewTab(item?.Path ?? string.Empty, false);
}

public async Task ExecuteOpenInNewWindowCommand(WidgetCardItem? item)
{
await NavigationHelpers.OpenPathInNewWindowAsync(item?.Path ?? string.Empty);
}

public virtual async Task ExecutePinToSidebarCommand(WidgetCardItem? item)
{
await QuickAccessService.PinToSidebarAsync(item?.Path ?? string.Empty);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ public sealed class DrivesWidgetViewModel : BaseWidgetViewModel, IWidgetViewMode
// Commands

private ICommand EjectDeviceCommand { get; } = null!;
private ICommand OpenInNewPaneCommand { get; } = null!;
private ICommand DisconnectNetworkDriveCommand { get; } = null!;

// Constructor
Expand All @@ -40,12 +39,9 @@ public DrivesWidgetViewModel()

DrivesViewModel.Drives.CollectionChanged += Drives_CollectionChanged;

OpenInNewTabCommand = new AsyncRelayCommand<WidgetCardItem>(ExecuteOpenInNewTabCommand);
OpenInNewWindowCommand = new AsyncRelayCommand<WidgetCardItem>(ExecuteOpenInNewWindowCommand);
PinToSidebarCommand = new AsyncRelayCommand<WidgetCardItem>(ExecutePinToSidebarCommand);
UnpinFromSidebarCommand = new AsyncRelayCommand<WidgetCardItem>(ExecuteUnpinFromSidebarCommand);
EjectDeviceCommand = new RelayCommand<WidgetDriveCardItem>(ExecuteEjectDeviceCommand);
OpenInNewPaneCommand = new AsyncRelayCommand<WidgetDriveCardItem>(ExecuteOpenInNewPaneCommand);
OpenPropertiesCommand = new RelayCommand<WidgetDriveCardItem>(ExecuteOpenPropertiesCommand);
DisconnectNetworkDriveCommand = new RelayCommand<WidgetDriveCardItem>(ExecuteDisconnectNetworkDriveCommand);
}
Expand Down Expand Up @@ -92,9 +88,18 @@ public override List<ContextMenuFlyoutItemViewModel> GetItemMenuItems(WidgetCard

return new List<ContextMenuFlyoutItemViewModel>()
{
new ContextMenuFlyoutItemViewModelBuilder(CommandManager.OpenInNewTabFromHomeAction).Build(),
new ContextMenuFlyoutItemViewModelBuilder(CommandManager.OpenInNewWindowFromHomeAction).Build(),
new ContextMenuFlyoutItemViewModelBuilder(CommandManager.OpenInNewPaneFromHomeAction).Build(),
new ContextMenuFlyoutItemViewModelBuilder(CommandManager.OpenInNewTabFromHomeAction)
{
IsVisible = UserSettingsService.GeneralSettingsService.ShowOpenInNewTab
}.Build(),
new ContextMenuFlyoutItemViewModelBuilder(CommandManager.OpenInNewWindowFromHomeAction)
{
IsVisible = UserSettingsService.GeneralSettingsService.ShowOpenInNewWindow
}.Build(),
new ContextMenuFlyoutItemViewModelBuilder(CommandManager.OpenInNewPaneFromHomeAction)
{
IsVisible = UserSettingsService.GeneralSettingsService.ShowOpenInNewPane
}.Build(),
new()
{
Text = Strings.PinFolderToSidebar.GetLocalizedResource(),
Expand Down Expand Up @@ -174,14 +179,6 @@ private void ExecuteEjectDeviceCommand(WidgetDriveCardItem? item)
DriveHelpers.EjectDeviceAsync(item.Item.Path);
}

private async Task ExecuteOpenInNewPaneCommand(WidgetDriveCardItem? item)
{
if (item is null || await DriveHelpers.CheckEmptyDrive(item.Item.Path))
return;

ContentPageContext.ShellPage!.PaneHolder?.OpenSecondaryPane(item.Item.Path);
}

private void ExecuteOpenPropertiesCommand(WidgetDriveCardItem? item)
{
if (!HomePageContext.IsAnyItemRightClicked || item is null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using Microsoft.UI.Xaml.Controls;
using System.IO;
using System.Windows.Input;
using Windows.Storage;

namespace Files.App.ViewModels.UserControls.Widgets
Expand All @@ -28,22 +27,16 @@ public sealed partial class FileTagsWidgetViewModel : BaseWidgetViewModel, IWidg

public static event EventHandler<IEnumerable<WidgetFileTagCardItem>>? SelectedTaggedItemsChanged;

// Commands

private ICommand OpenInNewPaneCommand { get; set; } = null!;

// Constructor

public FileTagsWidgetViewModel()
{
_ = InitializeWidget();

OpenInNewTabCommand = new AsyncRelayCommand<WidgetCardItem>(ExecuteOpenInNewTabCommand);
OpenInNewWindowCommand = new AsyncRelayCommand<WidgetCardItem>(ExecuteOpenInNewWindowCommand);
PinToSidebarCommand = new AsyncRelayCommand<WidgetCardItem>(ExecutePinToSidebarCommand);
UnpinFromSidebarCommand = new AsyncRelayCommand<WidgetCardItem>(ExecuteUnpinFromSidebarCommand);
OpenFileLocationCommand = new RelayCommand<WidgetCardItem>(ExecuteOpenFileLocationCommand);
OpenInNewPaneCommand = new RelayCommand<WidgetCardItem>(ExecuteOpenInNewPaneCommand);
OpenPropertiesCommand = new RelayCommand<WidgetCardItem>(ExecuteOpenPropertiesCommand);
}

Expand Down Expand Up @@ -73,9 +66,18 @@ public override List<ContextMenuFlyoutItemViewModel> GetItemMenuItems(WidgetCard
{
return new List<ContextMenuFlyoutItemViewModel>()
{
new ContextMenuFlyoutItemViewModelBuilder(CommandManager.OpenInNewTabFromHomeAction).Build(),
new ContextMenuFlyoutItemViewModelBuilder(CommandManager.OpenInNewWindowFromHomeAction).Build(),
new ContextMenuFlyoutItemViewModelBuilder(CommandManager.OpenInNewPaneFromHomeAction).Build(),
new ContextMenuFlyoutItemViewModelBuilder(CommandManager.OpenInNewTabFromHomeAction)
{
IsVisible = UserSettingsService.GeneralSettingsService.ShowOpenInNewTab
}.Build(),
new ContextMenuFlyoutItemViewModelBuilder(CommandManager.OpenInNewWindowFromHomeAction)
{
IsVisible = UserSettingsService.GeneralSettingsService.ShowOpenInNewWindow
}.Build(),
new ContextMenuFlyoutItemViewModelBuilder(CommandManager.OpenInNewPaneFromHomeAction)
{
IsVisible = UserSettingsService.GeneralSettingsService.ShowOpenInNewPane
}.Build(),
new()
{
Text = "OpenWith".GetLocalizedResource(),
Expand Down Expand Up @@ -172,11 +174,6 @@ private void ExecuteOpenPropertiesCommand(WidgetCardItem? item)
flyout!.Closed += flyoutClosed;
}

private void ExecuteOpenInNewPaneCommand(WidgetCardItem? item)
{
ContentPageContext.ShellPage!.PaneHolder?.OpenSecondaryPane(item?.Path ?? string.Empty);
}

private void ExecuteOpenFileLocationCommand(WidgetCardItem? item)
{
var itemPath = Directory.GetParent(item?.Path ?? string.Empty)?.FullName ?? string.Empty;
Expand Down
Loading

0 comments on commit 415c6ba

Please sign in to comment.