Skip to content

Commit a2be6e0

Browse files
committed
Use new api for shell plugin
1 parent 4ebfb3c commit a2be6e0

File tree

2 files changed

+2
-159
lines changed

2 files changed

+2
-159
lines changed

Plugins/Flow.Launcher.Plugin.Shell/Languages/en.xaml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,4 @@
1717
<system:String x:Key="flowlauncher_plugin_cmd_run_as_administrator">Run As Administrator</system:String>
1818
<system:String x:Key="flowlauncher_plugin_cmd_copy">Copy the command</system:String>
1919
<system:String x:Key="flowlauncher_plugin_cmd_history">Only show number of most used commands:</system:String>
20-
<system:String x:Key="flowlauncher_plugin_cmd_cmd">Windows Command Processor</system:String>
21-
<system:String x:Key="flowlauncher_plugin_cmd_powershell">Windows PowerShell</system:String>
22-
<system:String x:Key="flowlauncher_plugin_cmd_pwsh">Microsoft PowerShell 7</system:String>
23-
<system:String x:Key="flowlauncher_plugin_cmd_wt">Terminal</system:String>
2420
</ResourceDictionary>

Plugins/Flow.Launcher.Plugin.Shell/Main.cs

Lines changed: 2 additions & 155 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44
using System.Diagnostics;
55
using System.IO;
66
using System.Linq;
7-
using System.Security.Principal;
87
using System.Threading.Tasks;
9-
using System.Windows;
108
using Flow.Launcher.Plugin.SharedCommands;
119
using WindowsInput;
1210
using WindowsInput.Native;
@@ -25,32 +23,6 @@ public class Main : IPlugin, ISettingProvider, IPluginI18n, IContextMenu, IDispo
2523
private bool _winRStroked;
2624
private readonly KeyboardSimulator _keyboardSimulator = new(new InputSimulator());
2725

28-
private static readonly string[] possibleCmdPaths = new[]
29-
{
30-
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "cmd.exe"),
31-
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows), @"System32\cmd.exe")
32-
};
33-
34-
private static readonly string[] possiblePowershellPaths = new[]
35-
{
36-
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), @"WindowsPowerShell\v1.0\powershell.exe"),
37-
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows), @"System32\WindowsPowerShell\v1.0\powershell.exe")
38-
};
39-
40-
private static readonly string[] possiblePwshPaths = new[]
41-
{
42-
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), @"PowerShell\7\pwsh.exe"),
43-
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), @"Microsoft\WindowsApps\pwsh.exe"),
44-
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), @"scoop\apps\pwsh\current\pwsh.exe") // if using Scoop
45-
};
46-
47-
private static readonly List<string> possibleWindowsTerminalPaths = new()
48-
{
49-
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), @"Microsoft\WindowsApps\wt.exe"),
50-
};
51-
52-
private static readonly bool IsAdmin = IsAdministrator();
53-
5426
private Settings _settings;
5527

