From dde03eca7cf633c1f0a1f6df4dc5a2318b9ffe30 Mon Sep 17 00:00:00 2001 From: AT <14300910+theClueless@users.noreply.github.com> Date: Fri, 13 Dec 2019 01:48:05 +0200 Subject: [PATCH 1/6] started update with cancellation token --- Wox/ViewModel/MainViewModel.cs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/Wox/ViewModel/MainViewModel.cs b/Wox/ViewModel/MainViewModel.cs index a7e17d9a7..78ccc54d9 100644 --- a/Wox/ViewModel/MainViewModel.cs +++ b/Wox/ViewModel/MainViewModel.cs @@ -372,7 +372,7 @@ namespace Wox.ViewModel { _updateSource?.Cancel(); _updateSource = new CancellationTokenSource(); - _updateToken = _updateSource.Token; + var updateToken = _updateSource.Token; ProgressBarVisibility = Visibility.Hidden; _queryHasReturn = false; @@ -402,18 +402,19 @@ namespace Wox.ViewModel } _lastQuery = query; - Task.Delay(200, _updateToken).ContinueWith(_ => + Task.Delay(200, updateToken).ContinueWith(_ => { if (query.RawQuery == _lastQuery.RawQuery && !_queryHasReturn) { ProgressBarVisibility = Visibility.Visible; } - }, _updateToken); + }, updateToken); var plugins = PluginManager.ValidPluginsForQuery(query); Task.Run(() => { - Parallel.ForEach(plugins, plugin => + var parallelOptions = new ParallelOptions {CancellationToken = updateToken}; // so looping will stop once it was cancelled + Parallel.ForEach(plugins, parallelOptions, plugin => { var config = _settings.PluginSettings.Plugins[plugin.Metadata.ID]; if (!config.Disabled) @@ -421,13 +422,13 @@ namespace Wox.ViewModel var results = PluginManager.QueryForPlugin(plugin, query); UpdateResultView(results, plugin.Metadata, query); } - }); + });// TODO add cancel code. // this should happen once after all queries are done so progress bar should continue // until the end of all querying _queryHasReturn = true; ProgressBarVisibility = Visibility.Hidden; - }, _updateToken); + }, updateToken); } } else From ced0faf9164a11012b5f3276d3acf3416986589c Mon Sep 17 00:00:00 2001 From: AT <14300910+theClueless@users.noreply.github.com> Date: Sat, 14 Dec 2019 00:06:13 +0200 Subject: [PATCH 2/6] results and query update fixes --- Wox.Infrastructure/Image/ImageLoader.cs | 2 +- Wox/MainWindow.xaml | 2 +- Wox/ViewModel/MainViewModel.cs | 77 ++++++++++++++----------- Wox/ViewModel/ResultsViewModel.cs | 6 +- 4 files changed, 50 insertions(+), 37 deletions(-) diff --git a/Wox.Infrastructure/Image/ImageLoader.cs b/Wox.Infrastructure/Image/ImageLoader.cs index 3498e4f3b..184f78cad 100644 --- a/Wox.Infrastructure/Image/ImageLoader.cs +++ b/Wox.Infrastructure/Image/ImageLoader.cs @@ -133,7 +133,7 @@ namespace Wox.Infrastructure.Image } catch (System.Exception e) { - Log.Exception($"|ImageLoader.Load|Failed to get thumbnail for {path}", e); + // Log.Exception($"|ImageLoader.Load|Failed to get thumbnail for {path}", e); image = ImageCache[Constant.ErrorIcon]; ImageCache[path] = image; diff --git a/Wox/MainWindow.xaml b/Wox/MainWindow.xaml index 7dfe8cd1e..cc50e76dc 100644 --- a/Wox/MainWindow.xaml +++ b/Wox/MainWindow.xaml @@ -56,7 +56,7 @@ - { - if (query.RawQuery == _lastQuery.RawQuery && !_queryHasReturn) + Task.Delay(200, currentCancellationToken).ContinueWith(_ => + { // start the progress bar if query takes more than 200 ms and this is the current running query and it didn't finish yet + if (currentUpdateSource == _updateSource && _isQueryRunning) { ProgressBarVisibility = Visibility.Visible; } - }, updateToken); + }, currentCancellationToken); var plugins = PluginManager.ValidPluginsForQuery(query); Task.Run(() => { - var parallelOptions = new ParallelOptions {CancellationToken = updateToken}; // so looping will stop once it was cancelled + // so looping will stop once it was cancelled + var parallelOptions = new ParallelOptions {CancellationToken = currentCancellationToken}; Parallel.ForEach(plugins, parallelOptions, plugin => { var config = _settings.PluginSettings.Plugins[plugin.Metadata.ID]; @@ -422,13 +404,16 @@ namespace Wox.ViewModel var results = PluginManager.QueryForPlugin(plugin, query); UpdateResultView(results, plugin.Metadata, query); } - });// TODO add cancel code. + }); // this should happen once after all queries are done so progress bar should continue // until the end of all querying - _queryHasReturn = true; - ProgressBarVisibility = Visibility.Hidden; - }, updateToken); + _isQueryRunning = false; + if (currentUpdateSource == _updateSource) + { // update to hidden if this is still the current query + ProgressBarVisibility = Visibility.Hidden; + } + }, currentCancellationToken); } } else @@ -438,6 +423,30 @@ namespace Wox.ViewModel } } + private void RemoveOldQueryResults(Query query) + { + string lastKeyword = _lastQuery.ActionKeyword; + string keyword = query.ActionKeyword; + if (string.IsNullOrEmpty(lastKeyword)) + { + if (!string.IsNullOrEmpty(keyword)) + { + Results.RemoveResultsExcept(PluginManager.NonGlobalPlugins[keyword].Metadata); + } + } + else + { + if (string.IsNullOrEmpty(keyword)) + { + Results.RemoveResultsFor(PluginManager.NonGlobalPlugins[lastKeyword].Metadata); + } + else if (lastKeyword != keyword) + { + Results.RemoveResultsExcept(PluginManager.NonGlobalPlugins[keyword].Metadata); + } + } + } + private Result ContextMenuTopMost(Result result) { diff --git a/Wox/ViewModel/ResultsViewModel.cs b/Wox/ViewModel/ResultsViewModel.cs index e36ddc94d..45eb4bec2 100644 --- a/Wox/ViewModel/ResultsViewModel.cs +++ b/Wox/ViewModel/ResultsViewModel.cs @@ -248,6 +248,10 @@ namespace Wox.ViewModel } } + /// + /// Update the results collection with new results, try to keep identical results + /// + /// public void Update(List newItems) { int newCount = newItems.Count; @@ -259,7 +263,7 @@ namespace Wox.ViewModel ResultViewModel oldResult = this[i]; ResultViewModel newResult = newItems[i]; if (!oldResult.Equals(newResult)) - { + { // result is not the same update it in the current index this[i] = newResult; } else if (oldResult.Result.Score != newResult.Result.Score) From 3dfccea5229dc9943cca16760ce9b5ed011860c7 Mon Sep 17 00:00:00 2001 From: AT <14300910+theClueless@users.noreply.github.com> Date: Sat, 14 Dec 2019 00:07:27 +0200 Subject: [PATCH 3/6] Revert "results and query update fixes" This reverts commit ced0faf9164a11012b5f3276d3acf3416986589c. --- Wox.Infrastructure/Image/ImageLoader.cs | 2 +- Wox/MainWindow.xaml | 2 +- Wox/ViewModel/MainViewModel.cs | 77 +++++++++++-------------- Wox/ViewModel/ResultsViewModel.cs | 6 +- 4 files changed, 37 insertions(+), 50 deletions(-) diff --git a/Wox.Infrastructure/Image/ImageLoader.cs b/Wox.Infrastructure/Image/ImageLoader.cs index 184f78cad..3498e4f3b 100644 --- a/Wox.Infrastructure/Image/ImageLoader.cs +++ b/Wox.Infrastructure/Image/ImageLoader.cs @@ -133,7 +133,7 @@ namespace Wox.Infrastructure.Image } catch (System.Exception e) { - // Log.Exception($"|ImageLoader.Load|Failed to get thumbnail for {path}", e); + Log.Exception($"|ImageLoader.Load|Failed to get thumbnail for {path}", e); image = ImageCache[Constant.ErrorIcon]; ImageCache[path] = image; diff --git a/Wox/MainWindow.xaml b/Wox/MainWindow.xaml index cc50e76dc..7dfe8cd1e 100644 --- a/Wox/MainWindow.xaml +++ b/Wox/MainWindow.xaml @@ -56,7 +56,7 @@ - { // start the progress bar if query takes more than 200 ms and this is the current running query and it didn't finish yet - if (currentUpdateSource == _updateSource && _isQueryRunning) + Task.Delay(200, updateToken).ContinueWith(_ => + { + if (query.RawQuery == _lastQuery.RawQuery && !_queryHasReturn) { ProgressBarVisibility = Visibility.Visible; } - }, currentCancellationToken); + }, updateToken); var plugins = PluginManager.ValidPluginsForQuery(query); Task.Run(() => { - // so looping will stop once it was cancelled - var parallelOptions = new ParallelOptions {CancellationToken = currentCancellationToken}; + var parallelOptions = new ParallelOptions {CancellationToken = updateToken}; // so looping will stop once it was cancelled Parallel.ForEach(plugins, parallelOptions, plugin => { var config = _settings.PluginSettings.Plugins[plugin.Metadata.ID]; @@ -404,16 +422,13 @@ namespace Wox.ViewModel var results = PluginManager.QueryForPlugin(plugin, query); UpdateResultView(results, plugin.Metadata, query); } - }); + });// TODO add cancel code. // this should happen once after all queries are done so progress bar should continue // until the end of all querying - _isQueryRunning = false; - if (currentUpdateSource == _updateSource) - { // update to hidden if this is still the current query - ProgressBarVisibility = Visibility.Hidden; - } - }, currentCancellationToken); + _queryHasReturn = true; + ProgressBarVisibility = Visibility.Hidden; + }, updateToken); } } else @@ -423,30 +438,6 @@ namespace Wox.ViewModel } } - private void RemoveOldQueryResults(Query query) - { - string lastKeyword = _lastQuery.ActionKeyword; - string keyword = query.ActionKeyword; - if (string.IsNullOrEmpty(lastKeyword)) - { - if (!string.IsNullOrEmpty(keyword)) - { - Results.RemoveResultsExcept(PluginManager.NonGlobalPlugins[keyword].Metadata); - } - } - else - { - if (string.IsNullOrEmpty(keyword)) - { - Results.RemoveResultsFor(PluginManager.NonGlobalPlugins[lastKeyword].Metadata); - } - else if (lastKeyword != keyword) - { - Results.RemoveResultsExcept(PluginManager.NonGlobalPlugins[keyword].Metadata); - } - } - } - private Result ContextMenuTopMost(Result result) { diff --git a/Wox/ViewModel/ResultsViewModel.cs b/Wox/ViewModel/ResultsViewModel.cs index 45eb4bec2..e36ddc94d 100644 --- a/Wox/ViewModel/ResultsViewModel.cs +++ b/Wox/ViewModel/ResultsViewModel.cs @@ -248,10 +248,6 @@ namespace Wox.ViewModel } } - /// - /// Update the results collection with new results, try to keep identical results - /// - /// public void Update(List newItems) { int newCount = newItems.Count; @@ -263,7 +259,7 @@ namespace Wox.ViewModel ResultViewModel oldResult = this[i]; ResultViewModel newResult = newItems[i]; if (!oldResult.Equals(newResult)) - { // result is not the same update it in the current index + { this[i] = newResult; } else if (oldResult.Result.Score != newResult.Result.Score) From e6e1aab0984827f7ac92eda24ad3bdfd3b15287f Mon Sep 17 00:00:00 2001 From: AT <14300910+theClueless@users.noreply.github.com> Date: Sat, 14 Dec 2019 00:17:05 +0200 Subject: [PATCH 4/6] updates --- Wox/MainWindow.xaml | 2 +- Wox/ViewModel/MainViewModel.cs | 84 +++++++++++++++++-------------- Wox/ViewModel/ResultsViewModel.cs | 12 +++-- 3 files changed, 56 insertions(+), 42 deletions(-) diff --git a/Wox/MainWindow.xaml b/Wox/MainWindow.xaml index 7dfe8cd1e..cc50e76dc 100644 --- a/Wox/MainWindow.xaml +++ b/Wox/MainWindow.xaml @@ -56,7 +56,7 @@ StringMatcher.FuzzySearch(query, r.Title).IsSearchPrecisionScoreMet() + r => StringMatcher.FuzzySearch(query, r.Title).IsSearchPrecisionScoreMet() || StringMatcher.FuzzySearch(query, r.SubTitle).IsSearchPrecisionScoreMet() ).ToList(); ContextMenu.AddResults(filtered, id); @@ -371,49 +370,33 @@ namespace Wox.ViewModel if (!string.IsNullOrEmpty(QueryText)) { _updateSource?.Cancel(); - _updateSource = new CancellationTokenSource(); - var updateToken = _updateSource.Token; + var currentUpdateSource = new CancellationTokenSource(); + _updateSource = currentUpdateSource; + var currentCancellationToken = _updateSource.Token; + _updateToken = currentCancellationToken; ProgressBarVisibility = Visibility.Hidden; - _queryHasReturn = false; + _isQueryRunning = true; var query = PluginManager.QueryInit(QueryText.Trim()); if (query != null) { // handle the exclusiveness of plugin using action keyword - string lastKeyword = _lastQuery.ActionKeyword; - string keyword = query.ActionKeyword; - if (string.IsNullOrEmpty(lastKeyword)) - { - if (!string.IsNullOrEmpty(keyword)) - { - Results.RemoveResultsExcept(PluginManager.NonGlobalPlugins[keyword].Metadata); - } - } - else - { - if (string.IsNullOrEmpty(keyword)) - { - Results.RemoveResultsFor(PluginManager.NonGlobalPlugins[lastKeyword].Metadata); - } - else if (lastKeyword != keyword) - { - Results.RemoveResultsExcept(PluginManager.NonGlobalPlugins[keyword].Metadata); - } - } + RemoveOldQueryResults(query); _lastQuery = query; - Task.Delay(200, updateToken).ContinueWith(_ => - { - if (query.RawQuery == _lastQuery.RawQuery && !_queryHasReturn) + Task.Delay(200, currentCancellationToken).ContinueWith(_ => + { // start the progress bar if query takes more than 200 ms and this is the current running query and it didn't finish yet + if (currentUpdateSource == _updateSource && _isQueryRunning) { ProgressBarVisibility = Visibility.Visible; } - }, updateToken); + }, currentCancellationToken); var plugins = PluginManager.ValidPluginsForQuery(query); Task.Run(() => { - var parallelOptions = new ParallelOptions {CancellationToken = updateToken}; // so looping will stop once it was cancelled + // so looping will stop once it was cancelled + var parallelOptions = new ParallelOptions { CancellationToken = currentCancellationToken }; Parallel.ForEach(plugins, parallelOptions, plugin => { var config = _settings.PluginSettings.Plugins[plugin.Metadata.ID]; @@ -422,13 +405,16 @@ namespace Wox.ViewModel var results = PluginManager.QueryForPlugin(plugin, query); UpdateResultView(results, plugin.Metadata, query); } - });// TODO add cancel code. + }); // this should happen once after all queries are done so progress bar should continue // until the end of all querying - _queryHasReturn = true; - ProgressBarVisibility = Visibility.Hidden; - }, updateToken); + _isQueryRunning = false; + if (currentUpdateSource == _updateSource) + { // update to hidden if this is still the current query + ProgressBarVisibility = Visibility.Hidden; + } + }, currentCancellationToken); } } else @@ -438,6 +424,30 @@ namespace Wox.ViewModel } } + private void RemoveOldQueryResults(Query query) + { + string lastKeyword = _lastQuery.ActionKeyword; + string keyword = query.ActionKeyword; + if (string.IsNullOrEmpty(lastKeyword)) + { + if (!string.IsNullOrEmpty(keyword)) + { + Results.RemoveResultsExcept(PluginManager.NonGlobalPlugins[keyword].Metadata); + } + } + else + { + if (string.IsNullOrEmpty(keyword)) + { + Results.RemoveResultsFor(PluginManager.NonGlobalPlugins[lastKeyword].Metadata); + } + else if (lastKeyword != keyword) + { + Results.RemoveResultsExcept(PluginManager.NonGlobalPlugins[keyword].Metadata); + } + } + } + private Result ContextMenuTopMost(Result result) { @@ -661,4 +671,4 @@ namespace Wox.ViewModel #endregion } -} +} \ No newline at end of file diff --git a/Wox/ViewModel/ResultsViewModel.cs b/Wox/ViewModel/ResultsViewModel.cs index e36ddc94d..76a7ee75b 100644 --- a/Wox/ViewModel/ResultsViewModel.cs +++ b/Wox/ViewModel/ResultsViewModel.cs @@ -156,14 +156,14 @@ namespace Wox.ViewModel private List NewResults(List newRawResults, string resultId) { var results = Results.ToList(); - var newResults = newRawResults.Select(r => new ResultViewModel(r)).ToList(); + var newResults = newRawResults.Select(r => new ResultViewModel(r)).ToList(); var oldResults = results.Where(r => r.Result.PluginID == resultId).ToList(); // Find the same results in A (old results) and B (new newResults) var sameResults = oldResults .Where(t1 => newResults.Any(x => x.Result.Equals(t1.Result))) .ToList(); - + // remove result of relative complement of B in A foreach (var result in oldResults.Except(sameResults)) { @@ -248,6 +248,10 @@ namespace Wox.ViewModel } } + /// + /// Update the results collection with new results, try to keep identical results + /// + /// public void Update(List newItems) { int newCount = newItems.Count; @@ -259,7 +263,7 @@ namespace Wox.ViewModel ResultViewModel oldResult = this[i]; ResultViewModel newResult = newItems[i]; if (!oldResult.Equals(newResult)) - { + { // result is not the same update it in the current index this[i] = newResult; } else if (oldResult.Result.Score != newResult.Result.Score) @@ -286,4 +290,4 @@ namespace Wox.ViewModel } } } -} +} \ No newline at end of file From 4c2a09369d36d0df526c66d27acb0edd4243856f Mon Sep 17 00:00:00 2001 From: AT <14300910+theClueless@users.noreply.github.com> Date: Sat, 14 Dec 2019 00:41:29 +0200 Subject: [PATCH 5/6] added catch --- Wox/ViewModel/MainViewModel.cs | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/Wox/ViewModel/MainViewModel.cs b/Wox/ViewModel/MainViewModel.cs index bf8530df7..bd1314a27 100644 --- a/Wox/ViewModel/MainViewModel.cs +++ b/Wox/ViewModel/MainViewModel.cs @@ -397,15 +397,23 @@ namespace Wox.ViewModel { // so looping will stop once it was cancelled var parallelOptions = new ParallelOptions { CancellationToken = currentCancellationToken }; - Parallel.ForEach(plugins, parallelOptions, plugin => + try { - var config = _settings.PluginSettings.Plugins[plugin.Metadata.ID]; - if (!config.Disabled) + Parallel.ForEach(plugins, parallelOptions, plugin => { - var results = PluginManager.QueryForPlugin(plugin, query); - UpdateResultView(results, plugin.Metadata, query); - } - }); + var config = _settings.PluginSettings.Plugins[plugin.Metadata.ID]; + if (!config.Disabled) + { + var results = PluginManager.QueryForPlugin(plugin, query); + UpdateResultView(results, plugin.Metadata, query); + } + }); + } + catch (OperationCanceledException) + { + // nothing to do here + } + // this should happen once after all queries are done so progress bar should continue // until the end of all querying From 34342599b937be250a4791ce229a66e83cb817f2 Mon Sep 17 00:00:00 2001 From: theClueless <14300910+theClueless@users.noreply.github.com> Date: Mon, 30 Dec 2019 01:28:10 +0200 Subject: [PATCH 6/6] Update MainWindow.xaml removed delay in binding --- Wox/MainWindow.xaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Wox/MainWindow.xaml b/Wox/MainWindow.xaml index cc50e76dc..d50411b83 100644 --- a/Wox/MainWindow.xaml +++ b/Wox/MainWindow.xaml @@ -56,7 +56,7 @@ - \ No newline at end of file +