Skip to content

Commit 2c5963c

Browse files
authored
Feature: Add Copy, Cut and Delete RichCommands (#11504)
1 parent 6a112b0 commit 2c5963c

11 files changed

+109
-96
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using CommunityToolkit.Mvvm.DependencyInjection;
2+
using Files.App.Commands;
3+
using Files.App.Contexts;
4+
using Files.App.Extensions;
5+
using Files.App.Helpers;
6+
using System.Threading.Tasks;
7+
using Windows.System;
8+
9+
namespace Files.App.Actions
10+
{
11+
internal class CopyItemAction : IAction
12+
{
13+
public IContentPageContext context = Ioc.Default.GetRequiredService<IContentPageContext>();
14+
15+
public string Label { get; } = "Copy".GetLocalizedResource();
16+
17+
public HotKey HotKey = new(VirtualKey.C, VirtualKeyModifiers.Control);
18+
19+
public RichGlyph Glyph { get; } = new RichGlyph(opacityStyle: "ColorIconCopy");
20+
21+
public async Task ExecuteAsync()
22+
{
23+
await UIFilesystemHelpers.CopyItem(context.ShellPage);
24+
}
25+
}
26+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using CommunityToolkit.Mvvm.DependencyInjection;
2+
using Files.App.Commands;
3+
using Files.App.Contexts;
4+
using Files.App.Extensions;
5+
using Files.App.Helpers;
6+
using System.Threading.Tasks;
7+
using Windows.System;
8+
9+
namespace Files.App.Actions
10+
{
11+
internal class CutItemAction : IAction
12+
{
13+
public IContentPageContext context = Ioc.Default.GetRequiredService<IContentPageContext>();
14+
15+
public string Label { get; } = "BaseLayoutItemContextFlyoutCut/Text".GetLocalizedResource();
16+
17+
public HotKey HotKey = new(VirtualKey.X, VirtualKeyModifiers.Control);
18+
19+
public RichGlyph Glyph { get; } = new RichGlyph(opacityStyle: "ColorIconCut");
20+
21+
public async Task ExecuteAsync()
22+
{
23+
UIFilesystemHelpers.CutItem(context.ShellPage);
24+
}
25+
}
26+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using CommunityToolkit.Mvvm.DependencyInjection;
2+
using Files.App.Commands;
3+
using Files.App.Contexts;
4+
using Files.App.Extensions;
5+
using Files.App.Helpers;
6+
using System;
7+
using System.Collections.Generic;
8+
using System.Linq;
9+
using System.Text;
10+
using System.Threading.Tasks;
11+
using Windows.System;
12+
13+
namespace Files.App.Actions
14+
{
15+
internal class DeleteItemAction : IAction
16+
{
17+
public IContentPageContext context = Ioc.Default.GetRequiredService<IContentPageContext>();
18+
19+
public string Label { get; } = "Delete".GetLocalizedResource();
20+
21+
public RichGlyph Glyph { get; } = new RichGlyph(opacityStyle: "ColorIconDelete");
22+
23+
public HotKey HotKey = new(VirtualKey.Delete);
24+
25+
public async Task ExecuteAsync()
26+
{
27+
await RecycleBinHelpers.DeleteItem(context.ShellPage);
28+
}
29+
}
30+
}

src/Files.App/Commands/CommandCodes.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ public enum CommandCodes
1313
ToggleShowFileExtensions,
1414

1515
// File System
16+
CopyItem,
17+
CutItem,
18+
DeleteItem,
1619
CreateFolder,
1720
CreateShortcut,
1821
CreateShortcutFromDialog,

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ internal class CommandManager : ICommandManager
4646
public IRichCommand UnpinFromStart => commands[CommandCodes.UnpinFromStart];
4747
public IRichCommand PinItemToFavorites => commands[CommandCodes.PinItemToFavorites];
4848
public IRichCommand UnpinItemFromFavorites => commands[CommandCodes.UnpinItemFromFavorites];
49+
public IRichCommand CopyItem => commands[CommandCodes.CopyItem];
50+
public IRichCommand CutItem => commands[CommandCodes.CutItem];
51+
public IRichCommand DeleteItem => commands[CommandCodes.DeleteItem];
4952

5053
public CommandManager()
5154
{
@@ -83,6 +86,9 @@ public CommandManager()
8386
[CommandCodes.UnpinFromStart] = new UnpinFromStartAction(),
8487
[CommandCodes.PinItemToFavorites] = new PinItemAction(),
8588
[CommandCodes.UnpinItemFromFavorites] = new UnpinItemAction(),
89+
[CommandCodes.CopyItem] = new CopyItemAction(),
90+
[CommandCodes.CutItem] = new CutItemAction(),
91+
[CommandCodes.DeleteItem] = new DeleteItemAction(),
8692
};
8793

8894
[DebuggerDisplay("Command None")]

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@ public interface ICommandManager : IEnumerable<IRichCommand>
1818
IRichCommand ToggleShowHiddenItems { get; }
1919
IRichCommand ToggleShowFileExtensions { get; }
2020

21+
IRichCommand CopyItem { get; }
22+
IRichCommand CutItem { get; }
23+
IRichCommand DeleteItem { get; }
2124
IRichCommand MultiSelect { get; }
2225
IRichCommand SelectAll { get; }
2326
IRichCommand InvertSelection { get; }
2427
IRichCommand ClearSelection { get; }
25-
2628
IRichCommand CreateFolder { get; }
2729
IRichCommand CreateShortcut { get; }
2830
IRichCommand CreateShortcutFromDialog { get; }

src/Files.App/Helpers/ContextFlyoutItemHelper.cs

Lines changed: 9 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -749,47 +749,14 @@ public static List<ContextMenuFlyoutItemViewModel> GetBaseItemMenuItems(
749749
ShowInZipPage = true,
750750
ShowItem = itemsSelected
751751
},
752-
new ContextMenuFlyoutItemViewModel()
752+
new ContextMenuFlyoutItemViewModelBuilder(commands.CutItem)
753753
{
754-
Text = "BaseLayoutItemContextFlyoutCut/Text".GetLocalizedResource(),
755-
OpacityIcon = new OpacityIconModel()
756-
{
757-
OpacityIconStyle = "ColorIconCut",
758-
},
759-
Command = commandsViewModel.CutItemCommand,
760-
IsPrimary = true,
761-
KeyboardAccelerator = new KeyboardAccelerator
762-
{
763-
Key = VirtualKey.X,
764-
Modifiers = VirtualKeyModifiers.Control,
765-
IsEnabled = false,
766-
},
767-
ShowInSearchPage = true,
768-
ShowInFtpPage = true,
769-
ShowInZipPage = true,
770-
ShowItem = itemsSelected
771-
},
772-
new ContextMenuFlyoutItemViewModel()
754+
IsVisible = itemsSelected,
755+
}.Build(),
756+
new ContextMenuFlyoutItemViewModelBuilder(commands.CopyItem)
773757
{
774-
Text = "Copy".GetLocalizedResource(),
775-
OpacityIcon = new OpacityIconModel()
776-
{
777-
OpacityIconStyle = "ColorIconCopy",
778-
},
779-
Command = commandsViewModel.CopyItemCommand,
780-
ShowInRecycleBin = true,
781-
ShowInSearchPage = true,
782-
ShowInFtpPage = true,
783-
ShowInZipPage = true,
784-
IsPrimary = true,
785-
KeyboardAccelerator = new KeyboardAccelerator
786-
{
787-
Key = VirtualKey.C,
788-
Modifiers = VirtualKeyModifiers.Control,
789-
IsEnabled = false,
790-
},
791-
ShowItem = itemsSelected
792-
},
758+
IsVisible = itemsSelected,
759+
}.Build(),
793760
new ContextMenuFlyoutItemViewModel()
794761
{
795762
Text = "CopyLocation".GetLocalizedResource(),
@@ -871,26 +838,10 @@ public static List<ContextMenuFlyoutItemViewModel> GetBaseItemMenuItems(
871838
Command = commandsViewModel.ShareItemCommand,
872839
ShowItem = itemsSelected && DataTransferManager.IsSupported() && !selectedItems.Any(i => i.IsHiddenItem || (i.IsShortcut && !i.IsLinkItem) || (i.PrimaryItemAttribute == StorageItemTypes.Folder && !i.IsArchive)),
873840
},
874-
new ContextMenuFlyoutItemViewModel()
841+
new ContextMenuFlyoutItemViewModelBuilder(commands.DeleteItem)
875842
{
876-
Text = "Delete".GetLocalizedResource(),
877-
IsPrimary = true,
878-
OpacityIcon = new OpacityIconModel()
879-
{
880-
OpacityIconStyle = "ColorIconDelete",
881-
},
882-
Command = commandsViewModel.DeleteItemCommand,
883-
ShowInRecycleBin = true,
884-
ShowInSearchPage = true,
885-
ShowInFtpPage = true,
886-
ShowInZipPage = true,
887-
KeyboardAccelerator = new KeyboardAccelerator
888-
{
889-
Key = VirtualKey.Delete,
890-
IsEnabled = false,
891-
},
892-
ShowItem = itemsSelected
893-
},
843+
IsVisible = itemsSelected,
844+
}.Build(),
894845
new ContextMenuFlyoutItemViewModel()
895846
{
896847
Text = "Properties".GetLocalizedResource(),

src/Files.App/Interacts/BaseLayoutCommandImplementationModel.cs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -142,26 +142,11 @@ public virtual async void QuickLook(RoutedEventArgs e)
142142
await QuickLookHelpers.ToggleQuickLook(associatedInstance);
143143
}
144144

145-
public virtual async void CopyItem(RoutedEventArgs e)
146-
{
147-
await UIFilesystemHelpers.CopyItem(associatedInstance);
148-
}
149-
150-
public virtual void CutItem(RoutedEventArgs e)
151-
{
152-
UIFilesystemHelpers.CutItem(associatedInstance);
153-
}
154-
155145
public virtual async void RestoreItem(RoutedEventArgs e)
156146
{
157147
await RecycleBinHelpers.RestoreItem(associatedInstance);
158148
}
159149

160-
public virtual async void DeleteItem(RoutedEventArgs e)
161-
{
162-
await RecycleBinHelpers.DeleteItem(associatedInstance);
163-
}
164-
165150
public virtual void ShowFolderProperties(RoutedEventArgs e)
166151
=> ShowProperties(e);
167152

src/Files.App/Interacts/BaseLayoutCommandsViewModel.cs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,7 @@ private void InitializeCommands()
3737
RestoreRecycleBinCommand = new RelayCommand<RoutedEventArgs>(CommandsModel.RestoreRecycleBin);
3838
RestoreSelectionRecycleBinCommand = new RelayCommand<RoutedEventArgs>(CommandsModel.RestoreSelectionRecycleBin);
3939
QuickLookCommand = new RelayCommand<RoutedEventArgs>(CommandsModel.QuickLook);
40-
CopyItemCommand = new RelayCommand<RoutedEventArgs>(CommandsModel.CopyItem);
41-
CutItemCommand = new RelayCommand<RoutedEventArgs>(CommandsModel.CutItem);
4240
RestoreItemCommand = new RelayCommand<RoutedEventArgs>(CommandsModel.RestoreItem);
43-
DeleteItemCommand = new RelayCommand<RoutedEventArgs>(CommandsModel.DeleteItem);
4441
ShowFolderPropertiesCommand = new RelayCommand<RoutedEventArgs>(CommandsModel.ShowFolderProperties);
4542
ShowPropertiesCommand = new RelayCommand<RoutedEventArgs>(CommandsModel.ShowProperties);
4643
OpenFileLocationCommand = new RelayCommand<RoutedEventArgs>(CommandsModel.OpenFileLocation);
@@ -92,10 +89,6 @@ private void InitializeCommands()
9289

9390
public ICommand RunAsAnotherUserCommand { get; private set; }
9491

95-
public ICommand SidebarPinItemCommand { get; private set; }
96-
97-
public ICommand SidebarUnpinItemCommand { get; private set; }
98-
9992
public ICommand OpenItemCommand { get; private set; }
10093

10194
public ICommand RestoreRecycleBinCommand { get; private set; }
@@ -104,14 +97,8 @@ private void InitializeCommands()
10497

10598
public ICommand QuickLookCommand { get; private set; }
10699

107-
public ICommand CopyItemCommand { get; private set; }
108-
109-
public ICommand CutItemCommand { get; private set; }
110-
111100
public ICommand RestoreItemCommand { get; private set; }
112101

113-
public ICommand DeleteItemCommand { get; private set; }
114-
115102
public ICommand ShowFolderPropertiesCommand { get; private set; }
116103

117104
public ICommand ShowPropertiesCommand { get; private set; }

src/Files.App/Interacts/IBaseLayoutCommandImplementationModel.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,8 @@ public interface IBaseLayoutCommandImplementationModel : IDisposable
3333

3434
void QuickLook(RoutedEventArgs e);
3535

36-
void CopyItem(RoutedEventArgs e);
37-
38-
void CutItem(RoutedEventArgs e);
39-
4036
void RestoreItem(RoutedEventArgs e);
4137

42-
void DeleteItem(RoutedEventArgs e);
43-
4438
void ShowFolderProperties(RoutedEventArgs e);
4539

4640
void ShowProperties(RoutedEventArgs e);

src/Files.App/Views/BaseShellPage.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using CommunityToolkit.Mvvm.DependencyInjection;
22
using CommunityToolkit.Mvvm.Input;
33
using CommunityToolkit.WinUI;
4+
using Files.App.Commands;
45
using Files.App.DataModels;
56
using Files.App.EventArguments;
67
using Files.App.Extensions;
@@ -30,6 +31,7 @@
3031
using System.Diagnostics;
3132
using System.Linq;
3233
using System.Runtime.CompilerServices;
34+
using System.Security.Policy;
3335
using System.Threading;
3436
using System.Threading.Tasks;
3537
using Windows.Storage;
@@ -60,6 +62,7 @@ public abstract class BaseShellPage : Page, IShellPage, INotifyPropertyChanged
6062

6163
public IBaseLayout SlimContentPage => ContentPage;
6264

65+
public ICommandManager commands = Ioc.Default.GetRequiredService<ICommandManager>();
6366
public IFilesystemHelpers FilesystemHelpers { get; protected set; }
6467

6568
public Type CurrentPageType => ItemDisplay.SourcePageType;
@@ -630,11 +633,11 @@ protected void InitToolbarCommands()
630633
ToolbarViewModel.CreateNewFileCommand = new RelayCommand<ShellNewEntry>(x => UIFilesystemHelpers.CreateFileFromDialogResultType(AddItemDialogItemType.File, x, this));
631634
ToolbarViewModel.CreateNewFolderCommand = new RelayCommand(() => UIFilesystemHelpers.CreateFileFromDialogResultType(AddItemDialogItemType.Folder, null, this));
632635
ToolbarViewModel.CreateNewShortcutCommand = new RelayCommand(() => CreateNewShortcutFromDialog());
633-
ToolbarViewModel.CopyCommand = new RelayCommand(async () => await UIFilesystemHelpers.CopyItem(this));
636+
ToolbarViewModel.CopyCommand = new RelayCommand(async () => commands.CopyItem.Execute(null));
634637
ToolbarViewModel.Rename = new RelayCommand(() => SlimContentPage?.CommandsViewModel.RenameItemCommand.Execute(null));
635638
ToolbarViewModel.Share = new RelayCommand(() => SlimContentPage?.CommandsViewModel.ShareItemCommand.Execute(null));
636-
ToolbarViewModel.DeleteCommand = new RelayCommand(() => SlimContentPage?.CommandsViewModel.DeleteItemCommand.Execute(null));
637-
ToolbarViewModel.CutCommand = new RelayCommand(() => SlimContentPage?.CommandsViewModel.CutItemCommand.Execute(null));
639+
ToolbarViewModel.DeleteCommand = new RelayCommand(() => commands.DeleteItem.Execute(null));
640+
ToolbarViewModel.CutCommand = new RelayCommand(() => commands.CutItem.Execute(null));
638641
ToolbarViewModel.RestoreRecycleBinCommand = new RelayCommand(() => SlimContentPage?.CommandsViewModel.RestoreRecycleBinCommand.Execute(null));
639642
ToolbarViewModel.RestoreSelectionRecycleBinCommand = new RelayCommand(() => SlimContentPage?.CommandsViewModel.RestoreSelectionRecycleBinCommand.Execute(null));
640643
ToolbarViewModel.RunWithPowerShellCommand = new RelayCommand(async () => await Win32Helpers.InvokeWin32ComponentAsync("powershell", this, PathNormalization.NormalizePath(SlimContentPage?.SelectedItem.ItemPath)));

0 commit comments

Comments
 (0)