1+ using System ;
12using System . Collections . Generic ;
23using System . Diagnostics ;
34using System . IO ;
45using System . Windows ;
6+ using Wox . Infrastructure . Logger ;
57
68namespace Wox . Plugin . Folder
79{
@@ -19,71 +21,77 @@ public List<Result> LoadContextMenus(Result selectedResult)
1921 var contextMenus = new List < Result > ( ) ;
2022 if ( selectedResult . ContextData is SearchResult record )
2123 {
22- string editorPath = "notepad.exe" ; // TODO add the ability to create a custom editor
24+ if ( record . Type == ResultType . File )
25+ {
26+ contextMenus . Add ( CreateOpenWithEditorResult ( record ) ) ;
27+ contextMenus . Add ( CreateOpenContainingFolderResult ( record ) ) ;
28+ }
2329
24- var name = "Open With Editor: " + Path . GetFileNameWithoutExtension ( editorPath ) ;
30+ var icoPath = ( record . Type == ResultType . File ) ? Main . FileImagePath : Main . FolderImagePath ;
31+ var fileOrFolder = ( record . Type == ResultType . File ) ? "file" : "folder" ;
2532 contextMenus . Add ( new Result
2633 {
27- Title = name ,
28- Action = _ =>
34+ Title = "Copy Path" ,
35+ SubTitle = $ "Copy the path of { fileOrFolder } into the clipboard",
36+ Action = ( context ) =>
2937 {
3038 try
3139 {
32- Process . Start ( editorPath , record . FullPath ) ;
40+ Clipboard . SetText ( record . FullPath ) ;
41+ return true ;
3342 }
34- catch
43+ catch ( Exception e )
3544 {
36- // TODO: update this
37- _context . API . ShowMsg (
38- string . Format ( _context . API . GetTranslation ( "wox_plugin_everything_canot_start" ) ,
39- record . FullPath ) , string . Empty , string . Empty ) ;
45+ var message = "Fail to set text in clipboard" ;
46+ LogException ( message , e ) ;
47+ _context . API . ShowMsg ( message ) ;
4048 return false ;
4149 }
42-
43- return true ;
44- } ,
45- IcoPath = editorPath
46- } ) ;
47-
48- var icoPath = ( record . Type == ResultType . File ) ? "Images\\ file.png" : "Images\\ folder.png" ;
49- contextMenus . Add ( new Result
50- {
51- Title = _context . API . GetTranslation ( "wox_plugin_everything_copy_path" ) ,
52- Action = ( context ) =>
53- {
54- Clipboard . SetText ( record . FullPath ) ;
55- return true ;
5650 } ,
5751 IcoPath = icoPath
5852 } ) ;
5953
6054 contextMenus . Add ( new Result
6155 {
62- Title = _context . API . GetTranslation ( "wox_plugin_everything_copy" ) ,
56+ Title = "Copy" ,
57+ SubTitle = $ "Copy the { fileOrFolder } to the clipboard",
6358 Action = ( context ) =>
6459 {
65- Clipboard . SetFileDropList ( new System . Collections . Specialized . StringCollection { record . FullPath } ) ;
66- return true ;
60+ try
61+ {
62+ Clipboard . SetFileDropList ( new System . Collections . Specialized . StringCollection { record . FullPath } ) ;
63+ return true ;
64+ }
65+ catch ( Exception e )
66+ {
67+ var message = $ "Fail to set { fileOrFolder } in clipboard";
68+ LogException ( message , e ) ;
69+ _context . API . ShowMsg ( message ) ;
70+ return false ;
71+ }
72+
6773 } ,
6874 IcoPath = icoPath
6975 } ) ;
7076
7177 if ( record . Type == ResultType . File || record . Type == ResultType . Folder )
7278 contextMenus . Add ( new Result
7379 {
74- Title = _context . API . GetTranslation ( "wox_plugin_everything_delete" ) ,
80+ Title = "Delete" ,
7581 Action = ( context ) =>
7682 {
7783 try
7884 {
7985 if ( record . Type == ResultType . File )
80- System . IO . File . Delete ( record . FullPath ) ;
86+ File . Delete ( record . FullPath ) ;
8187 else
82- System . IO . Directory . Delete ( record . FullPath ) ;
88+ Directory . Delete ( record . FullPath ) ;
8389 }
84- catch
90+ catch ( Exception e )
8591 {
86- _context . API . ShowMsg ( string . Format ( _context . API . GetTranslation ( "wox_plugin_everything_canot_delete" ) , record . FullPath ) , string . Empty , string . Empty ) ;
92+ var message = $ "Fail to delete { fileOrFolder } at { record . FullPath } ";
93+ LogException ( message , e ) ;
94+ _context . API . ShowMsg ( message ) ;
8795 return false ;
8896 }
8997
@@ -96,6 +104,64 @@ public List<Result> LoadContextMenus(Result selectedResult)
96104
97105 return contextMenus ;
98106 }
107+
108+ private Result CreateOpenContainingFolderResult ( SearchResult record )
109+ {
110+ return new Result
111+ {
112+ Title = "Open containing folder" ,
113+ Action = _ =>
114+ {
115+ try
116+ {
117+ Process . Start ( "explorer.exe" , $ " /select,\" { record . FullPath } \" ") ;
118+ }
119+ catch ( Exception e )
120+ {
121+ var message = $ "Fail to open file at { record . FullPath } ";
122+ LogException ( message , e ) ;
123+ _context . API . ShowMsg ( message ) ;
124+ return false ;
125+ }
126+
127+ return true ;
128+ } ,
129+ IcoPath = Main . FolderImagePath
130+ } ;
131+ }
132+
133+
134+ private Result CreateOpenWithEditorResult ( SearchResult record )
135+ {
136+ string editorPath = "notepad.exe" ; // TODO add the ability to create a custom editor
137+
138+ var name = "Open With Editor: " + Path . GetFileNameWithoutExtension ( editorPath ) ;
139+ return new Result
140+ {
141+ Title = name ,
142+ Action = _ =>
143+ {
144+ try
145+ {
146+ Process . Start ( editorPath , record . FullPath ) ;
147+ return true ;
148+ }
149+ catch ( Exception e )
150+ {
151+ var message = $ "Fail to editor for file at { record . FullPath } ";
152+ LogException ( message , e ) ;
153+ _context . API . ShowMsg ( message ) ;
154+ return false ;
155+ }
156+ } ,
157+ IcoPath = editorPath
158+ } ;
159+ }
160+
161+ public void LogException ( string message , Exception e )
162+ {
163+ Log . Exception ( $ "|Wox.Plugin.Folder.ContextMenu|{ message } ", e ) ;
164+ }
99165 }
100166
101167 public class SearchResult
0 commit comments