diff --git a/Plugins/Wox.Plugin.Everything/ContextMenuStorage.cs b/Plugins/Wox.Plugin.Everything/ContextMenuStorage.cs index bbb0d33753..ba36209b6a 100644 --- a/Plugins/Wox.Plugin.Everything/ContextMenuStorage.cs +++ b/Plugins/Wox.Plugin.Everything/ContextMenuStorage.cs @@ -1,7 +1,8 @@ using System.Collections.Generic; using System.IO; using System.Reflection; -using Newtonsoft.Json; +using Exceptionless.Json; +using JsonProperty = Newtonsoft.Json.JsonPropertyAttribute; using Wox.Infrastructure.Storage; namespace Wox.Plugin.Everything diff --git a/Plugins/Wox.Plugin.PluginIndicator/PluginIndicator.cs b/Plugins/Wox.Plugin.PluginIndicator/PluginIndicator.cs index f7b2bd0314..f7f518889c 100644 --- a/Plugins/Wox.Plugin.PluginIndicator/PluginIndicator.cs +++ b/Plugins/Wox.Plugin.PluginIndicator/PluginIndicator.cs @@ -13,10 +13,9 @@ namespace Wox.Plugin.PluginIndicator public List Query(Query query) { - var results = from plugin in PluginManager.NonGlobalPlugins - select plugin.Metadata into metadata - from keyword in metadata.ActionKeywords + var results = from keyword in PluginManager.NonGlobalPlugins.Keys where keyword.StartsWith(query.Terms[0]) + let metadata = PluginManager.NonGlobalPlugins[keyword].Metadata let customizedPluginConfig = UserSettingStorage.Instance.CustomizedPluginConfigs.FirstOrDefault(o => o.ID == metadata.ID) where customizedPluginConfig == null || !customizedPluginConfig.Disabled diff --git a/Wox.Core/Plugin/PluginManager.cs b/Wox.Core/Plugin/PluginManager.cs index 6123508c3a..bb7a700796 100644 --- a/Wox.Core/Plugin/PluginManager.cs +++ b/Wox.Core/Plugin/PluginManager.cs @@ -29,8 +29,8 @@ namespace Wox.Core.Plugin public static IEnumerable AllPlugins { get; private set; } - public static IEnumerable GlobalPlugins { get; private set; } - public static IEnumerable NonGlobalPlugins { get; private set; } + public static List GlobalPlugins { get; } = new List(); + public static Dictionary NonGlobalPlugins { get; } = new Dictionary(); private static IEnumerable InstantQueryPlugins { get; set; } public static IPublicAPI API { private set; get; } @@ -104,8 +104,20 @@ namespace Wox.Core.Plugin { InstantQueryPlugins = GetPluginsForInterface(); contextMenuPlugins = GetPluginsForInterface(); - GlobalPlugins = AllPlugins.Where(p => IsGlobalPlugin(p.Metadata)); - NonGlobalPlugins = AllPlugins.Where(p => !IsGlobalPlugin(p.Metadata)); + foreach (var plugin in AllPlugins) + { + if (IsGlobalPlugin(plugin.Metadata)) + { + GlobalPlugins.Add(plugin); + } + else + { + foreach (string actionKeyword in plugin.Metadata.ActionKeywords) + { + NonGlobalPlugins[actionKeyword] = plugin; + } + } + } }); } @@ -123,7 +135,7 @@ namespace Wox.Core.Plugin var search = rawQuery; List actionParameters = terms.ToList(); if (terms.Length == 0) return null; - if (IsVailldActionKeyword(terms[0])) + if (NonGlobalPlugins.ContainsKey(terms[0])) { actionKeyword = terms[0]; actionParameters = terms.Skip(1).ToList(); @@ -143,8 +155,8 @@ namespace Wox.Core.Plugin public static void QueryForAllPlugins(Query query) { - var pluginPairs = GetPluginForActionKeyword(query.ActionKeyword) != null ? - new List { GetPluginForActionKeyword(query.ActionKeyword) } : GlobalPlugins; + var pluginPairs = NonGlobalPlugins.ContainsKey(query.ActionKeyword) ? + new List { NonGlobalPlugins[query.ActionKeyword] } : GlobalPlugins; foreach (var plugin in pluginPairs) { var customizedPluginConfig = UserSettingStorage.Instance. @@ -187,21 +199,6 @@ namespace Wox.Core.Plugin } } - /// - /// Check if a query contains valid action keyword - /// - /// - /// - private static bool IsVailldActionKeyword(string actionKeyword) - { - if (string.IsNullOrEmpty(actionKeyword) || actionKeyword == Query.GlobalPluginWildcardSign) return false; - PluginPair pair = AllPlugins.FirstOrDefault(o => o.Metadata.ActionKeywords.Contains(actionKeyword)); - if (pair == null) return false; - var customizedPluginConfig = UserSettingStorage.Instance. - CustomizedPluginConfigs.FirstOrDefault(o => o.ID == pair.Metadata.ID); - return customizedPluginConfig == null || !customizedPluginConfig.Disabled; - } - private static bool IsGlobalPlugin(PluginMetadata metadata) { return metadata.ActionKeywords.Contains(Query.GlobalPluginWildcardSign); @@ -225,13 +222,6 @@ namespace Wox.Core.Plugin return AllPlugins.FirstOrDefault(o => o.Metadata.ID == id); } - private static PluginPair GetPluginForActionKeyword(string actionKeyword) - { - //if a query doesn't contain a vaild action keyword, it should be a query for system plugin - if (string.IsNullOrEmpty(actionKeyword) || actionKeyword == Query.GlobalPluginWildcardSign) return null; - return NonGlobalPlugins.FirstOrDefault(o => o.Metadata.ActionKeywords.Contains(actionKeyword)); - } - public static IEnumerable GetPluginsForInterface() where T : IFeatures { return AllPlugins.Where(p => p.Plugin is T); diff --git a/Wox/Helper/ListBoxItems.cs b/Wox/Helper/ListBoxItems.cs new file mode 100644 index 0000000000..28a3e0062b --- /dev/null +++ b/Wox/Helper/ListBoxItems.cs @@ -0,0 +1,28 @@ +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[]")); + OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, itemsToRemove)); + } + } + } +} 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/Properties/Resources.Designer.cs b/Wox/Properties/Resources.Designer.cs index 7665c71436..20e0fb2257 100644 --- a/Wox/Properties/Resources.Designer.cs +++ b/Wox/Properties/Resources.Designer.cs @@ -1,10 +1,10 @@ //------------------------------------------------------------------------------ // -// 此代码由工具生成。 -// 运行时版本:4.0.30319.0 +// This code was generated by a tool. +// Runtime Version:4.0.30319.42000 // -// 对此文件的更改可能会导致不正确的行为,并且如果 -// 重新生成代码,这些更改将会丢失。 +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. // //------------------------------------------------------------------------------ @@ -13,12 +13,12 @@ namespace Wox.Properties { /// - /// 一个强类型的资源类,用于查找本地化的字符串等。 + /// A strongly-typed resource class, for looking up localized strings, etc. /// - // 此类是由 StronglyTypedResourceBuilder - // 类通过类似于 ResGen 或 Visual Studio 的工具自动生成的。 - // 若要添加或移除成员,请编辑 .ResX 文件,然后重新运行 ResGen - // (以 /str 作为命令选项),或重新生成 VS 项目。 + // This class was auto-generated by the StronglyTypedResourceBuilder + // class via a tool like ResGen or Visual Studio. + // To add or remove a member, edit your .ResX file then rerun ResGen + // with the /str option, or rebuild your VS project. [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] @@ -33,7 +33,7 @@ namespace Wox.Properties { } /// - /// 返回此类使用的缓存的 ResourceManager 实例。 + /// Returns the cached ResourceManager instance used by this class. /// [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] internal static global::System.Resources.ResourceManager ResourceManager { @@ -47,8 +47,8 @@ namespace Wox.Properties { } /// - /// 使用此强类型资源类,为所有资源查找 - /// 重写当前线程的 CurrentUICulture 属性。 + /// Overrides the current thread's CurrentUICulture property for all + /// resource lookups using this strongly typed resource class. /// [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] internal static global::System.Globalization.CultureInfo Culture { @@ -61,7 +61,7 @@ namespace Wox.Properties { } /// - /// 查找类似于 (Icon) 的 System.Drawing.Icon 类型的本地化资源。 + /// Looks up a localized resource of type System.Drawing.Icon similar to (Icon). /// internal static System.Drawing.Icon app { get { diff --git a/Wox/Properties/Settings.Designer.cs b/Wox/Properties/Settings.Designer.cs index eb3d4dc309..7a4226349e 100644 --- a/Wox/Properties/Settings.Designer.cs +++ b/Wox/Properties/Settings.Designer.cs @@ -1,10 +1,10 @@ //------------------------------------------------------------------------------ // -// 此代码由工具生成。 -// 运行时版本:4.0.30319.0 +// This code was generated by a tool. +// Runtime Version:4.0.30319.42000 // -// 对此文件的更改可能会导致不正确的行为,并且如果 -// 重新生成代码,这些更改将会丢失。 +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. // //------------------------------------------------------------------------------ @@ -12,7 +12,7 @@ namespace Wox.Properties { [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] - [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "12.0.0.0")] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "14.0.0.0")] internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase { private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); diff --git a/Wox/ResultPanel.xaml.cs b/Wox/ResultPanel.xaml.cs index 3115ece6ef..3d8510f818 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,25 @@ 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 +65,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 +247,7 @@ namespace Wox public ResultPanel() { InitializeComponent(); - _results = new ObservableCollection(); + _results = new ListBoxItems(); lbResults.ItemsSource = _results; }