diff --git a/src/Files.App/Actions/FileSystem/FormatDriveAction.cs b/src/Files.App/Actions/FileSystem/FormatDriveAction.cs new file mode 100644 index 000000000000..a4305e87d6ae --- /dev/null +++ b/src/Files.App/Actions/FileSystem/FormatDriveAction.cs @@ -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(); + + 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)); + } + } +} diff --git a/src/Files.App/Commands/CommandCodes.cs b/src/Files.App/Commands/CommandCodes.cs index dc44a63877ba..9de1ef056c88 100644 --- a/src/Files.App/Commands/CommandCodes.cs +++ b/src/Files.App/Commands/CommandCodes.cs @@ -29,6 +29,7 @@ public enum CommandCodes CreateShortcut, CreateShortcutFromDialog, EmptyRecycleBin, + FormatDrive, RestoreRecycleBin, RestoreAllRecycleBin, OpenItem, diff --git a/src/Files.App/Commands/Manager/CommandManager.cs b/src/Files.App/Commands/Manager/CommandManager.cs index b0ba7f1e1a63..33828601fc8a 100644 --- a/src/Files.App/Commands/Manager/CommandManager.cs +++ b/src/Files.App/Commands/Manager/CommandManager.cs @@ -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]; @@ -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(), diff --git a/src/Files.App/Commands/Manager/ICommandManager.cs b/src/Files.App/Commands/Manager/ICommandManager.cs index eca09476ae38..b3ab311cf220 100644 --- a/src/Files.App/Commands/Manager/ICommandManager.cs +++ b/src/Files.App/Commands/Manager/ICommandManager.cs @@ -38,6 +38,7 @@ public interface ICommandManager : IEnumerable IRichCommand EmptyRecycleBin { get; } IRichCommand RestoreRecycleBin { get; } IRichCommand RestoreAllRecycleBin { get; } + IRichCommand FormatDrive { get; } IRichCommand OpenItem { get; } IRichCommand OpenItemWithApplicationPicker { get; } IRichCommand OpenParentFolder { get; } diff --git a/src/Files.App/Helpers/ContextFlyoutItemHelper.cs b/src/Files.App/Helpers/ContextFlyoutItemHelper.cs index 52149e17e8f3..abac3d7c5e25 100644 --- a/src/Files.App/Helpers/ContextFlyoutItemHelper.cs +++ b/src/Files.App/Helpers/ContextFlyoutItemHelper.cs @@ -219,13 +219,7 @@ public static List 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, diff --git a/src/Files.App/Interacts/BaseLayoutCommandImplementationModel.cs b/src/Files.App/Interacts/BaseLayoutCommandImplementationModel.cs index 7cd3dbe40d69..99a596890190 100644 --- a/src/Files.App/Interacts/BaseLayoutCommandImplementationModel.cs +++ b/src/Files.App/Interacts/BaseLayoutCommandImplementationModel.cs @@ -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 } } diff --git a/src/Files.App/Interacts/BaseLayoutCommandsViewModel.cs b/src/Files.App/Interacts/BaseLayoutCommandsViewModel.cs index 92693e355378..50c381854617 100644 --- a/src/Files.App/Interacts/BaseLayoutCommandsViewModel.cs +++ b/src/Files.App/Interacts/BaseLayoutCommandsViewModel.cs @@ -40,7 +40,6 @@ private void InitializeCommands() SearchUnindexedItems = new RelayCommand(CommandsModel.SearchUnindexedItems); CreateFolderWithSelection = new AsyncRelayCommand(CommandsModel.CreateFolderWithSelection); PlayAllCommand = new AsyncRelayCommand(CommandsModel.PlayAll); - FormatDriveCommand = new RelayCommand(CommandsModel.FormatDrive); } #endregion Command Initialization @@ -73,8 +72,6 @@ private void InitializeCommands() public ICommand PlayAllCommand { get; private set; } - public ICommand FormatDriveCommand { get; private set; } - #endregion Commands #region IDisposable diff --git a/src/Files.App/Interacts/IBaseLayoutCommandImplementationModel.cs b/src/Files.App/Interacts/IBaseLayoutCommandImplementationModel.cs index b1fffe2664e9..e6475415bbef 100644 --- a/src/Files.App/Interacts/IBaseLayoutCommandImplementationModel.cs +++ b/src/Files.App/Interacts/IBaseLayoutCommandImplementationModel.cs @@ -34,7 +34,5 @@ public interface IBaseLayoutCommandImplementationModel : IDisposable Task CreateFolderWithSelection(RoutedEventArgs e); Task PlayAll(); - - void FormatDrive(ListedItem? obj); } } diff --git a/src/Files.App/Strings/en-US/Resources.resw b/src/Files.App/Strings/en-US/Resources.resw index 8b9cfa35c152..c915e21f0b0d 100644 --- a/src/Files.App/Strings/en-US/Resources.resw +++ b/src/Files.App/Strings/en-US/Resources.resw @@ -2661,6 +2661,9 @@ Exit compact overlay + + Opens the "Format Drive" menu for the selected item + Toggle the sidebar