22// Licensed under the MIT License. See the LICENSE.
33
44using Microsoft . Extensions . Logging ;
5- using Microsoft . Win32 ;
65using System . Collections . Specialized ;
76using Windows . Win32 ;
87using Windows . Win32 . Foundation ;
8+ using Windows . Win32 . System . Com ;
99using Windows . Win32 . UI . Shell ;
1010
1111namespace Files . App . Services
@@ -108,60 +108,48 @@ public unsafe bool Add(string path)
108108
109109 /// <inheritdoc/>
110110 public unsafe bool Remove ( RecentItem item )
111- {
112- return false ; // TODO: Use ContextMenu class to invoke "delete" verb
113- }
114-
115- /// <inheritdoc/>
116- public unsafe bool Clear ( )
117111 {
118112 try
119113 {
120- PInvoke . SHAddToRecentDocs ( ( uint ) SHARD . SHARD_PIDL , null ) ;
114+ // Initialize IFileOperation instance
115+ using ComPtr < IFileOperation > pFileOperation = default ;
116+ var fileOperationIid = typeof ( IFileOperation ) . GUID ;
117+ var fileOperationInstanceIid = typeof ( FileOperation ) . GUID ;
118+ HRESULT hr = PInvoke . CoCreateInstance ( & fileOperationInstanceIid , null , CLSCTX . CLSCTX_LOCAL_SERVER , & fileOperationIid , ( void * * ) pFileOperation . GetAddressOf ( ) ) ;
119+ hr = pFileOperation . Get ( ) ->SetOperationFlags ( FILEOPERATION_FLAGS . FOF_NO_UI ) ;
120+ hr = pFileOperation . Get ( ) ->SetOwnerWindow ( new ( MainWindow . Instance . WindowHandle ) ) ;
121+
122+ // Get IShellItem from PIDL
123+ var shellItemIid = typeof ( IShellItem ) . GUID ;
124+ ComPtr < IShellItem > pShellItem = default ;
125+ PInvoke . SHCreateItemFromIDList ( item . PIDL , & shellItemIid , ( void * * ) pShellItem . GetAddressOf ( ) ) ;
126+
127+ // Perform deletion
128+ hr = pFileOperation . Get ( ) ->DeleteItem ( pShellItem . Get ( ) , null ) ;
129+ hr = pFileOperation . Get ( ) ->PerformOperations ( ) ;
121130
122131 return true ;
123132 }
124- catch ( Exception ex )
133+ catch
125134 {
126- App . Logger . LogWarning ( ex , ex . Message ) ;
127135 return false ;
128136 }
129137 }
130138
131139 /// <inheritdoc/>
132- public bool CheckIsRecentItemsEnabled ( )
140+ public unsafe bool Clear ( )
133141 {
134- using var explorerSubKey = Registry . CurrentUser . OpenSubKey ( @"SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer" ) ;
135- using var advSubkey = Registry . CurrentUser . OpenSubKey ( @"SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Advanced" ) ;
136- using var userPolicySubkey = Registry . CurrentUser . OpenSubKey ( @"SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\Explorer" ) ;
137- using var sysPolicySubkey = Registry . LocalMachine . OpenSubKey ( @"SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\Explorer" ) ;
138-
139- if ( explorerSubKey is not null )
142+ try
140143 {
141- // File Explorer settings (default: 1)
142- bool showRecentValue = Convert . ToBoolean ( explorerSubKey . GetValue ( "ShowRecent" , true ) ) ;
143- if ( ! showRecentValue )
144- return false ;
145- }
144+ PInvoke . SHAddToRecentDocs ( ( uint ) SHARD . SHARD_PIDL , null ) ;
146145
147- if ( advSubkey is not null )
148- {
149- // Windows Settings (default: 1)
150- bool startTrackDocsValue = Convert . ToBoolean ( advSubkey . GetValue ( "Start_TrackDocs" , true ) ) ;
151- if ( ! startTrackDocsValue )
152- return false ;
146+ return true ;
153147 }
154-
155- // Group Policy settings (default: 0)
156- var policySubkey = userPolicySubkey ?? sysPolicySubkey ;
157- if ( policySubkey is not null )
148+ catch ( Exception ex )
158149 {
159- bool noRecentDocsHistoryValue = Convert . ToBoolean ( policySubkey . GetValue ( "NoRecentDocsHistory" , false ) ) ;
160- if ( noRecentDocsHistoryValue )
161- return false ;
150+ App . Logger . LogWarning ( ex , ex . Message ) ;
151+ return false ;
162152 }
163-
164- return true ;
165153 }
166154
167155 private unsafe bool UpdateRecentItems ( bool isFolder )
@@ -172,8 +160,8 @@ private unsafe bool UpdateRecentItems(bool isFolder)
172160
173161 string szFolderShellPath =
174162 isFolder
175- ? "Shell:::{22877a6d-37a1-461a-91b0-dbda5aaebc99 }" // Recent Places folder
176- : "Shell:::{679f85cb -0220-4080-b29b-5540cc05aab6 }" ; // Quick Access folder
163+ ? "Shell:::{22877A6D-37A1-461A-91B0-DBDA5AAEBC99 }" // Recent Places folder (recent folders)
164+ : "Shell:::{679F85CB -0220-4080-B29B-5540CC05AAB6 }" ; // Quick Access folder (recent files)
177165
178166 // Get IShellItem of the shell folder
179167 var shellItemIid = typeof ( IShellItem ) . GUID ;
@@ -200,12 +188,12 @@ private unsafe bool UpdateRecentItems(bool isFolder)
200188 // Get the target path
201189 pShellItem . Get ( ) ->GetDisplayName ( SIGDN . SIGDN_DESKTOPABSOLUTEEDITING , out var szDisplayName ) ;
202190 var targetPath = szDisplayName . ToString ( ) ;
203- PInvoke . CoTaskMemFree ( ( void * ) szDisplayName . Value ) ;
191+ PInvoke . CoTaskMemFree ( szDisplayName . Value ) ;
204192
205193 // Get the display name
206194 pShellItem . Get ( ) ->GetDisplayName ( SIGDN . SIGDN_NORMALDISPLAY , out szDisplayName ) ;
207195 var fileName = szDisplayName . ToString ( ) ;
208- PInvoke . CoTaskMemFree ( ( void * ) szDisplayName . Value ) ;
196+ PInvoke . CoTaskMemFree ( szDisplayName . Value ) ;
209197
210198 // Strip the file extension except when the file name only contains extension (e.g. ".gitignore")
211199 if ( ! FoldersSettingsService . ShowFileExtensions )
@@ -214,12 +202,13 @@ private unsafe bool UpdateRecentItems(bool isFolder)
214202 fileName = string . IsNullOrEmpty ( strippedExtension ) ? SystemIO . Path . GetFileName ( fileName ) : strippedExtension ;
215203 }
216204
217- // TODO: Get PIDL to prepare for removal of the item via "delete" verb for now
205+ PInvoke . SHGetIDListFromObject ( ( IUnknown * ) pShellItem . Get ( ) , out var pidl ) ;
218206
219207 recentItems . Add ( new ( )
220208 {
221209 Path = targetPath ,
222210 Name = fileName ,
211+ PIDL = pidl ,
223212 } ) ;
224213
225214 index ++ ;
@@ -238,15 +227,15 @@ private unsafe bool UpdateRecentItems(bool isFolder)
238227 {
239228 _RecentFolders . Clear ( ) ;
240229 _RecentFolders . AddRange ( recentItems ) ;
241- }
230+ }
242231 }
243232 else
244233 {
245234 lock ( _RecentFiles )
246235 {
247236 _RecentFiles . Clear ( ) ;
248237 _RecentFiles . AddRange ( recentItems ) ;
249- }
238+ }
250239 }
251240
252241 var eventArgs = GetChangedActionEventArgs ( snapshot , recentItems ) ;
0 commit comments