Fix exclusive for action keyword

This commit is contained in:
bao-qian 2015-11-07 03:50:26 +00:00
parent 8662e963ac
commit 7d52b0cc96
4 changed files with 86 additions and 25 deletions

View File

@ -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<Result>
{
public void RemoveAll(Predicate<Result> predicate)
{
CheckReentrancy();
List<Result> 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));
}
}
}
}

View File

@ -43,11 +43,11 @@ namespace Wox
private readonly Storyboard progressBarStoryboard = new Storyboard(); private readonly Storyboard progressBarStoryboard = new Storyboard();
private NotifyIcon notifyIcon; private NotifyIcon notifyIcon;
private bool queryHasReturn; private bool _queryHasReturn;
private string lastQuery; private Query _lastQuery = new Query();
private ToolTip toolTip = new ToolTip(); private ToolTip toolTip = new ToolTip();
private bool ignoreTextChange = false; private bool _ignoreTextChange = false;
private List<Result> CurrentContextMenus = new List<Result>(); private List<Result> CurrentContextMenus = new List<Result>();
private string textBeforeEnterContextMenuMode; private string textBeforeEnterContextMenuMode;
@ -72,7 +72,7 @@ namespace Wox
{ {
Dispatcher.Invoke(new Action(() => Dispatcher.Invoke(new Action(() =>
{ {
ignoreTextChange = true; _ignoreTextChange = true;
tbQuery.Text = query; tbQuery.Text = query;
tbQuery.CaretIndex = tbQuery.Text.Length; tbQuery.CaretIndex = tbQuery.Text.Length;
if (selectAll) if (selectAll)
@ -441,10 +441,10 @@ namespace Wox
private void TbQuery_OnTextChanged(object sender, TextChangedEventArgs e) 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; toolTip.IsOpen = false;
if (IsInContextMenuMode) if (IsInContextMenuMode)
@ -453,10 +453,10 @@ namespace Wox
return; return;
} }
Query(tbQuery.Text); Query(query);
Dispatcher.DelayInvoke("ShowProgressbar", () => Dispatcher.DelayInvoke("ShowProgressbar", () =>
{ {
if (!string.IsNullOrEmpty(tbQuery.Text.Trim()) && tbQuery.Text != lastQuery && !queryHasReturn) if (!string.IsNullOrEmpty(query) && query != _lastQuery.RawQuery && !_queryHasReturn)
{ {
StartProgress(); StartProgress();
} }
@ -479,7 +479,28 @@ namespace Wox
var query = PluginManager.QueryInit(text); var query = PluginManager.QueryInit(text);
if (query != null) 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); PluginManager.QueryForAllPlugins(query);
} }
StopProgress(); StopProgress();
@ -814,7 +835,7 @@ namespace Wox
private void UpdateResultView(List<Result> list) private void UpdateResultView(List<Result> list)
{ {
queryHasReturn = true; _queryHasReturn = true;
progressBar.Dispatcher.Invoke(new Action(StopProgress)); progressBar.Dispatcher.Invoke(new Action(StopProgress));
if (list == null || list.Count == 0) return; if (list == null || list.Count == 0) return;
@ -824,7 +845,7 @@ namespace Wox
{ {
o.Score += UserSelectedRecordStorage.Instance.GetSelectedCount(o) * 5; o.Score += UserSelectedRecordStorage.Instance.GetSelectedCount(o) * 5;
}); });
List<Result> l = list.Where(o => o.OriginQuery != null && o.OriginQuery.RawQuery == lastQuery).ToList(); List<Result> l = list.Where(o => o.OriginQuery != null && o.OriginQuery.RawQuery == _lastQuery.RawQuery).ToList();
UpdateResultViewInternal(l); UpdateResultViewInternal(l);
} }
} }

View File

@ -9,6 +9,7 @@ using System.Windows.Media;
using System.Linq; using System.Linq;
using System.Runtime.Remoting.Contexts; using System.Runtime.Remoting.Contexts;
using Wox.Core.UserSettings; using Wox.Core.UserSettings;
using Wox.Helper;
using Wox.Plugin; using Wox.Plugin;
using Wox.Storage; using Wox.Storage;
@ -20,7 +21,7 @@ namespace Wox
public event Action<Result> LeftMouseClickEvent; public event Action<Result> LeftMouseClickEvent;
public event Action<Result> RightMouseClickEvent; public event Action<Result> RightMouseClickEvent;
public event Action<Result, IDataObject, DragEventArgs> ItemDropEvent; public event Action<Result, IDataObject, DragEventArgs> ItemDropEvent;
private readonly ObservableCollection<Result> _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(); private readonly object _resultsUpdateLock = new object();
protected virtual void OnRightMouseClick(Result result) protected virtual void OnRightMouseClick(Result result)
@ -38,6 +39,22 @@ namespace Wox
public int MaxResultsToShow { get { return UserSettingStorage.Instance.MaxResultsToShow * 50; } } 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<Result> newResults) public void AddResults(List<Result> newResults)
{ {
if (newResults != null && newResults.Count > 0) if (newResults != null && newResults.Count > 0)
@ -45,16 +62,7 @@ namespace Wox
lock (_resultsUpdateLock) lock (_resultsUpdateLock)
{ {
var pluginId = newResults[0].PluginID; var pluginId = newResults[0].PluginID;
var actionKeyword = newResults[0].OriginQuery.ActionKeyword; var oldResults = _results.Where(r => r.PluginID == pluginId).ToList();
List<Result> oldResults;
if (string.IsNullOrEmpty(actionKeyword))
{
oldResults = _results.Where(r => r.PluginID == pluginId).ToList();
}
else
{
oldResults = _results.ToList();
}
// intersection of A (old results) and B (new newResults) // intersection of A (old results) and B (new newResults)
var intersection = oldResults.Intersect(newResults).ToList(); var intersection = oldResults.Intersect(newResults).ToList();
@ -236,7 +244,7 @@ namespace Wox
public ResultPanel() public ResultPanel()
{ {
InitializeComponent(); InitializeComponent();
_results = new ObservableCollection<Result>(); _results = new ListBoxItems();
lbResults.ItemsSource = _results; lbResults.ItemsSource = _results;
} }

View File

@ -106,6 +106,7 @@
<Compile Include="Converters\OpacityModeConverter.cs" /> <Compile Include="Converters\OpacityModeConverter.cs" />
<Compile Include="Converters\StringEmptyConverter.cs" /> <Compile Include="Converters\StringEmptyConverter.cs" />
<Compile Include="Converters\StringNullOrEmptyToVisibilityConverter.cs" /> <Compile Include="Converters\StringNullOrEmptyToVisibilityConverter.cs" />
<Compile Include="Helper\ListBoxItems.cs" />
<Compile Include="Helper\SingletonWindowOpener.cs" /> <Compile Include="Helper\SingletonWindowOpener.cs" />
<Compile Include="ImageLoader\ImageCacheStroage.cs" /> <Compile Include="ImageLoader\ImageCacheStroage.cs" />
<Compile Include="ShellContext\ShellContextMenuManager.cs" /> <Compile Include="ShellContext\ShellContextMenuManager.cs" />
@ -280,7 +281,9 @@
<Generator>ResXFileCodeGenerator</Generator> <Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput> <LastGenOutput>Resources.Designer.cs</LastGenOutput>
</EmbeddedResource> </EmbeddedResource>
<None Include="App.config" /> <None Include="App.config">
<SubType>Designer</SubType>
</None>
<None Include="Properties\Settings.settings"> <None Include="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator> <Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput> <LastGenOutput>Settings.Designer.cs</LastGenOutput>