This repository has been archived by the owner on Jul 18, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathMain.cs
387 lines (342 loc) · 16.4 KB
/
Main.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
using Droplex;
using Flow.Launcher.Plugin.Everything.Everything;
using Flow.Launcher.Plugin.Everything.Helper;
using Flow.Launcher.Plugin.Everything.ViewModels;
using Flow.Launcher.Plugin.SharedCommands;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
namespace Flow.Launcher.Plugin.Everything
{
public class Main : IPlugin, ISettingProvider, IPluginI18n, IContextMenu
{
public const string DLL = "Everything.dll";
private readonly IEverythingApi _api = new EverythingApi();
internal static PluginInitContext _context;
private Settings _settings;
private CancellationTokenSource _cancellationTokenSource;
public List<Result> Query(Query query)
{
_cancellationTokenSource?.Cancel(); // cancel if already exist
var cts = _cancellationTokenSource = new CancellationTokenSource();
var results = new List<Result>();
if (!string.IsNullOrEmpty(query.Search))
{
var keyword = query.Search;
try
{
var searchList = _api.Search(keyword, cts.Token, _settings.SortOption, maxCount: _settings.MaxSearchCount);
if (searchList == null)
{
return results;
}
foreach (var searchResult in searchList)
{
var r = CreateResult(keyword, searchResult);
results.Add(r);
}
}
catch (IPCErrorException)
{
results.Add(new Result
{
Title = _context.API.GetTranslation("flowlauncher_plugin_everything_is_not_running"),
SubTitle = _context.API.GetTranslation("flowlauncher_plugin_everything_run_service"),
IcoPath = "Images\\warning.png",
Action = _ =>
{
if (_settings.EverythingInstalledPath.FileExists())
_context.API.OpenDirectory(_settings.EverythingInstalledPath);
return true;
}
});
}
catch (Exception e)
{
_context.API.LogException("EverythingPlugin", "Query Error", e);
results.Add(new Result
{
Title = _context.API.GetTranslation("flowlauncher_plugin_everything_query_error"),
SubTitle = e.Message,
Action = _ =>
{
Clipboard.SetDataObject(e.Message + "\r\n" + e.StackTrace);
_context.API.ShowMsg(_context.API.GetTranslation("flowlauncher_plugin_everything_copied"), null, string.Empty);
return false;
},
IcoPath = "Images\\error.png"
});
}
}
return results;
}
private Result CreateResult(string keyword, SearchResult searchResult)
{
var path = searchResult.FullPath;
string workingDir = null;
if (_settings.UseLocationAsWorkingDir)
workingDir = Path.GetDirectoryName(path);
var r = new Result
{
Title = Path.GetFileName(path),
SubTitle = path,
IcoPath = path,
TitleHighlightData = _context.API.FuzzySearch(keyword, Path.GetFileName(path)).MatchData,
Action = c =>
{
bool hide;
try
{
switch (searchResult.Type)
{
case ResultType.Folder:
if (!_settings.LaunchHidden)
{
_context.API.OpenDirectory(path);
}
else
{
// Launch hidden is no longer supported due to API change.
// This the default behaviour kept
ProcessStartInfo startInfo = new ProcessStartInfo();
//Hide the process
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.CreateNoWindow = true;
//Set file and args
startInfo.FileName = "explorer";
startInfo.Arguments = $"\"{path}\"";
//Start the process
Process proc = Process.Start(startInfo);
}
break;
case ResultType.Volume:
case ResultType.File:
Process.Start(new ProcessStartInfo
{
FileName = path,
UseShellExecute = true,
WorkingDirectory = workingDir
});
break;
default:
break;
}
hide = true;
}
catch (Win32Exception)
{
var name = $"Plugin: {_context.CurrentPluginMetadata.Name}";
var message = "Can't open this file";
_context.API.ShowMsg(name, message, string.Empty);
hide = false;
}
return hide;
},
ContextData = searchResult,
SubTitleHighlightData = _context.API.FuzzySearch(keyword, path).MatchData
};
return r;
}
public void Init(PluginInitContext context)
{
_context = context;
_settings = context.API.LoadSettingJsonStorage<Settings>();
SortOptionTranlationHelper.API = context.API;
if (_settings.MaxSearchCount <= 0)
_settings.MaxSearchCount = Settings.DefaultMaxSearchCount;
if (!_settings.EverythingInstalledPath.FileExists())
{
var installedLocation = Utilities.GetInstalledPath();
if (string.IsNullOrEmpty(installedLocation) &&
System.Windows.Forms.MessageBox.Show(
string.Format(context.API.GetTranslation("flowlauncher_plugin_everything_installing_select"), Environment.NewLine),
context.API.GetTranslation("flowlauncher_plugin_everything_installing_title"),
System.Windows.Forms.MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
{
// Solves single thread apartment (STA) mode requirement error when using OpenFileDialog
Thread t = new Thread(() =>
{
var dlg = new System.Windows.Forms.OpenFileDialog
{
InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles)
};
var result = dlg.ShowDialog();
if (result == System.Windows.Forms.DialogResult.OK && !string.IsNullOrEmpty(dlg.FileName))
installedLocation = dlg.FileName;
});
// Run your code from a thread that joins the STA Thread
t.SetApartmentState(ApartmentState.STA);
t.Start();
t.Join();
}
if (string.IsNullOrEmpty(installedLocation))
{
Task.Run(async delegate
{
context.API.ShowMsg(context.API.GetTranslation("flowlauncher_plugin_everything_installing_title"),
context.API.GetTranslation("flowlauncher_plugin_everything_installing_subtitle"), "", useMainWindowAsOwner: false);
await DroplexPackage.Drop(App.Everything1_4_1_1009).ConfigureAwait(false);
context.API.ShowMsg(context.API.GetTranslation("flowlauncher_plugin_everything_installing_title"),
context.API.GetTranslation("flowlauncher_plugin_everything_installationsuccess_subtitle"), "", useMainWindowAsOwner: false);
_settings.EverythingInstalledPath = "C:\\Program Files\\Everything\\Everything.exe";
FilesFolders.OpenPath(_settings.EverythingInstalledPath);
}).ContinueWith(t =>
{
_context.API.LogException("Everything.Main", $"Failed to install Everything service", t.Exception.InnerException, "DroplexPackage.Drop");
MessageBox.Show(context.API.GetTranslation("flowlauncher_plugin_everything_installationfailed_subtitle"),
context.API.GetTranslation("flowlauncher_plugin_everything_installing_title"));
}, TaskContinuationOptions.OnlyOnFaulted);
}
else
{
_settings.EverythingInstalledPath = installedLocation;
}
}
var pluginDirectory = context.CurrentPluginMetadata.PluginDirectory;
const string sdk = "EverythingSDK";
var bundledSdkDirectory = Path.Combine(pluginDirectory, sdk, CpuType());
var sdkPath = Path.Combine(bundledSdkDirectory, DLL);
_api.Load(sdkPath);
}
private static string CpuType()
{
return Environment.Is64BitOperatingSystem ? "x64" : "x86";
}
public string GetTranslatedPluginTitle()
{
return _context.API.GetTranslation("flowlauncher_plugin_everything_plugin_name");
}
public string GetTranslatedPluginDescription()
{
return _context.API.GetTranslation("flowlauncher_plugin_everything_plugin_description");
}
public List<Result> LoadContextMenus(Result selectedResult)
{
if (selectedResult.ContextData is not SearchResult record)
return new List<Result>();
var icoPath = record.Type == ResultType.File ? "Images\\file.png" : "Images\\folder.png";
List<Result> contextMenus = new List<Result>
{
new()
{
Title = _context.API.GetTranslation("flowlauncher_plugin_everything_open_containing_folder"),
Action = _ =>
{
_context.API.OpenDirectory(Path.GetDirectoryName(record.FullPath), record.FullPath);
return true;
},
IcoPath = icoPath,
Glyph = new GlyphInfo("/Resources/#Segoe Fluent Icons", "\ue8de")
}
};
if (record.Type is ResultType.File)
{
var notepadPath = string.IsNullOrEmpty(_settings.EditorPath) ? "notepad.exe" : _settings.EditorPath;
contextMenus.Add(new Result
{
Title = string.Format(
_context.API.GetTranslation("flowlauncher_plugin_everything_open_with_editor"),
Path.GetFileNameWithoutExtension(notepadPath)
),
Action = state =>
{
using var notepad = new Process();
notepad.StartInfo = new ProcessStartInfo()
{
FileName = notepadPath,
Arguments = record.FullPath
};
if (state.SpecialKeyState.CtrlPressed && state.SpecialKeyState.ShiftPressed)
{
notepad.StartInfo.Verb = "runAs";
}
Task.Run(() => notepad.Start());
return true;
},
Glyph = new GlyphInfo("/Resources/#Segoe Fluent Icons", "\ue932")
});
}
if (_settings.ShowWindowsContextMenu)
{
contextMenus.Add(new Result
{
Title = _context.API.GetTranslation("flowlauncher_plugin_everything_show_windows_context_menu"),
Action = (context) =>
{
var fileInfos = new FileInfo[] { new(record.FullPath) };
var screenWithMouseCursor = System.Windows.Forms.Screen.FromPoint(System.Windows.Forms.Cursor.Position);
var xOfScreenCenter = screenWithMouseCursor.WorkingArea.Left + screenWithMouseCursor.WorkingArea.Width / 2;
var yOfScreenCenter = screenWithMouseCursor.WorkingArea.Top + screenWithMouseCursor.WorkingArea.Height / 2;
var showPosition = new System.Drawing.Point(xOfScreenCenter, yOfScreenCenter);
new Peter.ShellContextMenu().ShowContextMenu(fileInfos, showPosition);
return true;
},
IcoPath = icoPath
});
}
contextMenus.Add(
new()
{
Title = _context.API.GetTranslation("flowlauncher_plugin_everything_copy_path"),
Action = _ =>
{
Clipboard.SetDataObject(record.FullPath);
return true;
},
IcoPath = icoPath,
Glyph = new GlyphInfo("/Resources/#Segoe Fluent Icons", "\ue8c8")
});
contextMenus.Add(
new Result
{
Title = _context.API.GetTranslation("flowlauncher_plugin_everything_copy"),
Action = _ =>
{
Clipboard.SetFileDropList(new System.Collections.Specialized.StringCollection
{
record.FullPath
});
return true;
},
IcoPath = icoPath,
Glyph = new GlyphInfo("/Resources/#Segoe Fluent Icons", "\ue8c8")
});
if (record.Type is ResultType.File or ResultType.Folder)
contextMenus.Add(new Result
{
Title = _context.API.GetTranslation("flowlauncher_plugin_everything_delete"),
Action = _ =>
{
try
{
if (record.Type == ResultType.File)
File.Delete(record.FullPath);
else
Directory.Delete(record.FullPath);
}
catch
{
_context.API.ShowMsg(string.Format(_context.API.GetTranslation("flowlauncher_plugin_everything_canot_delete"), record.FullPath), string.Empty, string.Empty);
return false;
}
return true;
},
IcoPath = icoPath,
Glyph = new("/Resources/#Segoe Fluent Icons", "\ue74d")
});
return contextMenus;
}
public Control CreateSettingPanel()
{
return new EverythingSettings(_settings, new SettingsViewModel(_api, _settings, _context));
}
}
}