Skip to content

Commit 8ee2d48

Browse files
committed
Use score bump & Search window title
1 parent 6bc69b1 commit 8ee2d48

File tree

2 files changed

+36
-15
lines changed

2 files changed

+36
-15
lines changed

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

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,28 +64,38 @@ private record RunningProcessInfo(string ProcessName, string MainWindowTitle);
6464

6565
private List<Result> CreateResultsFromQuery(Query query)
6666
{
67-
string termToSearch = query.Search;
68-
var processList = processHelper.GetMatchingProcesses(termToSearch);
69-
var processWithNonEmptyMainWindowTitleList = ProcessHelper.GetProcessesWithNonEmptyWindowTitle();
67+
var searchTerm = query.Search;
68+
var processWindowTitle = ProcessHelper.GetProcessesWithNonEmptyWindowTitle();
69+
var processList = processHelper.GetMatchingProcesses(searchTerm, processWindowTitle);
7070

7171
if (!processList.Any())
7272
{
7373
return null;
7474
}
7575

7676
var results = new List<Result>();
77-
7877
foreach (var pr in processList)
7978
{
8079
var p = pr.Process;
8180
var path = processHelper.TryGetProcessFilename(p);
81+
var title = p.ProcessName + " - " + p.Id;
82+
var score = pr.Score;
83+
if (processWindowTitle.TryGetValue(p.Id, out var mainWindowTitle))
84+
{
85+
title = mainWindowTitle;
86+
if (string.IsNullOrWhiteSpace(searchTerm))
87+
{
88+
// Add score to prioritize processes with visible windows
89+
score += 200;
90+
}
91+
}
8292
results.Add(new Result()
8393
{
8494
IcoPath = path,
85-
Title = processWithNonEmptyMainWindowTitleList.TryGetValue(p.Id, out var mainWindowTitle) ? mainWindowTitle : p.ProcessName + " - " + p.Id,
95+
Title = title,
8696
SubTitle = path,
87-
TitleHighlightData = StringMatcher.FuzzySearch(termToSearch, p.ProcessName).MatchData,
88-
Score = pr.Score,
97+
TitleHighlightData = StringMatcher.FuzzySearch(searchTerm, p.ProcessName).MatchData,
98+
Score = score,
8999
ContextData = new RunningProcessInfo(p.ProcessName, mainWindowTitle),
90100
AutoCompleteText = $"{_context.CurrentPluginMetadata.ActionKeyword}{Plugin.Query.TermSeparator}{p.ProcessName}",
91101
Action = (c) =>
@@ -98,14 +108,13 @@ private List<Result> CreateResultsFromQuery(Query query)
98108
}
99109

100110
var sortedResults = results
101-
.OrderBy(x => string.IsNullOrEmpty(((RunningProcessInfo)x.ContextData).MainWindowTitle))
102-
.ThenBy(x => x.Title)
111+
.OrderBy(x => x.Title)
103112
.ToList();
104113

105114
// When there are multiple results AND all of them are instances of the same executable
106115
// add a quick option to kill them all at the top of the results.
107116
var firstResult = sortedResults.FirstOrDefault(x => !string.IsNullOrEmpty(x.SubTitle));
108-
if (processList.Count > 1 && !string.IsNullOrEmpty(termToSearch) && sortedResults.All(r => r.SubTitle == firstResult?.SubTitle))
117+
if (processList.Count > 1 && !string.IsNullOrEmpty(searchTerm) && sortedResults.All(r => r.SubTitle == firstResult?.SubTitle))
109118
{
110119
sortedResults.Insert(1, new Result()
111120
{

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

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ private bool IsSystemProcess(Process p) => _systemProcessList.Contains(p.Process
3939
/// <summary>
4040
/// Returns a ProcessResult for evey running non-system process whose name matches the given searchTerm
4141
/// </summary>
42-
public List<ProcessResult> GetMatchingProcesses(string searchTerm)
42+
public List<ProcessResult> GetMatchingProcesses(string searchTerm, Dictionary<int, string> processWindowTitle)
4343
{
4444
var processlist = new List<ProcessResult>();
4545

@@ -49,15 +49,27 @@ public List<ProcessResult> GetMatchingProcesses(string searchTerm)
4949

5050
if (string.IsNullOrWhiteSpace(searchTerm))
5151
{
52-
// show all non-system processes
52+
// Show all non-system processes
5353
processlist.Add(new ProcessResult(p, 0));
5454
}
5555
else
5656
{
57-
var score = StringMatcher.FuzzySearch(searchTerm, p.ProcessName + p.Id).Score;
58-
if (score > 0)
57+
// Search window title first
58+
if (processWindowTitle.TryGetValue(p.Id, out var windowTitle))
5959
{
60-
processlist.Add(new ProcessResult(p, score));
60+
var score = StringMatcher.FuzzySearch(searchTerm, windowTitle).Score;
61+
if (score > 0)
62+
{
63+
processlist.Add(new ProcessResult(p, score));
64+
}
65+
}
66+
67+
// Search process name and process id
68+
var score1 = StringMatcher.FuzzySearch(searchTerm, p.ProcessName + " - " + p.Id).Score;
69+
if (score1 > 0)
70+
{
71+
processlist.Add(new ProcessResult(p, score1));
72+
continue;
6173
}
6274
}
6375
}

0 commit comments

Comments
 (0)