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

495 lines
19 KiB
C#
Raw Permalink Normal View History

using System;
2014-01-03 10:16:05 +00:00
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
2014-01-03 10:16:05 +00:00
using System.Linq;
using System.Threading.Tasks;
using Flow.Launcher.Plugin.SharedCommands;
using Flow.Launcher.Plugin.Shell.Views;
using WindowsInput;
using WindowsInput.Native;
using Control = System.Windows.Controls.Control;
using Keys = System.Windows.Forms.Keys;
2014-01-03 10:16:05 +00:00
2020-04-21 09:12:17 +00:00
namespace Flow.Launcher.Plugin.Shell
2014-01-03 10:16:05 +00:00
{
2025-04-09 04:33:22 +00:00
public class Main : IPlugin, ISettingProvider, IPluginI18n, IContextMenu, IDisposable
2014-01-03 10:16:05 +00:00
{
2025-04-09 04:14:07 +00:00
private static readonly string ClassName = nameof(Main);
internal static PluginInitContext Context { get; private set; }
2025-04-09 04:14:07 +00:00
2016-05-19 22:44:08 +00:00
private const string Image = "Images/shell.png";
2016-06-23 21:53:30 +00:00
private bool _winRStroked;
private readonly KeyboardSimulator _keyboardSimulator = new(new InputSimulator());
private Settings _settings;
public List<Result> Query(Query query)
2014-01-03 10:16:05 +00:00
{
List<Result> results = [];
string cmd = query.Search;
if (string.IsNullOrEmpty(cmd))
{
2022-12-31 15:43:07 +00:00
return ResultsFromHistory();
}
else
2014-01-03 15:52:36 +00:00
{
2015-01-20 12:05:38 +00:00
var queryCmd = GetCurrentCmd(cmd);
2015-11-08 02:27:37 +00:00
results.Add(queryCmd);
2015-01-20 12:05:38 +00:00
var history = GetHistoryCmds(cmd, queryCmd);
2015-11-08 02:27:37 +00:00
results.AddRange(history);
2014-03-19 20:12:50 +00:00
try
{
string basedir = null;
string dir = null;
string excmd = Environment.ExpandEnvironmentVariables(cmd);
if (Directory.Exists(excmd) && (cmd.EndsWith('/') || cmd.EndsWith('\\')))
2014-03-19 20:12:50 +00:00
{
basedir = excmd;
dir = cmd;
}
else if (Directory.Exists(Path.GetDirectoryName(excmd) ?? string.Empty))
2014-03-19 20:12:50 +00:00
{
basedir = Path.GetDirectoryName(excmd);
2022-12-31 15:43:07 +00:00
var dirName = Path.GetDirectoryName(cmd);
dir = (dirName.EndsWith('/') || dirName.EndsWith('\\')) ? dirName : cmd[..(dirName.Length + 1)];
2014-03-19 20:12:50 +00:00
}
if (basedir != null)
{
var autocomplete =
2022-06-21 20:37:33 +00:00
Directory.GetFileSystemEntries(basedir)
.Select(o => dir + Path.GetFileName(o))
.Where(o => o.StartsWith(cmd, StringComparison.OrdinalIgnoreCase) &&
!results.Any(p => o.Equals(p.Title, StringComparison.OrdinalIgnoreCase)) &&
!results.Any(p => o.Equals(p.Title, StringComparison.OrdinalIgnoreCase))).ToList();
2014-03-19 20:12:50 +00:00
autocomplete.Sort();
2016-01-06 21:34:42 +00:00
results.AddRange(autocomplete.ConvertAll(m => new Result
2014-03-23 08:17:41 +00:00
{
2014-03-19 20:12:50 +00:00
Title = m,
2016-05-19 22:44:08 +00:00
IcoPath = Image,
2016-01-06 21:34:42 +00:00
Action = c =>
2014-03-19 20:12:50 +00:00
{
2022-06-21 20:37:33 +00:00
var runAsAdministrator =
c.SpecialKeyState.CtrlPressed &&
c.SpecialKeyState.ShiftPressed &&
!c.SpecialKeyState.AltPressed &&
2022-06-21 20:37:33 +00:00
!c.SpecialKeyState.WinPressed;
Execute(Process.Start, PrepareProcessStartInfo(m, runAsAdministrator));
2014-03-19 20:12:50 +00:00
return true;
2023-06-11 11:59:46 +00:00
},
CopyText = m
2014-03-19 20:12:50 +00:00
}));
}
}
catch (Exception e)
{
2025-04-09 04:14:07 +00:00
Context.API.LogException(ClassName, $"Exception when query for <{query}>", e);
}
return results;
2014-01-03 15:52:36 +00:00
}
2014-01-03 10:16:05 +00:00
}
2015-01-20 12:05:38 +00:00
private List<Result> GetHistoryCmds(string cmd, Result result)
{
IEnumerable<Result> history = _settings.CommandHistory.Where(o => o.Key.Contains(cmd))
2015-01-20 12:05:38 +00:00
.OrderByDescending(o => o.Value)
.Select(m =>
{
if (m.Key == cmd)
{
result.SubTitle = Localize.flowlauncher_plugin_cmd_cmd_has_been_executed_times(m.Value);
2015-01-20 12:05:38 +00:00
return null;
}
var ret = new Result
{
Title = m.Key,
SubTitle = Localize.flowlauncher_plugin_cmd_cmd_has_been_executed_times(m.Value),
2016-05-19 22:44:08 +00:00
IcoPath = Image,
2016-01-06 21:34:42 +00:00
Action = c =>
2015-01-20 12:05:38 +00:00
{
2022-06-21 20:37:33 +00:00
var runAsAdministrator =
c.SpecialKeyState.CtrlPressed &&
c.SpecialKeyState.ShiftPressed &&
!c.SpecialKeyState.AltPressed &&
2022-06-21 20:37:33 +00:00
!c.SpecialKeyState.WinPressed;
Execute(Process.Start, PrepareProcessStartInfo(m.Key, runAsAdministrator));
2015-01-20 12:05:38 +00:00
return true;
2023-06-11 11:59:46 +00:00
},
CopyText = m.Key
2015-01-20 12:05:38 +00:00
};
return ret;
2021-04-12 10:05:30 +00:00
}).Where(o => o != null);
if (_settings.ShowOnlyMostUsedCMDs)
return [.. history.Take(_settings.ShowOnlyMostUsedCMDsNumber)];
2021-04-12 10:05:30 +00:00
return [.. history];
2015-01-20 12:05:38 +00:00
}
private Result GetCurrentCmd(string cmd)
{
Result result = new Result
{
Title = cmd,
Score = 5000,
SubTitle = Localize.flowlauncher_plugin_cmd_execute_through_shell(),
2016-05-19 22:44:08 +00:00
IcoPath = Image,
2016-01-06 21:34:42 +00:00
Action = c =>
2015-01-20 12:05:38 +00:00
{
2022-06-21 20:37:33 +00:00
var runAsAdministrator =
c.SpecialKeyState.CtrlPressed &&
c.SpecialKeyState.ShiftPressed &&
!c.SpecialKeyState.AltPressed &&
2022-06-21 20:37:33 +00:00
!c.SpecialKeyState.WinPressed;
Execute(Process.Start, PrepareProcessStartInfo(cmd, runAsAdministrator));
2015-01-20 12:05:38 +00:00
return true;
2023-06-11 11:59:46 +00:00
},
CopyText = cmd
2015-01-20 12:05:38 +00:00
};
return result;
}
2022-12-31 15:43:07 +00:00
private List<Result> ResultsFromHistory()
2015-01-20 12:05:38 +00:00
{
IEnumerable<Result> history = _settings.CommandHistory.OrderByDescending(o => o.Value)
2015-01-20 12:05:38 +00:00
.Select(m => new Result
{
Title = m.Key,
SubTitle = Localize.flowlauncher_plugin_cmd_cmd_has_been_executed_times(m.Value),
2016-05-19 22:44:08 +00:00
IcoPath = Image,
2016-01-06 21:34:42 +00:00
Action = c =>
2015-01-20 12:05:38 +00:00
{
2022-06-21 20:37:33 +00:00
var runAsAdministrator =
c.SpecialKeyState.CtrlPressed &&
c.SpecialKeyState.ShiftPressed &&
!c.SpecialKeyState.AltPressed &&
2022-06-21 20:37:33 +00:00
!c.SpecialKeyState.WinPressed;
Execute(Process.Start, PrepareProcessStartInfo(m.Key, runAsAdministrator));
2015-01-20 12:05:38 +00:00
return true;
2023-06-11 11:59:46 +00:00
},
CopyText = m.Key
2021-04-12 10:05:30 +00:00
});
if (_settings.ShowOnlyMostUsedCMDs)
return [.. history.Take(_settings.ShowOnlyMostUsedCMDsNumber)];
2021-04-12 10:05:30 +00:00
return [.. history];
2015-01-20 12:05:38 +00:00
}
private ProcessStartInfo PrepareProcessStartInfo(string command, bool runAsAdministrator = false)
{
command = command.Trim();
command = Environment.ExpandEnvironmentVariables(command);
var workingDirectory = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
var runAsAdministratorArg = !runAsAdministrator && !_settings.RunAsAdministrator ? "" : "runas";
2025-06-15 13:21:46 +00:00
var info = new ProcessStartInfo()
{
2025-06-15 13:21:46 +00:00
Verb = runAsAdministratorArg,
WorkingDirectory = workingDirectory,
2021-11-25 21:47:11 +00:00
};
var notifyStr = Localize.flowlauncher_plugin_cmd_press_any_key_to_close();
2025-06-16 04:38:31 +00:00
var addedCharacter = _settings.UseWindowsTerminal ? "\\" : "";
2021-11-25 21:47:11 +00:00
switch (_settings.Shell)
{
2021-11-25 21:47:11 +00:00
case Shell.Cmd:
{
2025-06-05 10:55:58 +00:00
if (_settings.UseWindowsTerminal)
{
info.FileName = "wt.exe";
info.ArgumentList.Add("cmd");
}
else
{
info.FileName = "cmd.exe";
}
2025-06-16 04:38:31 +00:00
if (_settings.LeaveShellOpen)
{
info.ArgumentList.Add("/k");
}
else
{
info.ArgumentList.Add("/c");
}
2025-06-15 13:21:46 +00:00
info.ArgumentList.Add(
2025-06-16 04:38:31 +00:00
$"{command}" +
2025-06-15 13:21:46 +00:00
$"{(_settings.CloseShellAfterPress ?
$" && echo {notifyStr} && pause > nul /c" :
"")}");
2025-06-05 10:55:58 +00:00
break;
}
2021-11-25 21:47:11 +00:00
case Shell.Powershell:
{
2025-06-05 10:55:58 +00:00
// Using just a ; doesn't work with wt, as it's used to create a new tab for the terminal window
// \\ must be escaped for it to work properly, or breaking it into multiple arguments
if (_settings.UseWindowsTerminal)
{
info.FileName = "wt.exe";
info.ArgumentList.Add("powershell");
}
else
{
info.FileName = "powershell.exe";
}
if (_settings.LeaveShellOpen)
{
info.ArgumentList.Add("-NoExit");
info.ArgumentList.Add(command);
}
else
{
info.ArgumentList.Add("-Command");
2025-06-15 13:21:46 +00:00
info.ArgumentList.Add(
$"{command}{addedCharacter};" +
$"{(_settings.CloseShellAfterPress ?
$" Write-Host '{notifyStr}'{addedCharacter}; [System.Console]::ReadKey(){addedCharacter}; exit" :
"")}");
2025-06-05 10:55:58 +00:00
}
break;
2016-05-19 22:28:13 +00:00
}
2021-11-25 21:47:11 +00:00
case Shell.Pwsh:
{
2025-06-05 10:55:58 +00:00
// Using just a ; doesn't work with wt, as it's used to create a new tab for the terminal window
// \\ must be escaped for it to work properly, or breaking it into multiple arguments
if (_settings.UseWindowsTerminal)
{
info.FileName = "wt.exe";
info.ArgumentList.Add("pwsh");
}
else
{
info.FileName = "pwsh.exe";
}
if (_settings.LeaveShellOpen)
{
info.ArgumentList.Add("-NoExit");
}
info.ArgumentList.Add("-Command");
2025-06-15 13:21:46 +00:00
info.ArgumentList.Add(
$"{command}{addedCharacter};" +
$"{(_settings.CloseShellAfterPress ?
$" Write-Host '{notifyStr}'{addedCharacter}; [System.Console]::ReadKey(){addedCharacter}; exit" :
"")}");
2025-06-05 10:55:58 +00:00
break;
}
2021-11-25 21:47:11 +00:00
case Shell.RunCommand:
{
var parts = command.Split(
[
2025-06-05 10:55:58 +00:00
' '
], 2);
2025-06-05 10:55:58 +00:00
if (parts.Length == 2)
{
var filename = parts[0];
if (ExistInPath(filename))
{
var arguments = parts[1];
info.FileName = filename;
info.ArgumentList.Add(arguments);
}
else
{
info.FileName = command;
}
2021-11-25 21:47:11 +00:00
}
else
{
info.FileName = command;
}
2025-06-05 10:55:58 +00:00
info.UseShellExecute = true;
break;
}
2021-11-25 21:47:11 +00:00
default:
throw new NotImplementedException();
2016-05-19 23:03:12 +00:00
}
info.UseShellExecute = true;
_settings.AddCmdHistory(command);
return info;
}
private void Execute(Func<ProcessStartInfo, Process> startProcess, ProcessStartInfo info)
{
try
{
2021-11-17 10:00:36 +00:00
ShellCommand.Execute(startProcess, info);
}
catch (FileNotFoundException e)
{
2025-07-20 04:28:55 +00:00
Context.API.ShowMsgError(GetTranslatedPluginTitle(),
Localize.flowlauncher_plugin_cmd_command_not_found(e.Message));
}
catch (Win32Exception e)
{
2025-07-20 04:28:55 +00:00
Context.API.ShowMsgError(GetTranslatedPluginTitle(),
Localize.flowlauncher_plugin_cmd_error_running_command(e.Message));
2025-07-20 04:28:55 +00:00
}
catch (Exception e)
{
Context.API.LogException(ClassName, $"Error executing command: {info.FileName} {string.Join(" ", info.ArgumentList)}", e);
}
}
private static bool ExistInPath(string filename)
{
if (File.Exists(filename))
{
2016-05-19 22:28:13 +00:00
return true;
}
else
{
var values = Environment.GetEnvironmentVariable("PATH");
if (values != null)
{
foreach (var path in values.Split(';'))
{
2016-05-19 22:28:13 +00:00
var path1 = Path.Combine(path, filename);
var path2 = Path.Combine(path, filename + ".exe");
if (File.Exists(path1) || File.Exists(path2))
{
2016-05-19 22:28:13 +00:00
return true;
}
}
2016-05-19 22:28:13 +00:00
return false;
}
else
{
2016-05-19 22:28:13 +00:00
return false;
}
}
}
public void Init(PluginInitContext context)
2014-01-03 10:16:05 +00:00
{
2025-04-09 04:14:07 +00:00
Context = context;
_settings = context.API.LoadSettingJsonStorage<Settings>();
context.API.RegisterGlobalKeyboardCallback(API_GlobalKeyboardEvent);
// Since the old Settings class set default value of ShowOnlyMostUsedCMDsNumber to 0 which is a wrong value,
// we need to fix it here to make sure the default value is 5
2025-09-30 12:28:41 +00:00
// todo: remove this code block after release v2.2.0
if (_settings.ShowOnlyMostUsedCMDsNumber == 0)
{
_settings.ShowOnlyMostUsedCMDsNumber = 5;
}
}
private bool API_GlobalKeyboardEvent(int keyevent, int vkcode, SpecialKeyState state)
{
2025-04-09 04:14:07 +00:00
if (!Context.CurrentPluginMetadata.Disabled && _settings.ReplaceWinR)
{
if (keyevent == (int)KeyEvent.WM_KEYDOWN && vkcode == (int)Keys.R && state.WinPressed)
{
2016-06-23 21:53:30 +00:00
_winRStroked = true;
OnWinRPressed();
return false;
}
2016-06-23 21:53:30 +00:00
if (keyevent == (int)KeyEvent.WM_KEYUP && _winRStroked && vkcode == (int)Keys.LWin)
{
2016-06-23 21:53:30 +00:00
_winRStroked = false;
_keyboardSimulator.ModifiedKeyStroke(VirtualKeyCode.LWIN, VirtualKeyCode.CONTROL);
return false;
}
}
2016-05-25 11:31:00 +00:00
return true;
}
private static void OnWinRPressed()
{
Context.API.ShowMainWindow();
// show the main window and set focus to the query box
_ = Task.Run(async () =>
{
2025-04-09 04:14:07 +00:00
Context.API.ChangeQuery($"{Context.CurrentPluginMetadata.ActionKeywords[0]}{Plugin.Query.TermSeparator}");
// Win+R is a system-reserved shortcut, and though the plugin intercepts the keyboard event and
// shows the main window, Windows continues to process the Win key and briefly reclaims focus.
// So we need to wait until the keyboard event processing is completed and then set focus
await Task.Delay(50);
Context.API.FocusQueryTextBox();
});
2014-01-03 15:52:36 +00:00
}
public Control CreateSettingPanel()
{
return new CMDSetting(_settings);
}
2015-01-07 10:51:11 +00:00
2015-02-07 12:17:49 +00:00
public string GetTranslatedPluginTitle()
{
return Localize.flowlauncher_plugin_cmd_plugin_name();
2015-02-07 12:17:49 +00:00
}
public string GetTranslatedPluginDescription()
{
return Localize.flowlauncher_plugin_cmd_plugin_description();
2015-02-07 12:17:49 +00:00
}
public List<Result> LoadContextMenus(Result selectedResult)
{
2022-12-31 15:43:07 +00:00
var results = new List<Result>
2016-01-06 21:34:42 +00:00
{
2022-12-31 15:43:07 +00:00
new()
{
Title = Localize.flowlauncher_plugin_cmd_run_as_different_user(),
2025-04-09 04:14:07 +00:00
Action = c =>
{
2022-08-08 18:27:11 +00:00
Execute(ShellCommand.RunAsDifferentUser, PrepareProcessStartInfo(selectedResult.Title));
return true;
},
2022-12-12 04:08:22 +00:00
IcoPath = "Images/user.png",
Glyph = new GlyphInfo(FontFamily: "/Resources/#Segoe Fluent Icons", Glyph: "\xe7ee")
},
2022-12-31 15:43:07 +00:00
new()
{
Title = Localize.flowlauncher_plugin_cmd_run_as_administrator(),
Action = c =>
{
Execute(Process.Start, PrepareProcessStartInfo(selectedResult.Title, true));
return true;
},
2022-12-12 04:08:22 +00:00
IcoPath = "Images/admin.png",
Glyph = new GlyphInfo(FontFamily: "/Resources/#Segoe Fluent Icons", Glyph: "\xe7ef")
2021-03-09 10:04:28 +00:00
},
2022-12-31 15:43:07 +00:00
new()
2021-03-09 10:04:28 +00:00
{
Title = Localize.flowlauncher_plugin_cmd_copy(),
2021-03-09 10:04:28 +00:00
Action = c =>
{
2025-04-09 04:14:07 +00:00
Context.API.CopyToClipboard(selectedResult.Title);
2021-03-09 10:04:28 +00:00
return true;
},
2022-12-12 04:08:22 +00:00
IcoPath = "Images/copy.png",
Glyph = new GlyphInfo(FontFamily: "/Resources/#Segoe Fluent Icons", Glyph: "\xe8c8")
}
};
2022-12-31 15:43:07 +00:00
return results;
}
2025-04-09 04:33:22 +00:00
public void Dispose()
{
Context.API.RemoveGlobalKeyboardCallback(API_GlobalKeyboardEvent);
}
2014-01-03 10:16:05 +00:00
}
}