ProcessKiller: add context menu option

to kill all processes whose file path matches with the selected result.
This commit is contained in:
Ioannis G 2020-07-03 02:43:35 +03:00
parent 10ce003dac
commit 6e6a74beb9
No known key found for this signature in database
GPG key ID: EAC0E4E5E36AC49E
2 changed files with 44 additions and 18 deletions

View file

@ -6,5 +6,6 @@
<system:String x:Key="flowlauncher_plugin_processkiller_plugin_description">Kill running processes from Flow Launcher</system:String>
<system:String x:Key="flowlauncher_plugin_processkiller_kill_all">kill all "{0}" processes</system:String>
<system:String x:Key="flowlauncher_plugin_processkiller_kill_instances">kill all instances</system:String>
</ResourceDictionary>

View file

@ -10,7 +10,7 @@ using Flow.Launcher.Infrastructure.Logger;
namespace Flow.Launcher.Plugin.ProcessKiller
{
public class Main : IPlugin, IPluginI18n
public class Main : IPlugin, IPluginI18n, IContextMenu
{
private readonly HashSet<string> _systemProcessList = new HashSet<string>(){
"conhost",
@ -58,6 +58,34 @@ namespace Flow.Launcher.Plugin.ProcessKiller
return _context.API.GetTranslation("flowlauncher_plugin_processkiller_plugin_description");
}
public List<Result> LoadContextMenus(Result result)
{
var menuOptions = new List<Result>();
var processPath = result.SubTitle;
// get all non-system processes whose file path matches that of the given result (processPath)
var similarProcesses = Process.GetProcesses()
.Where(p => !IsSystemProcess(p) && GetProcessFilename(p) == processPath);
menuOptions.Add(new Result
{
Title = _context.API.GetTranslation("flowlauncher_plugin_processkiller_kill_instances"),
SubTitle = processPath,
Action = _ =>
{
foreach (var p in similarProcesses)
{
KillProcess(p);
}
return true;
},
IcoPath = "Images/app.png"
});
return menuOptions;
}
private List<Result> CreateResultsFromProcesses(List<ProcessResult> processlist, string termToSearch)
{
var results = new List<Result>();
@ -105,29 +133,30 @@ namespace Flow.Launcher.Plugin.ProcessKiller
}
return results;
}
void KillProcess(Process p)
private void KillProcess(Process p)
{
try
{
try
if (!p.HasExited)
{
if (!p.HasExited)
{
p.Kill();
}
}
catch (Exception e)
{
Log.Exception($"|ProcessKiller.CreateResultsFromProcesses|Failed to kill process {p.ProcessName}", e);
p.Kill();
}
}
catch (Exception e)
{
Log.Exception($"|ProcessKiller.CreateResultsFromProcesses|Failed to kill process {p.ProcessName}", e);
}
}
private List<ProcessResult> GetProcesslist(string termToSearch)
{
var processlist = new List<ProcessResult>();
foreach (var p in Process.GetProcesses())
{
if (FilterSystemProcesses(p)) continue;
if (IsSystemProcess(p)) continue;
if (string.IsNullOrWhiteSpace(termToSearch))
{
@ -145,14 +174,10 @@ namespace Flow.Launcher.Plugin.ProcessKiller
}
return processlist;
bool FilterSystemProcesses(Process p)
{
var name = p.ProcessName.ToLower();
return _systemProcessList.Contains(name);
}
}
private bool IsSystemProcess(Process p) => _systemProcessList.Contains(p.ProcessName.ToLower());
internal class ProcessResult
{
public ProcessResult(Process process, int score)