-
-
Notifications
You must be signed in to change notification settings - Fork 60
/
ContextMenuLoader.cs
278 lines (260 loc) · 13.3 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
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Security.AccessControl;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using Community.PowerToys.Run.Plugin.Everything.Properties;
using Wox.Infrastructure;
using Wox.Plugin;
using Wox.Plugin.Logger;
namespace Community.PowerToys.Run.Plugin.Everything
{
internal sealed class ContextMenuLoader(PluginInitContext context, string options) : IContextMenu
{
private readonly PluginInitContext _context = context;
// 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 = "Segoe MDL2 Assets",
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);
return false;
}
return true;
},
});
}
break;
case '1':
// Run as Adsmin
if (runAs)
{
contextMenus.Add(new ContextMenuResult
{
PluginName = Assembly.GetExecutingAssembly().GetName().Name,
Title = Resources.run_as_admin,
Glyph = "\xE7EF",
FontFamily = "Segoe MDL2 Assets",
AcceleratorKey = Key.Enter,
AcceleratorModifiers = ModifierKeys.Control | ModifierKeys.Shift,
Action = _ =>
{
try
{
Task.Run(() => Helper.RunAsAdmin(record.Path));
return true;
}
catch (Exception e)
{
Log.Exception($"Failed to run {record.Path} as admin, {e.Message}", e, MethodBase.GetCurrentMethod().DeclaringType);
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 = "Segoe MDL2 Assets",
AcceleratorKey = Key.U,
AcceleratorModifiers = ModifierKeys.Control | ModifierKeys.Shift,
Action = _ =>
{
try
{
Task.Run(() => Helper.RunAsUser(record.Path));
return true;
}
catch (Exception e)
{
Log.Exception($"Failed to run {record.Path} as different user, {e.Message}", e, MethodBase.GetCurrentMethod().DeclaringType);
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 = "Segoe MDL2 Assets",
AcceleratorKey = Key.C,
AcceleratorModifiers = _swapCopy ? ModifierKeys.Control : ModifierKeys.Control | ModifierKeys.Alt,
Action = (context) =>
{
try
{
Clipboard.SetData(DataFormats.FileDrop, new string[] { record.Path });
return true;
}
catch (Exception e)
{
var message = Resources.clipboard_failed;
Log.Exception(message, 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 = "Segoe MDL2 Assets",
AcceleratorKey = Key.C,
AcceleratorModifiers = _swapCopy ? ModifierKeys.Control | ModifierKeys.Alt : ModifierKeys.Control,
Action = (context) =>
{
try
{
Clipboard.SetDataObject(record.Path);
return true;
}
catch (Exception e)
{
var message = Resources.clipboard_failed;
Log.Exception(message, 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 = "Segoe MDL2 Assets",
AcceleratorKey = Key.C,
AcceleratorModifiers = ModifierKeys.Control | ModifierKeys.Shift,
Action = (context) =>
{
try
{
if (isFile)
{
Helper.OpenInConsole(Path.GetDirectoryName(record.Path));
}
else
{
Helper.OpenInConsole(record.Path);
}
return true;
}
catch (Exception e)
{
Log.Exception($"Failed to open {record.Path} in console, {e.Message}", 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 = "Segoe MDL2 Assets",
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();
return true;
}
catch (Exception e)
{
Log.Exception($"Failed to execute {_customProgram} with arguments {_customArg}", 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;
}
}
}