2025-08-31 07:53:21 +00:00
|
|
|
using System;
|
2014-01-03 10:16:05 +00:00
|
|
|
using System.Collections.Generic;
|
2019-12-12 09:55:41 +00:00
|
|
|
using System.ComponentModel;
|
2016-01-03 23:18:51 +00:00
|
|
|
using System.Diagnostics;
|
2014-01-26 03:01:13 +00:00
|
|
|
using System.IO;
|
2014-01-03 10:16:05 +00:00
|
|
|
using System.Linq;
|
2019-12-12 09:55:41 +00:00
|
|
|
using System.Threading.Tasks;
|
2015-01-05 14:41:17 +00:00
|
|
|
using WindowsInput;
|
2016-06-23 21:55:38 +00:00
|
|
|
using WindowsInput.Native;
|
2020-04-21 09:12:17 +00:00
|
|
|
using Flow.Launcher.Plugin.SharedCommands;
|
2014-07-04 07:34:31 +00:00
|
|
|
using Control = System.Windows.Controls.Control;
|
2016-05-25 00:00:10 +00:00
|
|
|
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 PluginInitContext Context { get; private set; }
|
|
|
|
|
|
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;
|
2025-04-09 04:30:50 +00:00
|
|
|
private readonly KeyboardSimulator _keyboardSimulator = new(new InputSimulator());
|
2016-04-21 00:53:21 +00:00
|
|
|
|
2021-05-11 12:18:57 +00:00
|
|
|
private Settings _settings;
|
2016-04-21 00:53:21 +00:00
|
|
|
|
2015-01-03 07:20:34 +00:00
|
|
|
public List<Result> Query(Query query)
|
2014-01-03 10:16:05 +00:00
|
|
|
{
|
2014-01-03 15:52:36 +00:00
|
|
|
List<Result> results = new List<Result>();
|
2015-11-01 19:47:20 +00:00
|
|
|
string cmd = query.Search;
|
|
|
|
|
if (string.IsNullOrEmpty(cmd))
|
2014-01-26 10:06:38 +00:00
|
|
|
{
|
2022-12-31 15:43:07 +00:00
|
|
|
return ResultsFromHistory();
|
2014-01-26 10:06:38 +00:00
|
|
|
}
|
2015-11-01 19:47:20 +00:00
|
|
|
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);
|
2015-01-27 14:59:03 +00:00
|
|
|
|
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(@"\")))
|
|
|
|
|
{
|
|
|
|
|
basedir = excmd;
|
|
|
|
|
dir = cmd;
|
|
|
|
|
}
|
2015-11-01 19:47:20 +00:00
|
|
|
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);
|
2025-04-09 04:30:50 +00:00
|
|
|
dir = (dirName.EndsWith("/") || dirName.EndsWith(@"\")) ? dirName : cmd[..(dirName.Length + 1)];
|
2014-03-19 20:12:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (basedir != null)
|
|
|
|
|
{
|
2022-12-08 03:13:07 +00:00
|
|
|
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) &&
|
2022-12-08 03:13:07 +00:00
|
|
|
!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();
|
2022-12-08 03:13:07 +00:00
|
|
|
|
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 =
|
2021-09-11 17:21:52 +00:00
|
|
|
c.SpecialKeyState.CtrlPressed &&
|
|
|
|
|
c.SpecialKeyState.ShiftPressed &&
|
|
|
|
|
!c.SpecialKeyState.AltPressed &&
|
2022-06-21 20:37:33 +00:00
|
|
|
!c.SpecialKeyState.WinPressed;
|
2021-09-11 17:21:52 +00:00
|
|
|
|
|
|
|
|
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
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-11-01 19:47:20 +00:00
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2025-04-09 04:14:07 +00:00
|
|
|
Context.API.LogException(ClassName, $"Exception when query for <{query}>", e);
|
2015-11-01 19:47:20 +00:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
|
{
|
2021-04-12 07:32:48 +00:00
|
|
|
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)
|
|
|
|
|
{
|
2025-04-09 04:14:07 +00:00
|
|
|
result.SubTitle = string.Format(Context.API.GetTranslation("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,
|
2025-04-09 04:14:07 +00:00
|
|
|
SubTitle = string.Format(Context.API.GetTranslation("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 =
|
2021-09-11 17:21:52 +00:00
|
|
|
c.SpecialKeyState.CtrlPressed &&
|
|
|
|
|
c.SpecialKeyState.ShiftPressed &&
|
|
|
|
|
!c.SpecialKeyState.AltPressed &&
|
2022-06-21 20:37:33 +00:00
|
|
|
!c.SpecialKeyState.WinPressed;
|
2021-09-11 17:21:52 +00:00
|
|
|
|
|
|
|
|
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).ToList();
|
|
|
|
|
|
2015-01-20 12:05:38 +00:00
|
|
|
return history.ToList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Result GetCurrentCmd(string cmd)
|
|
|
|
|
{
|
|
|
|
|
Result result = new Result
|
|
|
|
|
{
|
|
|
|
|
Title = cmd,
|
|
|
|
|
Score = 5000,
|
2025-04-09 04:14:07 +00:00
|
|
|
SubTitle = Context.API.GetTranslation("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 =
|
2021-09-11 17:21:52 +00:00
|
|
|
c.SpecialKeyState.CtrlPressed &&
|
|
|
|
|
c.SpecialKeyState.ShiftPressed &&
|
|
|
|
|
!c.SpecialKeyState.AltPressed &&
|
2022-06-21 20:37:33 +00:00
|
|
|
!c.SpecialKeyState.WinPressed;
|
2021-09-11 17:21:52 +00:00
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2021-04-12 07:32:48 +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,
|
2025-04-09 04:14:07 +00:00
|
|
|
SubTitle = string.Format(Context.API.GetTranslation("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 =
|
2021-09-11 17:21:52 +00:00
|
|
|
c.SpecialKeyState.CtrlPressed &&
|
|
|
|
|
c.SpecialKeyState.ShiftPressed &&
|
|
|
|
|
!c.SpecialKeyState.AltPressed &&
|
2022-06-21 20:37:33 +00:00
|
|
|
!c.SpecialKeyState.WinPressed;
|
2021-09-11 17:21:52 +00:00
|
|
|
|
|
|
|
|
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).ToList();
|
|
|
|
|
|
2015-01-20 12:05:38 +00:00
|
|
|
return history.ToList();
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-13 17:35:34 +00:00
|
|
|
private ProcessStartInfo PrepareProcessStartInfo(string command, bool runAsAdministrator = false)
|
2014-01-26 03:01:13 +00:00
|
|
|
{
|
2016-05-19 08:04:31 +00:00
|
|
|
command = command.Trim();
|
|
|
|
|
command = Environment.ExpandEnvironmentVariables(command);
|
2019-12-09 21:23:34 +00:00
|
|
|
var workingDirectory = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
|
|
|
|
|
var runAsAdministratorArg = !runAsAdministrator && !_settings.RunAsAdministrator ? "" : "runas";
|
2016-05-19 08:04:31 +00:00
|
|
|
|
2025-06-15 13:21:46 +00:00
|
|
|
var info = new ProcessStartInfo()
|
2015-12-14 00:29:13 +00:00
|
|
|
{
|
2025-06-15 13:21:46 +00:00
|
|
|
Verb = runAsAdministratorArg,
|
|
|
|
|
WorkingDirectory = workingDirectory,
|
2021-11-25 21:47:11 +00:00
|
|
|
};
|
2025-06-15 13:21:46 +00:00
|
|
|
var notifyStr = Context.API.GetTranslation("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)
|
2016-05-19 08:04:31 +00:00
|
|
|
{
|
2021-11-25 21:47:11 +00:00
|
|
|
case Shell.Cmd:
|
2025-02-05 23:22:41 +00:00
|
|
|
{
|
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;
|
|
|
|
|
}
|
2019-12-09 21:23:34 +00:00
|
|
|
|
2021-11-25 21:47:11 +00:00
|
|
|
case Shell.Powershell:
|
2025-02-05 23:22:41 +00:00
|
|
|
{
|
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
|
|
|
|
2023-06-12 13:18:23 +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;
|
2023-06-12 13:18:23 +00:00
|
|
|
}
|
|
|
|
|
|
2021-11-25 21:47:11 +00:00
|
|
|
case Shell.RunCommand:
|
2022-12-08 03:13:07 +00:00
|
|
|
{
|
2025-06-05 10:55:58 +00:00
|
|
|
var parts = command.Split(new[]
|
2021-11-25 21:47:11 +00:00
|
|
|
{
|
2025-06-05 10:55:58 +00:00
|
|
|
' '
|
|
|
|
|
}, 2);
|
|
|
|
|
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;
|
|
|
|
|
}
|
2022-12-08 03:13:07 +00:00
|
|
|
|
2025-06-05 10:55:58 +00:00
|
|
|
info.UseShellExecute = true;
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
2024-04-23 21:29:39 +00:00
|
|
|
|
2021-11-25 21:47:11 +00:00
|
|
|
default:
|
|
|
|
|
throw new NotImplementedException();
|
2016-05-19 23:03:12 +00:00
|
|
|
}
|
|
|
|
|
|
2016-05-19 08:04:31 +00:00
|
|
|
info.UseShellExecute = true;
|
|
|
|
|
|
2019-12-12 09:55:41 +00:00
|
|
|
_settings.AddCmdHistory(command);
|
|
|
|
|
|
|
|
|
|
return info;
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-14 18:17:57 +00:00
|
|
|
private void Execute(Func<ProcessStartInfo, Process> startProcess, ProcessStartInfo info)
|
2019-12-12 09:55:41 +00:00
|
|
|
{
|
2016-01-03 23:18:51 +00:00
|
|
|
try
|
|
|
|
|
{
|
2021-11-17 10:00:36 +00:00
|
|
|
ShellCommand.Execute(startProcess, info);
|
2015-12-14 00:29:13 +00:00
|
|
|
}
|
2016-01-03 23:18:51 +00:00
|
|
|
catch (FileNotFoundException e)
|
|
|
|
|
{
|
2025-07-20 04:28:55 +00:00
|
|
|
Context.API.ShowMsgError(GetTranslatedPluginTitle(),
|
|
|
|
|
string.Format(Context.API.GetTranslation("flowlauncher_plugin_cmd_command_not_found"), e.Message));
|
2019-12-12 09:55:41 +00:00
|
|
|
}
|
2021-05-14 18:17:57 +00:00
|
|
|
catch (Win32Exception e)
|
2019-12-12 09:55:41 +00:00
|
|
|
{
|
2025-07-20 04:28:55 +00:00
|
|
|
Context.API.ShowMsgError(GetTranslatedPluginTitle(),
|
|
|
|
|
string.Format(Context.API.GetTranslation("flowlauncher_plugin_cmd_error_running_command"), e.Message));
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
Context.API.LogException(ClassName, $"Error executing command: {info.FileName} {string.Join(" ", info.ArgumentList)}", e);
|
2016-01-03 23:18:51 +00:00
|
|
|
}
|
2014-01-26 03:01:13 +00:00
|
|
|
}
|
|
|
|
|
|
2025-04-09 04:30:50 +00:00
|
|
|
private static bool ExistInPath(string filename)
|
2016-05-19 08:04:31 +00:00
|
|
|
{
|
|
|
|
|
if (File.Exists(filename))
|
|
|
|
|
{
|
2016-05-19 22:28:13 +00:00
|
|
|
return true;
|
2016-05-19 08:04:31 +00:00
|
|
|
}
|
|
|
|
|
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 08:04:31 +00:00
|
|
|
{
|
2016-05-19 22:28:13 +00:00
|
|
|
return true;
|
2016-05-19 08:04:31 +00:00
|
|
|
}
|
|
|
|
|
}
|
2016-05-19 22:28:13 +00:00
|
|
|
return false;
|
2016-05-19 08:04:31 +00:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2016-05-19 22:28:13 +00:00
|
|
|
return false;
|
2016-05-19 08:04:31 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-03 07:20:34 +00:00
|
|
|
public void Init(PluginInitContext context)
|
2014-01-03 10:16:05 +00:00
|
|
|
{
|
2025-04-09 04:14:07 +00:00
|
|
|
Context = context;
|
2021-05-13 11:29:21 +00:00
|
|
|
_settings = context.API.LoadSettingJsonStorage<Settings>();
|
2021-11-27 20:26:20 +00:00
|
|
|
context.API.RegisterGlobalKeyboardCallback(API_GlobalKeyboardEvent);
|
2015-01-05 14:41:17 +00:00
|
|
|
}
|
|
|
|
|
|
2015-01-08 14:49:42 +00:00
|
|
|
bool API_GlobalKeyboardEvent(int keyevent, int vkcode, SpecialKeyState state)
|
2015-01-05 14:41:17 +00:00
|
|
|
{
|
2025-04-09 04:14:07 +00:00
|
|
|
if (!Context.CurrentPluginMetadata.Disabled && _settings.ReplaceWinR)
|
2015-01-05 14:41:17 +00:00
|
|
|
{
|
2015-01-08 14:49:42 +00:00
|
|
|
if (keyevent == (int)KeyEvent.WM_KEYDOWN && vkcode == (int)Keys.R && state.WinPressed)
|
2015-01-05 14:41:17 +00:00
|
|
|
{
|
2016-06-23 21:53:30 +00:00
|
|
|
_winRStroked = true;
|
2015-01-05 14:41:17 +00:00
|
|
|
OnWinRPressed();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2016-06-23 21:53:30 +00:00
|
|
|
if (keyevent == (int)KeyEvent.WM_KEYUP && _winRStroked && vkcode == (int)Keys.LWin)
|
2015-01-05 14:41:17 +00:00
|
|
|
{
|
2016-06-23 21:53:30 +00:00
|
|
|
_winRStroked = false;
|
2016-06-23 21:55:38 +00:00
|
|
|
_keyboardSimulator.ModifiedKeyStroke(VirtualKeyCode.LWIN, VirtualKeyCode.CONTROL);
|
2015-01-05 14:41:17 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-05-25 11:31:00 +00:00
|
|
|
return true;
|
2015-01-05 14:41:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnWinRPressed()
|
|
|
|
|
{
|
2025-05-21 19:14:30 +00:00
|
|
|
Context.API.ShowMainWindow();
|
2021-06-26 02:52:44 +00:00
|
|
|
// show the main window and set focus to the query box
|
2025-05-21 19:14:30 +00:00
|
|
|
_ = Task.Run(async () =>
|
2022-12-08 03:13:07 +00:00
|
|
|
{
|
2025-04-09 04:14:07 +00:00
|
|
|
Context.API.ChangeQuery($"{Context.CurrentPluginMetadata.ActionKeywords[0]}{Plugin.Query.TermSeparator}");
|
2025-05-22 02:39:09 +00:00
|
|
|
|
|
|
|
|
// 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();
|
2022-12-08 03:13:07 +00:00
|
|
|
});
|
2014-01-03 15:52:36 +00:00
|
|
|
}
|
|
|
|
|
|
2014-07-04 07:34:31 +00:00
|
|
|
public Control CreateSettingPanel()
|
|
|
|
|
{
|
2016-03-28 02:09:57 +00:00
|
|
|
return new CMDSetting(_settings);
|
2014-07-04 07:34:31 +00:00
|
|
|
}
|
2015-01-07 10:51:11 +00:00
|
|
|
|
2015-02-07 12:17:49 +00:00
|
|
|
public string GetTranslatedPluginTitle()
|
|
|
|
|
{
|
2025-04-09 04:14:07 +00:00
|
|
|
return Context.API.GetTranslation("flowlauncher_plugin_cmd_plugin_name");
|
2015-02-07 12:17:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string GetTranslatedPluginDescription()
|
|
|
|
|
{
|
2025-04-09 04:14:07 +00:00
|
|
|
return Context.API.GetTranslation("flowlauncher_plugin_cmd_plugin_description");
|
2015-02-07 12:17:49 +00:00
|
|
|
}
|
|
|
|
|
|
2015-02-07 15:49:46 +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()
|
2019-12-12 09:55:41 +00:00
|
|
|
{
|
2025-04-09 04:14:07 +00:00
|
|
|
Title = Context.API.GetTranslation("flowlauncher_plugin_cmd_run_as_different_user"),
|
|
|
|
|
Action = c =>
|
2019-12-12 09:55:41 +00:00
|
|
|
{
|
2022-08-08 18:27:11 +00:00
|
|
|
Execute(ShellCommand.RunAsDifferentUser, PrepareProcessStartInfo(selectedResult.Title));
|
2019-12-12 09:55:41 +00:00
|
|
|
return true;
|
|
|
|
|
},
|
2022-12-12 04:08:22 +00:00
|
|
|
IcoPath = "Images/user.png",
|
|
|
|
|
Glyph = new GlyphInfo(FontFamily: "/Resources/#Segoe Fluent Icons", Glyph: "\xe7ee")
|
2019-12-12 09:55:41 +00:00
|
|
|
},
|
2022-12-31 15:43:07 +00:00
|
|
|
new()
|
2019-12-12 09:55:41 +00:00
|
|
|
{
|
2025-04-09 04:14:07 +00:00
|
|
|
Title = Context.API.GetTranslation("flowlauncher_plugin_cmd_run_as_administrator"),
|
2019-12-12 09:55:41 +00:00
|
|
|
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
|
|
|
{
|
2025-04-09 04:14:07 +00:00
|
|
|
Title = Context.API.GetTranslation("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")
|
2019-12-12 09:55:41 +00:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2022-12-31 15:43:07 +00:00
|
|
|
return results;
|
2015-02-07 15:49:46 +00:00
|
|
|
}
|
2025-04-09 04:33:22 +00:00
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
Context.API.RemoveGlobalKeyboardCallback(API_GlobalKeyboardEvent);
|
|
|
|
|
}
|
2014-01-03 10:16:05 +00:00
|
|
|
}
|
2022-08-08 04:31:38 +00:00
|
|
|
}
|