Skip to content

RichCommand: Glyph + ContextMenu + EmptyRecycleBinAction #11415

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 6 commits into from
Feb 22, 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
19 changes: 19 additions & 0 deletions src/Files.App/Actions/FileSystem/EmptyRecycleBinAction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Files.App.Commands;
using Files.App.Extensions;
using Files.App.Helpers;
using System.Threading.Tasks;

namespace Files.App.Actions
{
internal class EmptyRecycleBinAction : IAction
{
public string Label { get; } = "EmptyRecycleBin".GetLocalizedResource();

public RichGlyph Glyph { get; } = new RichGlyph("\uEF88", fontFamily: "RecycleBinIcons");

public async Task ExecuteAsync()
{
await RecycleBinHelpers.EmptyRecycleBin();
}
}
}
2 changes: 2 additions & 0 deletions src/Files.App/Actions/IAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ public interface IAction
{
string Label { get; }

RichGlyph Glyph => RichGlyph.None;

HotKey HotKey => HotKey.None;

bool IsExecutable => true;
Expand Down
3 changes: 3 additions & 0 deletions src/Files.App/Commands/CommandCodes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,8 @@ public enum CommandCodes
// show
ToggleShowHiddenItems,
ToggleShowFileExtensions,

// file system
EmptyRecycleBin,
}
}
2 changes: 1 addition & 1 deletion src/Files.App/Commands/HotKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public static HotKey Parse(string hotKey)
continue;
}

if (key is not VirtualKey.None)
if (key is VirtualKey.None)
{
var k = ToKey(part);
if (k is not VirtualKey.None)
Expand Down
8 changes: 7 additions & 1 deletion src/Files.App/Commands/IRichCommand.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Microsoft.UI.Xaml.Input;
using Files.App.UserControls;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Input;
using System.ComponentModel;
using System.Threading.Tasks;
using System.Windows.Input;
Expand All @@ -13,6 +15,10 @@ public interface IRichCommand : ICommand, INotifyPropertyChanging, INotifyProper
string LabelWithHotKey { get; }
string AutomationName { get; }

RichGlyph Glyph { get; }
FontIcon? FontIcon { get; }
ColoredIcon? ColoredIcon { get; }

HotKey DefaultHotKey { get; }
HotKey CustomHotKey { get; set; }

Expand Down
18 changes: 18 additions & 0 deletions src/Files.App/Commands/Manager/CommandManager.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Files.App.Actions;
using Files.App.UserControls;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Input;
using System;
using System.Collections;
Expand Down Expand Up @@ -29,6 +31,7 @@ internal class CommandManager : ICommandManager
public IRichCommand ToggleFullScreen => commands[CommandCodes.ToggleFullScreen];
public IRichCommand ToggleShowHiddenItems => commands[CommandCodes.ToggleShowHiddenItems];
public IRichCommand ToggleShowFileExtensions => commands[CommandCodes.ToggleShowFileExtensions];
public IRichCommand EmptyRecycleBin => commands[CommandCodes.EmptyRecycleBin];

public CommandManager()
{
Expand All @@ -52,6 +55,7 @@ public CommandManager()
[CommandCodes.ToggleFullScreen] = new ToggleFullScreenAction(),
[CommandCodes.ToggleShowHiddenItems] = new ToggleShowHiddenItemsAction(),
[CommandCodes.ToggleShowFileExtensions] = new ToggleShowFileExtensionsAction(),
[CommandCodes.EmptyRecycleBin] = new EmptyRecycleBinAction(),
};

[DebuggerDisplay("Command None")]
Expand All @@ -67,6 +71,10 @@ public event PropertyChangedEventHandler? PropertyChanged { add {} remove {} }
public string LabelWithHotKey => string.Empty;
public string AutomationName => string.Empty;

public RichGlyph Glyph => RichGlyph.None;
public FontIcon? FontIcon => null;
public ColoredIcon? ColoredIcon => null;

public HotKey DefaultHotKey => HotKey.None;

public HotKey CustomHotKey
Expand Down Expand Up @@ -101,6 +109,14 @@ private class ActionCommand : ObservableObject, IRichCommand
public string LabelWithHotKey => $"{Label} ({CustomHotKey})";
public string AutomationName => Label;

public RichGlyph Glyph => action.Glyph;

private readonly Lazy<FontIcon?> fontIcon;
public FontIcon? FontIcon => fontIcon.Value;

private readonly Lazy<ColoredIcon?> coloredIcon;
public ColoredIcon? ColoredIcon => coloredIcon.Value;

public HotKey DefaultHotKey => action.HotKey;

private HotKey customHotKey;
Expand Down Expand Up @@ -152,6 +168,8 @@ public ActionCommand(CommandManager manager, CommandCodes code, IAction action)
this.manager = manager;
Code = code;
this.action = action;
fontIcon = new(action.Glyph.ToFontIcon);
coloredIcon = new(action.Glyph.ToColoredIcon);
customHotKey = action.HotKey;
command = new AsyncRelayCommand(ExecuteAsync, () => action.IsExecutable);

Expand Down
2 changes: 2 additions & 0 deletions src/Files.App/Commands/Manager/ICommandManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,7 @@ public interface ICommandManager : IEnumerable<IRichCommand>

IRichCommand ToggleShowHiddenItems { get; }
IRichCommand ToggleShowFileExtensions { get; }

IRichCommand EmptyRecycleBin { get; }
}
}
70 changes: 70 additions & 0 deletions src/Files.App/Commands/RichGlyph.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using Files.App.UserControls;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Media;
using static Microsoft.UI.Xaml.Application;

