Skip to content

Commit 6e6a74b

Browse files
committed
ProcessKiller: add context menu option
to kill all processes whose file path matches with the selected result.
1 parent 10ce003 commit 6e6a74b

File tree

2 files changed

+44
-18
lines changed

2 files changed

+44
-18
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@
66
<system:String x:Key="flowlauncher_plugin_processkiller_plugin_description">Kill running processes from Flow Launcher</system:String>
77

88
<system:String x:Key="flowlauncher_plugin_processkiller_kill_all">kill all "{0}" processes</system:String>
9+
<system:String x:Key="flowlauncher_plugin_processkiller_kill_instances">kill all instances</system:String>
910

1011
</ResourceDictionary>

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

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
namespace Flow.Launcher.Plugin.ProcessKiller
1212
{
13-
public class Main : IPlugin, IPluginI18n
13+
public class Main : IPlugin, IPluginI18n, IContextMenu
1414
{
1515
private readonly HashSet<string> _systemProcessList = new HashSet<string>(){
1616
"conhost",
@@ -58,6 +58,34 @@ public string GetTranslatedPluginDescription()
5858
return _context.API.GetTranslation("flowlauncher_plugin_processkiller_plugin_description");
5959
}
6060

61+
public List<Result> LoadContextMenus(Result result)
62+
{
63+
var menuOptions = new List<Result>();
64+
var processPath = result.SubTitle;
65+
66+
// get all non-system processes whose file path matches that of the given result (processPath)
67+
var similarProcesses = Process.GetProcesses()
68+
.Where(p => !IsSystemProcess(p) && GetProcessFilename(p) == processPath);
69+
70+
menuOptions.Add(new Result
71+
{
72+
Title = _context.API.GetTranslation("flowlauncher_plugin_processkiller_kill_instances"),
73+
SubTitle = processPath,
74+
Action = _ =>
75+
{
76+
foreach (var p in similarProcesses)
77+
{
78+
KillProcess(p);
79+
}
80+
81+
return true;
82+
},
83+
IcoPath = "Images/app.png"
84+
});
85+
86+
return menuOptions;
87+
}
88+
6189
private List<Result> CreateResultsFromProcesses(List<ProcessResult> processlist, string termToSearch)
6290
{
6391
var results = new List<Result>();
@@ -105,29 +133,30 @@ private List<Result> CreateResultsFromProcesses(List<ProcessResult> processlist,
105133
}
106134

107135
return results;
136+
}
108137

109-
void KillProcess(Process p)
138+
private void KillProcess(Process p)
139+
{
140+
try
110141
{
111-
try
142+
if (!p.HasExited)
112143
{
113-
if (!p.HasExited)
114-
{
115-
p.Kill();
116-
}
117-
}
118-
catch (Exception e)
119-
{
120-
Log.Exception($"|ProcessKiller.CreateResultsFromProcesses|Failed to kill process {p.ProcessName}", e);
144+
p.Kill();
121145
}
122146
}
147+
catch (Exception e)
148+
{
149+
Log.Exception($"|ProcessKiller.CreateResultsFromProcesses|Failed to kill process {p.ProcessName}", e);
150+
}
123151
}
152+
124153
private List<ProcessResult> GetProcesslist(string termToSearch)
125154
{
126155
var processlist = new List<ProcessResult>();
127156

128157
foreach (var p in Process.GetProcesses())
129158
{
130-
if (FilterSystemProcesses(p)) continue;
159+
if (IsSystemProcess(p)) continue;
131160

132161
if (string.IsNullOrWhiteSpace(termToSearch))
133162
{
@@ -145,14 +174,10 @@ private List<ProcessResult> GetProcesslist(string termToSearch)
145174
}
146175

147176
return processlist;
148-
149-
bool FilterSystemProcesses(Process p)
150-
{
151-
var name = p.ProcessName.ToLower();
152-
return _systemProcessList.Contains(name);
153-
}
154177
}
155178

179+
private bool IsSystemProcess(Process p) => _systemProcessList.Contains(p.ProcessName.ToLower());
180+
156181
internal class ProcessResult
157182
{
158183
public ProcessResult(Process process, int score)

0 commit comments

Comments
 (0)