From 99d9d14d3b0c20bfdb6e129731e0bcc1a18cda26 Mon Sep 17 00:00:00 2001 From: bao-qian Date: Thu, 5 Nov 2015 20:44:14 +0000 Subject: [PATCH] Misc 1. Rename 2. Fix progress bar: progress bar should not be loaded when only white spaces typed --- .../PluginIndicator.cs | 2 +- Wox.Core/Plugin/PluginInstaller.cs | 2 +- Wox.Core/Plugin/PluginManager.cs | 86 +++++++++---------- Wox.Core/UI/ResourceMerger.cs | 2 +- Wox.Plugin/Query.cs | 10 ++- Wox/ActionKeywords.xaml.cs | 4 +- Wox/MainWindow.xaml.cs | 6 +- Wox/SettingWindow.xaml.cs | 2 +- 8 files changed, 60 insertions(+), 54 deletions(-) diff --git a/Plugins/Wox.Plugin.PluginIndicator/PluginIndicator.cs b/Plugins/Wox.Plugin.PluginIndicator/PluginIndicator.cs index e247436eb8..d0ad7b9a6f 100644 --- a/Plugins/Wox.Plugin.PluginIndicator/PluginIndicator.cs +++ b/Plugins/Wox.Plugin.PluginIndicator/PluginIndicator.cs @@ -17,7 +17,7 @@ namespace Wox.Plugin.PluginIndicator List results = new List(); if (allPlugins.Count == 0) { - allPlugins = context.API.GetAllPlugins().Where(o => !PluginManager.IsSystemPlugin(o.Metadata)).ToList(); + allPlugins = context.API.GetAllPlugins().Where(o => !PluginManager.IsGlobalPlugin(o.Metadata)).ToList(); } foreach (PluginMetadata metadata in allPlugins.Select(o => o.Metadata)) diff --git a/Wox.Core/Plugin/PluginInstaller.cs b/Wox.Core/Plugin/PluginInstaller.cs index b26254576f..ce6318a81b 100644 --- a/Wox.Core/Plugin/PluginInstaller.cs +++ b/Wox.Core/Plugin/PluginInstaller.cs @@ -51,7 +51,7 @@ namespace Wox.Core.Plugin string content = string.Format( "Do you want to install following plugin?\r\n\r\nName: {0}\r\nVersion: {1}\r\nAuthor: {2}", plugin.Name, plugin.Version, plugin.Author); - PluginPair existingPlugin = PluginManager.GetPlugin(plugin.ID); + PluginPair existingPlugin = PluginManager.GetPluginForId(plugin.ID); if (existingPlugin != null) { diff --git a/Wox.Core/Plugin/PluginManager.cs b/Wox.Core/Plugin/PluginManager.cs index 6ae8492b24..749a91f30e 100644 --- a/Wox.Core/Plugin/PluginManager.cs +++ b/Wox.Core/Plugin/PluginManager.cs @@ -8,7 +8,6 @@ using Wox.Core.Exception; using Wox.Core.i18n; using Wox.Core.UI; using Wox.Core.UserSettings; -using Wox.Infrastructure; using Wox.Infrastructure.Logger; using Wox.Plugin; using Stopwatch = Wox.Infrastructure.Stopwatch; @@ -21,23 +20,19 @@ namespace Wox.Core.Plugin public static class PluginManager { public const string DirectoryName = "Plugins"; - private static List pluginMetadatas; - private static IEnumerable instantQueryPlugins; - private static IEnumerable exclusiveSearchPlugins; private static IEnumerable contextMenuPlugins; - private static List plugins; /// /// Directories that will hold Wox plugin directory /// private static List pluginDirectories = new List(); - public static IEnumerable AllPlugins - { - get { return plugins; } - private set { plugins = value.OrderBy(o => o.Metadata.Name).ToList(); } - } + public static IEnumerable AllPlugins { get; private set; } + private static List GlobalPlugins { get; set; } + private static List NonGlobalPlugins { get; set; } + + private static IEnumerable InstantQueryPlugins { get; set; } public static IPublicAPI API { private set; get; } public static string PluginDirectory @@ -79,9 +74,9 @@ namespace Wox.Core.Plugin SetupPluginDirectories(); API = api; - pluginMetadatas = PluginConfig.Parse(pluginDirectories); - AllPlugins = (new CSharpPluginLoader().LoadPlugin(pluginMetadatas)). - Concat(new JsonRPCPluginLoader().LoadPlugin(pluginMetadatas)); + var metadatas = PluginConfig.Parse(pluginDirectories); + AllPlugins = (new CSharpPluginLoader().LoadPlugin(metadatas)). + Concat(new JsonRPCPluginLoader().LoadPlugin(metadatas)); //load plugin i18n languages ResourceMerger.ApplyPluginLanguages(); @@ -107,8 +102,21 @@ namespace Wox.Core.Plugin ThreadPool.QueueUserWorkItem(o => { - instantQueryPlugins = GetPlugins(); - contextMenuPlugins = GetPlugins(); + InstantQueryPlugins = GetPluginsForInterface(); + contextMenuPlugins = GetPluginsForInterface(); + GlobalPlugins = new List(); + NonGlobalPlugins = new List(); + foreach (var plugin in AllPlugins) + { + if (IsGlobalPlugin(plugin.Metadata)) + { + GlobalPlugins.Add(plugin); + } + else + { + NonGlobalPlugins.Add(plugin); + } + } }); } @@ -121,18 +129,15 @@ namespace Wox.Core.Plugin { // replace multiple white spaces with one white space var terms = text.Split(new[] { Query.TermSeperater }, StringSplitOptions.RemoveEmptyEntries); - var rawQuery = string.Join(Query.TermSeperater, terms.ToArray()); + var rawQuery = string.Join(Query.TermSeperater, terms); var actionKeyword = string.Empty; var search = rawQuery; - IEnumerable actionParameters = terms; + List actionParameters = terms.ToList(); if (terms.Length == 0) return null; if (IsVailldActionKeyword(terms[0])) { actionKeyword = terms[0]; - } - if (!string.IsNullOrEmpty(actionKeyword)) - { - actionParameters = terms.Skip(1); + actionParameters = terms.Skip(1).ToList(); search = string.Join(Query.TermSeperater, actionParameters.ToArray()); } return new Query @@ -143,14 +148,14 @@ namespace Wox.Core.Plugin Search = search, // Obsolete value initialisation ActionName = actionKeyword, - ActionParameters = actionParameters.ToList() + ActionParameters = actionParameters }; } public static void QueryForAllPlugins(Query query) { - var pluginPairs = GetNonSystemPlugin(query) != null ? - new List { GetNonSystemPlugin(query) } : GetSystemPlugins(); + var pluginPairs = GetPluginForActionKeyword(query.ActionKeyword) != null ? + new List { GetPluginForActionKeyword(query.ActionKeyword) } : GlobalPlugins; foreach (var plugin in pluginPairs) { var customizedPluginConfig = UserSettingStorage.Instance. @@ -200,7 +205,7 @@ namespace Wox.Core.Plugin /// private static bool IsVailldActionKeyword(string actionKeyword) { - if (string.IsNullOrEmpty(actionKeyword) || actionKeyword == Query.WildcardSign) return false; + 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. @@ -208,9 +213,9 @@ namespace Wox.Core.Plugin return customizedPluginConfig == null || !customizedPluginConfig.Disabled; } - public static bool IsSystemPlugin(PluginMetadata metadata) + public static bool IsGlobalPlugin(PluginMetadata metadata) { - return metadata.ActionKeywords.Contains(Query.WildcardSign); + return metadata.ActionKeywords.Contains(Query.GlobalPluginWildcardSign); } private static bool IsInstantQueryPlugin(PluginPair plugin) @@ -218,7 +223,7 @@ namespace Wox.Core.Plugin //any plugin that takes more than 200ms for AvgQueryTime won't be treated as IInstantQuery plugin anymore. return plugin.AvgQueryTime < 200 && plugin.Plugin is IInstantQuery && - instantQueryPlugins.Any(p => p.Metadata.ID == plugin.Metadata.ID); + InstantQueryPlugins.Any(p => p.Metadata.ID == plugin.Metadata.ID); } /// @@ -226,29 +231,24 @@ namespace Wox.Core.Plugin /// /// /// - public static PluginPair GetPlugin(string id) + public static PluginPair GetPluginForId(string id) { return AllPlugins.FirstOrDefault(o => o.Metadata.ID == id); } - public static IEnumerable GetPlugins() where T : IFeatures + 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); } - private static PluginPair GetNonSystemPlugin(Query query) - { - //if a query doesn't contain a vaild action keyword, it should be a query for system plugin - if (string.IsNullOrEmpty(query.ActionKeyword)) return null; - return AllPlugins.FirstOrDefault(o => o.Metadata.ActionKeywords.Contains(query.ActionKeyword)); - } - - private static List GetSystemPlugins() - { - return AllPlugins.Where(o => IsSystemPlugin(o.Metadata)).ToList(); - } - - public static List GetPluginContextMenus(Result result) + public static List GetContextMenusForPlugin(Result result) { var pluginPair = contextMenuPlugins.FirstOrDefault(o => o.Metadata.ID == result.PluginID); var plugin = (IContextMenu)pluginPair?.Plugin; diff --git a/Wox.Core/UI/ResourceMerger.cs b/Wox.Core/UI/ResourceMerger.cs index 818f3073b0..2be07e50c6 100644 --- a/Wox.Core/UI/ResourceMerger.cs +++ b/Wox.Core/UI/ResourceMerger.cs @@ -39,7 +39,7 @@ namespace Wox.Core.UI internal static void ApplyPluginLanguages() { RemoveResource(PluginManager.DirectoryName); - foreach (var languageFile in PluginManager.GetPlugins(). + foreach (var languageFile in PluginManager.GetPluginsForInterface(). Select(plugin => InternationalizationManager.Instance.GetLanguageFile(((IPluginI18n)plugin.Plugin).GetLanguagesFolder())). Where(file => !string.IsNullOrEmpty(file))) { diff --git a/Wox.Plugin/Query.cs b/Wox.Plugin/Query.cs index 89d8030a00..f5c4145ebf 100644 --- a/Wox.Plugin/Query.cs +++ b/Wox.Plugin/Query.cs @@ -25,13 +25,19 @@ namespace Wox.Plugin /// internal string[] Terms { private get; set; } + /// + /// Query can be splited into multiple terms by whitespace + /// public const string TermSeperater = " "; + /// + /// User can set multiple action keywords seperated by ';' + /// public const string ActionKeywordSeperater = ";"; /// - /// * is used for System Plugin + /// '*' is used for System Plugin /// - public const string WildcardSign = "*"; + public const string GlobalPluginWildcardSign = "*"; public string ActionKeyword { get; set; } diff --git a/Wox/ActionKeywords.xaml.cs b/Wox/ActionKeywords.xaml.cs index 25a11c992f..9fc60979fa 100644 --- a/Wox/ActionKeywords.xaml.cs +++ b/Wox/ActionKeywords.xaml.cs @@ -15,7 +15,7 @@ namespace Wox public ActionKeywords(string pluginId) { InitializeComponent(); - PluginPair plugin = PluginManager.GetPlugin(pluginId); + PluginPair plugin = PluginManager.GetPluginForId(pluginId); if (plugin == null) { MessageBox.Show(InternationalizationManager.Instance.GetTranslation("cannotFindSpecifiedPlugin")); @@ -47,7 +47,7 @@ namespace Wox var actionKeywords = tbAction.Text.Trim().Split(new[] { Query.ActionKeywordSeperater }, StringSplitOptions.RemoveEmptyEntries).ToArray(); //check new action keyword didn't used by other plugin - if (actionKeywords[0] != Query.WildcardSign && PluginManager.AllPlugins. + if (actionKeywords[0] != Query.GlobalPluginWildcardSign && PluginManager.AllPlugins. SelectMany(p => p.Metadata.ActionKeywords). Any(k => actionKeywords.Contains(k))) { diff --git a/Wox/MainWindow.xaml.cs b/Wox/MainWindow.xaml.cs index 5ba8a6f20c..1792eafae7 100644 --- a/Wox/MainWindow.xaml.cs +++ b/Wox/MainWindow.xaml.cs @@ -464,7 +464,7 @@ namespace Wox Query(tbQuery.Text); Dispatcher.DelayInvoke("ShowProgressbar", () => { - if (!queryHasReturn && !string.IsNullOrEmpty(tbQuery.Text) && tbQuery.Text != lastQuery) + if (!string.IsNullOrEmpty(tbQuery.Text.Trim()) && tbQuery.Text != lastQuery && !queryHasReturn) { StartProgress(); } @@ -873,10 +873,10 @@ namespace Wox private void ShowContextMenu(Result result) { - List results = PluginManager.GetPluginContextMenus(result); + List results = PluginManager.GetContextMenusForPlugin(result); results.ForEach(o => { - o.PluginDirectory = PluginManager.GetPlugin(result.PluginID).Metadata.PluginDirectory; + o.PluginDirectory = PluginManager.GetPluginForId(result.PluginID).Metadata.PluginDirectory; o.PluginID = result.PluginID; o.OriginQuery = result.OriginQuery; }); diff --git a/Wox/SettingWindow.xaml.cs b/Wox/SettingWindow.xaml.cs index 297d9bb6d4..a387914653 100644 --- a/Wox/SettingWindow.xaml.cs +++ b/Wox/SettingWindow.xaml.cs @@ -605,7 +605,7 @@ namespace Wox string id = pair.Metadata.ID; ActionKeywords changeKeywordsWindow = new ActionKeywords(id); changeKeywordsWindow.ShowDialog(); - PluginPair plugin = PluginManager.GetPlugin(id); + PluginPair plugin = PluginManager.GetPluginForId(id); if (plugin != null) pluginActionKeywords.Text = string.Join(Query.ActionKeywordSeperater, pair.Metadata.ActionKeywords); } }