|
5 | 5 | using System.Text; |
6 | 6 | using Windows.Win32; |
7 | 7 | using Windows.Win32.Foundation; |
| 8 | +using Windows.Win32.System.Com.StructuredStorage; |
8 | 9 | using Windows.Win32.UI.Shell; |
| 10 | +using Windows.Win32.UI.Shell.PropertiesSystem; |
9 | 11 | using Windows.Win32.UI.WindowsAndMessaging; |
10 | 12 |
|
11 | 13 | namespace Files.App.Services |
@@ -89,30 +91,22 @@ unsafe List<string> GetPinnedFolders() |
89 | 91 | HRESULT hr = default; |
90 | 92 |
|
91 | 93 | // Get IShellItem of the shell folder |
92 | | - var shellItemIid = typeof(IShellItem).GUID; |
| 94 | + var IID_IShellItem = typeof(IShellItem).GUID; |
93 | 95 | using ComPtr<IShellItem> pFolderShellItem = default; |
94 | 96 | 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()); |
96 | 98 |
|
97 | 99 | // 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; |
100 | 101 | 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()); |
102 | 103 |
|
103 | 104 | // Enumerate pinned folders |
104 | 105 | int index = 0; |
105 | 106 | List<string> paths = []; |
106 | 107 | using ComPtr<IShellItem> pShellItem = default; |
107 | 108 | while (pEnumShellItems.Get()->Next(1, pShellItem.GetAddressOf()) == HRESULT.S_OK) |
108 | 109 | { |
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 | | - |
116 | 110 | // Get the full path |
117 | 111 | pShellItem.Get()->GetDisplayName(SIGDN.SIGDN_FILESYSPATH, out var szDisplayName); |
118 | 112 | var path = szDisplayName.ToString(); |
@@ -147,6 +141,7 @@ async Task<LocationItem> CreateItemOf(string path) |
147 | 141 | } |
148 | 142 |
|
149 | 143 | locationItem.Path = path; |
| 144 | + locationItem.IsPinned = IsPinned(path); |
150 | 145 | locationItem.Section = SectionType.Pinned; |
151 | 146 | locationItem.MenuOptions = new() |
152 | 147 | { |
@@ -227,7 +222,19 @@ NotifyCollectionChangedEventArgs GetChangedActionEventArgs(IReadOnlyList<INaviga |
227 | 222 |
|
228 | 223 | public bool IsPinned(string path) |
229 | 224 | { |
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; |
231 | 238 | } |
232 | 239 |
|
233 | 240 | public async Task<bool> PinFolderAsync(string[] paths) |
|
0 commit comments