-
-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathContextMenuLoader.cs
339 lines (321 loc) · 16.9 KB
/
ContextMenuLoader.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using Community.PowerToys.Run.Plugin.Everything.Interop;
using Community.PowerToys.Run.Plugin.Everything.Properties;
using Community.PowerToys.Run.Plugin.Everything.SearchHelper;
using Wox.Infrastructure;
using Wox.Plugin;
using Wox.Plugin.Logger;
using wf = System.Windows.Forms;
namespace Community.PowerToys.Run.Plugin.Everything.ContextMenu
{
internal sealed class ContextMenuLoader(PluginInitContext context, string options) : IContextMenu
{
private readonly PluginInitContext _context = context;
private readonly string font = "Segoe Fluent Icons,Segoe MDL2 Assets";
// Extensions for adding run as admin context menu item for applications
private readonly string[] _appExtensions = [".exe", ".bat", ".appref-ms", ".lnk"];
private bool _swapCopy;
private string _options = options;
private string _customProgram;
private string _customArg;
internal void Update(Settings s)
{
_swapCopy = s.Copy;
_options = s.Context;
_customProgram = s.CustomProgram;
_customArg = s.CustomArg;
}
public List<ContextMenuResult> LoadContextMenus(Result selectedResult)
{
var contextMenus = new List<ContextMenuResult>();
if (selectedResult.ContextData is SearchResult record)
{
bool isFile = record.File, runAs = CanFileBeRunAsAdmin(record.Path);
foreach (char o in _options)
{
switch (o)
{
case '0':
// Open folder
if (isFile)
{
contextMenus.Add(new ContextMenuResult
{
PluginName = Assembly.GetExecutingAssembly().GetName().Name,
Title = Resources.open_containing_folder,
Glyph = "\xE838",
FontFamily = font,
AcceleratorKey = Key.E,
AcceleratorModifiers = ModifierKeys.Control | ModifierKeys.Shift,
Action = _ =>
{
if (!Helper.OpenInShell("explorer.exe", $"/select,\"{record.Path}\""))
{
var message = $"{Resources.folder_open_failed} {Path.GetDirectoryName(record.Path)}";
_context.API.ShowMsg(message);
Log.Exception($"EPT: Failed to open folder {Path.GetDirectoryName(record.Path)}", new IOException(), GetType());
return false;
}
NativeMethods.Everything_IncRunCountFromFileNameW(record.Path);
return true;
},
});
}
break;
case '1':
// Run as Admin
if (runAs)
{
contextMenus.Add(new ContextMenuResult
{
PluginName = Assembly.GetExecutingAssembly().GetName().Name,
Title = Resources.run_as_admin,
Glyph = "\xE7EF",
FontFamily = font,
AcceleratorKey = Key.Enter,
AcceleratorModifiers = ModifierKeys.Control | ModifierKeys.Shift,
Action = _ =>
{
try
{
Task.Run(() => Helper.RunAsAdmin(record.Path));
NativeMethods.Everything_IncRunCountFromFileNameW(record.Path);
return true;
}
catch (Exception e)
{
Log.Exception($"EPT: Failed to run {record.Path} as admin", e, GetType());
return false;
}
},
});
}
break;
case '2':
// Run as User
if (runAs)
{
contextMenus.Add(new ContextMenuResult
{
PluginName = Assembly.GetExecutingAssembly().GetName().Name,
Title = Resources.run_as_user,
Glyph = "\xE7EE",
FontFamily = font,
AcceleratorKey = Key.U,
AcceleratorModifiers = ModifierKeys.Control | ModifierKeys.Shift,
Action = _ =>
{
try
{
Task.Run(() => Helper.RunAsUser(record.Path));
NativeMethods.Everything_IncRunCountFromFileNameW(record.Path);
return true;
}
catch (Exception e)
{
Log.Exception($"EPT: Failed to run {record.Path} as different user", e, GetType());
return false;
}
},
});
}
break;
case '3':
// Copy File/Folder
contextMenus.Add(new ContextMenuResult
{
PluginName = Assembly.GetExecutingAssembly().GetName().Name,
Title = Resources.copy_file + (_swapCopy ? Resources.copy_shortcut : Resources.copy_shortcutAlt),
Glyph = "\xE8C8",
FontFamily = font,
AcceleratorKey = Key.C,
AcceleratorModifiers = _swapCopy ? ModifierKeys.Control : ModifierKeys.Control | ModifierKeys.Alt,
Action = (context) =>
{
try
{
Clipboard.SetData(DataFormats.FileDrop, new string[] { record.Path });
NativeMethods.Everything_IncRunCountFromFileNameW(record.Path);
return true;
}
catch (Exception e)
{
var message = Resources.clipboard_failed;
Log.Exception($"ETP3: Failed to copy {(_swapCopy ? "file" : "path")} ({record.Path}) to clipboard", e, GetType());
_context.API.ShowMsg(message);
return false;
}
},
});
break;
case '4':
// Copy Path
contextMenus.Add(new ContextMenuResult
{
PluginName = Assembly.GetExecutingAssembly().GetName().Name,
Title = Resources.copy_path + (_swapCopy ? Resources.copy_shortcutAlt : Resources.copy_shortcut),
Glyph = "\xE71B",
FontFamily = font,
AcceleratorKey = Key.C,
AcceleratorModifiers = _swapCopy ? ModifierKeys.Control | ModifierKeys.Alt : ModifierKeys.Control,
Action = (context) =>
{
try
{
Clipboard.SetDataObject(record.Path);
NativeMethods.Everything_IncRunCountFromFileNameW(record.Path);
return true;
}
catch (Exception e)
{
var message = Resources.clipboard_failed;
Log.Exception($"ETP3: Failed to copy {(_swapCopy ? "file" : "path")} ({record.Path}) to clipboard", e, GetType());
_context.API.ShowMsg(message);
return false;
}
},
});
break;
case '5':
// Open in Shell
contextMenus.Add(new ContextMenuResult
{
PluginName = Assembly.GetExecutingAssembly().GetName().Name,
Title = Resources.open_in_console,
Glyph = "\xE756",
FontFamily = font,
AcceleratorKey = Key.C,
AcceleratorModifiers = ModifierKeys.Control | ModifierKeys.Shift,
Action = (context) =>
{
try
{
if (isFile)
Helper.OpenInConsole(Path.GetDirectoryName(record.Path));
else
Helper.OpenInConsole(record.Path);
NativeMethods.Everything_IncRunCountFromFileNameW(record.Path);
return true;
}
catch (Exception e)
{
Log.Exception($"EPT: Failed to open {record.Path} in console", e, GetType());
return false;
}
},
});
break;
case '6':
// Pass to custom program as parameter
contextMenus.Add(new ContextMenuResult
{
PluginName = Assembly.GetExecutingAssembly().GetName().Name,
Title = Resources.open_in_custom,
Glyph = "\xE8A7",
FontFamily = font,
AcceleratorKey = Key.N,
AcceleratorModifiers = ModifierKeys.Control,
Action = (context) =>
{
using var process = new Process();
process.StartInfo.FileName = _customProgram;
process.StartInfo.Arguments = $"\"{_customArg.Replace("$P", record.Path)}\"";
try
{
process.Start();
NativeMethods.Everything_IncRunCountFromFileNameW(record.Path);
return true;
}
catch (Exception e)
{
Log.Exception($"EPT: Failed to execute {_customProgram} with arguments {_customArg}", e, GetType());
return false;
}
},
});
break;
case '7':
// Delete
contextMenus.Add(new ContextMenuResult
{
PluginName = Assembly.GetExecutingAssembly().GetName().Name,
Title = Resources.delete_result,
Glyph = "\xE74D",
FontFamily = font,
AcceleratorKey = Key.Delete,
AcceleratorModifiers = ModifierKeys.Control,
Action = (context) =>
{
try
{
if (isFile)
File.Delete(record.Path);
else
Directory.Delete(record.Path, true);
return true;
}
catch (Exception e)
{
Log.Exception($"EPT: Failed to delete {record.Path}", e, GetType());
return false;
}
},
});
break;
case '8':
// Shell Context Menu
contextMenus.Add(new ContextMenuResult
{
PluginName = Assembly.GetExecutingAssembly().GetName().Name,
Title = Resources.scm,
Glyph = "\xE712",
FontFamily = font,
AcceleratorKey = Key.M,
AcceleratorModifiers = ModifierKeys.Control,
Action = (context) =>
{
try
{
ShellContextMenu scm = new();
if (isFile)
scm.ShowContextMenu(new FileInfo(record.Path), wf.Cursor.Position);
else
scm.ShowContextMenu(new DirectoryInfo(record.Path), wf.Cursor.Position);
NativeMethods.Everything_IncRunCountFromFileNameW(record.Path);
return true;
}
catch (Exception e)
{
Log.Exception($"EPT: Failed to open shell context menu for {record.Path}", e, GetType());
return false;
}
},
});
break;
default:
break;
}
}
}
return contextMenus;
}
private bool CanFileBeRunAsAdmin(string path)
{
string fileExtension = Path.GetExtension(path);
foreach (string extension in _appExtensions)
{
// Using OrdinalIgnoreCase since this is internal
if (extension.Equals(fileExtension, StringComparison.OrdinalIgnoreCase))
return true;
}
return false;
}
}
}