Skip to content

Feature: Implement more RichCommands #11493

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/Files.App/Actions/FileSystem/CreateFolderAction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using CommunityToolkit.Mvvm.DependencyInjection;
using Files.App.Commands;
using Files.App.Contexts;
using Files.App.Extensions;
using Files.App.Helpers;
using Files.Backend.Enums;
using System.Threading.Tasks;

namespace Files.App.Actions
{
internal class CreateFolderAction : IAction
{
public IContentPageContext context = Ioc.Default.GetRequiredService<IContentPageContext>();

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

public RichGlyph Glyph { get; } = new RichGlyph(baseGlyph: "\uE8B7");

public async Task ExecuteAsync()
{
UIFilesystemHelpers.CreateFileFromDialogResultType(AddItemDialogItemType.Folder, null!, context.ShellPage!);
}
}
}
39 changes: 39 additions & 0 deletions src/Files.App/Actions/FileSystem/CreateShortcutAction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using CommunityToolkit.Mvvm.DependencyInjection;
using Files.App.Commands;
using Files.App.Contexts;
using Files.App.Extensions;
using Files.App.Filesystem;
using Files.App.Helpers;
using System.IO;
using System.Threading.Tasks;

namespace Files.App.Actions
{
internal class CreateShortcutAction : IAction
{
public IContentPageContext context = Ioc.Default.GetRequiredService<IContentPageContext>();

public string Label { get; } = "BaseLayoutItemContextFlyoutShortcut/Text".GetLocalizedResource();

public RichGlyph Glyph { get; } = new RichGlyph(opacityStyle: "ColorIconShortcut");

public async Task ExecuteAsync()
{
var currentPath = context.ShellPage?.FilesystemViewModel.WorkingDirectory;

if (App.LibraryManager.TryGetLibrary(currentPath ?? string.Empty, out var library) && !library.IsEmpty)
{
currentPath = library.DefaultSaveFolder;
}

foreach (ListedItem selectedItem in context.SelectedItems)
{
var fileName = string.Format("ShortcutCreateNewSuffix".GetLocalizedResource(), selectedItem.Name) + ".lnk";
var filePath = Path.Combine(currentPath ?? string.Empty, fileName);

if (!await FileOperationsHelpers.CreateOrUpdateLinkAsync(filePath, selectedItem.ItemPath))
await UIFilesystemHelpers.HandleShortcutCannotBeCreated(fileName, selectedItem.ItemPath);
}
}
}
}
23 changes: 23 additions & 0 deletions src/Files.App/Actions/FileSystem/CreateShortcutFromDialogAction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using CommunityToolkit.Mvvm.DependencyInjection;
using Files.App.Commands;
using Files.App.Contexts;
using Files.App.Extensions;
using Files.App.Helpers;
using System.Threading.Tasks;

namespace Files.App.Actions
{
internal class CreateShortcutFromDialogAction : IAction
{
public IContentPageContext context = Ioc.Default.GetRequiredService<IContentPageContext>();

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

public RichGlyph Glyph { get; } = new RichGlyph(baseGlyph: "\uF10A", fontFamily: "CustomGlyph");

public async Task ExecuteAsync()
{
await UIFilesystemHelpers.CreateShortcutFromDialogAsync(context.ShellPage);
}
}
}
35 changes: 35 additions & 0 deletions src/Files.App/Actions/Start/PinToStartAction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using CommunityToolkit.Mvvm.DependencyInjection;
using Files.App.Commands;
using Files.App.Contexts;
using Files.App.Extensions;
using Files.App.Filesystem;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Files.App.Actions
{
internal class PinToStartAction : IAction
{
public IContentPageContext context = Ioc.Default.GetRequiredService<IContentPageContext>();

public string Label { get; } = "PinItemToStart/Text".GetLocalizedResource();

public RichGlyph Glyph { get; } = new RichGlyph(opacityStyle: "ColorIconPinToFavorites");

public async Task ExecuteAsync()
{
if (context.SelectedItems.Count > 0)
{
foreach (ListedItem listedItem in context.ShellPage?.SlimContentPage.SelectedItems)
await App.SecondaryTileHelper.TryPinFolderAsync(listedItem.ItemPath, listedItem.Name);
}
else
{
await App.SecondaryTileHelper.TryPinFolderAsync(context.ShellPage?.FilesystemViewModel.CurrentFolder.ItemPath, context.ShellPage?.FilesystemViewModel.CurrentFolder.Name);
}
}
}
}
35 changes: 35 additions & 0 deletions src/Files.App/Actions/Start/UnpinFromStartAction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using CommunityToolkit.Mvvm.DependencyInjection;
using Files.App.Commands;
using Files.App.Contexts;
using Files.App.Extensions;
using Files.App.Filesystem;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Files.App.Actions
{
internal class UnpinFromStartAction : IAction
{
public IContentPageContext context = Ioc.Default.GetRequiredService<IContentPageContext>();

public string Label { get; } = "UnpinItemFromStart/Text".GetLocalizedResource();

public RichGlyph Glyph { get; } = new RichGlyph(opacityStyle: "ColorIconUnpinFromFavorites");

public async Task ExecuteAsync()
{
if (context.SelectedItems.Count > 0)
{
foreach (ListedItem listedItem in context.ShellPage?.SlimContentPage.SelectedItems)
await App.SecondaryTileHelper.TryPinFolderAsync(listedItem.ItemPath, listedItem.Name);
}
else
{
await App.SecondaryTileHelper.TryPinFolderAsync(context.ShellPage?.FilesystemViewModel.CurrentFolder.ItemPath, context.ShellPage?.FilesystemViewModel.CurrentFolder.Name);
}
}
}
}
7 changes: 7 additions & 0 deletions src/Files.App/Commands/CommandCodes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,17 @@ public enum CommandCodes
ToggleShowFileExtensions,

