Skip to content

Commit dc1b828

Browse files
committed
Use IApplicationDestinations
1 parent cf808bc commit dc1b828

File tree

2 files changed

+47
-31
lines changed

2 files changed

+47
-31
lines changed

src/Files.App.CsWin32/NativeMethods.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,3 +143,8 @@ SHCreateItemFromIDList
143143
BHID_SFUIObject
144144
IContextMenu
145145
CMF_NORMAL
146+
CMF_OPTIMIZEFORINVOKE
147+
IApplicationDestinations
148+
ApplicationDestinations
149+
IApplicationDocumentLists
150+
ApplicationDocumentLists

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

Lines changed: 42 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,6 @@ public unsafe bool Add(string path)
101101
fixed (char* cPath = path)
102102
PInvoke.SHAddToRecentDocs((uint)SHARD.SHARD_PATHW, cPath);
103103

104-
// Update
105-
_ = UpdateRecentFilesAsync();
106-
_ = UpdateRecentFoldersAsync();
107-
108104
return true;
109105
}
110106
catch (Exception ex)
@@ -119,37 +115,19 @@ public unsafe bool Remove(RecentItem item)
119115
{
120116
try
121117
{
122-
/*
123-
// Initialize IFileOperation instance
124-
using ComPtr<IFileOperation> pFileOperation = default;
125-
var fileOperationIid = typeof(IFileOperation).GUID;
126-
var fileOperationInstanceIid = typeof(FileOperation).GUID;
127-
HRESULT hr = PInvoke.CoCreateInstance(&fileOperationInstanceIid, null, CLSCTX.CLSCTX_LOCAL_SERVER, &fileOperationIid, (void**)pFileOperation.GetAddressOf());
128-
hr = pFileOperation.Get()->SetOperationFlags(FILEOPERATION_FLAGS.FOF_NO_UI);
129-
hr = pFileOperation.Get()->SetOwnerWindow(new(MainWindow.Instance.WindowHandle));
130-
131-
// Perform deletion
132-
hr = pFileOperation.Get()->DeleteItem(item.ShellItem.Get(), null);
133-
hr = pFileOperation.Get()->PerformOperations();
134-
135-
// Update
136-
_ = UpdateRecentFilesAsync();
137-
_ = UpdateRecentFoldersAsync();
138-
*/
139-
140118
/*
141119
var bhid = PInvoke.BHID_SFUIObject;
142120
var contextMenuIid = typeof(IContextMenu).GUID;
143121
using ComPtr<IContextMenu> pContextMenu = default;
144122
HRESULT hr = item.ShellItem.Get()->BindToHandler(null, &bhid, &contextMenuIid, (void**)pContextMenu.GetAddressOf());
145123
HMENU hMenu = PInvoke.CreatePopupMenu();
146-
hr = pContextMenu.Get()->QueryContextMenu(hMenu, 0, 1, 0x7FFF, PInvoke.CMF_NORMAL);
124+
hr = pContextMenu.Get()->QueryContextMenu(hMenu, 0, 1, 0x7FFF, PInvoke.CMF_OPTIMIZEFORINVOKE);
147125
148126
// Initialize invocation info
149127
CMINVOKECOMMANDINFO cmi = default;
150128
cmi.cbSize = (uint)sizeof(CMINVOKECOMMANDINFO);
151129
cmi.nShow = (int)SHOW_WINDOW_CMD.SW_HIDE;
152-
fixed (byte* pVerb = Encoding.ASCII.GetBytes("delete"))
130+
fixed (byte* pVerb = Encoding.ASCII.GetBytes("remove"))
153131
cmi.lpVerb = new(pVerb);
154132
155133
// Invoke the verb
@@ -158,10 +136,29 @@ public unsafe bool Remove(RecentItem item)
158136
throw new COMException("Failed to remove the recent item from Recent.", hr.Value);
159137
*/
160138

139+
using ComPtr<IApplicationDestinations> pApplicationDestinations = default;
140+
var applicationDestinationsIid = typeof(IApplicationDestinations).GUID;
141+
var applicationDestinationsInstanceIid = typeof(ApplicationDestinations).GUID;
142+
143+
PInvoke.CoCreateInstance(
144+
&applicationDestinationsInstanceIid,
145+
null,
146+
CLSCTX.CLSCTX_LOCAL_SERVER,
147+
&applicationDestinationsIid,
148+
(void**)pApplicationDestinations.GetAddressOf())
149+
.ThrowOnFailure();
150+
151+
// Set App ID
152+
fixed (char* pszAppId = "Microsoft.Windows.Explorer")
153+
pApplicationDestinations.Get()->SetAppID(pszAppId);
154+
155+
pApplicationDestinations.Get()->RemoveDestination((IUnknown*)item.ShellItem.Get());
156+
161157
return true;
162158
}
163-
catch
159+
catch (Exception ex)
164160
{
161+
App.Logger.LogWarning(ex, ex.Message);
165162
return false;
166163
}
167164
}
@@ -171,11 +168,25 @@ public unsafe bool Clear()
171168
{
172169
try
173170
{
174-
PInvoke.SHAddToRecentDocs((uint)SHARD.SHARD_PIDL, null);
171+
//PInvoke.SHAddToRecentDocs((uint)SHARD.SHARD_PIDL, null);
172+
173+
using ComPtr<IApplicationDestinations> pApplicationDestinations = default;
174+
var applicationDestinationsIid = typeof(IApplicationDestinations).GUID;
175+
var applicationDestinationsInstanceIid = typeof(ApplicationDestinations).GUID;
176+
177+
PInvoke.CoCreateInstance(
178+
&applicationDestinationsInstanceIid,
179+
null,
180+
CLSCTX.CLSCTX_LOCAL_SERVER,
181+
&applicationDestinationsIid,
182+
(void**)pApplicationDestinations.GetAddressOf())
183+
.ThrowOnFailure();
184+
185+
// Set App ID
186+
fixed (char* pszAppId = "Microsoft.Windows.Explorer")
187+
pApplicationDestinations.Get()->SetAppID(pszAppId);
175188

176-
// Update
177-
_ = UpdateRecentFilesAsync();
178-
_ = UpdateRecentFoldersAsync();
189+
pApplicationDestinations.Get()->RemoveAllDestinations();
179190

180191
return true;
181192
}
@@ -239,7 +250,7 @@ private unsafe bool UpdateRecentItems(bool isFolder)
239250
// Get the PIDL
240251
PInvoke.SHGetIDListFromObject((IUnknown*)pShellItem.Get(), out var pidl);
241252

242-
// Get the original path
253+
// Get the date last modified
243254
using ComPtr<IShellItem2> pShellItem2 = default;
244255
var shellItem2Iid = typeof(IShellItem2).GUID;
245256
hr = pShellItem.Get()->QueryInterface(&shellItem2Iid, (void**)pShellItem2.GetAddressOf());
@@ -262,7 +273,7 @@ private unsafe bool UpdateRecentItems(bool isFolder)
262273
if (recentItems.Count is 0)
263274
return false;
264275

265-
// Sort by last modified
276+
// Sort by the display name
266277
var orderedRecentItems = recentItems.OrderBy(x => x.Name).ToList();
267278

268279
var snapshot = isFolder ? RecentFolders : RecentFiles;

0 commit comments

Comments
 (0)