Skip to content

Commit

Permalink
Code Quality: Replace 'Format Drive' commands with actions (#16468)
Browse files Browse the repository at this point in the history
  • Loading branch information
yaira2 authored Nov 14, 2024
1 parent d963427 commit 7282b47
Show file tree
Hide file tree
Showing 11 changed files with 104 additions and 76 deletions.
28 changes: 14 additions & 14 deletions src/Files.App/Actions/FileSystem/FormatDriveAction.cs
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
// Copyright (c) 2024 Files Community
// Licensed under the MIT License. See the LICENSE.

using Files.App.Utils.Shell;

namespace Files.App.Actions
{
internal sealed class FormatDriveAction : ObservableObject, IAction
internal class FormatDriveAction : ObservableObject, IAction
{
private readonly IContentPageContext context;
private readonly IContentPageContext context = Ioc.Default.GetRequiredService<IContentPageContext>();

private readonly DrivesViewModel drivesViewModel;
private readonly DrivesViewModel drivesViewModel = Ioc.Default.GetRequiredService<DrivesViewModel>();

public string Label
=> "FormatDriveText".GetLocalizedResource();
=> Strings.FormatDriveText.GetLocalizedResource();

public string Description
=> "FormatDriveDescription".GetLocalizedResource();
=> Strings.FormatDriveDescription.GetLocalizedResource();

public bool IsExecutable =>
public virtual bool IsExecutable =>
context.HasItem &&
!context.HasSelection &&
(drivesViewModel.Drives.Cast<DriveItem>().FirstOrDefault(x =>
string.Equals(x.Path, context.Folder?.ItemPath))?.MenuOptions.ShowFormatDrive ?? false);
drivesViewModel.Drives
.Cast<DriveItem>()
.FirstOrDefault(x => string.Equals(x.Path, context.Folder?.ItemPath)) is DriveItem driveItem &&
!(driveItem.Type == DriveType.Network || string.Equals(context.Folder?.ItemPath, $@"{Constants.UserEnvironmentPaths.SystemDrivePath}\", StringComparison.OrdinalIgnoreCase));

public virtual bool IsAccessibleGlobally
=> true;

public FormatDriveAction()
{
context = Ioc.Default.GetRequiredService<IContentPageContext>();
drivesViewModel = Ioc.Default.GetRequiredService<DrivesViewModel>();

context.PropertyChanged += Context_PropertyChanged;
}

public Task ExecuteAsync(object? parameter = null)
public virtual Task ExecuteAsync(object? parameter = null)
{
return Win32Helper.OpenFormatDriveDialog(context.Folder?.ItemPath ?? string.Empty);
}
Expand Down
29 changes: 29 additions & 0 deletions src/Files.App/Actions/FileSystem/FormatDriveFromHomeAction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (c) 2024 Files Community
// Licensed under the MIT License. See the LICENSE.

namespace Files.App.Actions
{
internal sealed class FormatDriveFromHomeAction : FormatDriveAction
{
private IHomePageContext HomePageContext { get; } = Ioc.Default.GetRequiredService<IHomePageContext>();

private readonly DrivesViewModel drivesViewModel = Ioc.Default.GetRequiredService<DrivesViewModel>();

public override bool IsExecutable =>
HomePageContext.IsAnyItemRightClicked &&
HomePageContext.RightClickedItem is not null &&
HomePageContext.RightClickedItem.Path is not null &&
drivesViewModel.Drives
.Cast<DriveItem>()
.FirstOrDefault(x => string.Equals(x.Path, HomePageContext.RightClickedItem.Path)) is DriveItem driveItem &&
!(driveItem.Type == DriveType.Network || string.Equals(HomePageContext.RightClickedItem.Path, $@"{Constants.UserEnvironmentPaths.SystemDrivePath}\", StringComparison.OrdinalIgnoreCase));

public override bool IsAccessibleGlobally
=> false;

public override Task ExecuteAsync(object? parameter = null)
{
return Win32Helper.OpenFormatDriveDialog(HomePageContext?.RightClickedItem?.Path ?? string.Empty);
}
}
}
29 changes: 29 additions & 0 deletions src/Files.App/Actions/FileSystem/FormatDriveFromSidebarAction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (c) 2024 Files Community
// Licensed under the MIT License. See the LICENSE.

namespace Files.App.Actions
{
internal sealed class FormatDriveFromSidebarAction : FormatDriveAction
{
private ISidebarContext SidebarContext { get; } = Ioc.Default.GetRequiredService<ISidebarContext>();

private readonly DrivesViewModel drivesViewModel = Ioc.Default.GetRequiredService<DrivesViewModel>();

public override bool IsExecutable =>
SidebarContext.IsItemRightClicked &&
SidebarContext.RightClickedItem is not null &&
SidebarContext.RightClickedItem.Path is not null &&
drivesViewModel.Drives
.Cast<DriveItem>()
.FirstOrDefault(x => string.Equals(x.Path, SidebarContext.RightClickedItem.Path)) is DriveItem driveItem &&
!(driveItem.Type == DriveType.Network || string.Equals(SidebarContext.RightClickedItem.Path, $@"{Constants.UserEnvironmentPaths.SystemDrivePath}\", StringComparison.OrdinalIgnoreCase));

public override bool IsAccessibleGlobally
=> false;

public override Task ExecuteAsync(object? parameter = null)
{
return Win32Helper.OpenFormatDriveDialog(SidebarContext?.RightClickedItem?.Path ?? string.Empty);
}
}
}
2 changes: 2 additions & 0 deletions src/Files.App/Data/Commands/Manager/CommandCodes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public enum CommandCodes
CreateShortcutFromDialog,
EmptyRecycleBin,
FormatDrive,
FormatDriveFromHome,
FormatDriveFromSidebar,
RestoreRecycleBin,
RestoreAllRecycleBin,
OpenItem,
Expand Down
4 changes: 4 additions & 0 deletions src/Files.App/Data/Commands/Manager/CommandManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@ public IRichCommand this[HotKey hotKey]
public IRichCommand NewWindow => commands[CommandCodes.NewWindow];
public IRichCommand NewTab => commands[CommandCodes.NewTab];
public IRichCommand FormatDrive => commands[CommandCodes.FormatDrive];
public IRichCommand FormatDriveFromHome => commands[CommandCodes.FormatDriveFromHome];
public IRichCommand FormatDriveFromSidebar => commands[CommandCodes.FormatDriveFromSidebar];
public IRichCommand NavigateBack => commands[CommandCodes.NavigateBack];
public IRichCommand NavigateForward => commands[CommandCodes.NavigateForward];
public IRichCommand NavigateUp => commands[CommandCodes.NavigateUp];
Expand Down Expand Up @@ -379,6 +381,8 @@ public IEnumerator<IRichCommand> GetEnumerator() =>
[CommandCodes.NewWindow] = new NewWindowAction(),
[CommandCodes.NewTab] = new NewTabAction(),
[CommandCodes.FormatDrive] = new FormatDriveAction(),
[CommandCodes.FormatDriveFromHome] = new FormatDriveFromHomeAction(),
[CommandCodes.FormatDriveFromSidebar] = new FormatDriveFromSidebarAction(),
[CommandCodes.NavigateBack] = new NavigateBackAction(),
[CommandCodes.NavigateForward] = new NavigateForwardAction(),
[CommandCodes.NavigateUp] = new NavigateUpAction(),
Expand Down
2 changes: 2 additions & 0 deletions src/Files.App/Data/Commands/Manager/ICommandManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ public interface ICommandManager : IEnumerable<IRichCommand>
IRichCommand RestoreRecycleBin { get; }
IRichCommand RestoreAllRecycleBin { get; }
IRichCommand FormatDrive { get; }
IRichCommand FormatDriveFromHome { get; }
IRichCommand FormatDriveFromSidebar { get; }
IRichCommand OpenItem { get; }
IRichCommand OpenItemWithApplicationPicker { get; }
IRichCommand OpenParentFolder { get; }
Expand Down
2 changes: 0 additions & 2 deletions src/Files.App/Data/Contracts/INavigationControlItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ public sealed class ContextMenuOptions

public bool ShowEjectDevice { get; set; }

public bool ShowFormatDrive { get; set; }

public bool ShowShellItems { get; set; }
}
}
1 change: 0 additions & 1 deletion src/Files.App/Data/Items/DriveItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,6 @@ public static async Task<DriveItem> CreateFromPropertiesAsync(StorageFolder root
IsLocationItem = true,
ShowEjectDevice = item.IsRemovable,
ShowShellItems = true,
ShowFormatDrive = !(item.Type == DriveType.Network || string.Equals(root.Path, $@"{Constants.UserEnvironmentPaths.SystemDrivePath}\", StringComparison.OrdinalIgnoreCase)),
ShowProperties = true
};
item.Path = string.IsNullOrEmpty(root.Path) ? $"\\\\?\\{root.Name}\\" : root.Path;
Expand Down
18 changes: 2 additions & 16 deletions src/Files.App/ViewModels/UserControls/SidebarViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,6 @@ public SidebarViewModel()
OpenInNewWindowCommand = new AsyncRelayCommand(OpenInNewWindowAsync);
OpenInNewPaneCommand = new AsyncRelayCommand(OpenInNewPaneAsync);
EjectDeviceCommand = new RelayCommand(EjectDevice);
FormatDriveCommand = new RelayCommand(FormatDrive);
OpenPropertiesCommand = new RelayCommand<CommandBarFlyout>(OpenProperties);
ReorderItemsCommand = new AsyncRelayCommand(ReorderItemsAsync);
}
Expand Down Expand Up @@ -839,8 +838,6 @@ public async void HandleItemInvokedAsync(object item, PointerUpdateKind pointerU

private ICommand EjectDeviceCommand { get; }

private ICommand FormatDriveCommand { get; }

private ICommand OpenPropertiesCommand { get; }

private ICommand ReorderItemsCommand { get; }
Expand Down Expand Up @@ -955,11 +952,6 @@ private void EjectDevice()
DriveHelpers.EjectDeviceAsync(rightClickedItem.Path);
}

private void FormatDrive()
{
Win32Helper.OpenFormatDriveDialog(rightClickedItem.Path);
}

private List<ContextMenuFlyoutItemViewModel> GetLocationItemMenuItems(INavigationControlItem item, CommandBarFlyout menu)
{
var options = item.MenuOptions;
Expand Down Expand Up @@ -1058,17 +1050,11 @@ private List<ContextMenuFlyoutItemViewModel> GetLocationItemMenuItems(INavigatio
ItemType = ContextMenuFlyoutItemType.Separator,
ShowItem = Commands.OpenTerminalFromSidebar.IsExecutable ||
Commands.OpenStorageSenseFromSidebar.IsExecutable ||
options.ShowFormatDrive
Commands.FormatDriveFromSidebar.IsExecutable
},
new ContextMenuFlyoutItemViewModelBuilder(Commands.OpenTerminalFromSidebar).Build(),
new ContextMenuFlyoutItemViewModelBuilder(Commands.OpenStorageSenseFromSidebar).Build(),
new ContextMenuFlyoutItemViewModel()
{
Text = Strings.FormatDriveText.GetLocalizedResource(),
Command = FormatDriveCommand,
CommandParameter = item,
ShowItem = options.ShowFormatDrive
},
new ContextMenuFlyoutItemViewModelBuilder(Commands.FormatDriveFromSidebar).Build(),
new ContextMenuFlyoutItemViewModel()
{
ItemType = ContextMenuFlyoutItemType.Separator,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,14 @@ public sealed class DrivesWidgetViewModel : BaseWidgetViewModel, IWidgetViewMode
public ObservableCollection<WidgetDriveCardItem> Items { get; } = [];

public string WidgetName => nameof(DrivesWidget);
public string AutomationProperties => "Drives".GetLocalizedResource();
public string WidgetHeader => "Drives".GetLocalizedResource();
public string AutomationProperties => Strings.Drives.GetLocalizedResource();
public string WidgetHeader => Strings.Drives.GetLocalizedResource();
public bool IsWidgetSettingEnabled => UserSettingsService.GeneralSettingsService.ShowDrivesWidget;
public bool ShowMenuFlyout => false;
public MenuFlyoutItem? MenuFlyoutItem => null;

// Commands

private ICommand FormatDriveCommand { get; } = null!;
private ICommand EjectDeviceCommand { get; } = null!;
private ICommand OpenInNewPaneCommand { get; } = null!;
private ICommand DisconnectNetworkDriveCommand { get; } = null!;
Expand All @@ -45,7 +44,6 @@ public DrivesWidgetViewModel()
OpenInNewWindowCommand = new AsyncRelayCommand<WidgetCardItem>(ExecuteOpenInNewWindowCommand);
PinToSidebarCommand = new AsyncRelayCommand<WidgetCardItem>(ExecutePinToSidebarCommand);
UnpinFromSidebarCommand = new AsyncRelayCommand<WidgetCardItem>(ExecuteUnpinFromSidebarCommand);
FormatDriveCommand = new RelayCommand<WidgetDriveCardItem>(ExecuteFormatDriveCommand);
EjectDeviceCommand = new RelayCommand<WidgetDriveCardItem>(ExecuteEjectDeviceCommand);
OpenInNewPaneCommand = new AsyncRelayCommand<WidgetDriveCardItem>(ExecuteOpenInNewPaneCommand);
OpenPropertiesCommand = new RelayCommand<WidgetDriveCardItem>(ExecuteOpenPropertiesCommand);
Expand Down Expand Up @@ -99,43 +97,43 @@ public override List<ContextMenuFlyoutItemViewModel> GetItemMenuItems(WidgetCard
new ContextMenuFlyoutItemViewModelBuilder(CommandManager.OpenInNewPaneFromHomeAction).Build(),
new()
{
Text = "PinFolderToSidebar".GetLocalizedResource(),
Text = Strings.PinFolderToSidebar.GetLocalizedResource(),
ThemedIconModel = new ThemedIconModel() { ThemedIconStyle = "App.ThemedIcons.FavoritePin" },
Command = PinToSidebarCommand,
CommandParameter = item,
ShowItem = !isPinned
},
new()
{
Text = "UnpinFolderFromSidebar".GetLocalizedResource(),
Text = Strings.UnpinFolderFromSidebar.GetLocalizedResource(),
ThemedIconModel = new ThemedIconModel() { ThemedIconStyle = "App.ThemedIcons.FavoritePinRemove" },
Command = UnpinFromSidebarCommand,
CommandParameter = item,
ShowItem = isPinned
},
new()
{
Text = "Eject".GetLocalizedResource(),
Text = Strings.Eject.GetLocalizedResource(),
Command = EjectDeviceCommand,
CommandParameter = item,
ShowItem = options?.ShowEjectDevice ?? false
},
new()
{
Text = "Properties".GetLocalizedResource(),
Text = Strings.Properties.GetLocalizedResource(),
ThemedIconModel = new ThemedIconModel() { ThemedIconStyle = "App.ThemedIcons.Properties" },
Command = OpenPropertiesCommand,
CommandParameter = item
},
new()
{
Text = "TurnOnBitLocker".GetLocalizedResource(),
Text = Strings.TurnOnBitLocker.GetLocalizedResource(),
Tag = "TurnOnBitLockerPlaceholder",
IsEnabled = false
},
new()
{
Text = "ManageBitLocker".GetLocalizedResource(),
Text = Strings.ManageBitLocker.GetLocalizedResource(),
Tag = "ManageBitLockerPlaceholder",
IsEnabled = false
},
Expand All @@ -144,25 +142,19 @@ public override List<ContextMenuFlyoutItemViewModel> GetItemMenuItems(WidgetCard
ItemType = ContextMenuFlyoutItemType.Separator,
ShowItem = CommandManager.OpenTerminalFromHome.IsExecutable ||
CommandManager.OpenStorageSenseFromHome.IsExecutable ||
(options?.ShowFormatDrive ?? false)
CommandManager.FormatDriveFromHome.IsExecutable
},
new ContextMenuFlyoutItemViewModelBuilder(CommandManager.OpenTerminalFromHome).Build(),
new ContextMenuFlyoutItemViewModelBuilder(CommandManager.OpenStorageSenseFromHome).Build(),
new()
{
Text = "FormatDriveText".GetLocalizedResource(),
Command = FormatDriveCommand,
CommandParameter = item,
ShowItem = options?.ShowFormatDrive ?? false
},
new ContextMenuFlyoutItemViewModelBuilder(CommandManager.FormatDriveFromHome).Build(),
new()
{
ItemType = ContextMenuFlyoutItemType.Separator,
Tag = "OverflowSeparator",
},
new()
{
Text = "Loading".GetLocalizedResource(),
Text = Strings.Loading.GetLocalizedResource(),
Glyph = "\xE712",
Items = [],
ID = "ItemOverflow",
Expand Down Expand Up @@ -190,11 +182,6 @@ private async Task ExecuteOpenInNewPaneCommand(WidgetDriveCardItem? item)
ContentPageContext.ShellPage!.PaneHolder?.OpenSecondaryPane(item.Item.Path);
}

private void ExecuteFormatDriveCommand(WidgetDriveCardItem? item)
{
Win32Helper.OpenFormatDriveDialog(item?.Path ?? string.Empty);
}

private void ExecuteOpenPropertiesCommand(WidgetDriveCardItem? item)
{
if (!HomePageContext.IsAnyItemRightClicked || item is null)
Expand Down
Loading

0 comments on commit 7282b47

Please sign in to comment.