Skip to content

Commit

Permalink
RichCommands: Format Drive (#11831)
Browse files Browse the repository at this point in the history
  • Loading branch information
hecksmosis authored Apr 2, 2023
1 parent 494ca6b commit aec3c54
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 17 deletions.
39 changes: 39 additions & 0 deletions src/Files.App/Actions/FileSystem/FormatDriveAction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.DependencyInjection;
using Files.App.Contexts;
using Files.App.Extensions;
using Files.App.Shell;
using Files.App.ViewModels;
using System.ComponentModel;
using System.Linq;
using System.Threading.Tasks;

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

public string Label { get; } = "FormatDriveText".GetLocalizedResource();

public string Description { get; } = "FormatDriveDescription".GetLocalizedResource();
public bool IsExecutable => context.HasItem && (App.DrivesManager.Drives.FirstOrDefault(x => string.Equals(x.Path, context.Folder?.ItemPath))?.MenuOptions.ShowFormatDrive ?? false);

public FormatDriveAction()
{
context.PropertyChanged += Context_PropertyChanged;
}

public Task ExecuteAsync()
{
Win32API.OpenFormatDriveDialog(context.Folder?.ItemPath ?? string.Empty);
return Task.CompletedTask;
}

public void Context_PropertyChanged(object? sender, PropertyChangedEventArgs e)
{
if (e.PropertyName is nameof(IContentPageContext.HasItem))
OnPropertyChanged(nameof(IsExecutable));
}
}
}
1 change: 1 addition & 0 deletions src/Files.App/Commands/CommandCodes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public enum CommandCodes
CreateShortcut,
CreateShortcutFromDialog,
EmptyRecycleBin,
FormatDrive,
RestoreRecycleBin,
RestoreAllRecycleBin,
OpenItem,
Expand Down
2 changes: 2 additions & 0 deletions src/Files.App/Commands/Manager/CommandManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ internal class CommandManager : ICommandManager
public IRichCommand GroupDescending => commands[CommandCodes.GroupDescending];
public IRichCommand ToggleGroupDirection => commands[CommandCodes.ToggleGroupDirection];
public IRichCommand NewTab => commands[CommandCodes.NewTab];
public IRichCommand FormatDrive => commands[CommandCodes.FormatDrive];
public IRichCommand NavigateBack => commands[CommandCodes.NavigateBack];
public IRichCommand NavigateForward => commands[CommandCodes.NavigateForward];
public IRichCommand NavigateUp => commands[CommandCodes.NavigateUp];
Expand Down Expand Up @@ -251,6 +252,7 @@ public CommandManager()
[CommandCodes.GroupDescending] = new GroupDescendingAction(),
[CommandCodes.ToggleGroupDirection] = new ToggleGroupDirectionAction(),
[CommandCodes.NewTab] = new NewTabAction(),
[CommandCodes.FormatDrive] = new FormatDriveAction(),
[CommandCodes.NavigateBack] = new NavigateBackAction(),
[CommandCodes.NavigateForward] = new NavigateForwardAction(),
[CommandCodes.NavigateUp] = new NavigateUpAction(),
Expand Down
1 change: 1 addition & 0 deletions src/Files.App/Commands/Manager/ICommandManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public interface ICommandManager : IEnumerable<IRichCommand>
IRichCommand EmptyRecycleBin { get; }
IRichCommand RestoreRecycleBin { get; }
IRichCommand RestoreAllRecycleBin { get; }
IRichCommand FormatDrive { get; }
IRichCommand OpenItem { get; }
IRichCommand OpenItemWithApplicationPicker { get; }
IRichCommand OpenParentFolder { get; }
Expand Down
8 changes: 1 addition & 7 deletions src/Files.App/Helpers/ContextFlyoutItemHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -219,13 +219,7 @@ public static List<ContextMenuFlyoutItemViewModel> GetBaseItemMenuItems(
ShowInZipPage = true,
ShowItem = !itemsSelected
},
new ContextMenuFlyoutItemViewModel()
{
Text = "FormatDriveText".GetLocalizedResource(),
Command = commandsViewModel.FormatDriveCommand,
CommandParameter = itemViewModel?.CurrentFolder,
ShowItem = itemViewModel?.CurrentFolder is not null && (App.DrivesManager.Drives.FirstOrDefault(x => string.Equals(x.Path, itemViewModel?.CurrentFolder.ItemPath))?.MenuOptions.ShowFormatDrive ?? false),
},
new ContextMenuFlyoutItemViewModelBuilder(commands.FormatDrive).Build(),
new ContextMenuFlyoutItemViewModelBuilder(commands.EmptyRecycleBin)
{
IsVisible = currentInstanceViewModel.IsPageTypeRecycleBin && !itemsSelected,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -301,11 +301,6 @@ public async Task PlayAll()
await NavigationHelpers.OpenSelectedItems(associatedInstance);
}

public void FormatDrive(ListedItem? e)
{
Win32API.OpenFormatDriveDialog(e?.ItemPath ?? string.Empty);
}

#endregion Command Implementation
}
}
3 changes: 0 additions & 3 deletions src/Files.App/Interacts/BaseLayoutCommandsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ private void InitializeCommands()
SearchUnindexedItems = new RelayCommand<RoutedEventArgs>(CommandsModel.SearchUnindexedItems);
CreateFolderWithSelection = new AsyncRelayCommand<RoutedEventArgs>(CommandsModel.CreateFolderWithSelection);
PlayAllCommand = new AsyncRelayCommand(CommandsModel.PlayAll);
FormatDriveCommand = new RelayCommand<ListedItem>(CommandsModel.FormatDrive);
}

#endregion Command Initialization
Expand Down Expand Up @@ -73,8 +72,6 @@ private void InitializeCommands()

public ICommand PlayAllCommand { get; private set; }

public ICommand FormatDriveCommand { get; private set; }

#endregion Commands

#region IDisposable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,5 @@ public interface IBaseLayoutCommandImplementationModel : IDisposable
Task CreateFolderWithSelection(RoutedEventArgs e);

Task PlayAll();

void FormatDrive(ListedItem? obj);
}
}
3 changes: 3 additions & 0 deletions src/Files.App/Strings/en-US/Resources.resw
Original file line number Diff line number Diff line change
Expand Up @@ -2661,6 +2661,9 @@
<data name="ExitCompactOverlayDescription" xml:space="preserve">
<value>Exit compact overlay</value>
</data>
<data name="FormatDriveDescription" xml:space="preserve">
<value>Opens the "Format Drive" menu for the selected item</value>
</data>
<data name="ToggleSidebar" xml:space="preserve">
<value>Toggle the sidebar</value>
</data>
Expand Down

0 comments on commit aec3c54

Please sign in to comment.