// file system
CreateFolder,
CreateShortcut,
CreateShortcutFromDialog,
EmptyRecycleBin,
RestoreRecycleBin,
RestoreAllRecycleBin,

// Start
PinToStart,
UnpinFromStart,

// Favorites
PinItemToFavorites,
UnpinItemFromFavorites,
Expand Down
12 changes: 11 additions & 1 deletion src/Files.App/Commands/Manager/CommandManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,14 @@ internal class CommandManager : ICommandManager
public IRichCommand EmptyRecycleBin => commands[CommandCodes.EmptyRecycleBin];
public IRichCommand RestoreRecycleBin => commands[CommandCodes.RestoreRecycleBin];
public IRichCommand RestoreAllRecycleBin => commands[CommandCodes.RestoreAllRecycleBin];
public IRichCommand CreateShortcut => commands[CommandCodes.CreateShortcut];
public IRichCommand CreateShortcutFromDialog => commands[CommandCodes.CreateShortcutFromDialog];
public IRichCommand CreateFolder => commands[CommandCodes.CreateFolder];
public IRichCommand PinToStart => commands[CommandCodes.PinToStart];
public IRichCommand UnpinFromStart => commands[CommandCodes.UnpinFromStart];
public IRichCommand PinItemToFavorites => commands[CommandCodes.PinItemToFavorites];
public IRichCommand UnpinItemFromFavorites => commands[CommandCodes.UnpinItemFromFavorites];

public CommandManager()
{
commands = CreateActions()
Expand All @@ -63,6 +68,11 @@ public CommandManager()
[CommandCodes.EmptyRecycleBin] = new EmptyRecycleBinAction(),
[CommandCodes.RestoreRecycleBin] = new RestoreRecycleBinAction(),
[CommandCodes.RestoreAllRecycleBin] = new RestoreAllRecycleBinAction(),
[CommandCodes.CreateShortcut] = new CreateShortcutAction(),
[CommandCodes.CreateShortcutFromDialog] = new CreateShortcutFromDialogAction(),
[CommandCodes.CreateFolder] = new CreateFolderAction(),
[CommandCodes.PinToStart] = new PinToStartAction(),
[CommandCodes.UnpinFromStart] = new UnpinFromStartAction(),
[CommandCodes.PinItemToFavorites] = new PinItemAction(),
[CommandCodes.UnpinItemFromFavorites] = new UnpinItemAction(),
};
Expand Down
5 changes: 5 additions & 0 deletions src/Files.App/Commands/Manager/ICommandManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,15 @@ public interface ICommandManager : IEnumerable<IRichCommand>
IRichCommand ToggleShowHiddenItems { get; }
IRichCommand ToggleShowFileExtensions { get; }

IRichCommand CreateFolder { get; }
IRichCommand CreateShortcut { get; }
IRichCommand CreateShortcutFromDialog { get; }
IRichCommand EmptyRecycleBin { get; }
IRichCommand RestoreRecycleBin { get; }
IRichCommand RestoreAllRecycleBin { get; }

