Skip to content

Commit 1a0d29b

Browse files
committed
Code Quality: Replace Format Drive command with actions
1 parent d6fd89c commit 1a0d29b

File tree

6 files changed

+43
-28
lines changed

6 files changed

+43
-28
lines changed

src/Files.App/Actions/FileSystem/FormatDriveAction.cs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,35 @@
11
// Copyright (c) 2024 Files Community
22
// Licensed under the MIT License. See the LICENSE.
33

4-
using Files.App.Utils.Shell;
5-
64
namespace Files.App.Actions
75
{
8-
internal sealed class FormatDriveAction : ObservableObject, IAction
6+
internal class FormatDriveAction : ObservableObject, IAction
97
{
10-
private readonly IContentPageContext context;
8+
private readonly IContentPageContext context = Ioc.Default.GetRequiredService<IContentPageContext>();
119

12-
private readonly DrivesViewModel drivesViewModel;
10+
private readonly DrivesViewModel drivesViewModel = Ioc.Default.GetRequiredService<DrivesViewModel>();
1311

1412
public string Label
15-
=> "FormatDriveText".GetLocalizedResource();
13+
=> Strings.FormatDriveText.GetLocalizedResource();
1614

1715
public string Description
18-
=> "FormatDriveDescription".GetLocalizedResource();
16+
=> Strings.FormatDriveDescription.GetLocalizedResource();
1917

20-
public bool IsExecutable =>
18+
public virtual bool IsExecutable =>
2119
context.HasItem &&
2220
!context.HasSelection &&
2321
(drivesViewModel.Drives.Cast<DriveItem>().FirstOrDefault(x =>
2422
string.Equals(x.Path, context.Folder?.ItemPath))?.MenuOptions.ShowFormatDrive ?? false);
2523

24+
public virtual bool IsAccessibleGlobally
25+
=> true;
26+
2627
public FormatDriveAction()
2728
{
28-
context = Ioc.Default.GetRequiredService<IContentPageContext>();
29-
drivesViewModel = Ioc.Default.GetRequiredService<DrivesViewModel>();
30-
3129
context.PropertyChanged += Context_PropertyChanged;
3230
}
3331

34-
public Task ExecuteAsync(object? parameter = null)
32+
public virtual Task ExecuteAsync(object? parameter = null)
3533
{
3634
return Win32Helper.OpenFormatDriveDialog(context.Folder?.ItemPath ?? string.Empty);
3735
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright (c) 2024 Files Community
2+
// Licensed under the MIT License. See the LICENSE.
3+
4+
namespace Files.App.Actions
5+
{
6+
internal sealed class FormatDriveFromSidebarAction : FormatDriveAction
7+
{
8+
private ISidebarContext SidebarContext { get; } = Ioc.Default.GetRequiredService<ISidebarContext>();
9+
10+
private readonly DrivesViewModel drivesViewModel = Ioc.Default.GetRequiredService<DrivesViewModel>();
11+
12+
public override bool IsExecutable =>
13+
SidebarContext.IsItemRightClicked &&
14+
SidebarContext.RightClickedItem is not null &&
15+
SidebarContext.RightClickedItem.Path is not null &&
16+
(drivesViewModel.Drives.Cast<DriveItem>().FirstOrDefault(x =>
17+
string.Equals(x.Path, SidebarContext.RightClickedItem.Path))?.MenuOptions.ShowFormatDrive ?? false);
18+
19+
public override bool IsAccessibleGlobally
20+
=> false;
21+
22+
public override Task ExecuteAsync(object? parameter = null)
23+
{
24+
return Win32Helper.OpenFormatDriveDialog(SidebarContext?.RightClickedItem?.Path ?? string.Empty);
25+
}
26+
}
27+
}

src/Files.App/Data/Commands/Manager/CommandCodes.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public enum CommandCodes
4747
CreateShortcutFromDialog,
4848
EmptyRecycleBin,
4949
FormatDrive,
50+
FormatDriveFromSidebar,
5051
RestoreRecycleBin,
5152
RestoreAllRecycleBin,
5253
OpenItem,

src/Files.App/Data/Commands/Manager/CommandManager.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ public IRichCommand this[HotKey hotKey]
179179
public IRichCommand NewWindow => commands[CommandCodes.NewWindow];
180180
public IRichCommand NewTab => commands[CommandCodes.NewTab];
181181
public IRichCommand FormatDrive => commands[CommandCodes.FormatDrive];
182+
public IRichCommand FormatDriveFromSidebar => commands[CommandCodes.FormatDriveFromSidebar];
182183
public IRichCommand NavigateBack => commands[CommandCodes.NavigateBack];
183184
public IRichCommand NavigateForward => commands[CommandCodes.NavigateForward];
184185
public IRichCommand NavigateUp => commands[CommandCodes.NavigateUp];
@@ -379,6 +380,7 @@ public IEnumerator<IRichCommand> GetEnumerator() =>
379380
[CommandCodes.NewWindow] = new NewWindowAction(),
380381
[CommandCodes.NewTab] = new NewTabAction(),
381382
[CommandCodes.FormatDrive] = new FormatDriveAction(),
383+
[CommandCodes.FormatDriveFromSidebar] = new FormatDriveFromSidebarAction(),
382384
[CommandCodes.NavigateBack] = new NavigateBackAction(),
383385
[CommandCodes.NavigateForward] = new NavigateForwardAction(),
384386
[CommandCodes.NavigateUp] = new NavigateUpAction(),

src/Files.App/Data/Commands/Manager/ICommandManager.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public interface ICommandManager : IEnumerable<IRichCommand>
5858
IRichCommand RestoreRecycleBin { get; }
5959
IRichCommand RestoreAllRecycleBin { get; }
6060
IRichCommand FormatDrive { get; }
61+
IRichCommand FormatDriveFromSidebar { get; }
6162
IRichCommand OpenItem { get; }
6263
IRichCommand OpenItemWithApplicationPicker { get; }
6364
IRichCommand OpenParentFolder { get; }

src/Files.App/ViewModels/UserControls/SidebarViewModel.cs

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,6 @@ public SidebarViewModel()
258258
OpenInNewWindowCommand = new AsyncRelayCommand(OpenInNewWindowAsync);
259259
OpenInNewPaneCommand = new AsyncRelayCommand(OpenInNewPaneAsync);
260260
EjectDeviceCommand = new RelayCommand(EjectDevice);
261-
FormatDriveCommand = new RelayCommand(FormatDrive);
262261
OpenPropertiesCommand = new RelayCommand<CommandBarFlyout>(OpenProperties);
263262
ReorderItemsCommand = new AsyncRelayCommand(ReorderItemsAsync);
264263
}
@@ -839,8 +838,6 @@ public async void HandleItemInvokedAsync(object item, PointerUpdateKind pointerU
839838

840839
private ICommand EjectDeviceCommand { get; }
841840

842-
private ICommand FormatDriveCommand { get; }
843-
844841
private ICommand OpenPropertiesCommand { get; }
845842

846843
private ICommand ReorderItemsCommand { get; }
@@ -955,11 +952,6 @@ private void EjectDevice()
955952
DriveHelpers.EjectDeviceAsync(rightClickedItem.Path);
956953
}
957954

958-
private void FormatDrive()
959-
{
960-
Win32Helper.OpenFormatDriveDialog(rightClickedItem.Path);
961-
}
962-
963955
private List<ContextMenuFlyoutItemViewModel> GetLocationItemMenuItems(INavigationControlItem item, CommandBarFlyout menu)
964956
{
965957
var options = item.MenuOptions;
@@ -1058,17 +1050,11 @@ private List<ContextMenuFlyoutItemViewModel> GetLocationItemMenuItems(INavigatio
10581050
ItemType = ContextMenuFlyoutItemType.Separator,
10591051
ShowItem = Commands.OpenTerminalFromSidebar.IsExecutable ||
10601052
Commands.OpenStorageSenseFromSidebar.IsExecutable ||
1061-
options.ShowFormatDrive
1053+
Commands.FormatDriveFromSidebar.IsExecutable
10621054
},
10631055
new ContextMenuFlyoutItemViewModelBuilder(Commands.OpenTerminalFromSidebar).Build(),
10641056
new ContextMenuFlyoutItemViewModelBuilder(Commands.OpenStorageSenseFromSidebar).Build(),
1065-
new ContextMenuFlyoutItemViewModel()
1066-
{
1067-
Text = Strings.FormatDriveText.GetLocalizedResource(),
1068-
Command = FormatDriveCommand,
1069-
CommandParameter = item,
1070-
ShowItem = options.ShowFormatDrive
1071-
},
1057+
new ContextMenuFlyoutItemViewModelBuilder(Commands.FormatDriveFromSidebar).Build(),
10721058
new ContextMenuFlyoutItemViewModel()
10731059
{
10741060
ItemType = ContextMenuFlyoutItemType.Separator,

0 commit comments

Comments
 (0)