Skip to content

Commit 1258c44

Browse files
authored
Merge pull request #104 from theClueless/queryPluginsUpdates
Querypluginsupdates
2 parents 4cf3cff + 3434259 commit 1258c44

File tree

3 files changed

+70
-47
lines changed

3 files changed

+70
-47
lines changed

Wox/MainWindow.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,4 @@
8787
</ContentControl>
8888
</StackPanel>
8989
</Border>
90-
</Window>
90+
</Window>

Wox/ViewModel/MainViewModel.cs

Lines changed: 61 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
using Wox.Helper;
1414
using Wox.Infrastructure;
1515
using Wox.Infrastructure.Hotkey;
16-
using Wox.Infrastructure.Image;
1716
using Wox.Infrastructure.Storage;
1817
using Wox.Infrastructure.UserSettings;
1918
using Wox.Plugin;
@@ -25,7 +24,7 @@ public class MainViewModel : BaseModel, ISavable
2524
{
2625
#region Private Fields
2726

28-
private bool _queryHasReturn;
27+
private bool _isQueryRunning;
2928
private Query _lastQuery;
3029
private string _queryTextBeforeLeaveResults;
3130

@@ -41,7 +40,7 @@ public class MainViewModel : BaseModel, ISavable
4140
private CancellationToken _updateToken;
4241
private bool _saved;
4342

44-
private Internationalization _translator = InternationalizationManager.Instance;
43+
private readonly Internationalization _translator = InternationalizationManager.Instance;
4544

4645
#endregion
4746

@@ -312,7 +311,7 @@ private void QueryContextMenu()
312311
{
313312
var filtered = results.Where
314313
(
315-
r => StringMatcher.FuzzySearch(query, r.Title).IsSearchPrecisionScoreMet()
314+
r => StringMatcher.FuzzySearch(query, r.Title).IsSearchPrecisionScoreMet()
316315
|| StringMatcher.FuzzySearch(query, r.SubTitle).IsSearchPrecisionScoreMet()
317316
).ToList();
318317
ContextMenu.AddResults(filtered, id);
@@ -371,63 +370,59 @@ private void QueryResults()
371370
if (!string.IsNullOrEmpty(QueryText))
372371
{
373372
_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;
376377

377378
ProgressBarVisibility = Visibility.Hidden;
378-
_queryHasReturn = false;
379+
_isQueryRunning = true;
379380
var query = PluginManager.QueryInit(QueryText.Trim());
380381
if (query != null)
381382
{
382383
// 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);
403385

404386
_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)
408390
{
409391
ProgressBarVisibility = Visibility.Visible;
410392
}
411-
}, _updateToken);
393+
}, currentCancellationToken);
412394

413395
var plugins = PluginManager.ValidPluginsForQuery(query);
414396
Task.Run(() =>
415397
{
416-
Parallel.ForEach(plugins, plugin =>
398+
// so looping will stop once it was cancelled
399+
var parallelOptions = new ParallelOptions { CancellationToken = currentCancellationToken };
400+
try
417401
{
418-
var config = _settings.PluginSettings.Plugins[plugin.Metadata.ID];
419-
if (!config.Disabled)
402+
Parallel.ForEach(plugins, parallelOptions, plugin =>
420403
{
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+
425417

426418
// this should happen once after all queries are done so progress bar should continue
427419
// 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);
431426
}
432427
}
433428
else
@@ -437,6 +432,30 @@ private void QueryResults()
437432
}
438433
}
439434

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+
440459

441460
private Result ContextMenuTopMost(Result result)
442461
{
@@ -660,4 +679,4 @@ public void UpdateResultView(List<Result> list, PluginMetadata metadata, Query o
660679

661680
#endregion
662681
}
663-
}
682+
}

Wox/ViewModel/ResultsViewModel.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,14 +156,14 @@ public void AddResults(List<Result> newRawResults, string resultId)
156156
private List<ResultViewModel> NewResults(List<Result> newRawResults, string resultId)
157157
{
158158
var results = Results.ToList();
159-
var newResults = newRawResults.Select(r => new ResultViewModel(r)).ToList();
159+
var newResults = newRawResults.Select(r => new ResultViewModel(r)).ToList();
160160
var oldResults = results.Where(r => r.Result.PluginID == resultId).ToList();
161161

162162
// Find the same results in A (old results) and B (new newResults)
163163
var sameResults = oldResults
164164
.Where(t1 => newResults.Any(x => x.Result.Equals(t1.Result)))
165165
.ToList();
166-
166+
167167
// remove result of relative complement of B in A
168168
foreach (var result in oldResults.Except(sameResults))
169169
{
@@ -248,6 +248,10 @@ public void RemoveAll(Predicate<ResultViewModel> predicate)
248248
}
249249
}
250250

251+
/// <summary>
252+
/// Update the results collection with new results, try to keep identical results
253+
/// </summary>
254+
/// <param name="newItems"></param>
251255
public void Update(List<ResultViewModel> newItems)
252256
{
253257
int newCount = newItems.Count;
@@ -259,7 +263,7 @@ public void Update(List<ResultViewModel> newItems)
259263
ResultViewModel oldResult = this[i];
260264
ResultViewModel newResult = newItems[i];
261265
if (!oldResult.Equals(newResult))
262-
{
266+
{ // result is not the same update it in the current index
263267
this[i] = newResult;
264268
}
265269
else if (oldResult.Result.Score != newResult.Result.Score)
@@ -286,4 +290,4 @@ public void Update(List<ResultViewModel> newItems)
286290
}
287291
}
288292
}
289-
}
293+
}

0 commit comments

Comments
 (0)