Skip to content

Commit dc40fe1

Browse files
authored
Feature: Improved commands for Pin/Unpin (#11729)
1 parent 872ab44 commit dc40fe1

File tree

8 files changed

+101
-49
lines changed

8 files changed

+101
-49
lines changed

src/Files.App/Actions/Favorites/PinItemAction.cs

Lines changed: 48 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,53 +3,82 @@
33
using Files.App.Commands;
44
using Files.App.Contexts;
55
using Files.App.Extensions;
6+
using Files.App.Filesystem;
67
using Files.App.ServicesImplementation;
8+
using Files.App.UserControls.Widgets;
9+
using System.ComponentModel;
710
using System.Linq;
811
using System.Threading.Tasks;
12+
using Windows.Storage;
913

1014
namespace Files.App.Actions.Favorites
1115
{
1216
internal class PinItemAction : ObservableObject, IAction
1317
{
14-
public IContentPageContext context = Ioc.Default.GetRequiredService<IContentPageContext>();
18+
private readonly IContentPageContext context = Ioc.Default.GetRequiredService<IContentPageContext>();
19+
private readonly IQuickAccessService service = Ioc.Default.GetRequiredService<IQuickAccessService>();
1520

16-
private readonly IQuickAccessService quickAccessService = Ioc.Default.GetRequiredService<IQuickAccessService>();
21+
public string Label { get; } = "PinToFavorites".GetLocalizedResource();
1722

18-
public string Label { get; } = "BaseLayoutItemContextFlyoutPinToFavorites/Text".GetLocalizedResource();
23+
public RichGlyph Glyph { get; } = new(opacityStyle: "ColorIconPinToFavorites");
1924

20-
public RichGlyph Glyph { get; } = new RichGlyph(opacityStyle: "ColorIconPinToFavorites");
25+
private bool isExecutable;
26+
public bool IsExecutable => isExecutable;
2127

22-
public bool IsExecutable
28+
public PinItemAction()
2329
{
24-
get
25-
{
26-
if ((context.SelectedItems.Any() && context.SelectedItems.All(x => !x.IsPinned))
27-
|| (context.Folder is not null && !context.Folder.IsPinned))
28-
return true;
30+
isExecutable = GetIsExecutable();
2931

30-
return false;
31-
}
32+
context.PropertyChanged += Context_PropertyChanged;
33+
App.QuickAccessManager.UpdateQuickAccessWidget += QuickAccessManager_DataChanged;
3234
}
3335

34-
public PinItemAction()
36+
public async Task ExecuteAsync()
3537
{
36-
context.PropertyChanged += Context_PropertyChanged;
38+
if (context.HasSelection)
39+
{
40+
var items = context.SelectedItems.Select(x => x.ItemPath).ToArray();
41+
await service.PinToSidebar(items);
42+
}
43+
else if (context.Folder is not null)
44+
{
45+
await service.PinToSidebar(context.Folder.ItemPath);
46+
}
3747
}
3848

39-
public async Task ExecuteAsync()
49+
private bool GetIsExecutable()
4050
{
41-
await quickAccessService.PinToSidebar(context.SelectedItems.Any() ? context.SelectedItems.Select(x => x.ItemPath).ToArray() : new[] { context.Folder.ItemPath });
51+
string[] favorites = App.QuickAccessManager.Model.FavoriteItems.ToArray();
52+
53+
return context.HasSelection
54+
? context.SelectedItems.All(IsPinnable)
55+
: context.Folder is not null && IsPinnable(context.Folder);
56+
57+
bool IsPinnable(ListedItem item)
58+
{
59+
return item.PrimaryItemAttribute is StorageItemTypes.Folder
60+
&& !favorites.Contains(item.ItemPath);
61+
}
62+
}
63+
private void UpdateIsExecutable()
64+
{
65+
SetProperty(ref isExecutable, GetIsExecutable(), nameof(IsExecutable));
4266
}
4367

44-
public void Context_PropertyChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs e)
68+
private void Context_PropertyChanged(object? sender, PropertyChangedEventArgs e)
4569
{
4670
switch (e.PropertyName)
4771
{
48-
case nameof(IContentPageContext.SelectedItems):
4972
case nameof(IContentPageContext.Folder):
50-
OnPropertyChanged(nameof(IsExecutable));
73+
case nameof(IContentPageContext.SelectedItems):
74+
UpdateIsExecutable();
5175
break;
5276
}
5377
}
78+
79+
private void QuickAccessManager_DataChanged(object? sender, ModifyQuickAccessEventArgs e)
80+
{
81+
UpdateIsExecutable();
82+
}
5483
}
5584
}