namespace Files.App.Commands
{
public readonly struct RichGlyph
{
public static RichGlyph None { get; } = new(string.Empty);

public bool IsNone => string.IsNullOrEmpty(BaseGlyph);

public string BaseGlyph { get; }
public string OverlayGlyph { get; }
public string FontFamily { get; }

public RichGlyph(string baseGlyph, string overlayGlyph = "", string fontFamily = "")
{
BaseGlyph = baseGlyph;
OverlayGlyph = overlayGlyph;
FontFamily = fontFamily;
}

public void Deconstruct(out string baseGlyph, out string overlayGlyph, out string fontFamily)
{
baseGlyph = BaseGlyph;
overlayGlyph = OverlayGlyph;
fontFamily = FontFamily;
}

public FontFamily ToFontFamily()
{
return string.IsNullOrEmpty(FontFamily)
? App.AppModel.SymbolFontFamily
: (FontFamily)Current.Resources[FontFamily];
}

public FontIcon? ToFontIcon()
{
if (IsNone)
return null;

var icon = new FontIcon
{
Glyph = BaseGlyph,
};
if (!string.IsNullOrEmpty(FontFamily))
icon.FontFamily = (FontFamily)Current.Resources[FontFamily];

return icon;
}

public ColoredIcon? ToColoredIcon()
{
if (IsNone)
return null;

var icon = new ColoredIcon
{
BaseLayerGlyph = BaseGlyph,
OverlayLayerGlyph = OverlayGlyph,
};
if (!string.IsNullOrEmpty(FontFamily))
icon.FontFamily = (FontFamily)Current.Resources[FontFamily];

return icon;
}
}
}
16 changes: 5 additions & 11 deletions src/Files.App/Helpers/ContextFlyoutItemHelper.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using CommunityToolkit.Mvvm.DependencyInjection;
using CommunityToolkit.Mvvm.Input;
using Files.App.Commands;
using Files.App.Extensions;
using Files.App.Filesystem;
using Files.App.Interacts;
Expand All @@ -8,7 +9,6 @@
using Files.Backend.Services;
using Files.Backend.Services.Settings;
using Files.Shared.Enums;
using Microsoft.UI.Text;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media.Imaging;
using System;
Expand All @@ -25,6 +25,8 @@ namespace Files.App.Helpers
{
public static class ContextFlyoutItemHelper
{
private static readonly IUserSettingsService userSettingsService = Ioc.Default.GetRequiredService<IUserSettingsService>();
private static readonly ICommandManager commands = Ioc.Default.GetRequiredService<ICommandManager>();
private static readonly IAddItemService addItemService = Ioc.Default.GetRequiredService<IAddItemService>();

public static List<ContextMenuFlyoutItemViewModel> GetItemContextCommandsWithoutShellItems(CurrentInstanceViewModel currentInstanceViewModel, List<ListedItem> selectedItems, BaseLayoutCommandsViewModel commandsViewModel, bool shiftPressed, SelectedItemsPropertiesViewModel? selectedItemsPropertiesViewModel, ItemViewModel? itemViewModel = null)
Expand All @@ -42,8 +44,6 @@ public static List<ContextMenuFlyoutItemViewModel> Filter(List<ContextMenuFlyout
items = items.Where(x => Check(item: x, currentInstanceViewModel: currentInstanceViewModel, selectedItems: selectedItems)).ToList();
items.ForEach(x => x.Items = x.Items?.Where(y => Check(item: y, currentInstanceViewModel: currentInstanceViewModel, selectedItems: selectedItems)).ToList());

IUserSettingsService userSettingsService = Ioc.Default.GetRequiredService<IUserSettingsService>();

var overflow = items.Where(x => x.ID == "ItemOverflow").FirstOrDefault();
if (overflow is not null)
{
Expand Down Expand Up @@ -84,8 +84,6 @@ public static List<ContextMenuFlyoutItemViewModel> GetBaseItemMenuItems(
CurrentInstanceViewModel currentInstanceViewModel,
ItemViewModel itemViewModel = null)
{
IUserSettingsService userSettingsService = Ioc.Default.GetRequiredService<IUserSettingsService>();

bool itemsSelected = itemViewModel is null;
bool canDecompress = selectedItems.Any() && selectedItems.All(x => x.IsArchive)
|| selectedItems.All(x => x.PrimaryItemAttribute == StorageItemTypes.File && FileExtensionHelpers.IsZipFile(x.FileExtension));
Expand Down Expand Up @@ -565,14 +563,10 @@ public static List<ContextMenuFlyoutItemViewModel> GetBaseItemMenuItems(
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 ContextMenuFlyoutItemViewModel()
new ContextMenuFlyoutItemViewModel(commands.EmptyRecycleBin)
{
Text = "BaseLayoutContextFlyoutEmptyRecycleBin/Text".GetLocalizedResource(),
Glyph = "\uEF88",
GlyphFontFamilyName = "RecycleBinIcons",
Command = commandsViewModel.EmptyRecycleBinCommand,
IsEnabled = RecycleBinHelpers.RecycleBinHasItems(),
ShowItem = !itemsSelected && currentInstanceViewModel.IsPageTypeRecycleBin,
ShowInRecycleBin = true,
},
new ContextMenuFlyoutItemViewModel()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,6 @@ public virtual void UnpinDirectoryFromFavorites(RoutedEventArgs e)
_ = QuickAccessService.UnpinFromSidebar(associatedInstance.FilesystemViewModel.WorkingDirectory);
}

public virtual async void EmptyRecycleBin(RoutedEventArgs e)
{
await RecycleBinHelpers.EmptyRecycleBin();
}

public virtual async void RestoreRecycleBin(RoutedEventArgs e)
{
await RecycleBinHelpers.RestoreRecycleBin(associatedInstance);
Expand Down
3 changes: 0 additions & 3 deletions src/Files.App/Interacts/BaseLayoutCommandsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ private void InitializeCommands()
SidebarUnpinItemCommand = new RelayCommand<RoutedEventArgs>(CommandsModel.SidebarUnpinItem);
UnpinDirectoryFromFavoritesCommand = new RelayCommand<RoutedEventArgs>(CommandsModel.UnpinDirectoryFromFavorites);
OpenItemCommand = new RelayCommand<RoutedEventArgs>(CommandsModel.OpenItem);
EmptyRecycleBinCommand = new RelayCommand<RoutedEventArgs>(CommandsModel.EmptyRecycleBin);
RestoreRecycleBinCommand = new RelayCommand<RoutedEventArgs>(CommandsModel.RestoreRecycleBin);
RestoreSelectionRecycleBinCommand = new RelayCommand<RoutedEventArgs>(CommandsModel.RestoreSelectionRecycleBin);
QuickLookCommand = new RelayCommand<RoutedEventArgs>(CommandsModel.QuickLook);
Expand Down Expand Up @@ -114,8 +113,6 @@ private void InitializeCommands()

public ICommand UnpinDirectoryFromFavoritesCommand { get; private set; }

public ICommand EmptyRecycleBinCommand { get; private set; }

public ICommand RestoreRecycleBinCommand { get; private set; }

public ICommand RestoreSelectionRecycleBinCommand { get; private set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ public interface IBaseLayoutCommandImplementationModel : IDisposable

void OpenItem(RoutedEventArgs e);

void EmptyRecycleBin(RoutedEventArgs e);

void RestoreRecycleBin(RoutedEventArgs e);

void RestoreSelectionRecycleBin(RoutedEventArgs e);
Expand Down
3 changes: 0 additions & 3 deletions src/Files.App/Strings/en-US/Resources.resw
Original file line number Diff line number Diff line change
Expand Up @@ -468,9 +468,6 @@
<data name="PropertiesDialogHidden.Text" xml:space="preserve">
<value>Hidden</value>
</data>
<data name="BaseLayoutContextFlyoutEmptyRecycleBin.Text" xml:space="preserve">
<value>Empty recycle bin</value>
</data>
<data name="BaseLayoutItemContextFlyoutRestore.Text" xml:space="preserve">
<value>Restore</value>
</data>
Expand Down
23 changes: 10 additions & 13 deletions src/Files.App/UserControls/InnerNavigationToolbar.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -210,14 +210,11 @@
x:Name="EmptyRecycleBinButton"
x:Load="{x:Bind ViewModel.InstanceViewModel.IsPageTypeRecycleBin, Mode=OneWay, FallbackValue=False}"
AccessKey="B"
Command="{x:Bind ViewModel.EmptyRecycleBinCommand, Mode=OneWay}"
Command="{x:Bind Commands.EmptyRecycleBin}"
Icon="{x:Bind Commands.EmptyRecycleBin.FontIcon}"
IsEnabled="{x:Bind ViewModel.CanEmptyRecycleBin, Mode=OneWay}"
Label="{helpers:ResourceString Name=EmptyRecycleBin}"
ToolTipService.ToolTip="{helpers:ResourceString Name=EmptyRecycleBin}">
<AppBarButton.Icon>
<FontIcon FontFamily="{StaticResource RecycleBinIcons}" Glyph="&#xEF88;" />
</AppBarButton.Icon>
</AppBarButton>
Label="{x:Bind Commands.EmptyRecycleBin.Label}"
ToolTipService.ToolTip="{x:Bind Commands.EmptyRecycleBin.Label}" />
<AppBarButton
x:Name="RestoreRecycleBinButton"
x:Load="{x:Bind ViewModel.InstanceViewModel.IsPageTypeRecycleBin, Mode=OneWay, FallbackValue=False}"
Expand Down Expand Up @@ -798,26 +795,26 @@
Grid.Row="0"
Grid.Column="0"
VerticalAlignment="Center"
Text="{x:Bind CommandManager.ToggleShowHiddenItems.Label}" />
Text="{x:Bind Commands.ToggleShowHiddenItems.Label}" />
<ToggleSwitch
Grid.Row="0"
Grid.Column="1"
HorizontalAlignment="Right"
AutomationProperties.Name="{x:Bind CommandManager.ToggleShowHiddenItems.AutomationName}"
IsOn="{x:Bind CommandManager.ToggleShowHiddenItems.IsOn, Mode=TwoWay}"
AutomationProperties.Name="{x:Bind Commands.ToggleShowHiddenItems.AutomationName}"
IsOn="{x:Bind Commands.ToggleShowHiddenItems.IsOn, Mode=TwoWay}"
Style="{StaticResource RightAlignedToggleSwitchStyle}" />

<TextBlock
Grid.Row="1"
Grid.Column="0"
VerticalAlignment="Center"
Text="{x:Bind CommandManager.ToggleShowFileExtensions.Label}" />
Text="{x:Bind Commands.ToggleShowFileExtensions.Label}" />
<ToggleSwitch
Grid.Row="1"
Grid.Column="1"
HorizontalAlignment="Right"
AutomationProperties.Name="{x:Bind CommandManager.ToggleShowFileExtensions.AutomationName}"
IsOn="{x:Bind CommandManager.ToggleShowFileExtensions.IsOn, Mode=TwoWay}"
AutomationProperties.Name="{x:Bind Commands.ToggleShowFileExtensions.AutomationName}"
IsOn="{x:Bind Commands.ToggleShowFileExtensions.IsOn, Mode=TwoWay}"
Rotation="1"
Style="{StaticResource RightAlignedToggleSwitchStyle}" />
</Grid>
Expand Down
2 changes: 1 addition & 1 deletion src/Files.App/UserControls/InnerNavigationToolbar.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public InnerNavigationToolbar()
}

public IUserSettingsService UserSettingsService { get; } = Ioc.Default.GetRequiredService<IUserSettingsService>();
public ICommandManager CommandManager { get; } = Ioc.Default.GetRequiredService<ICommandManager>();
public ICommandManager Commands { get; } = Ioc.Default.GetRequiredService<ICommandManager>();

private readonly IAddItemService addItemService = Ioc.Default.GetRequiredService<IAddItemService>();

Expand Down
Loading