Skip to content

Commit aec3c54

Browse files
authored
RichCommands: Format Drive (#11831)
1 parent 494ca6b commit aec3c54

File tree

9 files changed

+47
-17
lines changed

9 files changed

+47
-17
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using CommunityToolkit.Mvvm.ComponentModel;
2+
using CommunityToolkit.Mvvm.DependencyInjection;
3+
using Files.App.Contexts;
4+
using Files.App.Extensions;
5+
using Files.App.Shell;
6+
using Files.App.ViewModels;
7+
using System.ComponentModel;
8+
using System.Linq;
9+
using System.Threading.Tasks;
10+
11+
namespace Files.App.Actions
12+
{
13+
internal class FormatDriveAction : ObservableObject, IAction
14+
{
15+
private readonly IContentPageContext context = Ioc.Default.GetRequiredService<IContentPageContext>();
16+
17+
public string Label { get; } = "FormatDriveText".GetLocalizedResource();
18+
19+
public string Description { get; } = "FormatDriveDescription".GetLocalizedResource();
20+
public bool IsExecutable => context.HasItem && (App.DrivesManager.Drives.FirstOrDefault(x => string.Equals(x.Path, context.Folder?.ItemPath))?.MenuOptions.ShowFormatDrive ?? false);
21+
22+
public FormatDriveAction()
23+
{
24+
context.PropertyChanged += Context_PropertyChanged;
25+
}
26+
27+
public Task ExecuteAsync()
28+
{
29+
Win32API.OpenFormatDriveDialog(context.Folder?.ItemPath ?? string.Empty);
30+
return Task.CompletedTask;
31+
}
32+
33+
public void Context_PropertyChanged(object? sender, PropertyChangedEventArgs e)
34+
{
35+
if (e.PropertyName is nameof(IContentPageContext.HasItem))
36+
OnPropertyChanged(nameof(IsExecutable));
37+
}
38+
}
39+
}

src/Files.App/Commands/CommandCodes.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public enum CommandCodes
2929
CreateShortcut,
3030
CreateShortcutFromDialog,
3131
EmptyRecycleBin,
32+
FormatDrive,
3233
RestoreRecycleBin,
3334
RestoreAllRecycleBin,
3435
OpenItem,

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ internal class CommandManager : ICommandManager
115115
public IRichCommand GroupDescending => commands[CommandCodes.GroupDescending];
116116
public IRichCommand ToggleGroupDirection => commands[CommandCodes.ToggleGroupDirection];
117117
public IRichCommand NewTab => commands[CommandCodes.NewTab];
118+
public IRichCommand FormatDrive => commands[CommandCodes.FormatDrive];
118119
public IRichCommand NavigateBack => commands[CommandCodes.NavigateBack];
119120
public IRichCommand NavigateForward => commands[CommandCodes.NavigateForward];
120121
public IRichCommand NavigateUp => commands[CommandCodes.NavigateUp];
@@ -251,6 +252,7 @@ public CommandManager()
251252
[CommandCodes.GroupDescending] = new GroupDescendingAction(),
252253
[CommandCodes.ToggleGroupDirection] = new ToggleGroupDirectionAction(),
253254
[CommandCodes.NewTab] = new NewTabAction(),
255+
[CommandCodes.FormatDrive] = new FormatDriveAction(),
254256
[CommandCodes.NavigateBack] = new NavigateBackAction(),
255257
[CommandCodes.NavigateForward] = new NavigateForwardAction(),
256258
[CommandCodes.NavigateUp] = new NavigateUpAction(),

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public interface ICommandManager : IEnumerable<IRichCommand>
3838
IRichCommand EmptyRecycleBin { get; }
3939
IRichCommand RestoreRecycleBin { get; }
4040
IRichCommand RestoreAllRecycleBin { get; }
41+
IRichCommand FormatDrive { get; }
4142
IRichCommand OpenItem { get; }
4243
IRichCommand OpenItemWithApplicationPicker { get; }
4344
IRichCommand OpenParentFolder { get; }

src/Files.App/Helpers/ContextFlyoutItemHelper.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -219,13 +219,7 @@ public static List<ContextMenuFlyoutItemViewModel> GetBaseItemMenuItems(
219219
ShowInZipPage = true,
220220
ShowItem = !itemsSelected
221221
},
222-
new ContextMenuFlyoutItemViewModel()
223-
{
224-
Text = "FormatDriveText".GetLocalizedResource(),
225-
Command = commandsViewModel.FormatDriveCommand,
226-
CommandParameter = itemViewModel?.CurrentFolder,
227-
ShowItem = itemViewModel?.CurrentFolder is not null && (App.DrivesManager.Drives.FirstOrDefault(x => string.Equals(x.Path, itemViewModel?.CurrentFolder.ItemPath))?.MenuOptions.ShowFormatDrive ?? false),
228-
},
222+
new ContextMenuFlyoutItemViewModelBuilder(commands.FormatDrive).Build(),
229223
new ContextMenuFlyoutItemViewModelBuilder(commands.EmptyRecycleBin)
230224
{
231225
IsVisible = currentInstanceViewModel.IsPageTypeRecycleBin && !itemsSelected,

src/Files.App/Interacts/BaseLayoutCommandImplementationModel.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -301,11 +301,6 @@ public async Task PlayAll()
301301
await NavigationHelpers.OpenSelectedItems(associatedInstance);
302302
}
303303

304-
public void FormatDrive(ListedItem? e)
305-
{
306-
Win32API.OpenFormatDriveDialog(e?.ItemPath ?? string.Empty);
307-
}
308-
309304
#endregion Command Implementation
310305
}
311306
}

src/Files.App/Interacts/BaseLayoutCommandsViewModel.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ private void InitializeCommands()
4040
SearchUnindexedItems = new RelayCommand<RoutedEventArgs>(CommandsModel.SearchUnindexedItems);
4141
CreateFolderWithSelection = new AsyncRelayCommand<RoutedEventArgs>(CommandsModel.CreateFolderWithSelection);
4242
PlayAllCommand = new AsyncRelayCommand(CommandsModel.PlayAll);
43-
FormatDriveCommand = new RelayCommand<ListedItem>(CommandsModel.FormatDrive);
4443
}
4544

4645
#endregion Command Initialization
@@ -73,8 +72,6 @@ private void InitializeCommands()
7372

7473
public ICommand PlayAllCommand { get; private set; }
7574

76-
public ICommand FormatDriveCommand { get; private set; }
77-
7875
#endregion Commands
7976

8077
#region IDisposable

src/Files.App/Interacts/IBaseLayoutCommandImplementationModel.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,5 @@ public interface IBaseLayoutCommandImplementationModel : IDisposable
3434
Task CreateFolderWithSelection(RoutedEventArgs e);
3535

3636
Task PlayAll();
37-
38-
void FormatDrive(ListedItem? obj);
3937
}
4038
}

src/Files.App/Strings/en-US/Resources.resw

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2661,6 +2661,9 @@
26612661
<data name="ExitCompactOverlayDescription" xml:space="preserve">
26622662
<value>Exit compact overlay</value>
26632663
</data>
2664+
<data name="FormatDriveDescription" xml:space="preserve">
2665+
<value>Opens the "Format Drive" menu for the selected item</value>
2666+
</data>
26642667
<data name="ToggleSidebar" xml:space="preserve">
26652668
<value>Toggle the sidebar</value>
26662669
</data>

0 commit comments

Comments
 (0)