src/Files.App/Actions/Favorites/UnpinItemAction.cs

Lines changed: 45 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,57 +3,80 @@
33
using Files.App.Commands;
44
using Files.App.Contexts;
55
using Files.App.Extensions;
6+
using Files.App.Filesystem;
67
using Files.App.ServicesImplementation;
7-
using System;
8-
using System.Collections.Generic;
8+
using Files.App.UserControls.Widgets;
9+
using System.ComponentModel;
910
using System.Linq;
10-
using System.Text;
1111
using System.Threading.Tasks;
1212

1313
namespace Files.App.Actions
1414
{
1515
internal class UnpinItemAction : ObservableObject, IAction
1616
{
17-
public IContentPageContext context = Ioc.Default.GetRequiredService<IContentPageContext>();
18-
19-
private readonly IQuickAccessService quickAccessService = Ioc.Default.GetRequiredService<IQuickAccessService>();
17+
private readonly IContentPageContext context = Ioc.Default.GetRequiredService<IContentPageContext>();
18+
private readonly IQuickAccessService service = Ioc.Default.GetRequiredService<IQuickAccessService>();
2019

2120
public string Label { get; } = "UnpinFromFavorites".GetLocalizedResource();
2221

23-
public RichGlyph Glyph { get; } = new RichGlyph(opacityStyle: "ColorIconUnpinFromFavorites");
22+
public RichGlyph Glyph { get; } = new(opacityStyle: "ColorIconUnpinFromFavorites");
23+
24+
private bool isExecutable;
25+
public bool IsExecutable => isExecutable;
2426

25-
public bool IsExecutable
27+
public UnpinItemAction()
2628
{
27-
get
28-
{
29-
if ((context.SelectedItems.Any() && context.SelectedItems.All(x => x.IsPinned))
30-
|| (context.Folder is not null && context.Folder.IsPinned))
31-
return true;
29+
isExecutable = GetIsExecutable();
3230

33-
return false;
34-
}
31+
context.PropertyChanged += Context_PropertyChanged;
32+
App.QuickAccessManager.UpdateQuickAccessWidget += QuickAccessManager_DataChanged;
3533
}
3634

37-
public UnpinItemAction()
35+
public async Task ExecuteAsync()
3836
{
39-
context.PropertyChanged += Context_PropertyChanged;
37+
if (context.HasSelection)
38+
{
39+
var items = context.SelectedItems.Select(x => x.ItemPath).ToArray();
40+
await service.UnpinFromSidebar(items);
41+
}
42+
else if (context.Folder is not null)
43+
{
44+
await service.UnpinFromSidebar(context.Folder.ItemPath);
45+
}
4046
}
4147

48+
private bool GetIsExecutable()
49+
{
50+
string[] favorites = App.QuickAccessManager.Model.FavoriteItems.ToArray();
4251

43-
public async Task ExecuteAsync()
52+
return context.HasSelection
53+
? context.SelectedItems.All(IsPinned)
54+
: context.Folder is not null && IsPinned(context.Folder);
55+
56+
bool IsPinned(ListedItem item)
57+
{
58+
return favorites.Contains(item.ItemPath);
59+
}
60+
}
61+
private void UpdateIsExecutable()
4462
{
45-
await quickAccessService.UnpinFromSidebar(context.SelectedItems.Any() ? context.SelectedItems.Select(x => x.ItemPath).ToArray() : new[] { context.Folder.ItemPath });
63+
SetProperty(ref isExecutable, GetIsExecutable(), nameof(IsExecutable));
4664
}
4765

48-
public void Context_PropertyChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs e)
66+
private void Context_PropertyChanged(object? sender, PropertyChangedEventArgs e)
4967
{
5068
switch (e.PropertyName)
5169
{
52-
case nameof(IContentPageContext.SelectedItems):
5370
case nameof(IContentPageContext.Folder):
54-
OnPropertyChanged(nameof(IsExecutable));
71+
case nameof(IContentPageContext.SelectedItems):
72+
UpdateIsExecutable();
5573
break;
5674
}
5775
}
76+
77+
private void QuickAccessManager_DataChanged(object? sender, ModifyQuickAccessEventArgs e)
78+
{
79+
UpdateIsExecutable();
80+
}
5881
}
5982
}

