Skip to content

Commit 841d07e

Browse files
committed
Update
1 parent ecdbcf4 commit 841d07e

File tree

3 files changed

+22
-16
lines changed

3 files changed

+22
-16
lines changed

src/Files.App.CsWin32/NativeMethods.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,3 +160,4 @@ BHID_EnumItems
160160
BHID_SFUIObject
161161
IContextMenu
162162
CMF_OPTIMIZEFORINVOKE
163+
IPropertyStore

src/Files.App/Data/Items/LocationItem.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ namespace Files.App.Data.Items
1111
{
1212
public class LocationItem : ObservableObject, INavigationControlItem
1313
{
14-
protected readonly IQuickAccessService QuickAccessService = Ioc.Default.GetRequiredService<IQuickAccessService>();
15-
1614
public BitmapImage icon;
1715
public BitmapImage Icon
1816
{
@@ -82,7 +80,7 @@ public bool IsExpanded
8280

8381
public bool IsInvalid { get; set; } = false;
8482

85-
public bool IsPinned => QuickAccessService.IsPinned(path);
83+
public bool IsPinned { get; set; }
8684

8785
public SectionType Section { get; set; }
8886

src/Files.App/Services/Windows/WindowsQuickAccessService.cs

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
using System.Text;
66
using Windows.Win32;
77
using Windows.Win32.Foundation;
8+
using Windows.Win32.System.Com.StructuredStorage;
89
using Windows.Win32.UI.Shell;
10+
using Windows.Win32.UI.Shell.PropertiesSystem;
911
using Windows.Win32.UI.WindowsAndMessaging;
1012

1113
namespace Files.App.Services
@@ -89,30 +91,22 @@ unsafe List<string> GetPinnedFolders()
8991
HRESULT hr = default;
9092

9193
// Get IShellItem of the shell folder
92-
var shellItemIid = typeof(IShellItem).GUID;
94+
var IID_IShellItem = typeof(IShellItem).GUID;
9395
using ComPtr<IShellItem> pFolderShellItem = default;
9496
fixed (char* pszFolderShellPath = "Shell:::{3936E9E4-D92C-4EEE-A85A-BC16D5EA0819}")
95-
hr = PInvoke.SHCreateItemFromParsingName(pszFolderShellPath, null, &shellItemIid, (void**)pFolderShellItem.GetAddressOf());
97+
hr = PInvoke.SHCreateItemFromParsingName(pszFolderShellPath, null, &IID_IShellItem, (void**)pFolderShellItem.GetAddressOf());
9698

9799
// Get IEnumShellItems of the quick access shell folder
98-
var enumItemsBHID = PInvoke.BHID_EnumItems;
99-
Guid enumShellItemIid = typeof(IEnumShellItems).GUID;
100+
var BHID_EnumItems = PInvoke.BHID_EnumItems, IID_IEnumShellItems = typeof(IEnumShellItems).GUID;
100101
using ComPtr<IEnumShellItems> pEnumShellItems = default;
101-
hr = pFolderShellItem.Get()->BindToHandler(null, &enumItemsBHID, &enumShellItemIid, (void**)pEnumShellItems.GetAddressOf());
102+
hr = pFolderShellItem.Get()->BindToHandler(null, &BHID_EnumItems, &IID_IEnumShellItems, (void**)pEnumShellItems.GetAddressOf());
102103

103104
// Enumerate pinned folders
104105
int index = 0;
105106
List<string> paths = [];
106107
using ComPtr<IShellItem> pShellItem = default;
107108
while (pEnumShellItems.Get()->Next(1, pShellItem.GetAddressOf()) == HRESULT.S_OK)
108109
{
109-
// Get whether the item is pined or not
110-
using ComPtr<IShellItem2> pShellItem2 = pShellItem.As<IShellItem2>();
111-
hr = PInvoke.PSGetPropertyKeyFromName("System.Home.IsPinned", out var propertyKey);
112-
hr = pShellItem2.Get()->GetString(propertyKey, out var szPropertyValue);
113-
if (!bool.TryParse(szPropertyValue.ToString(), out var isPinned) || !isPinned)
114-
continue;
115-
116110
// Get the full path
117111
pShellItem.Get()->GetDisplayName(SIGDN.SIGDN_FILESYSPATH, out var szDisplayName);
118112
var path = szDisplayName.ToString();
@@ -147,6 +141,7 @@ async Task<LocationItem> CreateItemOf(string path)
147141
}
148142

149143
locationItem.Path = path;
144+
locationItem.IsPinned = IsPinned(path);
150145
locationItem.Section = SectionType.Pinned;
151146
locationItem.MenuOptions = new()
152147
{
@@ -227,7 +222,19 @@ NotifyCollectionChangedEventArgs GetChangedActionEventArgs(IReadOnlyList<INaviga
227222

228223
public bool IsPinned(string path)
229224
{
230-
return QuickAccessFolders.Cast<LocationItem>().ToList().Where(x => x.Path == path).Count() > 0;
225+
HRESULT hr = default;
226+
var IID_IShellItem = typeof(IShellItem).GUID;
227+
using ComPtr<IShellItem> pShellItem = default;
228+
fixed (char* pszPath = path)
229+
hr = PInvoke.SHCreateItemFromParsingName(pszPath, null, &IID_IShellItem, (void**)pShellItem.GetAddressOf());
230+
231+
using ComPtr<IShellItem2> pShellItem2 = pShellItem.As<IShellItem2>();
232+
var IID_IPropertyStore = typeof(IPropertyStore).GUID;
233+
using ComPtr<IPropertyStore> pPropertyStore = default;
234+
hr = pShellItem2.Get()->GetPropertyStore(GETPROPERTYSTOREFLAGS.GPS_DEFAULT, &IID_IPropertyStore, (void**)pPropertyStore.GetAddressOf());
235+
hr = PInvoke.PSGetPropertyKeyFromName("System.Home.IsPinned", out var propertyKey);
236+
hr = pPropertyStore.Get()->GetValue(propertyKey, out var propertyValue);
237+
return (bool)propertyValue.Anonymous.boolVal;
231238
}
232239

233240
public async Task<bool> PinFolderAsync(string[] paths)

0 commit comments

Comments
 (0)