From ccded56639a3f283bfae12c87274f89e93fa1f55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20Garc=C3=ADa=20Ruiz?= Date: Sat, 4 Feb 2023 16:58:42 +0100 Subject: [PATCH 1/6] Add recent items widget --- .../NavigationControlItems/LocationItem.cs | 1 + .../DataModels/SidebarPinnedModel.cs | 19 +- .../Filesystem/QuickAccessManager.cs | 2 + .../QuickAccessService.cs | 19 +- src/Files.App/Strings/en-US/Resources.resw | 3 + .../UserControls/Widgets/FolderWidget.xaml | 90 ++++++---- .../UserControls/Widgets/FolderWidget.xaml.cs | 165 ++++++++++++------ src/Files.App/ViewModels/ToolbarViewModel.cs | 5 + src/Files.App/Views/WidgetsPage.xaml.cs | 31 ++-- .../Services/IQuickAccessService.cs | 2 +- 10 files changed, 229 insertions(+), 108 deletions(-) diff --git a/src/Files.App/DataModels/NavigationControlItems/LocationItem.cs b/src/Files.App/DataModels/NavigationControlItems/LocationItem.cs index 2ad2908b9cee..c55ba6506064 100644 --- a/src/Files.App/DataModels/NavigationControlItems/LocationItem.cs +++ b/src/Files.App/DataModels/NavigationControlItems/LocationItem.cs @@ -56,6 +56,7 @@ public bool IsExpanded public bool IsInvalid { get; set; } = false; + public bool IsPinned => App.QuickAccessManager.Model.FavoriteItems.Contains(path); public SectionType Section { get; set; } public ContextMenuOptions MenuOptions { get; set; } diff --git a/src/Files.App/DataModels/SidebarPinnedModel.cs b/src/Files.App/DataModels/SidebarPinnedModel.cs index 209c27cd3864..477551e8ebbb 100644 --- a/src/Files.App/DataModels/SidebarPinnedModel.cs +++ b/src/Files.App/DataModels/SidebarPinnedModel.cs @@ -75,12 +75,7 @@ public int IndexOfItem(INavigationControlItem locationItem) } } - /// - /// Adds the item (from a path) to the navigation sidebar - /// - /// The path which to save - /// Task - public async Task AddItemToSidebarAsync(string path) + public async Task CreateLocationItemFromPathAsync(string path) { var item = await FilesystemTasks.Wrap(() => DrivesManager.GetRootFromPathAsync(path)); var res = await FilesystemTasks.Wrap(() => StorageFileExtensions.DangerousGetFolderFromPathAsync(path, item)); @@ -130,6 +125,18 @@ public async Task AddItemToSidebarAsync(string path) Debug.WriteLine($"Pinned item was invalid {res.ErrorCode}, item: {path}"); } + return locationItem; + } + + /// + /// Adds the item (from a path) to the navigation sidebar + /// + /// The path which to save + /// Task + public async Task AddItemToSidebarAsync(string path) + { + var locationItem = await CreateLocationItemFromPathAsync(path); + AddLocationItemToSidebar(locationItem); } diff --git a/src/Files.App/Filesystem/QuickAccessManager.cs b/src/Files.App/Filesystem/QuickAccessManager.cs index 543a01e4608e..dff0e36139fb 100644 --- a/src/Files.App/Filesystem/QuickAccessManager.cs +++ b/src/Files.App/Filesystem/QuickAccessManager.cs @@ -1,6 +1,7 @@ using CommunityToolkit.Mvvm.DependencyInjection; using Files.App.DataModels; using Files.App.ServicesImplementation; +using Files.App.UserControls.Widgets; using Files.Shared.Extensions; using System; using System.Collections.Generic; @@ -17,6 +18,7 @@ public sealed class QuickAccessManager { public FileSystemWatcher? PinnedItemsWatcher; public event FileSystemEventHandler? PinnedItemsModified; + public EventHandler? UpdateFolderWidget; public IQuickAccessService QuickAccessService { get; } = Ioc.Default.GetRequiredService(); public SidebarPinnedModel Model; diff --git a/src/Files.App/ServicesImplementation/QuickAccessService.cs b/src/Files.App/ServicesImplementation/QuickAccessService.cs index 5b9e48ecda4a..554114c719c9 100644 --- a/src/Files.App/ServicesImplementation/QuickAccessService.cs +++ b/src/Files.App/ServicesImplementation/QuickAccessService.cs @@ -1,8 +1,13 @@ -using Files.App.Shell; +using Files.App.DataModels.NavigationControlItems; +using Files.App.Filesystem; +using Files.App.Shell; +using Files.App.UserControls.Widgets; +using Files.Shared; using Files.Shared.Extensions; using System; using System.Collections.Generic; using System.Linq; +using System.Security.Permissions; using System.Threading.Tasks; namespace Files.App.ServicesImplementation @@ -10,19 +15,19 @@ namespace Files.App.ServicesImplementation internal class QuickAccessService : IQuickAccessService { private readonly static string guid = "::{679f85cb-0220-4080-b29b-5540cc05aab6}"; - - public async Task> GetPinnedFoldersAsync() + + public async Task> GetPinnedFoldersAsync(bool getRecentItems = false) { var sidebarItems = (await Win32Shell.GetShellFolderAsync(guid, "Enumerate", 0, 10000)).Enumerate .Where(link => link.IsFolder) .Select(link => link.FilePath).ToList(); - if (sidebarItems.Count > 4) // Avoid first opening crash #11139 + if (sidebarItems.Count > 4 && !getRecentItems) // Avoid first opening crash #11139 sidebarItems.RemoveRange(sidebarItems.Count - 4, 4); // 4 is the number of recent items shown in explorer sidebar return sidebarItems; } - + public async Task PinToSidebar(string folderPath) => await PinToSidebar(new[] { folderPath }); @@ -30,8 +35,9 @@ public async Task PinToSidebar(string[] folderPaths) { await ContextMenu.InvokeVerb("pintohome", folderPaths); await App.QuickAccessManager.Model.LoadAsync(); + App.QuickAccessManager.UpdateFolderWidget?.Invoke(this, new ModifyQuickAccessEventArgs(folderPaths, true)); } - + public async Task UnpinFromSidebar(string folderPath) => await UnpinFromSidebar(new[] { folderPath }); @@ -48,6 +54,7 @@ await SafetyExtensions.IgnoreExceptions(async () => { }); await App.QuickAccessManager.Model.LoadAsync(); + App.QuickAccessManager.UpdateFolderWidget?.Invoke(this, new ModifyQuickAccessEventArgs(folderPaths, false)); } } } diff --git a/src/Files.App/Strings/en-US/Resources.resw b/src/Files.App/Strings/en-US/Resources.resw index 89c6767f81ee..24d5fc1b75b0 100644 --- a/src/Files.App/Strings/en-US/Resources.resw +++ b/src/Files.App/Strings/en-US/Resources.resw @@ -2901,4 +2901,7 @@ Display the edit tags flyout + + Quick Access + \ No newline at end of file diff --git a/src/Files.App/UserControls/Widgets/FolderWidget.xaml b/src/Files.App/UserControls/Widgets/FolderWidget.xaml index 7da6d737f348..f3f498776838 100644 --- a/src/Files.App/UserControls/Widgets/FolderWidget.xaml +++ b/src/Files.App/UserControls/Widgets/FolderWidget.xaml @@ -26,7 +26,7 @@ -