src/Files.App/Helpers/ContextFlyoutItemHelper.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -501,11 +501,11 @@ public static List<ContextMenuFlyoutItemViewModel> GetBaseItemMenuItems(
501501
},
502502
new ContextMenuFlyoutItemViewModelBuilder(commands.PinItemToFavorites)
503503
{
504-
IsVisible = userSettingsService.PreferencesSettingsService.ShowFavoritesSection && selectedItems.All(x => x.PrimaryItemAttribute == StorageItemTypes.Folder && !x.IsArchive && !x.IsPinned),
504+
IsVisible = commands.PinItemToFavorites.IsExecutable && userSettingsService.PreferencesSettingsService.ShowFavoritesSection,
505505
}.Build(),
506506
new ContextMenuFlyoutItemViewModelBuilder(commands.UnpinItemFromFavorites)
507507
{
508-
IsVisible = userSettingsService.PreferencesSettingsService.ShowFavoritesSection && selectedItems.All(x => x.PrimaryItemAttribute == StorageItemTypes.Folder && !x.IsArchive && x.IsPinned),
508+
IsVisible = commands.UnpinItemFromFavorites.IsExecutable && userSettingsService.PreferencesSettingsService.ShowFavoritesSection,
509509
}.Build(),
510510
new ContextMenuFlyoutItemViewModelBuilder(commands.PinToStart)
511511
{

src/Files.App/Strings/en-US/Resources.resw

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@
273273
<data name="Delete" xml:space="preserve">
274274
<value>Delete</value>
275275
</data>
276-
<data name="BaseLayoutItemContextFlyoutPinToFavorites.Text" xml:space="preserve">
276+
<data name="PinToFavorites" xml:space="preserve">
277277
<value>Pin to Favorites</value>
278278
</data>
279279
<data name="WelcomeDialog.Title" xml:space="preserve">

src/Files.App/UserControls/SidebarControl.xaml.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ private List<ContextMenuFlyoutItemViewModel> GetLocationItemMenuItems(INavigatio
240240
},
241241
new ContextMenuFlyoutItemViewModel()
242242
{
243-
Text = "BaseLayoutItemContextFlyoutPinToFavorites/Text".GetLocalizedResource(),
243+
Text = "PinToFavorites".GetLocalizedResource(),
244244
OpacityIcon = new OpacityIconModel()
245245
{
246246
OpacityIconStyle = "ColorIconPinToFavorites",
@@ -565,7 +565,7 @@ private async void NavigationViewLocationItem_DragOver(object sender, DragEventA
565565
}
566566
else
567567
{
568-
var captionText = "BaseLayoutItemContextFlyoutPinToFavorites/Text".GetLocalizedResource();
568+
var captionText = "PinToFavorites".GetLocalizedResource();
569569
CompleteDragEventArgs(e, captionText, DataPackageOperation.Move);
570570
}
571571
}

src/Files.App/UserControls/Widgets/DrivesWidget.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ public override List<ContextMenuFlyoutItemViewModel> GetItemMenuItems(WidgetCard
179179
},
180180
new ContextMenuFlyoutItemViewModel()
181181
{
182-
Text = "BaseLayoutItemContextFlyoutPinToFavorites/Text".GetLocalizedResource(),
182+
Text = "PinToFavorites".GetLocalizedResource(),
183183
OpacityIcon = new OpacityIconModel()
184184
{
185185
OpacityIconStyle = "ColorIconPinToFavorites",

src/Files.App/UserControls/Widgets/FileTagsWidget.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ public override List<ContextMenuFlyoutItemViewModel> GetItemMenuItems(WidgetCard
195195
},
196196
new ContextMenuFlyoutItemViewModel()
197197
{
198-
Text = "BaseLayoutItemContextFlyoutPinToFavorites/Text".GetLocalizedResource(),
198+
Text = "PinToFavorites".GetLocalizedResource(),
199199
OpacityIcon = new OpacityIconModel()
200200
{
201201
OpacityIconStyle = "ColorIconPinToFavorites",

src/Files.App/UserControls/Widgets/QuickAccessWidget.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ public override List<ContextMenuFlyoutItemViewModel> GetItemMenuItems(WidgetCard
193193
},
194194
new ContextMenuFlyoutItemViewModel()
195195
{
196-
Text = "BaseLayoutItemContextFlyoutPinToFavorites/Text".GetLocalizedResource(),
196+
Text = "PinToFavorites".GetLocalizedResource(),
197197
OpacityIcon = new OpacityIconModel()
198198
{
199199
OpacityIconStyle = "ColorIconPinToFavorites",

0 commit comments

Comments
 (0)