Skip to content

Commit 05b8cef

Browse files
committed
Wrap up
1 parent c1f5776 commit 05b8cef

File tree

1 file changed

+30
-6
lines changed

1 file changed

+30
-6
lines changed

src/Files.App/ViewModels/UserControls/Widgets/QuickAccessWidgetViewModel.cs

+30-6
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,11 @@ public override async Task ExecutePinToSidebarCommand(WidgetCardItem? item)
186186
if (item is not WidgetFolderCardItem folderCardItem || folderCardItem.Path is null)
187187
return;
188188

189+
var lastPinnedItemIndex = Items.LastOrDefault(x => x.IsPinned) is { } lastPinnedItem ? Items.IndexOf(lastPinnedItem) : 0;
190+
var currentPinnedItemIndex = Items.IndexOf(folderCardItem);
191+
if (currentPinnedItemIndex is -1)
192+
return;
193+
189194
HRESULT hr = default;
190195
using ComPtr<IAgileReference> pAgileReference = default;
191196

@@ -203,6 +208,7 @@ public override async Task ExecutePinToSidebarCommand(WidgetCardItem? item)
203208
hr = pAgileReference.Get()->Resolve(IID.IID_IShellItem, (void**)pShellItem.GetAddressOf());
204209
var windowsFile = new WindowsFile(pShellItem);
205210

211+
// NOTE: "pintohome" is an undocumented verb, which calls an undocumented COM class, windows.storage.dll!CPinToFrequentExecute : public IExecuteCommand, ...
206212
return windowsFile.TryInvokeContextMenuVerb("pintohome");
207213
}
208214
});
@@ -211,21 +217,39 @@ public override async Task ExecutePinToSidebarCommand(WidgetCardItem? item)
211217
return;
212218

213219
// Add this to right before the last pinned item
214-
var lastPinnedItemIndex = Items.LastOrDefault(x => x.IsPinned) is { } lastPinnedItem ? Items.IndexOf(lastPinnedItem) : 0;
215-
var currentPinnedItemIndex = Items.IndexOf(folderCardItem);
216-
if (lastPinnedItemIndex != currentPinnedItemIndex && currentPinnedItemIndex is not -1)
220+
// NOTE: To be honest, this is not needed as the file watcher will take care of this
221+
if (lastPinnedItemIndex + 1 != currentPinnedItemIndex)
217222
Items.Move(currentPinnedItemIndex, lastPinnedItemIndex + 1);
218223
}
219224

220225
public override async Task ExecuteUnpinFromSidebarCommand(WidgetCardItem? item)
221226
{
222-
if (item is not WidgetFolderCardItem folderCardItem || item.Path is null)
227+
if (item is not WidgetFolderCardItem folderCardItem || folderCardItem.Path is null)
223228
return;
224229

225-
// Undocumented verb, which calls an undocumented COM class, windows.storage.dll!CPinToFrequentExecute CRemoveFromFrequentPlacesExecute CRemoveFromRecentPlacesExecute
230+
HRESULT hr = default;
231+
using ComPtr<IAgileReference> pAgileReference = default;
232+
233+
unsafe
234+
{
235+
hr = PInvoke.RoGetAgileReference(AgileReferenceOptions.AGILEREFERENCE_DEFAULT, IID.IID_IShellItem, (IUnknown*)folderCardItem.Item.ThisPtr.Get(), pAgileReference.GetAddressOf());
236+
}
226237

227238
// Unpin from Quick Access on Windows
228-
HRESULT hr = await STATask.Run(() => folderCardItem.Item.TryInvokeContextMenuVerbs(["unpinfromhome", "remove"], true));
239+
hr = await STATask.Run(() =>
240+
{
241+
unsafe
242+
{
243+
using ComPtr<IShellItem> pShellItem = default;
244+
hr = pAgileReference.Get()->Resolve(IID.IID_IShellItem, (void**)pShellItem.GetAddressOf());
245+
var windowsFile = new WindowsFile(pShellItem);
246+
247+
// NOTE: "unpinfromhome" is an undocumented verb, which calls an undocumented COM class, windows.storage.dll!CRemoveFromFrequentPlacesExecute : public IExecuteCommand, ...
248+
// NOTE: "remove" is for some shell folders where the "unpinfromhome" may not work
249+
return windowsFile.TryInvokeContextMenuVerbs(["unpinfromhome", "remove"], true);
250+
}
251+
});
252+
229253
if (hr.ThrowIfFailedOnDebug().Failed)
230254
return;
231255

0 commit comments

Comments
 (0)