33// SPDX-License-Identifier: MIT
44
55use crate :: error:: Result ;
6+ #[ allow( unused_imports) ]
7+ use std:: fs;
68use tauri:: command;
79
10+ #[ cfg( target_os = "windows" ) ]
11+ use windows:: {
12+ core:: { Interface , HSTRING , PWSTR } ,
13+ Win32 :: {
14+ Foundation :: { GetLastError , MAX_PATH } ,
15+ System :: Com :: {
16+ CoCreateInstance , CoTaskMemFree , IPersistFile , CLSCTX_INPROC_SERVER , STGM_READ ,
17+ } ,
18+ UI :: Shell :: { FOLDERID_Recent , SHGetKnownFolderPath , KNOWN_FOLDER_FLAG } ,
19+ UI :: Shell :: { IShellLinkW , SHAddToRecentDocs , ShellLink , SHARD_APPIDINFO , SHARD_PATHW } ,
20+ } ,
21+ } ;
22+
23+ #[ cfg( target_os = "macos" ) ]
24+ use {
25+ objc2:: MainThreadMarker ,
26+ objc2_app_kit:: NSDocumentController ,
27+ objc2_foundation:: { NSString , NSURL } ,
28+ } ;
29+
830#[ command]
931/// add recent
1032pub ( crate ) fn add_recent_document ( _path : & str ) -> Result < ( ) > {
1133 #[ cfg( target_os = "windows" ) ]
12- {
13- use windows:: {
14- core:: HSTRING ,
15- Win32 :: UI :: Shell :: { SHAddToRecentDocs , SHARD_PATHW } ,
16- } ;
17- unsafe {
18- // Convert path to HSTRING
19- let path_hstring = HSTRING :: from ( _path) ;
20- SHAddToRecentDocs (
21- SHARD_PATHW . 0 as u32 ,
22- Some ( path_hstring. as_ptr ( ) as * const core:: ffi:: c_void ) ,
23- ) ;
24- }
34+ unsafe {
35+ // Convert path to HSTRING
36+ let path_hstring = HSTRING :: from ( _path) ;
37+ SHAddToRecentDocs (
38+ SHARD_PATHW . 0 as u32 ,
39+ Some ( path_hstring. as_ptr ( ) as * const core:: ffi:: c_void ) ,
40+ ) ;
2541 }
2642
2743 #[ cfg( target_os = "macos" ) ]
2844 {
29- use objc2:: MainThreadMarker ;
30- use objc2_app_kit:: NSDocumentController ;
31- use objc2_foundation:: { NSString , NSURL } ;
32-
3345 let ns_path = NSURL :: fileURLWithPath ( & NSString :: from_str ( _path) ) ;
3446 let mtm = MainThreadMarker :: new ( ) . expect ( "AppKit API must be called on the main thread" ) ;
3547 let controller = NSDocumentController :: sharedDocumentController ( mtm) ;
@@ -50,24 +62,15 @@ pub(crate) fn add_recent_document(_path: &str) -> Result<()> {
5062#[ command]
5163pub ( crate ) fn clear_recent_documents ( ) -> Result < ( ) > {
5264 #[ cfg( target_os = "windows" ) ]
53- {
54- use windows:: Win32 :: UI :: Shell :: { SHAddToRecentDocs , SHARD_APPIDINFO } ;
55- unsafe {
56- SHAddToRecentDocs ( SHARD_APPIDINFO . 0 as u32 , None ) ;
57- }
65+ unsafe {
66+ SHAddToRecentDocs ( SHARD_APPIDINFO . 0 as u32 , None ) ;
5867 }
5968
6069 #[ cfg( target_os = "macos" ) ]
61- {
62- use objc2:: MainThreadMarker ;
63- use objc2_app_kit:: NSDocumentController ;
64-
65- unsafe {
66- let mtm =
67- MainThreadMarker :: new ( ) . expect ( "AppKit API must be called on the main thread" ) ;
68- let controller = NSDocumentController :: sharedDocumentController ( mtm) ;
69- controller. clearRecentDocuments ( None ) ;
70- }
70+ unsafe {
71+ let mtm = MainThreadMarker :: new ( ) . expect ( "AppKit API must be called on the main thread" ) ;
72+ let controller = NSDocumentController :: sharedDocumentController ( mtm) ;
73+ controller. clearRecentDocuments ( None ) ;
7174 }
7275
7376 #[ cfg( unix) ]
@@ -87,41 +90,30 @@ pub(crate) fn get_recent_documents() -> Result<Vec<String>> {
8790 let mut recent_docs = Vec :: new ( ) ;
8891
8992 #[ cfg( target_os = "windows" ) ]
90- {
91- use std:: fs;
92- use windows:: Win32 :: {
93- Foundation :: GetLastError ,
94- System :: Com :: CoTaskMemFree ,
95- UI :: Shell :: { FOLDERID_Recent , SHGetKnownFolderPath , KNOWN_FOLDER_FLAG } ,
96- } ;
97- unsafe {
98- let recent_path = SHGetKnownFolderPath ( & FOLDERID_Recent , KNOWN_FOLDER_FLAG ( 0 ) , None ) ?;
99- if recent_path. is_null ( ) {
100- return Err ( crate :: error:: Error :: WindowsError ( GetLastError ( ) . into ( ) ) ) ;
101- }
102- let recent_path_os_string = recent_path. to_hstring ( ) . to_os_string ( ) ;
93+ unsafe {
94+ let recent_path = SHGetKnownFolderPath ( & FOLDERID_Recent , KNOWN_FOLDER_FLAG ( 0 ) , None ) ?;
95+ if recent_path. is_null ( ) {
96+ return Err ( crate :: error:: Error :: WindowsError ( GetLastError ( ) . into ( ) ) ) ;
97+ }
98+ let recent_path_os_string = recent_path. to_hstring ( ) . to_os_string ( ) ;
10399
104- if let Ok ( entries) = fs:: read_dir ( recent_path_os_string) {
105- for entry in entries. flatten ( ) {
106- let path = entry. path ( ) ;
100+ if let Ok ( entries) = fs:: read_dir ( recent_path_os_string) {
101+ for entry in entries. flatten ( ) {
102+ let path = entry. path ( ) ;
107103
108- if path. extension ( ) . and_then ( |s : & std:: ffi:: OsStr | s. to_str ( ) ) == Some ( "lnk" ) {
109- if let Ok ( resolved_path) = resolve_shortcut ( & path) {
110- recent_docs. push ( resolved_path) ;
111- }
104+ if path. extension ( ) . and_then ( |s : & std:: ffi:: OsStr | s. to_str ( ) ) == Some ( "lnk" ) {
105+ if let Ok ( resolved_path) = resolve_shortcut ( & path) {
106+ recent_docs. push ( resolved_path) ;
112107 }
113108 }
114109 }
115-
116- CoTaskMemFree ( Some ( recent_path. 0 as * mut _ ) ) ;
117110 }
111+
112+ CoTaskMemFree ( Some ( recent_path. 0 as * mut _ ) ) ;
118113 }
119114
120115 #[ cfg( target_os = "macos" ) ]
121116 {
122- use objc2:: MainThreadMarker ;
123- use objc2_app_kit:: NSDocumentController ;
124-
125117 let mtm = MainThreadMarker :: new ( ) . expect ( "AppKit API must be called on the main thread" ) ;
126118 let controller = NSDocumentController :: sharedDocumentController ( mtm) ;
127119 let urls = controller. recentDocumentURLs ( ) ;
@@ -146,13 +138,6 @@ pub(crate) fn get_recent_documents() -> Result<Vec<String>> {
146138
147139#[ cfg( target_os = "windows" ) ]
148140fn resolve_shortcut ( lnk_path : & std:: path:: Path ) -> Result < String > {
149- use windows:: {
150- core:: { Interface , HSTRING , PWSTR } ,
151- Win32 :: Foundation :: MAX_PATH ,
152- Win32 :: System :: Com :: { CoCreateInstance , IPersistFile , CLSCTX_INPROC_SERVER , STGM_READ } ,
153- Win32 :: UI :: Shell :: { IShellLinkW , ShellLink } ,
154- } ;
155-
156141 unsafe {
157142 // Create IShellLink instance
158143 let shell_link: IShellLinkW =
0 commit comments