11// Copyright (c) 2024 Files Community
22// Licensed under the MIT License. See the LICENSE.
33
4- using Windows . Storage ;
4+ using System . Collections . Specialized ;
55
66namespace Files . App . Actions
77{
88 internal sealed class PinFolderToSidebarAction : ObservableObject , IAction
99 {
10- private readonly IContentPageContext context ;
11- private readonly IQuickAccessService service ;
10+ private readonly IContentPageContext context = Ioc . Default . GetRequiredService < IContentPageContext > ( ) ;
11+ private readonly IQuickAccessService service = Ioc . Default . GetRequiredService < IQuickAccessService > ( ) ;
1212
1313 public string Label
1414 => "PinFolderToSidebar" . GetLocalizedResource ( ) ;
@@ -24,30 +24,25 @@ public bool IsExecutable
2424
2525 public PinFolderToSidebarAction ( )
2626 {
27- context = Ioc . Default . GetRequiredService < IContentPageContext > ( ) ;
28- service = Ioc . Default . GetRequiredService < IQuickAccessService > ( ) ;
29-
3027 context . PropertyChanged += Context_PropertyChanged ;
31- App . QuickAccessManager . UpdateQuickAccessWidget += QuickAccessManager_DataChanged ;
28+ service . PinnedFoldersChanged += QuickAccessService_CollectionChanged ;
3229 }
3330
3431 public async Task ExecuteAsync ( object ? parameter = null )
3532 {
36- if ( context . HasSelection )
37- {
38- var items = context . SelectedItems . Select ( x => x . ItemPath ) . ToArray ( ) ;
33+ var items = context . HasSelection
34+ ? context . SelectedItems . Select ( x => x . ItemPath ) . ToArray ( )
35+ : context . Folder is not null
36+ ? [ context . Folder . ItemPath ]
37+ : null ;
3938
40- await service . PinToSidebarAsync ( items ) ;
41- }
42- else if ( context . Folder is not null )
43- {
44- await service . PinToSidebarAsync ( [ context . Folder . ItemPath ] ) ;
45- }
39+ if ( items is not null )
40+ await service . PinFolderAsync ( items ) ;
4641 }
4742
4843 private bool GetIsExecutable ( )
4944 {
50- string [ ] pinnedFolders = [ .. App . QuickAccessManager . Model . PinnedFolders ] ;
45+ string [ ] pinnedFolders = [ .. service . PinnedFolders . Select ( x => x . Path ) ] ;
5146
5247 return context . HasSelection
5348 ? context . SelectedItems . All ( IsPinnable )
@@ -56,7 +51,7 @@ private bool GetIsExecutable()
5651 bool IsPinnable ( ListedItem item )
5752 {
5853 return
59- item . PrimaryItemAttribute is StorageItemTypes . Folder &&
54+ item . PrimaryItemAttribute is Windows . Storage . StorageItemTypes . Folder &&
6055 ! pinnedFolders . Contains ( item . ItemPath ) ;
6156 }
6257 }
@@ -72,7 +67,7 @@ private void Context_PropertyChanged(object? sender, PropertyChangedEventArgs e)
7267 }
7368 }
7469
75- private void QuickAccessManager_DataChanged ( object ? sender , ModifyQuickAccessEventArgs e )
70+ private void QuickAccessService_CollectionChanged ( object ? sender , NotifyCollectionChangedEventArgs e )
7671 {
7772 OnPropertyChanged ( nameof ( IsExecutable ) ) ;
7873 }
0 commit comments