1- using System . Collections . Generic ;
1+ using System ;
2+ using System . Collections . Generic ;
23using System . Linq ;
3- using Flow . Launcher . Infrastructure ;
44
55namespace Flow . Launcher . Plugin . ProcessKiller
66{
@@ -48,7 +48,7 @@ public List<Result> LoadContextMenus(Result result)
4848 {
4949 foreach ( var p in similarProcesses )
5050 {
51- processHelper . TryKill ( p ) ;
51+ processHelper . TryKill ( _context , p ) ;
5252 }
5353
5454 return true ;
@@ -60,73 +60,113 @@ public List<Result> LoadContextMenus(Result result)
6060 return menuOptions ;
6161 }
6262
63- private record RunningProcessInfo ( string ProcessName , string MainWindowTitle ) ;
64-
6563 private List < Result > CreateResultsFromQuery ( Query query )
6664 {
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 ( ) )
7268 {
7369 return null ;
7470 }
7571
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 ) )
7877 {
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 )
8479 {
85- title = mainWindowTitle ;
86- if ( string . IsNullOrWhiteSpace ( searchTerm ) )
80+ var progressNameIdTitle = ProcessHelper . GetProcessNameIdTitle ( p ) ;
81+
82+ if ( processWindowTitle . TryGetValue ( p . Id , out var windowTitle ) )
8783 {
8884 // 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 ) ) ;
9091 }
9192 }
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 ) ;
92132 results . Add ( new Result ( )
93133 {
94134 IcoPath = path ,
95- Title = title ,
135+ Title = pr . Title ,
136+ TitleToolTip = pr . Tooltip ,
96137 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 ,
100141 AutoCompleteText = $ "{ _context . CurrentPluginMetadata . ActionKeyword } { Plugin . Query . TermSeparator } { p . ProcessName } ",
101142 Action = ( c ) =>
102143 {
103- processHelper . TryKill ( p ) ;
144+ processHelper . TryKill ( _context , p ) ;
104145 _context . API . ReQuery ( ) ;
105146 return false ;
106147 }
107148 } ) ;
108149 }
109150
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 ( ) ;
113153
114154 // When there are multiple results AND all of them are instances of the same executable
115155 // add a quick option to kill them all at the top of the results.
116156 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 ) )
118158 {
119159 sortedResults . Insert ( 1 , new Result ( )
120160 {
121161 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 ) ,
124164 Score = 200 ,
125165 Action = ( c ) =>
126166 {
127- foreach ( var p in processList )
167+ foreach ( var p in processlist )
128168 {
129- processHelper . TryKill ( p . Process ) ;
169+ processHelper . TryKill ( _context , p . Process ) ;
130170 }
131171 _context . API . ReQuery ( ) ;
132172 return false ;
0 commit comments