Skip to content

Commit ddf1c68

Browse files
committed
Use IContextMenu
1 parent dc1b828 commit ddf1c68

File tree

2 files changed

+30
-61
lines changed

2 files changed

+30
-61
lines changed

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

+28-56
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
using System.Collections.Specialized;
66
using System.Runtime.InteropServices;
77
using System.Text;
8-
using Vanara.Windows.Shell;
98
using Windows.Win32;
109
using Windows.Win32.Foundation;
1110
using Windows.Win32.System.Com;
11+
using Windows.Win32.System.SystemServices;
1212
using Windows.Win32.UI.Shell;
1313
using Windows.Win32.UI.WindowsAndMessaging;
1414

@@ -115,7 +115,6 @@ public unsafe bool Remove(RecentItem item)
115115
{
116116
try
117117
{
118-
/*
119118
var bhid = PInvoke.BHID_SFUIObject;
120119
var contextMenuIid = typeof(IContextMenu).GUID;
121120
using ComPtr<IContextMenu> pContextMenu = default;
@@ -127,32 +126,16 @@ public unsafe bool Remove(RecentItem item)
127126
CMINVOKECOMMANDINFO cmi = default;
128127
cmi.cbSize = (uint)sizeof(CMINVOKECOMMANDINFO);
129128
cmi.nShow = (int)SHOW_WINDOW_CMD.SW_HIDE;
130-
fixed (byte* pVerb = Encoding.ASCII.GetBytes("remove"))
131-
cmi.lpVerb = new(pVerb);
132129

133-
// Invoke the verb
130+
// Try unpin first for pinned files
131+
fixed (byte* pVerb = Encoding.ASCII.GetBytes("unpinfromhome"))
132+
cmi.lpVerb = new(pVerb);
134133
hr = pContextMenu.Get()->InvokeCommand(cmi);
135-
if (hr.Value < 0)
136-
throw new COMException("Failed to remove the recent item from Recent.", hr.Value);
137-
*/
138-
139-
using ComPtr<IApplicationDestinations> pApplicationDestinations = default;
140-
var applicationDestinationsIid = typeof(IApplicationDestinations).GUID;
141-
var applicationDestinationsInstanceIid = typeof(ApplicationDestinations).GUID;
142134

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());
135+
// Remove recent files
136+
fixed (byte* pVerb = Encoding.ASCII.GetBytes("remove"))
137+
cmi.lpVerb = new(pVerb);
138+
hr = pContextMenu.Get()->InvokeCommand(cmi);
156139

157140
return true;
158141
}
@@ -168,25 +151,7 @@ public unsafe bool Clear()
168151
{
169152
try
170153
{
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);
188-
189-
pApplicationDestinations.Get()->RemoveAllDestinations();
154+
PInvoke.SHAddToRecentDocs((uint)SHARD.SHARD_PIDL, null);
190155

191156
return true;
192157
}
@@ -230,6 +195,20 @@ private unsafe bool UpdateRecentItems(bool isFolder)
230195
if (index is 20)
231196
break;
232197

198+
// Exclude folders
199+
if (pShellItem.Get()->GetAttributes(SFGAO_FLAGS.SFGAO_FOLDER, out var attribute) == HRESULT.S_OK &&
200+
(attribute & SFGAO_FLAGS.SFGAO_FOLDER) == SFGAO_FLAGS.SFGAO_FOLDER)
201+
continue;
202+
203+
// Exclude favorite files
204+
using ComPtr<IShellItem2> pShellItem2 = default;
205+
var shellItem2Iid = typeof(IShellItem2).GUID;
206+
hr = pShellItem.Get()->QueryInterface(&shellItem2Iid, (void**)pShellItem2.GetAddressOf());
207+
hr = PInvoke.PSGetPropertyKeyFromName("System.Home.IsPinned", out var propertyKey);
208+
hr = pShellItem2.Get()->GetString(propertyKey, out var szPropertyValue);
209+
if (bool.TryParse(szPropertyValue.ToString(), out var isPinned) && isPinned)
210+
continue;
211+
233212
// Get the target path
234213
pShellItem.Get()->GetDisplayName(SIGDN.SIGDN_DESKTOPABSOLUTEEDITING, out var szDisplayName);
235214
var targetPath = szDisplayName.ToString();
@@ -241,21 +220,14 @@ private unsafe bool UpdateRecentItems(bool isFolder)
241220
PInvoke.CoTaskMemFree(szDisplayName.Value);
242221

243222
// Strip the file extension except when the file name only contains extension (e.g. ".gitignore")
244-
if (!FoldersSettingsService.ShowFileExtensions)
245-
{
246-
string strippedExtension = SystemIO.Path.GetFileNameWithoutExtension(fileName);
247-
fileName = string.IsNullOrEmpty(strippedExtension) ? SystemIO.Path.GetFileName(fileName) : strippedExtension;
248-
}
249-
250-
// Get the PIDL
251-
PInvoke.SHGetIDListFromObject((IUnknown*)pShellItem.Get(), out var pidl);
223+
if (!FoldersSettingsService.ShowFileExtensions &&
224+
SystemIO.Path.GetFileNameWithoutExtension(fileName) is string fileNameWithoutExtension)
225+
fileName = string.IsNullOrEmpty(fileNameWithoutExtension) ? SystemIO.Path.GetFileName(fileName) : fileNameWithoutExtension;
252226

253227
// Get the date last modified
254-
using ComPtr<IShellItem2> pShellItem2 = default;
255-
var shellItem2Iid = typeof(IShellItem2).GUID;
256228
hr = pShellItem.Get()->QueryInterface(&shellItem2Iid, (void**)pShellItem2.GetAddressOf());
257-
hr = PInvoke.PSGetPropertyKeyFromName("System.DateCreated", out var propertyKey);
258-
hr = pShellItem2.Get()->GetString(propertyKey, out var szPropertyValue);
229+
hr = PInvoke.PSGetPropertyKeyFromName("System.DateModified", out propertyKey);
230+
hr = pShellItem2.Get()->GetString(propertyKey, out szPropertyValue);
259231
if (DateTime.TryParse(szPropertyValue.ToString(), out var lastModified))
260232
lastModified = DateTime.MinValue;
261233

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

+2-5
Original file line numberDiff line numberDiff line change
@@ -286,13 +286,10 @@ await Task.Run(() =>
286286
private async Task ExecuteClearRecentItemsCommand()
287287
{
288288
await _refreshRecentFilesSemaphore.WaitAsync();
289+
289290
try
290291
{
291-
Items.Clear();
292-
bool success = WindowsRecentItemsService.Clear();
293-
294-
if (success)
295-
IsEmptyRecentFilesTextVisible = true;
292+
WindowsRecentItemsService.Clear();
296293
}
297294
finally
298295
{

0 commit comments

Comments
 (0)