From 7d52b0cc962ead79e24dafab88655487009c6387 Mon Sep 17 00:00:00 2001 From: bao-qian Date: Sat, 7 Nov 2015 03:50:26 +0000 Subject: [PATCH] Fix exclusive for action keyword --- Wox/Helper/ListBoxItems.cs | 29 ++++++++++++++++++++++++ Wox/MainWindow.xaml.cs | 45 ++++++++++++++++++++++++++++---------- Wox/ResultPanel.xaml.cs | 32 +++++++++++++++++---------- Wox/Wox.csproj | 5 ++++- 4 files changed, 86 insertions(+), 25 deletions(-) create mode 100644 Wox/Helper/ListBoxItems.cs diff --git a/Wox/Helper/ListBoxItems.cs b/Wox/Helper/ListBoxItems.cs new file mode 100644 index 0000000000..2a7f880bad --- /dev/null +++ b/Wox/Helper/ListBoxItems.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Collections.Specialized; +using System.ComponentModel; +using System.Linq; +using Wox.Plugin; + +namespace Wox.Helper +{ + class ListBoxItems : ObservableCollection + { + public void RemoveAll(Predicate predicate) + { + CheckReentrancy(); + + List itemsToRemove = Items.Where(x => predicate(x)).ToList(); + if (itemsToRemove.Count > 0) + { + itemsToRemove.ForEach(item => Items.Remove(item)); + + OnPropertyChanged(new PropertyChangedEventArgs("Count")); + OnPropertyChanged(new PropertyChangedEventArgs("Item[]")); + // fuck ms http://blogs.msdn.com/b/nathannesbit/archive/2009/04/20/addrange-and-observablecollection.aspx + OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset)); + } + } + } +} diff --git a/Wox/MainWindow.xaml.cs b/Wox/MainWindow.xaml.cs index 13481cd019..61d56a032b 100644 --- a/Wox/MainWindow.xaml.cs +++ b/Wox/MainWindow.xaml.cs @@ -43,11 +43,11 @@ namespace Wox private readonly Storyboard progressBarStoryboard = new Storyboard(); private NotifyIcon notifyIcon; - private bool queryHasReturn; - private string lastQuery; + private bool _queryHasReturn; + private Query _lastQuery = new Query(); private ToolTip toolTip = new ToolTip(); - private bool ignoreTextChange = false; + private bool _ignoreTextChange = false; private List CurrentContextMenus = new List(); private string textBeforeEnterContextMenuMode; @@ -72,7 +72,7 @@ namespace Wox { Dispatcher.Invoke(new Action(() => { - ignoreTextChange = true; + _ignoreTextChange = true; tbQuery.Text = query; tbQuery.CaretIndex = tbQuery.Text.Length; if (selectAll) @@ -441,10 +441,10 @@ namespace Wox private void TbQuery_OnTextChanged(object sender, TextChangedEventArgs e) { - - if (ignoreTextChange) { ignoreTextChange = false; return; } - if (!string.IsNullOrEmpty(tbQuery.Text.Trim())) + if (_ignoreTextChange) { _ignoreTextChange = false; return; } + string query = tbQuery.Text.Trim(); + if (!string.IsNullOrEmpty(query)) { toolTip.IsOpen = false; if (IsInContextMenuMode) @@ -453,10 +453,10 @@ namespace Wox return; } - Query(tbQuery.Text); + Query(query); Dispatcher.DelayInvoke("ShowProgressbar", () => { - if (!string.IsNullOrEmpty(tbQuery.Text.Trim()) && tbQuery.Text != lastQuery && !queryHasReturn) + if (!string.IsNullOrEmpty(query) && query != _lastQuery.RawQuery && !_queryHasReturn) { StartProgress(); } @@ -479,7 +479,28 @@ namespace Wox var query = PluginManager.QueryInit(text); if (query != null) { - lastQuery = query.RawQuery; + // handle the exclusiveness of plugin using action keyword + string lastKeyword = _lastQuery.ActionKeyword; + string keyword = query.ActionKeyword; + if (string.IsNullOrEmpty(lastKeyword)) + { + if (!string.IsNullOrEmpty(keyword)) + { + pnlResult.RemoveResultsExcept(PluginManager.NonGlobalPlugins[keyword]); + } + } + else + { + if (string.IsNullOrEmpty(keyword)) + { + pnlResult.RemoveResultsFor(PluginManager.NonGlobalPlugins[lastKeyword]); + } + else if (lastKeyword != keyword) + { + pnlResult.RemoveResultsExcept(PluginManager.NonGlobalPlugins[keyword]); + } + } + _lastQuery = query; PluginManager.QueryForAllPlugins(query); } StopProgress(); @@ -814,7 +835,7 @@ namespace Wox private void UpdateResultView(List list) { - queryHasReturn = true; + _queryHasReturn = true; progressBar.Dispatcher.Invoke(new Action(StopProgress)); if (list == null || list.Count == 0) return; @@ -824,7 +845,7 @@ namespace Wox { o.Score += UserSelectedRecordStorage.Instance.GetSelectedCount(o) * 5; }); - List l = list.Where(o => o.OriginQuery != null && o.OriginQuery.RawQuery == lastQuery).ToList(); + List l = list.Where(o => o.OriginQuery != null && o.OriginQuery.RawQuery == _lastQuery.RawQuery).ToList(); UpdateResultViewInternal(l); } } diff --git a/Wox/ResultPanel.xaml.cs b/Wox/ResultPanel.xaml.cs index 3115ece6ef..f3e0148fd7 100644 --- a/Wox/ResultPanel.xaml.cs +++ b/Wox/ResultPanel.xaml.cs @@ -9,6 +9,7 @@ using System.Windows.Media; using System.Linq; using System.Runtime.Remoting.Contexts; using Wox.Core.UserSettings; +using Wox.Helper; using Wox.Plugin; using Wox.Storage; @@ -20,7 +21,7 @@ namespace Wox public event Action LeftMouseClickEvent; public event Action RightMouseClickEvent; public event Action ItemDropEvent; - private readonly ObservableCollection _results; //todo, for better performance, override the default linear search + private readonly ListBoxItems _results; //todo, for better performance, override the default linear search private readonly object _resultsUpdateLock = new object(); protected virtual void OnRightMouseClick(Result result) @@ -38,6 +39,22 @@ namespace Wox public int MaxResultsToShow { get { return UserSettingStorage.Instance.MaxResultsToShow * 50; } } + internal void RemoveResultsFor(PluginPair plugin) + { + lock (_resultsUpdateLock) + { + _results.RemoveAll(r => r.PluginID == plugin.Metadata.ID); + } + } + + internal void RemoveResultsExcept(PluginPair plugin) + { + lock (_resultsUpdateLock) + { + _results.RemoveAll(r => r.PluginID != plugin.Metadata.ID); + } + } + public void AddResults(List newResults) { if (newResults != null && newResults.Count > 0) @@ -45,16 +62,7 @@ namespace Wox lock (_resultsUpdateLock) { var pluginId = newResults[0].PluginID; - var actionKeyword = newResults[0].OriginQuery.ActionKeyword; - List oldResults; - if (string.IsNullOrEmpty(actionKeyword)) - { - oldResults = _results.Where(r => r.PluginID == pluginId).ToList(); - } - else - { - oldResults = _results.ToList(); - } + var oldResults = _results.Where(r => r.PluginID == pluginId).ToList(); // intersection of A (old results) and B (new newResults) var intersection = oldResults.Intersect(newResults).ToList(); @@ -236,7 +244,7 @@ namespace Wox public ResultPanel() { InitializeComponent(); - _results = new ObservableCollection(); + _results = new ListBoxItems(); lbResults.ItemsSource = _results; } diff --git a/Wox/Wox.csproj b/Wox/Wox.csproj index ff6ac6882c..06d0cb7946 100644 --- a/Wox/Wox.csproj +++ b/Wox/Wox.csproj @@ -106,6 +106,7 @@ + @@ -280,7 +281,9 @@ ResXFileCodeGenerator Resources.Designer.cs - + + Designer + SettingsSingleFileGenerator Settings.Designer.cs