Skip to content

Feature: Improved commands for Pin/Unpin #11729

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
Mar 17, 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
67 changes: 48 additions & 19 deletions src/Files.App/Actions/Favorites/PinItemAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<IContentPageContext>();
private readonly IContentPageContext context = Ioc.Default.GetRequiredService<IContentPageContext>();
private readonly IQuickAccessService service = Ioc.Default.GetRequiredService<IQuickAccessService>();

private readonly IQuickAccessService quickAccessService = Ioc.Default.GetRequiredService<IQuickAccessService>();
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();
}
}
}
67 changes: 45 additions & 22 deletions src/Files.App/Actions/Favorites/UnpinItemAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<IContentPageContext>();

private readonly IQuickAccessService quickAccessService = Ioc.Default.GetRequiredService<IQuickAccessService>();
private readonly IContentPageContext context = Ioc.Default.GetRequiredService<IContentPageContext>();
private readonly IQuickAccessService service = Ioc.Default.GetRequiredService<IQuickAccessService>();

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();
}
}
}
4 changes: 2 additions & 2 deletions src/Files.App/Helpers/ContextFlyoutItemHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -501,11 +501,11 @@ public static List<ContextMenuFlyoutItemViewModel> 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)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Files.App/Strings/en-US/Resources.resw
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@
<data name="Delete" xml:space="preserve">
<value>Delete</value>
</data>
<data name="BaseLayoutItemContextFlyoutPinToFavorites.Text" xml:space="preserve">
<data name="PinToFavorites" xml:space="preserve">
<value>Pin to Favorites</value>
</data>
<data name="WelcomeDialog.Title" xml:space="preserve">
Expand Down
4 changes: 2 additions & 2 deletions src/Files.App/UserControls/SidebarControl.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ private List<ContextMenuFlyoutItemViewModel> GetLocationItemMenuItems(INavigatio
},
new ContextMenuFlyoutItemViewModel()
{
Text = "BaseLayoutItemContextFlyoutPinToFavorites/Text".GetLocalizedResource(),
Text = "PinToFavorites".GetLocalizedResource(),
OpacityIcon = new OpacityIconModel()
{
OpacityIconStyle = "ColorIconPinToFavorites",
Expand Down Expand Up @@ -565,7 +565,7 @@ private async void NavigationViewLocationItem_DragOver(object sender, DragEventA
}
else
{
var captionText = "BaseLayoutItemContextFlyoutPinToFavorites/Text".GetLocalizedResource();
var captionText = "PinToFavorites".GetLocalizedResource();
CompleteDragEventArgs(e, captionText, DataPackageOperation.Move);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Files.App/UserControls/Widgets/DrivesWidget.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ public override List<ContextMenuFlyoutItemViewModel> GetItemMenuItems(WidgetCard
},
new ContextMenuFlyoutItemViewModel()
{
Text = "BaseLayoutItemContextFlyoutPinToFavorites/Text".GetLocalizedResource(),
Text = "PinToFavorites".GetLocalizedResource(),
OpacityIcon = new OpacityIconModel()
{
OpacityIconStyle = "ColorIconPinToFavorites",
Expand Down
2 changes: 1 addition & 1 deletion src/Files.App/UserControls/Widgets/FileTagsWidget.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ public override List<ContextMenuFlyoutItemViewModel> GetItemMenuItems(WidgetCard
},
new ContextMenuFlyoutItemViewModel()
{
Text = "BaseLayoutItemContextFlyoutPinToFavorites/Text".GetLocalizedResource(),
Text = "PinToFavorites".GetLocalizedResource(),
OpacityIcon = new OpacityIconModel()
{
OpacityIconStyle = "ColorIconPinToFavorites",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ public override List<ContextMenuFlyoutItemViewModel> GetItemMenuItems(WidgetCard
},
new ContextMenuFlyoutItemViewModel()
{
Text = "BaseLayoutItemContextFlyoutPinToFavorites/Text".GetLocalizedResource(),
Text = "PinToFavorites".GetLocalizedResource(),
OpacityIcon = new OpacityIconModel()
{
OpacityIconStyle = "ColorIconPinToFavorites",
Expand Down