13
13
using Wox . Helper ;
14
14
using Wox . Infrastructure ;
15
15
using Wox . Infrastructure . Hotkey ;
16
- using Wox . Infrastructure . Image ;
17
16
using Wox . Infrastructure . Storage ;
18
17
using Wox . Infrastructure . UserSettings ;
19
18
using Wox . Plugin ;
@@ -25,7 +24,7 @@ public class MainViewModel : BaseModel, ISavable
25
24
{
26
25
#region Private Fields
27
26
28
- private bool _queryHasReturn ;
27
+ private bool _isQueryRunning ;
29
28
private Query _lastQuery ;
30
29
private string _queryTextBeforeLeaveResults ;
31
30
@@ -41,7 +40,7 @@ public class MainViewModel : BaseModel, ISavable
41
40
private CancellationToken _updateToken ;
42
41
private bool _saved ;
43
42
44
- private Internationalization _translator = InternationalizationManager . Instance ;
43
+ private readonly Internationalization _translator = InternationalizationManager . Instance ;
45
44
46
45
#endregion
47
46
@@ -312,7 +311,7 @@ private void QueryContextMenu()
312
311
{
313
312
var filtered = results . Where
314
313
(
315
- r => StringMatcher . FuzzySearch ( query , r . Title ) . IsSearchPrecisionScoreMet ( )
314
+ r => StringMatcher . FuzzySearch ( query , r . Title ) . IsSearchPrecisionScoreMet ( )
316
315
|| StringMatcher . FuzzySearch ( query , r . SubTitle ) . IsSearchPrecisionScoreMet ( )
317
316
) . ToList ( ) ;
318
317
ContextMenu . AddResults ( filtered , id ) ;
@@ -371,63 +370,59 @@ private void QueryResults()
371
370
if ( ! string . IsNullOrEmpty ( QueryText ) )
372
371
{
373
372
_updateSource ? . Cancel ( ) ;
374
- _updateSource = new CancellationTokenSource ( ) ;
375
- _updateToken = _updateSource . Token ;
373
+ var currentUpdateSource = new CancellationTokenSource ( ) ;
374
+ _updateSource = currentUpdateSource ;
375
+ var currentCancellationToken = _updateSource . Token ;
376
+ _updateToken = currentCancellationToken ;
376
377
377
378
ProgressBarVisibility = Visibility . Hidden ;
378
- _queryHasReturn = false ;
379
+ _isQueryRunning = true ;
379
380
var query = PluginManager . QueryInit ( QueryText . Trim ( ) ) ;
380
381
if ( query != null )
381
382
{
382
383
// handle the exclusiveness of plugin using action keyword
383
- string lastKeyword = _lastQuery . ActionKeyword ;
384
- string keyword = query . ActionKeyword ;
385
- if ( string . IsNullOrEmpty ( lastKeyword ) )
386
- {
387
- if ( ! string . IsNullOrEmpty ( keyword ) )
388
- {
389
- Results . RemoveResultsExcept ( PluginManager . NonGlobalPlugins [ keyword ] . Metadata ) ;
390
- }
391
- }
392
- else
393
- {
394
- if ( string . IsNullOrEmpty ( keyword ) )
395
- {
396
- Results . RemoveResultsFor ( PluginManager . NonGlobalPlugins [ lastKeyword ] . Metadata ) ;
397
- }
398
- else if ( lastKeyword != keyword )
399
- {
400
- Results . RemoveResultsExcept ( PluginManager . NonGlobalPlugins [ keyword ] . Metadata ) ;
401
- }
402
- }
384
+ RemoveOldQueryResults ( query ) ;
403
385
404
386
_lastQuery = query ;
405
- Task . Delay ( 200 , _updateToken ) . ContinueWith ( _ =>
406
- {
407
- if ( query . RawQuery == _lastQuery . RawQuery && ! _queryHasReturn )
387
+ Task . Delay ( 200 , currentCancellationToken ) . ContinueWith ( _ =>
388
+ { // start the progress bar if query takes more than 200 ms and this is the current running query and it didn't finish yet
389
+ if ( currentUpdateSource == _updateSource && _isQueryRunning )
408
390
{
409
391
ProgressBarVisibility = Visibility . Visible ;
410
392
}
411
- } , _updateToken ) ;
393
+ } , currentCancellationToken ) ;
412
394
413
395
var plugins = PluginManager . ValidPluginsForQuery ( query ) ;
414
396
Task . Run ( ( ) =>
415
397
{
416
- Parallel . ForEach ( plugins , plugin =>
398
+ // so looping will stop once it was cancelled
399
+ var parallelOptions = new ParallelOptions { CancellationToken = currentCancellationToken } ;
400
+ try
417
401
{
418
- var config = _settings . PluginSettings . Plugins [ plugin . Metadata . ID ] ;
419
- if ( ! config . Disabled )
402
+ Parallel . ForEach ( plugins , parallelOptions , plugin =>
420
403
{
421
- var results = PluginManager . QueryForPlugin ( plugin , query ) ;
422
- UpdateResultView ( results , plugin . Metadata , query ) ;
423
- }
424
- } ) ;
404
+ var config = _settings . PluginSettings . Plugins [ plugin . Metadata . ID ] ;
405
+ if ( ! config . Disabled )
406
+ {
407
+ var results = PluginManager . QueryForPlugin ( plugin , query ) ;
408
+ UpdateResultView ( results , plugin . Metadata , query ) ;
409
+ }
410
+ } ) ;
411
+ }
412
+ catch ( OperationCanceledException )
413
+ {
414
+ // nothing to do here
415
+ }
416
+
425
417
426
418
// this should happen once after all queries are done so progress bar should continue
427
419
// until the end of all querying
428
- _queryHasReturn = true ;
429
- ProgressBarVisibility = Visibility . Hidden ;
430
- } , _updateToken ) ;
420
+ _isQueryRunning = false ;
421
+ if ( currentUpdateSource == _updateSource )
422
+ { // update to hidden if this is still the current query
423
+ ProgressBarVisibility = Visibility . Hidden ;
424
+ }
425
+ } , currentCancellationToken ) ;
431
426
}
432
427
}
433
428
else
@@ -437,6 +432,30 @@ private void QueryResults()
437
432
}
438
433
}
439
434
435
+ private void RemoveOldQueryResults ( Query query )
436
+ {
437
+ string lastKeyword = _lastQuery . ActionKeyword ;
438
+ string keyword = query . ActionKeyword ;
439
+ if ( string . IsNullOrEmpty ( lastKeyword ) )
440
+ {
441
+ if ( ! string . IsNullOrEmpty ( keyword ) )
442
+ {
443
+ Results . RemoveResultsExcept ( PluginManager . NonGlobalPlugins [ keyword ] . Metadata ) ;
444
+ }
445
+ }
446
+ else
447
+ {
448
+ if ( string . IsNullOrEmpty ( keyword ) )
449
+ {
450
+ Results . RemoveResultsFor ( PluginManager . NonGlobalPlugins [ lastKeyword ] . Metadata ) ;
451
+ }
452
+ else if ( lastKeyword != keyword )
453
+ {
454
+ Results . RemoveResultsExcept ( PluginManager . NonGlobalPlugins [ keyword ] . Metadata ) ;
455
+ }
456
+ }
457
+ }
458
+
440
459
441
460
private Result ContextMenuTopMost ( Result result )
442
461
{
@@ -660,4 +679,4 @@ public void UpdateResultView(List<Result> list, PluginMetadata metadata, Query o
660
679
661
680
#endregion
662
681
}
663
- }
682
+ }
0 commit comments