From 5682b2b1c8b866ce62b810a0e6b471069a578c85 Mon Sep 17 00:00:00 2001 From: cinqmilleans Date: Wed, 15 Mar 2023 14:32:54 +0100 Subject: [PATCH 1/4] Improves commands Pin/Unpin --- .../Actions/Favorites/PinItemAction.cs | 67 +++++++++++++------ .../Actions/Favorites/UnpinItemAction.cs | 67 +++++++++++++------ .../Helpers/ContextFlyoutItemHelper.cs | 4 +- src/Files.App/Strings/en-US/Resources.resw | 2 +- 4 files changed, 96 insertions(+), 44 deletions(-) diff --git a/src/Files.App/Actions/Favorites/PinItemAction.cs b/src/Files.App/Actions/Favorites/PinItemAction.cs index 094573082b77..59ac52b130f1 100644 --- a/src/Files.App/Actions/Favorites/PinItemAction.cs +++ b/src/Files.App/Actions/Favorites/PinItemAction.cs @@ -3,53 +3,82 @@ using Files.App.Commands; using Files.App.Contexts; using Files.App.Extensions; +using Files.App.Filesystem; using Files.App.ServicesImplementation; +using Files.App.UserControls.Widgets; +using System.ComponentModel; using System.Linq; using System.Threading.Tasks; +using Windows.Storage; namespace Files.App.Actions.Favorites { internal class PinItemAction : ObservableObject, IAction { - public IContentPageContext context = Ioc.Default.GetRequiredService(); + private readonly IContentPageContext context = Ioc.Default.GetRequiredService(); + private readonly IQuickAccessService service = Ioc.Default.GetRequiredService(); - private readonly IQuickAccessService quickAccessService = Ioc.Default.GetRequiredService(); + public string Label { get; } = "PinToFavorites".GetLocalizedResource(); - public string Label { get; } = "BaseLayoutItemContextFlyoutPinToFavorites/Text".GetLocalizedResource(); + public RichGlyph Glyph { get; } = new(opacityStyle: "ColorIconPinToFavorites"); - public RichGlyph Glyph { get; } = new RichGlyph(opacityStyle: "ColorIconPinToFavorites"); + private bool isExecutable; + public bool IsExecutable => isExecutable; - public bool IsExecutable + public PinItemAction() { - get - { - if ((context.SelectedItems.Any() && context.SelectedItems.All(x => !x.IsPinned)) - || (context.Folder is not null && !context.Folder.IsPinned)) - return true; + isExecutable = GetIsExecutable(); - return false; - } + context.PropertyChanged += Context_PropertyChanged; + App.QuickAccessManager.UpdateQuickAccessWidget += QuickAccessManager_DataChanged; } - public PinItemAction() + public async Task ExecuteAsync() { - context.PropertyChanged += Context_PropertyChanged; + if (context.HasSelection) + { + var items = context.SelectedItems.Select(x => x.ItemPath).ToArray(); + await service.PinToSidebar(items); + } + else if (context.Folder is not null) + { + await service.PinToSidebar(context.Folder.ItemPath); + } } - public async Task ExecuteAsync() + private bool GetIsExecutable() { - await quickAccessService.PinToSidebar(context.SelectedItems.Any() ? context.SelectedItems.Select(x => x.ItemPath).ToArray() : new[] { context.Folder.ItemPath }); + string[] favorites = App.QuickAccessManager.Model.FavoriteItems.ToArray(); + + return context.HasSelection + ? context.SelectedItems.All(IsPinnable) + : context.Folder is not null && IsPinnable(context.Folder); + + bool IsPinnable(ListedItem item) + { + return item.PrimaryItemAttribute is StorageItemTypes.Folder + && !favorites.Contains(item.ItemPath); + } + } + private void UpdateIsExecutable() + { + SetProperty(ref isExecutable, GetIsExecutable(), nameof(IsExecutable)); } - public void Context_PropertyChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs e) + private void Context_PropertyChanged(object? sender, PropertyChangedEventArgs e) { switch (e.PropertyName) { - case nameof(IContentPageContext.SelectedItems): case nameof(IContentPageContext.Folder): - OnPropertyChanged(nameof(IsExecutable)); + case nameof(IContentPageContext.SelectedItems): + UpdateIsExecutable(); break; } } + + private void QuickAccessManager_DataChanged(object? sender, ModifyQuickAccessEventArgs e) + { + UpdateIsExecutable(); + } } } diff --git a/src/Files.App/Actions/Favorites/UnpinItemAction.cs b/src/Files.App/Actions/Favorites/UnpinItemAction.cs index f27834ee6bdf..caaa7a237a4b 100644 --- a/src/Files.App/Actions/Favorites/UnpinItemAction.cs +++ b/src/Files.App/Actions/Favorites/UnpinItemAction.cs @@ -3,57 +3,80 @@ using Files.App.Commands; using Files.App.Contexts; using Files.App.Extensions; +using Files.App.Filesystem; using Files.App.ServicesImplementation; -using System; -using System.Collections.Generic; +using Files.App.UserControls.Widgets; +using System.ComponentModel; using System.Linq; -using System.Text; using System.Threading.Tasks; namespace Files.App.Actions { internal class UnpinItemAction : ObservableObject, IAction { - public IContentPageContext context = Ioc.Default.GetRequiredService(); - - private readonly IQuickAccessService quickAccessService = Ioc.Default.GetRequiredService(); + private readonly IContentPageContext context = Ioc.Default.GetRequiredService(); + private readonly IQuickAccessService service = Ioc.Default.GetRequiredService(); public string Label { get; } = "UnpinFromFavorites".GetLocalizedResource(); - public RichGlyph Glyph { get; } = new RichGlyph(opacityStyle: "ColorIconUnpinFromFavorites"); + public RichGlyph Glyph { get; } = new(opacityStyle: "ColorIconUnpinFromFavorites"); + + private bool isExecutable; + public bool IsExecutable => isExecutable; - public bool IsExecutable + public UnpinItemAction() { - get - { - if ((context.SelectedItems.Any() && context.SelectedItems.All(x => x.IsPinned)) - || (context.Folder is not null && context.Folder.IsPinned)) - return true; + isExecutable = GetIsExecutable(); - return false; - } + context.PropertyChanged += Context_PropertyChanged; + App.QuickAccessManager.UpdateQuickAccessWidget += QuickAccessManager_DataChanged; } - public UnpinItemAction() + public async Task ExecuteAsync() { - context.PropertyChanged += Context_PropertyChanged; + if (context.HasSelection) + { + var items = context.SelectedItems.Select(x => x.ItemPath).ToArray(); + await service.UnpinFromSidebar(items); + } + else if (context.Folder is not null) + { + await service.UnpinFromSidebar(context.Folder.ItemPath); + } } + private bool GetIsExecutable() + { + string[] favorites = App.QuickAccessManager.Model.FavoriteItems.ToArray(); - public async Task ExecuteAsync() + return context.HasSelection + ? context.SelectedItems.All(IsPinned) + : context.Folder is not null && IsPinned(context.Folder); + + bool IsPinned(ListedItem item) + { + return favorites.Contains(item.ItemPath); + } + } + private void UpdateIsExecutable() { - await quickAccessService.UnpinFromSidebar(context.SelectedItems.Any() ? context.SelectedItems.Select(x => x.ItemPath).ToArray() : new[] { context.Folder.ItemPath }); + SetProperty(ref isExecutable, GetIsExecutable(), nameof(IsExecutable)); } - public void Context_PropertyChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs e) + private void Context_PropertyChanged(object? sender, PropertyChangedEventArgs e) { switch (e.PropertyName) { - case nameof(IContentPageContext.SelectedItems): case nameof(IContentPageContext.Folder): - OnPropertyChanged(nameof(IsExecutable)); + case nameof(IContentPageContext.SelectedItems): + UpdateIsExecutable(); break; } } + + private void QuickAccessManager_DataChanged(object? sender, ModifyQuickAccessEventArgs e) + { + UpdateIsExecutable(); + } } } diff --git a/src/Files.App/Helpers/ContextFlyoutItemHelper.cs b/src/Files.App/Helpers/ContextFlyoutItemHelper.cs index 1ccada09f6cf..981b70bd48d3 100644 --- a/src/Files.App/Helpers/ContextFlyoutItemHelper.cs +++ b/src/Files.App/Helpers/ContextFlyoutItemHelper.cs @@ -828,11 +828,11 @@ public static List GetBaseItemMenuItems( }, new ContextMenuFlyoutItemViewModelBuilder(commands.PinItemToFavorites) { - IsVisible = userSettingsService.PreferencesSettingsService.ShowFavoritesSection && selectedItems.All(x => x.PrimaryItemAttribute == StorageItemTypes.Folder && !x.IsArchive && !x.IsPinned), + IsVisible = commands.PinItemToFavorites.IsExecutable && userSettingsService.PreferencesSettingsService.ShowFavoritesSection, }.Build(), new ContextMenuFlyoutItemViewModelBuilder(commands.UnpinItemFromFavorites) { - IsVisible = userSettingsService.PreferencesSettingsService.ShowFavoritesSection && selectedItems.All(x => x.PrimaryItemAttribute == StorageItemTypes.Folder && !x.IsArchive && x.IsPinned), + IsVisible = commands.UnpinItemFromFavorites.IsExecutable && userSettingsService.PreferencesSettingsService.ShowFavoritesSection, }.Build(), new ContextMenuFlyoutItemViewModelBuilder(commands.PinToStart) { diff --git a/src/Files.App/Strings/en-US/Resources.resw b/src/Files.App/Strings/en-US/Resources.resw index 57d50e6b6796..63bce2e990d6 100644 --- a/src/Files.App/Strings/en-US/Resources.resw +++ b/src/Files.App/Strings/en-US/Resources.resw @@ -273,7 +273,7 @@ Delete - + Pin to Favorites From dd13cf318e579df39660db1f8068e86ebbbead99 Mon Sep 17 00:00:00 2001 From: cinqmilleans Date: Wed, 15 Mar 2023 14:44:18 +0100 Subject: [PATCH 2/4] string --- src/Files.App/UserControls/SidebarControl.xaml.cs | 4 ++-- src/Files.App/UserControls/Widgets/DrivesWidget.xaml.cs | 2 +- src/Files.App/UserControls/Widgets/FileTagsWidget.xaml.cs | 2 +- src/Files.App/UserControls/Widgets/QuickAccessWidget.xaml.cs | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Files.App/UserControls/SidebarControl.xaml.cs b/src/Files.App/UserControls/SidebarControl.xaml.cs index 0c05994c9db4..c1731e794c5f 100644 --- a/src/Files.App/UserControls/SidebarControl.xaml.cs +++ b/src/Files.App/UserControls/SidebarControl.xaml.cs @@ -235,7 +235,7 @@ private List GetLocationItemMenuItems(INavigatio }, new ContextMenuFlyoutItemViewModel() { - Text = "BaseLayoutItemContextFlyoutPinToFavorites/Text".GetLocalizedResource(), + Text = "PinToFavorites".GetLocalizedResource(), OpacityIcon = new OpacityIconModel() { OpacityIconStyle = "ColorIconPinToFavorites", @@ -555,7 +555,7 @@ private async void NavigationViewLocationItem_DragOver(object sender, DragEventA } else { - var captionText = "BaseLayoutItemContextFlyoutPinToFavorites/Text".GetLocalizedResource(); + var captionText = "PinToFavorites".GetLocalizedResource(); CompleteDragEventArgs(e, captionText, DataPackageOperation.Move); } } diff --git a/src/Files.App/UserControls/Widgets/DrivesWidget.xaml.cs b/src/Files.App/UserControls/Widgets/DrivesWidget.xaml.cs index 3d6a773a8c50..e3be375d9ebf 100644 --- a/src/Files.App/UserControls/Widgets/DrivesWidget.xaml.cs +++ b/src/Files.App/UserControls/Widgets/DrivesWidget.xaml.cs @@ -179,7 +179,7 @@ public override List GetItemMenuItems(WidgetCard }, new ContextMenuFlyoutItemViewModel() { - Text = "BaseLayoutItemContextFlyoutPinToFavorites/Text".GetLocalizedResource(), + Text = "PinToFavorites".GetLocalizedResource(), OpacityIcon = new OpacityIconModel() { OpacityIconStyle = "ColorIconPinToFavorites", diff --git a/src/Files.App/UserControls/Widgets/FileTagsWidget.xaml.cs b/src/Files.App/UserControls/Widgets/FileTagsWidget.xaml.cs index 9489d09b123b..9d94826bda35 100644 --- a/src/Files.App/UserControls/Widgets/FileTagsWidget.xaml.cs +++ b/src/Files.App/UserControls/Widgets/FileTagsWidget.xaml.cs @@ -195,7 +195,7 @@ public override List GetItemMenuItems(WidgetCard }, new ContextMenuFlyoutItemViewModel() { - Text = "BaseLayoutItemContextFlyoutPinToFavorites/Text".GetLocalizedResource(), + Text = "PinToFavorites".GetLocalizedResource(), OpacityIcon = new OpacityIconModel() { OpacityIconStyle = "ColorIconPinToFavorites", diff --git a/src/Files.App/UserControls/Widgets/QuickAccessWidget.xaml.cs b/src/Files.App/UserControls/Widgets/QuickAccessWidget.xaml.cs index f80cbedfb5bc..e10db7897c1f 100644 --- a/src/Files.App/UserControls/Widgets/QuickAccessWidget.xaml.cs +++ b/src/Files.App/UserControls/Widgets/QuickAccessWidget.xaml.cs @@ -193,7 +193,7 @@ public override List GetItemMenuItems(WidgetCard }, new ContextMenuFlyoutItemViewModel() { - Text = "BaseLayoutItemContextFlyoutPinToFavorites/Text".GetLocalizedResource(), + Text = "PinToFavorites".GetLocalizedResource(), OpacityIcon = new OpacityIconModel() { OpacityIconStyle = "ColorIconPinToFavorites", From 4e2bf388312ddacb45f4642c1f88819613ce679d Mon Sep 17 00:00:00 2001 From: cinqmilleans Date: Wed, 15 Mar 2023 21:35:59 +0100 Subject: [PATCH 3/4] style --- src/Files.App/Actions/Favorites/PinItemAction.cs | 2 -- src/Files.App/Actions/Favorites/UnpinItemAction.cs | 2 -- 2 files changed, 4 deletions(-) diff --git a/src/Files.App/Actions/Favorites/PinItemAction.cs b/src/Files.App/Actions/Favorites/PinItemAction.cs index 59ac52b130f1..e2ea1ef6d148 100644 --- a/src/Files.App/Actions/Favorites/PinItemAction.cs +++ b/src/Files.App/Actions/Favorites/PinItemAction.cs @@ -41,9 +41,7 @@ public async Task ExecuteAsync() await service.PinToSidebar(items); } else if (context.Folder is not null) - { await service.PinToSidebar(context.Folder.ItemPath); - } } private bool GetIsExecutable() diff --git a/src/Files.App/Actions/Favorites/UnpinItemAction.cs b/src/Files.App/Actions/Favorites/UnpinItemAction.cs index caaa7a237a4b..2b4546dbbd15 100644 --- a/src/Files.App/Actions/Favorites/UnpinItemAction.cs +++ b/src/Files.App/Actions/Favorites/UnpinItemAction.cs @@ -40,9 +40,7 @@ public async Task ExecuteAsync() await service.UnpinFromSidebar(items); } else if (context.Folder is not null) - { await service.UnpinFromSidebar(context.Folder.ItemPath); - } } private bool GetIsExecutable() From c0b57a28251df97c32b358e54cba7a8a7dfb5ab4 Mon Sep 17 00:00:00 2001 From: cinqmilleans Date: Thu, 16 Mar 2023 20:21:54 +0100 Subject: [PATCH 4/4] remove style --- src/Files.App/Actions/Favorites/PinItemAction.cs | 2 ++ src/Files.App/Actions/Favorites/UnpinItemAction.cs | 2 ++ 2 files changed, 4 insertions(+) diff --git a/src/Files.App/Actions/Favorites/PinItemAction.cs b/src/Files.App/Actions/Favorites/PinItemAction.cs index e2ea1ef6d148..59ac52b130f1 100644 --- a/src/Files.App/Actions/Favorites/PinItemAction.cs +++ b/src/Files.App/Actions/Favorites/PinItemAction.cs @@ -41,7 +41,9 @@ public async Task ExecuteAsync() await service.PinToSidebar(items); } else if (context.Folder is not null) + { await service.PinToSidebar(context.Folder.ItemPath); + } } private bool GetIsExecutable() diff --git a/src/Files.App/Actions/Favorites/UnpinItemAction.cs b/src/Files.App/Actions/Favorites/UnpinItemAction.cs index 2b4546dbbd15..caaa7a237a4b 100644 --- a/src/Files.App/Actions/Favorites/UnpinItemAction.cs +++ b/src/Files.App/Actions/Favorites/UnpinItemAction.cs @@ -40,7 +40,9 @@ public async Task ExecuteAsync() await service.UnpinFromSidebar(items); } else if (context.Folder is not null) + { await service.UnpinFromSidebar(context.Folder.ItemPath); + } } private bool GetIsExecutable()