1
- using System . Collections . Generic ;
1
+ using System ;
2
+ using System . Collections . Generic ;
2
3
using System . Linq ;
3
- using Flow . Launcher . Infrastructure ;
4
4
5
5
namespace Flow . Launcher . Plugin . ProcessKiller
6
6
{
@@ -48,7 +48,7 @@ public List<Result> LoadContextMenus(Result result)
48
48
{
49
49
foreach ( var p in similarProcesses )
50
50
{
51
- processHelper . TryKill ( p ) ;
51
+ processHelper . TryKill ( _context , p ) ;
52
52
}
53
53
54
54
return true ;
@@ -60,73 +60,113 @@ public List<Result> LoadContextMenus(Result result)
60
60
return menuOptions ;
61
61
}
62
62
63
- private record RunningProcessInfo ( string ProcessName , string MainWindowTitle ) ;
64
-
65
63
private List < Result > CreateResultsFromQuery ( Query query )
66
64
{
67
- var searchTerm = query . Search ;
68
- var processWindowTitle = ProcessHelper . GetProcessesWithNonEmptyWindowTitle ( ) ;
69
- var processList = processHelper . GetMatchingProcesses ( searchTerm , processWindowTitle ) ;
70
-
71
- if ( ! processList . Any ( ) )
65
+ // Get all non-system processes
66
+ var allPocessList = processHelper . GetMatchingProcesses ( ) ;
67
+ if ( ! allPocessList . Any ( ) )
72
68
{
73
69
return null ;
74
70
}
75
71
76
- var results = new List < Result > ( ) ;
77
- foreach ( var pr in processList )
72
+ // Filter processes based on search term
73
+ var searchTerm = query . Search ;
74
+ var processlist = new List < ProcessResult > ( ) ;
75
+ var processWindowTitle = ProcessHelper . GetProcessesWithNonEmptyWindowTitle ( ) ;
76
+ if ( string . IsNullOrWhiteSpace ( searchTerm ) )
78
77
{
79
- var p = pr . Process ;
80
- 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 ) )
78
+ foreach ( var p in allPocessList )
84
79
{
85
- title = mainWindowTitle ;
86
- if ( string . IsNullOrWhiteSpace ( searchTerm ) )
80
+ var progressNameIdTitle = ProcessHelper . GetProcessNameIdTitle ( p ) ;
81
+
82
+ if ( processWindowTitle . TryGetValue ( p . Id , out var windowTitle ) )
87
83
{
88
84
// Add score to prioritize processes with visible windows
89
- score += 200 ;
85
+ // And use window title for those processes
86
+ processlist . Add ( new ProcessResult ( p , 200 , windowTitle , null , progressNameIdTitle ) ) ;
87
+ }
88
+ else
89
+ {
90
+ processlist . Add ( new ProcessResult ( p , 0 , progressNameIdTitle , null , progressNameIdTitle ) ) ;
90
91
}
91
92
}
93
+ }
94
+ else
95
+ {
96
+ foreach ( var p in allPocessList )
97
+ {
98
+ var progressNameIdTitle = ProcessHelper . GetProcessNameIdTitle ( p ) ;
99
+
100
+ if ( processWindowTitle . TryGetValue ( p . Id , out var windowTitle ) )
101
+ {
102
+ // Get max score from searching process name, window title and process id
103
+ var windowTitleMatch = _context . API . FuzzySearch ( searchTerm , windowTitle ) ;
104
+ var processNameIdMatch = _context . API . FuzzySearch ( searchTerm , progressNameIdTitle ) ;
105
+ var score = Math . Max ( windowTitleMatch . Score , processNameIdMatch . Score ) ;
106
+ if ( score > 0 )
107
+ {
108
+ // Add score to prioritize processes with visible windows
109
+ // And use window title for those processes
110
+ score += 200 ;
111
+ processlist . Add ( new ProcessResult ( p , score , windowTitle ,
112
+ score == windowTitleMatch . Score ? windowTitleMatch : null , progressNameIdTitle ) ) ;
113
+ }
114
+ }
115
+ else
116
+ {
117
+ var processNameIdMatch = _context . API . FuzzySearch ( searchTerm , progressNameIdTitle ) ;
118
+ var score = processNameIdMatch . Score ;
119
+ if ( score > 0 )
120
+ {
121
+ processlist . Add ( new ProcessResult ( p , score , progressNameIdTitle , processNameIdMatch , progressNameIdTitle ) ) ;
122
+ }
123
+ }
124
+ }
125
+ }
126
+
127
+ var results = new List < Result > ( ) ;
128
+ foreach ( var pr in processlist )
129
+ {
130
+ var p = pr . Process ;
131
+ var path = processHelper . TryGetProcessFilename ( p ) ;
92
132
results . Add ( new Result ( )
93
133
{
94
134
IcoPath = path ,
95
- Title = title ,
135
+ Title = pr . Title ,
136
+ TitleToolTip = pr . Tooltip ,
96
137
SubTitle = path ,
97
- TitleHighlightData = StringMatcher . FuzzySearch ( searchTerm , p . ProcessName ) . MatchData ,
98
- Score = score ,
99
- ContextData = new RunningProcessInfo ( p . ProcessName , mainWindowTitle ) ,
138
+ TitleHighlightData = pr . TitleMatch ? . MatchData ,
139
+ Score = pr . Score ,
140
+ ContextData = p . ProcessName ,
100
141
AutoCompleteText = $ "{ _context . CurrentPluginMetadata . ActionKeyword } { Plugin . Query . TermSeparator } { p . ProcessName } ",
101
142
Action = ( c ) =>
102
143
{
103
- processHelper . TryKill ( p ) ;
144
+ processHelper . TryKill ( _context , p ) ;
104
145
_context . API . ReQuery ( ) ;
105
146
return false ;
106
147
}
107
148
} ) ;
108
149
}
109
150
110
- var sortedResults = results
111
- . OrderBy ( x => x . Title )
112
- . ToList ( ) ;
151
+ // Order results by process name for processes without visible windows
152
+ var sortedResults = results . OrderBy ( x => x . Title ) . ToList ( ) ;
113
153
114
154
// When there are multiple results AND all of them are instances of the same executable
115
155
// add a quick option to kill them all at the top of the results.
116
156
var firstResult = sortedResults . FirstOrDefault ( x => ! string . IsNullOrEmpty ( x . SubTitle ) ) ;
117
- if ( processList . Count > 1 && ! string . IsNullOrEmpty ( searchTerm ) && sortedResults . All ( r => r . SubTitle == firstResult ? . SubTitle ) )
157
+ if ( processlist . Count > 1 && ! string . IsNullOrEmpty ( searchTerm ) && sortedResults . All ( r => r . SubTitle == firstResult ? . SubTitle ) )
118
158
{
119
159
sortedResults . Insert ( 1 , new Result ( )
120
160
{
121
161
IcoPath = firstResult ? . IcoPath ,
122
- Title = string . Format ( _context . API . GetTranslation ( "flowlauncher_plugin_processkiller_kill_all" ) , ( ( RunningProcessInfo ) firstResult ? . ContextData ) . ProcessName ) ,
123
- SubTitle = string . Format ( _context . API . GetTranslation ( "flowlauncher_plugin_processkiller_kill_all_count" ) , processList . Count ) ,
162
+ Title = string . Format ( _context . API . GetTranslation ( "flowlauncher_plugin_processkiller_kill_all" ) , firstResult ? . ContextData ) ,
163
+ SubTitle = string . Format ( _context . API . GetTranslation ( "flowlauncher_plugin_processkiller_kill_all_count" ) , processlist . Count ) ,
124
164
Score = 200 ,
125
165
Action = ( c ) =>
126
166
{
127
- foreach ( var p in processList )
167
+ foreach ( var p in processlist )
128
168
{
129
- processHelper . TryKill ( p . Process ) ;
169
+ processHelper . TryKill ( _context , p . Process ) ;
130
170
}
131
171
_context . API . ReQuery ( ) ;
132
172
return false ;
0 commit comments