Skip to content

Commit 7282b47

Browse files
authored
Code Quality: Replace 'Format Drive' commands with actions (files-community#16468)
1 parent d963427 commit 7282b47

File tree

11 files changed

+104
-76
lines changed

11 files changed

+104
-76
lines changed

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

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,37 @@
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 &&
23-
(drivesViewModel.Drives.Cast<DriveItem>().FirstOrDefault(x =>
24-
string.Equals(x.Path, context.Folder?.ItemPath))?.MenuOptions.ShowFormatDrive ?? false);
21+
drivesViewModel.Drives
22+
.Cast<DriveItem>()
23+
.FirstOrDefault(x => string.Equals(x.Path, context.Folder?.ItemPath)) is DriveItem driveItem &&
24+
!(driveItem.Type == DriveType.Network || string.Equals(context.Folder?.ItemPath, $@"{Constants.UserEnvironmentPaths.SystemDrivePath}\", StringComparison.OrdinalIgnoreCase));
25+
26+
public virtual bool IsAccessibleGlobally
27+
=> true;
2528

2629
public FormatDriveAction()
2730
{
28-
context = Ioc.Default.GetRequiredService<IContentPageContext>();
29-
drivesViewModel = Ioc.Default.GetRequiredService<DrivesViewModel>();
30-
3131
context.PropertyChanged += Context_PropertyChanged;
3232
}
3333

34-
public Task ExecuteAsync(object? parameter = null)
34+
public virtual Task ExecuteAsync(object? parameter = null)
3535
{
3636
return Win32Helper.OpenFormatDriveDialog(context.Folder?.ItemPath ?? string.Empty);
3737
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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 FormatDriveFromHomeAction : FormatDriveAction
7+
{
8+
private IHomePageContext HomePageContext { get; } = Ioc.Default.GetRequiredService<IHomePageContext>();
9+
10+
private readonly DrivesViewModel drivesViewModel = Ioc.Default.GetRequiredService<DrivesViewModel>();
11+
12+
public override bool IsExecutable =>
13+
HomePageContext.IsAnyItemRightClicked &&
14+
HomePageContext.RightClickedItem is not null &&
15+
HomePageContext.RightClickedItem.Path is not null &&
16+
drivesViewModel.Drives
17+
.Cast<DriveItem>()
18+
.FirstOrDefault(x => string.Equals(x.Path, HomePageContext.RightClickedItem.Path)) is DriveItem driveItem &&
19+
!(driveItem.Type == DriveType.Network || string.Equals(HomePageContext.RightClickedItem.Path, $@"{Constants.UserEnvironmentPaths.SystemDrivePath}\", StringComparison.OrdinalIgnoreCase));
20+
21+
public override bool IsAccessibleGlobally
22+
=> false;
23+
24+
public override Task ExecuteAsync(object? parameter = null)
25+
{
26+
return Win32Helper.OpenFormatDriveDialog(HomePageContext?.RightClickedItem?.Path ?? string.Empty);
27+
}
28+
}
29+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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
17+
.Cast<DriveItem>()
18+
.FirstOrDefault(x => string.Equals(x.Path, SidebarContext.RightClickedItem.Path)) is DriveItem driveItem &&
19+
!(driveItem.Type == DriveType.Network || string.Equals(SidebarContext.RightClickedItem.Path, $@"{Constants.UserEnvironmentPaths.SystemDrivePath}\", StringComparison.OrdinalIgnoreCase));
20+
21+
public override bool IsAccessibleGlobally
22+
=> false;
23+
24+
public override Task ExecuteAsync(object? parameter = null)
25+
{
26+
return Win32Helper.OpenFormatDriveDialog(SidebarContext?.RightClickedItem?.Path ?? string.Empty);
27+
}
28+
}
29+
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ public enum CommandCodes
4747
CreateShortcutFromDialog,
4848
EmptyRecycleBin,
4949
FormatDrive,
50+
FormatDriveFromHome,
51+
FormatDriveFromSidebar,
5052
RestoreRecycleBin,
5153
RestoreAllRecycleBin,
5254
OpenItem,

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,8 @@ 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 FormatDriveFromHome => commands[CommandCodes.FormatDriveFromHome];
183+
public IRichCommand FormatDriveFromSidebar => commands[CommandCodes.FormatDriveFromSidebar];
182184
public IRichCommand NavigateBack => commands[CommandCodes.NavigateBack];
183185
public IRichCommand NavigateForward => commands[CommandCodes.NavigateForward];
184186
public IRichCommand NavigateUp => commands[CommandCodes.NavigateUp];
@@ -379,6 +381,8 @@ public IEnumerator<IRichCommand> GetEnumerator() =>
379381
[CommandCodes.NewWindow] = new NewWindowAction(),
380382
[CommandCodes.NewTab] = new NewTabAction(),
381383
[CommandCodes.FormatDrive] = new FormatDriveAction(),
384+
[CommandCodes.FormatDriveFromHome] = new FormatDriveFromHomeAction(),
385+
[CommandCodes.FormatDriveFromSidebar] = new FormatDriveFromSidebarAction(),
382386
[CommandCodes.NavigateBack] = new NavigateBackAction(),
383387
[CommandCodes.NavigateForward] = new NavigateForwardAction(),
384388
[CommandCodes.NavigateUp] = new NavigateUpAction(),

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ public interface ICommandManager : IEnumerable<IRichCommand>
5858
IRichCommand RestoreRecycleBin { get; }
5959
IRichCommand RestoreAllRecycleBin { get; }
6060
IRichCommand FormatDrive { get; }
61+
IRichCommand FormatDriveFromHome { get; }
62+
IRichCommand FormatDriveFromSidebar { get; }
6163
IRichCommand OpenItem { get; }
6264
IRichCommand OpenItemWithApplicationPicker { get; }
6365
IRichCommand OpenParentFolder { get; }

src/Files.App/Data/Contracts/INavigationControlItem.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,6 @@ public sealed class ContextMenuOptions
5454

5555
public bool ShowEjectDevice { get; set; }
5656

57-
public bool ShowFormatDrive { get; set; }
58-
5957
public bool ShowShellItems { get; set; }
6058
}
6159
}

src/Files.App/Data/Items/DriveItem.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,6 @@ public static async Task<DriveItem> CreateFromPropertiesAsync(StorageFolder root
239239
IsLocationItem = true,
240240
ShowEjectDevice = item.IsRemovable,
241241
ShowShellItems = true,
242-
ShowFormatDrive = !(item.Type == DriveType.Network || string.Equals(root.Path, $@"{Constants.UserEnvironmentPaths.SystemDrivePath}\", StringComparison.OrdinalIgnoreCase)),
243242
ShowProperties = true
244243
};
245244
item.Path = string.IsNullOrEmpty(root.Path) ? $"\\\\?\\{root.Name}\\" : root.Path;

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,

src/Files.App/ViewModels/UserControls/Widgets/DrivesWidgetViewModel.cs

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,14 @@ public sealed class DrivesWidgetViewModel : BaseWidgetViewModel, IWidgetViewMode
2020
public ObservableCollection<WidgetDriveCardItem> Items { get; } = [];
2121

2222
public string WidgetName => nameof(DrivesWidget);
23-
public string AutomationProperties => "Drives".GetLocalizedResource();
24-
public string WidgetHeader => "Drives".GetLocalizedResource();
23+
public string AutomationProperties => Strings.Drives.GetLocalizedResource();
24+
public string WidgetHeader => Strings.Drives.GetLocalizedResource();
2525
public bool IsWidgetSettingEnabled => UserSettingsService.GeneralSettingsService.ShowDrivesWidget;
2626
public bool ShowMenuFlyout => false;
2727
public MenuFlyoutItem? MenuFlyoutItem => null;
2828

2929
// Commands
3030

31-
private ICommand FormatDriveCommand { get; } = null!;
3231
private ICommand EjectDeviceCommand { get; } = null!;
3332
private ICommand OpenInNewPaneCommand { get; } = null!;
3433
private ICommand DisconnectNetworkDriveCommand { get; } = null!;
@@ -45,7 +44,6 @@ public DrivesWidgetViewModel()
4544
OpenInNewWindowCommand = new AsyncRelayCommand<WidgetCardItem>(ExecuteOpenInNewWindowCommand);
4645
PinToSidebarCommand = new AsyncRelayCommand<WidgetCardItem>(ExecutePinToSidebarCommand);
4746
UnpinFromSidebarCommand = new AsyncRelayCommand<WidgetCardItem>(ExecuteUnpinFromSidebarCommand);
48-
FormatDriveCommand = new RelayCommand<WidgetDriveCardItem>(ExecuteFormatDriveCommand);
4947
EjectDeviceCommand = new RelayCommand<WidgetDriveCardItem>(ExecuteEjectDeviceCommand);
5048
OpenInNewPaneCommand = new AsyncRelayCommand<WidgetDriveCardItem>(ExecuteOpenInNewPaneCommand);
5149
OpenPropertiesCommand = new RelayCommand<WidgetDriveCardItem>(ExecuteOpenPropertiesCommand);
@@ -99,43 +97,43 @@ public override List<ContextMenuFlyoutItemViewModel> GetItemMenuItems(WidgetCard
9997
new ContextMenuFlyoutItemViewModelBuilder(CommandManager.OpenInNewPaneFromHomeAction).Build(),
10098
new()
10199
{
102-
Text = "PinFolderToSidebar".GetLocalizedResource(),
100+
Text = Strings.PinFolderToSidebar.GetLocalizedResource(),
103101
ThemedIconModel = new ThemedIconModel() { ThemedIconStyle = "App.ThemedIcons.FavoritePin" },
104102
Command = PinToSidebarCommand,
105103
CommandParameter = item,
106104
ShowItem = !isPinned
107105
},
108106
new()
109107
{
110-
Text = "UnpinFolderFromSidebar".GetLocalizedResource(),
108+
Text = Strings.UnpinFolderFromSidebar.GetLocalizedResource(),
111109
ThemedIconModel = new ThemedIconModel() { ThemedIconStyle = "App.ThemedIcons.FavoritePinRemove" },
112110
Command = UnpinFromSidebarCommand,
113111
CommandParameter = item,
114112
ShowItem = isPinned
115113
},
116114
new()
117115
{
118-
Text = "Eject".GetLocalizedResource(),
116+
Text = Strings.Eject.GetLocalizedResource(),
119117
Command = EjectDeviceCommand,
120118
CommandParameter = item,
121119
ShowItem = options?.ShowEjectDevice ?? false
122120
},
123121
new()
124122
{
125-
Text = "Properties".GetLocalizedResource(),
123+
Text = Strings.Properties.GetLocalizedResource(),
126124
ThemedIconModel = new ThemedIconModel() { ThemedIconStyle = "App.ThemedIcons.Properties" },
127125
Command = OpenPropertiesCommand,
128126
CommandParameter = item
129127
},
130128
new()
131129
{
132-
Text = "TurnOnBitLocker".GetLocalizedResource(),
130+
Text = Strings.TurnOnBitLocker.GetLocalizedResource(),
133131
Tag = "TurnOnBitLockerPlaceholder",
134132
IsEnabled = false
135133
},
136134
new()
137135
{
138-
Text = "ManageBitLocker".GetLocalizedResource(),
136+
Text = Strings.ManageBitLocker.GetLocalizedResource(),
139137
Tag = "ManageBitLockerPlaceholder",
140138
IsEnabled = false
141139
},
@@ -144,25 +142,19 @@ public override List<ContextMenuFlyoutItemViewModel> GetItemMenuItems(WidgetCard
144142
ItemType = ContextMenuFlyoutItemType.Separator,
145143
ShowItem = CommandManager.OpenTerminalFromHome.IsExecutable ||
146144
CommandManager.OpenStorageSenseFromHome.IsExecutable ||
147-
(options?.ShowFormatDrive ?? false)
145+
CommandManager.FormatDriveFromHome.IsExecutable
148146
},
149147
new ContextMenuFlyoutItemViewModelBuilder(CommandManager.OpenTerminalFromHome).Build(),
150148
new ContextMenuFlyoutItemViewModelBuilder(CommandManager.OpenStorageSenseFromHome).Build(),
151-
new()
152-
{
153-
Text = "FormatDriveText".GetLocalizedResource(),
154-
Command = FormatDriveCommand,
155-
CommandParameter = item,
156-
ShowItem = options?.ShowFormatDrive ?? false
157-
},
149+
new ContextMenuFlyoutItemViewModelBuilder(CommandManager.FormatDriveFromHome).Build(),
158150
new()
159151
{
160152
ItemType = ContextMenuFlyoutItemType.Separator,
161153
Tag = "OverflowSeparator",
162154
},
163155
new()
164156
{
165-
Text = "Loading".GetLocalizedResource(),
157+
Text = Strings.Loading.GetLocalizedResource(),
166158
Glyph = "\xE712",
167159
Items = [],
168160
ID = "ItemOverflow",
@@ -190,11 +182,6 @@ private async Task ExecuteOpenInNewPaneCommand(WidgetDriveCardItem? item)
190182
ContentPageContext.ShellPage!.PaneHolder?.OpenSecondaryPane(item.Item.Path);
191183
}
192184

193-
private void ExecuteFormatDriveCommand(WidgetDriveCardItem? item)
194-
{
195-
Win32Helper.OpenFormatDriveDialog(item?.Path ?? string.Empty);
196-
}
197-
198185
private void ExecuteOpenPropertiesCommand(WidgetDriveCardItem? item)
199186
{
200187
if (!HomePageContext.IsAnyItemRightClicked || item is null)

0 commit comments

Comments
 (0)