IRichCommand PinToStart { get; }
IRichCommand UnpinFromStart { get; }
IRichCommand PinItemToFavorites { get; }
IRichCommand UnpinItemFromFavorites { get; }
}
Expand Down
63 changes: 11 additions & 52 deletions src/Files.App/Helpers/ContextFlyoutItemHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -838,18 +838,10 @@ public static List<ContextMenuFlyoutItemViewModel> GetBaseItemMenuItems(
Command = commandsViewModel.CreateFolderWithSelection,
ShowItem = itemsSelected,
},
new ContextMenuFlyoutItemViewModel()
new ContextMenuFlyoutItemViewModelBuilder(commands.CreateShortcut)
{
Text = "BaseLayoutItemContextFlyoutShortcut/Text".GetLocalizedResource(),
OpacityIcon = new OpacityIconModel()
{
OpacityIconStyle = "ColorIconShortcut",
},
Command = commandsViewModel.CreateShortcutCommand,
ShowItem = itemsSelected && (!selectedItems.FirstOrDefault()?.IsShortcut ?? false),
SingleItemOnly = true,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just noticed this was removed, create shortcut should only show if a single item is selected.

ShowInSearchPage = true,
},
IsVisible = itemsSelected && (!selectedItems.FirstOrDefault()?.IsShortcut ?? false),
}.Build(),
new ContextMenuFlyoutItemViewModel()
{
Text = "Rename".GetLocalizedResource(),
Expand Down Expand Up @@ -932,34 +924,14 @@ public static List<ContextMenuFlyoutItemViewModel> GetBaseItemMenuItems(
{
IsVisible = userSettingsService.PreferencesSettingsService.ShowFavoritesSection && selectedItems.All(x => x.PrimaryItemAttribute == StorageItemTypes.Folder && !x.IsArchive && x.IsPinned),
}.Build(),
new ContextMenuFlyoutItemViewModel()
new ContextMenuFlyoutItemViewModelBuilder(commands.PinToStart)
{
Text = "PinItemToStart/Text".GetLocalizedResource(),
OpacityIcon = new OpacityIconModel()
{
OpacityIconStyle = "ColorIconPinToFavorites",
},
Command = commandsViewModel.PinItemToStartCommand,
ShowOnShift = true,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like show on shift was also removed, can you add this back?

ShowItem = selectedItems.All(x => !x.IsShortcut && (x.PrimaryItemAttribute == StorageItemTypes.Folder || x.IsExecutable) && !x.IsArchive && !x.IsItemPinnedToStart),
ShowInSearchPage = true,
ShowInFtpPage = true,
SingleItemOnly = true,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This too

},
new ContextMenuFlyoutItemViewModel()
IsVisible = selectedItems.All(x => !x.IsShortcut && (x.PrimaryItemAttribute == StorageItemTypes.Folder || x.IsExecutable) && !x.IsArchive && !x.IsItemPinnedToStart),
}.Build(),
new ContextMenuFlyoutItemViewModelBuilder(commands.UnpinFromStart)
{
Text = "UnpinItemFromStart/Text".GetLocalizedResource(),
OpacityIcon = new OpacityIconModel()
{
OpacityIconStyle = "ColorIconUnpinFromFavorites",
},
Command = commandsViewModel.UnpinItemFromStartCommand,
ShowOnShift = true,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This as well

ShowItem = selectedItems.All(x => !x.IsShortcut && (x.PrimaryItemAttribute == StorageItemTypes.Folder || x.IsExecutable) && !x.IsArchive && x.IsItemPinnedToStart),
ShowInSearchPage = true,
ShowInFtpPage = true,
SingleItemOnly = true,
},
IsVisible = selectedItems.All(x => !x.IsShortcut && (x.PrimaryItemAttribute == StorageItemTypes.Folder || x.IsExecutable) && !x.IsArchive && x.IsItemPinnedToStart),
}.Build(),
new ContextMenuFlyoutItemViewModel
{
Text = "Archive".GetLocalizedResource(),
Expand Down Expand Up @@ -1070,14 +1042,7 @@ public static List<ContextMenuFlyoutItemViewModel> GetNewItemItems(BaseLayoutCom
{
var list = new List<ContextMenuFlyoutItemViewModel>()
{
new ContextMenuFlyoutItemViewModel()
{
Text = "Folder".GetLocalizedResource(),
Glyph = "\uE8B7",
Command = commandsViewModel.CreateNewFolderCommand,
ShowInFtpPage = true,
ShowInZipPage = true,
},
new ContextMenuFlyoutItemViewModelBuilder(commands.CreateFolder).Build(),
new ContextMenuFlyoutItemViewModel()
{
Text = "File".GetLocalizedResource(),
Expand All @@ -1088,13 +1053,7 @@ public static List<ContextMenuFlyoutItemViewModel> GetNewItemItems(BaseLayoutCom
ShowInZipPage = true,
IsEnabled = canCreateFileInPage
},
new ContextMenuFlyoutItemViewModel
{
Text = "Shortcut".GetLocalizedResource(),
Glyph = "\uF10A",
GlyphFontFamilyName = "CustomGlyph",
Command = commandsViewModel.CreateShortcutFromDialogCommand
},
new ContextMenuFlyoutItemViewModelBuilder(commands.CreateShortcutFromDialog).Build(),
new ContextMenuFlyoutItemViewModel()
{
ItemType = ItemType.Separator,
Expand Down
55 changes: 0 additions & 55 deletions src/Files.App/Interacts/BaseLayoutCommandImplementationModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,30 +86,6 @@ public virtual void RenameItem(RoutedEventArgs e)
itemManipulationModel.StartRenameItem();
}

public virtual async void CreateShortcut(RoutedEventArgs e)
{
var currentPath = associatedInstance.FilesystemViewModel.WorkingDirectory;

if (App.LibraryManager.TryGetLibrary(currentPath, out var library) && !library.IsEmpty)
{
currentPath = library.DefaultSaveFolder;
}

foreach (ListedItem selectedItem in SlimContentPage.SelectedItems)
{
var fileName = string.Format("ShortcutCreateNewSuffix".GetLocalizedResource(), selectedItem.Name) + ".lnk";
var filePath = Path.Combine(currentPath, fileName);

if (!await FileOperationsHelpers.CreateOrUpdateLinkAsync(filePath, selectedItem.ItemPath))
await UIFilesystemHelpers.HandleShortcutCannotBeCreated(fileName, selectedItem.ItemPath);
}
}

public virtual async void CreateShortcutFromDialog(RoutedEventArgs e)
{
await UIFilesystemHelpers.CreateShortcutFromDialogAsync(associatedInstance);
}

public virtual void SetAsLockscreenBackgroundItem(RoutedEventArgs e)
{
WallpaperHelpers.SetAsBackground(WallpaperType.LockScreen, SlimContentPage.SelectedItem.ItemPath);
Expand Down Expand Up @@ -281,11 +257,6 @@ public virtual async void OpenInNewWindowItem(RoutedEventArgs e)
}
}

public virtual void CreateNewFolder(RoutedEventArgs e)
{
UIFilesystemHelpers.CreateFileFromDialogResultType(AddItemDialogItemType.Folder, null, associatedInstance);
}

public virtual void CreateNewFile(ShellNewEntry f)
{
UIFilesystemHelpers.CreateFileFromDialogResultType(AddItemDialogItemType.File, f, associatedInstance);
Expand Down Expand Up @@ -410,32 +381,6 @@ public virtual async void ItemPointerPressed(PointerRoutedEventArgs e)
}
}

public virtual async void UnpinItemFromStart(RoutedEventArgs e)
{
if (associatedInstance.SlimContentPage.SelectedItems.Count > 0)
{
foreach (ListedItem listedItem in associatedInstance.SlimContentPage.SelectedItems)
await App.SecondaryTileHelper.UnpinFromStartAsync(listedItem.ItemPath);
}
else
{
await App.SecondaryTileHelper.UnpinFromStartAsync(associatedInstance.FilesystemViewModel.WorkingDirectory);
}
}

public async void PinItemToStart(RoutedEventArgs e)
{
if (associatedInstance.SlimContentPage.SelectedItems.Count > 0)
{
foreach (ListedItem listedItem in associatedInstance.SlimContentPage.SelectedItems)
await App.SecondaryTileHelper.TryPinFolderAsync(listedItem.ItemPath, listedItem.Name);
}
else
{
await App.SecondaryTileHelper.TryPinFolderAsync(associatedInstance.FilesystemViewModel.CurrentFolder.ItemPath, associatedInstance.FilesystemViewModel.CurrentFolder.Name);
}
}

public virtual void PointerWheelChanged(PointerRoutedEventArgs e)
{
if (e.KeyModifiers == VirtualKeyModifiers.Control)
Expand Down
Loading