5628
public List<Result> Query(Query query)
@@ -337,111 +309,8 @@ private ProcessStartInfo PrepareProcessStartInfo(string command, bool runAsAdmin
337309

338310
private static Process StartProcess(ProcessStartInfo info)
339311
{
340-
switch (info.FileName)
341-
{
342-
case "cmd.exe":
343-
// Check if cmd.exe exists in the system PATH or the specified directories
344-
var cmdPath = possibleCmdPaths.FirstOrDefault(File.Exists);
345-
if (string.IsNullOrEmpty(cmdPath))
346-
{
347-
Context.API.LogError(ClassName, $"Cannot find cmd.exe");
348-
// Fall back to using the Process.Start method directly
349-
// Show user account control dialog if Flow is running as administrator
350-
if (IsAdmin)
351-
{
352-
var uacDialog = Context.API.ShowUACDialog(Context.API.GetTranslation("flowlauncher_plugin_cmd_cmd"), cmdPath, cmdPath);
353-
if (uacDialog != MessageBoxResult.Yes)
354-
{
355-
return null;
356-
}
357-
}
358-
359-
return Process.Start(info);
360-
}
361-
362-
// If Flow is running as administrator and the command is run as administrator, we need to use UAC dialog
363-
if (IsAdmin && info.Verb == "runas")
364-
{
365-
var uacDialog = Context.API.ShowUACDialog(Context.API.GetTranslation("flowlauncher_plugin_cmd_cmd"), cmdPath, cmdPath);
366-
if (uacDialog != MessageBoxResult.Yes)
367-
{
368-
return null;
369-
}
370-
}
371-
372-
Context.API.StartProcess(cmdPath, info.WorkingDirectory, string.Join(" ", info.ArgumentList), info.Verb == "runas");
373-
return null;
374-
375-
case "powershell.exe":
376-
var powershellPath = possiblePowershellPaths.FirstOrDefault(File.Exists);
377-
if (string.IsNullOrEmpty(powershellPath))
378-
{
379-
Context.API.LogError(ClassName, $"Cannot find powershell.exe");
380-
// Fall back to using the Process.Start method directly
381-
// Show user account control dialog if Flow is running as administrator
382-
if (IsAdmin)
383-
{
384-
var uacDialog = Context.API.ShowUACDialog(Context.API.GetTranslation("flowlauncher_plugin_cmd_powershell"), powershellPath, powershellPath);
385-
if (uacDialog != MessageBoxResult.Yes)
386-
{
387-
return null;
388-
}
389-
}
390-
391-
return Process.Start(info);
392-
}
393-
394-
// If Flow is running as administrator and the command is run as administrator, we need to use UAC dialog
395-
if (IsAdmin && info.Verb == "runas")
396-
{
397-
var uacDialog = Context.API.ShowUACDialog(Context.API.GetTranslation("flowlauncher_plugin_cmd_powershell"), powershellPath, powershellPath);
398-
if (uacDialog != MessageBoxResult.Yes)
399-
{
400-
return null;
401-
}
402-
}
403-
404-
Context.API.StartProcess(powershellPath, info.WorkingDirectory, string.Join(" ", info.ArgumentList), info.Verb == "runas");
405-
return null;
406-
407-
// Due to arguments pass issues, powershell 7 fails to work with deelevate model
408-
// So we use shell to execuete them which means if Flow is running as administrator,
409-
// all commands must be running as administrator
410-
case "pwsh.exe":
411-
// Fall back to using the Process.Start method directly
412-
// Show user account control dialog if Flow is running as administrator
413-
if (IsAdmin)
414-
{
415-
var pwshPath = possiblePwshPaths.FirstOrDefault(File.Exists);
416-
var uacDialog = Context.API.ShowUACDialog(Context.API.GetTranslation("flowlauncher_plugin_cmd_pwsh"), pwshPath, pwshPath ?? "pwsh.exe");
417-
if (uacDialog != MessageBoxResult.Yes)
418-
{
419-
return null;
420-
}
421-
}
422-
423-
return Process.Start(info);
424-
425-
// Due to arguments pass issues, windows terminal fails to work with deelevate model
426-
// So we use shell to execuete them which means if Flow is running as administrator,
427-
// all commands must be running as administrator
428-
case "wt.exe":
429-
// Fall back to using the Process.Start method directly
430-
// Show user account control dialog if Flow is running as administrator
431-
if (IsAdmin)
432-
{
433-
var wtPath = possibleWindowsTerminalPaths.FirstOrDefault(File.Exists);
434-
var uacDialog = Context.API.ShowUACDialog(Context.API.GetTranslation("flowlauncher_plugin_cmd_wt"), wtPath, wtPath ?? "wt.exe");
435-
if (uacDialog != MessageBoxResult.Yes)
436-
{
437-
return null;
438-
}
439-
}
440-
441-
return Process.Start(info);
442-
}
443-
444-
return Process.Start(info);
312+
Context.API.StartProcess(info.FileName, info.WorkingDirectory, info.ArgumentList, info.UseShellExecute, info.Verb);
313+
return null;
445314
}
446315

447316
private static void Execute(Func<ProcessStartInfo, Process> startProcess, ProcessStartInfo info)
@@ -498,21 +367,6 @@ public void Init(PluginInitContext context)
498367
Context = context;
499368
_settings = context.API.LoadSettingJsonStorage<Settings>();
500369
context.API.RegisterGlobalKeyboardCallback(API_GlobalKeyboardEvent);
501-
502-
// Search for all folders with Microsoft.WindowsTerminal_ prefix
503-
/*var windowsAppsPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), "WindowsApps");
504-
var windowsTerminalFolders = Directory.EnumerateDirectories(windowsAppsPath, "Microsoft.WindowsTerminal_*",
505-
SearchOption.TopDirectoryOnly);
506-
507-
// Find wt.exe among them
508-
foreach (var windowsTerminalFolder in windowsTerminalFolders)
509-
{
510-
var windowsTerminalPath = Path.Combine(windowsTerminalFolder, "wt.exe");
511-
if (File.Exists(windowsTerminalPath))
512-
{
513-
possibleWindowsTerminalPaths.Insert(0, windowsTerminalPath);
514-
}
515-
}*/
516370
}
517371

518372
bool API_GlobalKeyboardEvent(int keyevent, int vkcode, SpecialKeyState state)
@@ -612,12 +466,5 @@ public void Dispose()
612466
{
613467
Context.API.RemoveGlobalKeyboardCallback(API_GlobalKeyboardEvent);
614468
}
615-
616-
private static bool IsAdministrator()
617-
{
618-
using var identity = WindowsIdentity.GetCurrent();
619-
var principal = new WindowsPrincipal(identity);
620-
return principal.IsInRole(WindowsBuiltInRole.Administrator);
621-
}
622469
}
623470
}

0 commit comments

Comments
 (0)