From ccc8d7e5cdf637d6f4572859f6fd3142a39ad353 Mon Sep 17 00:00:00 2001 From: qianlifeng Date: Fri, 26 Dec 2014 22:51:04 +0800 Subject: [PATCH 1/6] Refactoring. --- Plugins/Wox.Plugin.PluginManagement/Main.cs | 8 +++--- Wox.Core/Plugin/JsonRPCPlugin.cs | 13 +++++---- Wox.Core/Plugin/PluginConfig.cs | 6 ++-- Wox.Core/Plugin/PluginManager.cs | 28 +++++++++++++------ .../QueryDispatcher/IQueryDispatcher.cs | 7 +++++ .../Plugin/QueryDispatcher/QueryDispatcher.cs | 21 ++++++++++++++ .../SystemPluginQueryDispatcher.cs | 13 ++++----- .../UserPluginQueryDispatcher.cs | 22 +++++++-------- Wox.Core/README.txt | 2 +- Wox.Core/Wox.Core.csproj | 7 ++++- Wox.Infrastructure/ChineseToPinYin.cs | 15 ---------- Wox.Infrastructure/Wox.Infrastructure.csproj | 11 +++----- .../Program/ProgramSetting.xaml | 3 +- .../Program}/StringEmptyConverter.cs | 5 +--- ...ginIndicator.cs => UserPluginIndicator.cs} | 2 +- .../Wox.Plugin.SystemPlugins.csproj | 3 +- Wox.Plugin/IPublicAPI.cs | 7 ++++- Wox.Plugin/PluginType.cs | 2 +- Wox/CommandArgs/ReloadPluginCommandArg.cs | 2 +- Wox/Commands/BaseCommand.cs | 18 ------------ Wox/Commands/CommandFactory.cs | 28 ------------------- Wox/Helper/PluginInstaller.cs | 2 +- Wox/ImageLoader/ImageCacheStroage.cs | 4 +-- Wox/MainWindow.xaml.cs | 10 +++---- Wox/SettingWindow.xaml.cs | 2 +- .../Storage/UserSelectedRecordStorage.cs | 7 ++--- Wox/Wox.csproj | 5 +--- 27 files changed, 114 insertions(+), 139 deletions(-) create mode 100644 Wox.Core/Plugin/QueryDispatcher/IQueryDispatcher.cs create mode 100644 Wox.Core/Plugin/QueryDispatcher/QueryDispatcher.cs rename Wox/Commands/SystemCommand.cs => Wox.Core/Plugin/QueryDispatcher/SystemPluginQueryDispatcher.cs (79%) rename Wox/Commands/PluginCommand.cs => Wox.Core/Plugin/QueryDispatcher/UserPluginQueryDispatcher.cs (57%) delete mode 100644 Wox.Infrastructure/ChineseToPinYin.cs rename {Wox.Infrastructure => Wox.Plugin.SystemPlugins/Program}/StringEmptyConverter.cs (87%) rename Wox.Plugin.SystemPlugins/{ThirdpartyPluginIndicator.cs => UserPluginIndicator.cs} (97%) delete mode 100644 Wox/Commands/BaseCommand.cs delete mode 100644 Wox/Commands/CommandFactory.cs rename {Wox.Infrastructure => Wox}/Storage/UserSelectedRecordStorage.cs (88%) diff --git a/Plugins/Wox.Plugin.PluginManagement/Main.cs b/Plugins/Wox.Plugin.PluginManagement/Main.cs index 960a2c328c..a2153610d7 100644 --- a/Plugins/Wox.Plugin.PluginManagement/Main.cs +++ b/Plugins/Wox.Plugin.PluginManagement/Main.cs @@ -197,7 +197,7 @@ namespace Wox.Plugin.PluginManagement private List ListUnInstalledPlugins(Query query) { List results = new List(); - List allInstalledPlugins = ParseThirdPartyPlugins(); + List allInstalledPlugins = ParseUserPlugins(); if (query.ActionParameters.Count > 1) { string pluginName = query.ActionParameters[1]; @@ -235,7 +235,7 @@ namespace Wox.Plugin.PluginManagement private List ListInstalledPlugins() { List results = new List(); - foreach (PluginMetadata plugin in ParseThirdPartyPlugins()) + foreach (PluginMetadata plugin in ParseUserPlugins()) { results.Add(new Result() { @@ -247,7 +247,7 @@ namespace Wox.Plugin.PluginManagement return results; } - private static List ParseThirdPartyPlugins() + private static List ParseUserPlugins() { List pluginMetadatas = new List(); if (!Directory.Exists(PluginPath)) @@ -276,7 +276,7 @@ namespace Wox.Plugin.PluginManagement try { metadata = JsonConvert.DeserializeObject(File.ReadAllText(configPath)); - metadata.PluginType = PluginType.ThirdParty; + metadata.PluginType = PluginType.User; metadata.PluginDirectory = pluginDirectory; } catch (Exception) diff --git a/Wox.Core/Plugin/JsonRPCPlugin.cs b/Wox.Core/Plugin/JsonRPCPlugin.cs index 1b878dfce3..0809826300 100644 --- a/Wox.Core/Plugin/JsonRPCPlugin.cs +++ b/Wox.Core/Plugin/JsonRPCPlugin.cs @@ -4,6 +4,7 @@ using System.Diagnostics; using System.IO; using System.Reflection; using System.Threading; +using System.Windows.Forms; using Newtonsoft.Json; using Wox.Infrastructure.Exceptions; using Wox.Infrastructure.Logger; @@ -74,7 +75,6 @@ namespace Wox.Core.Plugin } catch (Exception e) { - ErrorReporting.TryShowErrorMessageBox(e.Message, e); Log.Error(e.Message); } } @@ -83,12 +83,12 @@ namespace Wox.Core.Plugin private void ExecuteWoxAPI(string method, object[] parameters) { - MethodInfo methodInfo = App.Window.GetType().GetMethod(method); - if (methodInfo != null) + MethodInfo methodInfo = PluginManager.API.GetType().GetMethod(method); + if (methodInfo != null) { try { - methodInfo.Invoke(App.Window, parameters); + methodInfo.Invoke(PluginManager.API, parameters); } catch (Exception) { @@ -132,7 +132,7 @@ namespace Wox.Core.Plugin string result = reader.ReadToEnd(); if (result.StartsWith("DEBUG:")) { - System.Windows.Forms.MessageBox.Show(new Form { TopMost = true }, result.Substring(6)); + MessageBox.Show(new Form { TopMost = true }, result.Substring(6)); return ""; } if (string.IsNullOrEmpty(result)) @@ -142,7 +142,8 @@ namespace Wox.Core.Plugin string error = errorReader.ReadToEnd(); if (!string.IsNullOrEmpty(error)) { - ErrorReporting.TryShowErrorMessageBox(error, new WoxJsonRPCException(error)); + //todo: + // ErrorReporting.TryShowErrorMessageBox(error, new WoxJsonRPCException(error)); } } } diff --git a/Wox.Core/Plugin/PluginConfig.cs b/Wox.Core/Plugin/PluginConfig.cs index 0359ef6483..5d6c8137d4 100644 --- a/Wox.Core/Plugin/PluginConfig.cs +++ b/Wox.Core/Plugin/PluginConfig.cs @@ -28,7 +28,7 @@ namespace Wox.Core.Plugin ParseSystemPlugins(); foreach (string pluginDirectory in pluginDirectories) { - ParseThirdPartyPlugins(pluginDirectory); + ParseUserPlugins(pluginDirectory); } if (PluginManager.DebuggerMode != null) @@ -56,7 +56,7 @@ namespace Wox.Core.Plugin }); } - private static void ParseThirdPartyPlugins(string pluginDirectory) + private static void ParseUserPlugins(string pluginDirectory) { string[] directories = Directory.GetDirectories(pluginDirectory); @@ -88,7 +88,7 @@ namespace Wox.Core.Plugin try { metadata = JsonConvert.DeserializeObject(File.ReadAllText(configPath)); - metadata.PluginType = PluginType.ThirdParty; + metadata.PluginType = PluginType.User; metadata.PluginDirectory = pluginDirectory; } catch (Exception) diff --git a/Wox.Core/Plugin/PluginManager.cs b/Wox.Core/Plugin/PluginManager.cs index 91af115ba8..e3b0217869 100644 --- a/Wox.Core/Plugin/PluginManager.cs +++ b/Wox.Core/Plugin/PluginManager.cs @@ -15,8 +15,10 @@ namespace Wox.Core.Plugin public static class PluginManager { public static String DebuggerMode { get; private set; } + public static IPublicAPI API { get; private set; } + private static List plugins = new List(); - + /// /// Directories that will hold Wox plugin directory /// @@ -26,10 +28,14 @@ namespace Wox.Core.Plugin { pluginDirectories.Add( Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Plugins")); - pluginDirectories.Add( - Path.Combine(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), ".Wox"),"Plugins")); - MakesurePluginDirectoriesExist(); + string userProfilePath = Environment.GetEnvironmentVariable("USERPROFILE"); + if (userProfilePath != null) + { + pluginDirectories.Add(Path.Combine(Path.Combine(userProfilePath, ".Wox"), "Plugins")); + } + + MakesurePluginDirectoriesExist(); } private static void MakesurePluginDirectoriesExist() @@ -46,8 +52,9 @@ namespace Wox.Core.Plugin /// /// Load and init all Wox plugins /// - public static void Init() + public static void Init(IPublicAPI api) { + API = api; plugins.Clear(); List pluginMetadatas = PluginConfig.Parse(pluginDirectories); @@ -61,11 +68,16 @@ namespace Wox.Core.Plugin { CurrentPluginMetadata = pair.Metadata, Proxy = HttpProxy.Instance, - API = App.Window + API = API })); } } + public static void Query(Query query) + { + QueryDispatcher.QueryDispatcher.Dispatch(query); + } + public static List AllPlugins { get @@ -74,11 +86,11 @@ namespace Wox.Core.Plugin } } - public static bool HitThirdpartyKeyword(Query query) + public static bool IsUserPluginQuery(Query query) { if (string.IsNullOrEmpty(query.ActionName)) return false; - return plugins.Any(o => o.Metadata.PluginType == PluginType.ThirdParty && o.Metadata.ActionKeyword == query.ActionName); + return plugins.Any(o => o.Metadata.PluginType == PluginType.User && o.Metadata.ActionKeyword == query.ActionName); } public static void ActivatePluginDebugger(string path) diff --git a/Wox.Core/Plugin/QueryDispatcher/IQueryDispatcher.cs b/Wox.Core/Plugin/QueryDispatcher/IQueryDispatcher.cs new file mode 100644 index 0000000000..c5312a0332 --- /dev/null +++ b/Wox.Core/Plugin/QueryDispatcher/IQueryDispatcher.cs @@ -0,0 +1,7 @@ +namespace Wox.Core.Plugin.QueryDispatcher +{ + internal interface IQueryDispatcher + { + void Dispatch(Wox.Plugin.Query query); + } +} diff --git a/Wox.Core/Plugin/QueryDispatcher/QueryDispatcher.cs b/Wox.Core/Plugin/QueryDispatcher/QueryDispatcher.cs new file mode 100644 index 0000000000..0e0eb62fb9 --- /dev/null +++ b/Wox.Core/Plugin/QueryDispatcher/QueryDispatcher.cs @@ -0,0 +1,21 @@ + +namespace Wox.Core.Plugin.QueryDispatcher +{ + internal static class QueryDispatcher + { + private static IQueryDispatcher pluginCmd = new UserPluginQueryDispatcher(); + private static IQueryDispatcher systemCmd = new SystemPluginQueryDispatcher(); + + public static void Dispatch(Wox.Plugin.Query query) + { + if (PluginManager.IsUserPluginQuery(query)) + { + pluginCmd.Dispatch(query); + } + else + { + systemCmd.Dispatch(query); + } + } + } +} diff --git a/Wox/Commands/SystemCommand.cs b/Wox.Core/Plugin/QueryDispatcher/SystemPluginQueryDispatcher.cs similarity index 79% rename from Wox/Commands/SystemCommand.cs rename to Wox.Core/Plugin/QueryDispatcher/SystemPluginQueryDispatcher.cs index d1ca9eb4fc..b72c2e0760 100644 --- a/Wox/Commands/SystemCommand.cs +++ b/Wox.Core/Plugin/QueryDispatcher/SystemPluginQueryDispatcher.cs @@ -1,20 +1,17 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using System.Linq; -using System.Text; using System.Threading; -using Wox.Core.Plugin; using Wox.Infrastructure.Storage.UserSettings; using Wox.Plugin; using Wox.Plugin.SystemPlugins; -namespace Wox.Commands +namespace Wox.Core.Plugin.QueryDispatcher { - public class SystemCommand : BaseCommand + public class SystemPluginQueryDispatcher : IQueryDispatcher { private IEnumerable allSytemPlugins = PluginManager.AllPlugins.Where(o => o.Metadata.PluginType == PluginType.System); - public override void Dispatch(Query query) + public void Dispatch(Query query) { var queryPlugins = allSytemPlugins; if (UserSettingStorage.Instance.WebSearches.Exists(o => o.ActionWord == query.ActionName && o.Enabled)) @@ -34,7 +31,7 @@ namespace Wox.Commands List results = pair1.Plugin.Query(query); results.ForEach(o => { o.AutoAjustScore = true; }); - App.Window.PushResults(query, pair1.Metadata, results); + PluginManager.API.PushResults(query, pair1.Metadata, results); }); } } diff --git a/Wox/Commands/PluginCommand.cs b/Wox.Core/Plugin/QueryDispatcher/UserPluginQueryDispatcher.cs similarity index 57% rename from Wox/Commands/PluginCommand.cs rename to Wox.Core/Plugin/QueryDispatcher/UserPluginQueryDispatcher.cs index d233f766b9..6c709e5bdc 100644 --- a/Wox/Commands/PluginCommand.cs +++ b/Wox.Core/Plugin/QueryDispatcher/UserPluginQueryDispatcher.cs @@ -2,26 +2,24 @@ using System.Collections.Generic; using System.Linq; using System.Threading; -using Wox.Core.Plugin; -using Wox.Helper; using Wox.Infrastructure.Logger; using Wox.Infrastructure.Storage.UserSettings; using Wox.Plugin; -namespace Wox.Commands +namespace Wox.Core.Plugin.QueryDispatcher { - public class PluginCommand : BaseCommand + public class UserPluginQueryDispatcher : IQueryDispatcher { - public override void Dispatch(Query query) + public void Dispatch(Query query) { - PluginPair thirdPlugin = PluginManager.AllPlugins.FirstOrDefault(o => o.Metadata.ActionKeyword == query.ActionName); - if (thirdPlugin != null && !string.IsNullOrEmpty(thirdPlugin.Metadata.ActionKeyword)) + PluginPair userPlugin = PluginManager.AllPlugins.FirstOrDefault(o => o.Metadata.ActionKeyword == query.ActionName); + if (userPlugin != null && !string.IsNullOrEmpty(userPlugin.Metadata.ActionKeyword)) { - var customizedPluginConfig = UserSettingStorage.Instance.CustomizedPluginConfigs.FirstOrDefault(o => o.ID == thirdPlugin.Metadata.ID); + var customizedPluginConfig = UserSettingStorage.Instance.CustomizedPluginConfigs.FirstOrDefault(o => o.ID == userPlugin.Metadata.ID); if (customizedPluginConfig != null && customizedPluginConfig.Disabled) { //need to stop the loading animation - UpdateResultView(null); + PluginManager.API.StopLoadingBar(); return; } @@ -29,12 +27,12 @@ namespace Wox.Commands { try { - List results = thirdPlugin.Plugin.Query(query) ?? new List(); - App.Window.PushResults(query,thirdPlugin.Metadata,results); + List results = userPlugin.Plugin.Query(query) ?? new List(); + PluginManager.API.PushResults(query,userPlugin.Metadata,results); } catch (Exception queryException) { - Log.Error(string.Format("Plugin {0} query failed: {1}", thirdPlugin.Metadata.Name, + Log.Error(string.Format("Plugin {0} query failed: {1}", userPlugin.Metadata.Name, queryException.Message)); #if (DEBUG) { diff --git a/Wox.Core/README.txt b/Wox.Core/README.txt index 3cd91680db..5a0b695dc5 100644 --- a/Wox.Core/README.txt +++ b/Wox.Core/README.txt @@ -1,4 +1,4 @@ What does Wox.Core do? * Handle Query -* Loading Plugins \ No newline at end of file +* Loading Plugins (including system plugin and user plugin) \ No newline at end of file diff --git a/Wox.Core/Wox.Core.csproj b/Wox.Core/Wox.Core.csproj index 34f0938d78..ec5cf28ca2 100644 --- a/Wox.Core/Wox.Core.csproj +++ b/Wox.Core/Wox.Core.csproj @@ -18,7 +18,7 @@ true full false - bin\Debug\ + ..\Output\Debug\ DEBUG;TRACE prompt 4 @@ -38,12 +38,17 @@ + + + + + diff --git a/Wox.Infrastructure/ChineseToPinYin.cs b/Wox.Infrastructure/ChineseToPinYin.cs deleted file mode 100644 index 1f59d7c282..0000000000 --- a/Wox.Infrastructure/ChineseToPinYin.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace Wox.Infrastructure -{ - public static class ChineseToPinYin - { - [Obsolete] - public static string ToPinYin(string txt) - { - return txt.Unidecode(); - } - } -} diff --git a/Wox.Infrastructure/Wox.Infrastructure.csproj b/Wox.Infrastructure/Wox.Infrastructure.csproj index c50978f96d..e3cfa6398a 100644 --- a/Wox.Infrastructure/Wox.Infrastructure.csproj +++ b/Wox.Infrastructure/Wox.Infrastructure.csproj @@ -67,21 +67,18 @@ + + + + - - - - - - - diff --git a/Wox.Plugin.SystemPlugins/Program/ProgramSetting.xaml b/Wox.Plugin.SystemPlugins/Program/ProgramSetting.xaml index ded91a25c2..6f00f4cd5c 100644 --- a/Wox.Plugin.SystemPlugins/Program/ProgramSetting.xaml +++ b/Wox.Plugin.SystemPlugins/Program/ProgramSetting.xaml @@ -4,6 +4,7 @@ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:infrastructure="clr-namespace:Wox.Infrastructure;assembly=Wox.Infrastructure" + xmlns:program="clr-namespace:Wox.Plugin.SystemPlugins.Program" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="600"> @@ -26,7 +27,7 @@ - + diff --git a/Wox.Infrastructure/StringEmptyConverter.cs b/Wox.Plugin.SystemPlugins/Program/StringEmptyConverter.cs similarity index 87% rename from Wox.Infrastructure/StringEmptyConverter.cs rename to Wox.Plugin.SystemPlugins/Program/StringEmptyConverter.cs index f553695737..c2d5366306 100644 --- a/Wox.Infrastructure/StringEmptyConverter.cs +++ b/Wox.Plugin.SystemPlugins/Program/StringEmptyConverter.cs @@ -1,11 +1,8 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; using System.Windows.Data; using System.Windows.Markup; -namespace Wox.Infrastructure +namespace Wox.Plugin.SystemPlugins.Program { public class StringEmptyConverter : MarkupExtension, IValueConverter { diff --git a/Wox.Plugin.SystemPlugins/ThirdpartyPluginIndicator.cs b/Wox.Plugin.SystemPlugins/UserPluginIndicator.cs similarity index 97% rename from Wox.Plugin.SystemPlugins/ThirdpartyPluginIndicator.cs rename to Wox.Plugin.SystemPlugins/UserPluginIndicator.cs index 31e3ab0acf..cc1d492a3b 100644 --- a/Wox.Plugin.SystemPlugins/ThirdpartyPluginIndicator.cs +++ b/Wox.Plugin.SystemPlugins/UserPluginIndicator.cs @@ -8,7 +8,7 @@ using Wox.Infrastructure.Storage.UserSettings; namespace Wox.Plugin.SystemPlugins { - public class ThirdpartyPluginIndicator : BaseSystemPlugin + public class UserPluginIndicator : BaseSystemPlugin { private List allPlugins = new List(); private PluginInitContext context; diff --git a/Wox.Plugin.SystemPlugins/Wox.Plugin.SystemPlugins.csproj b/Wox.Plugin.SystemPlugins/Wox.Plugin.SystemPlugins.csproj index 7fb13e044d..75604f9377 100644 --- a/Wox.Plugin.SystemPlugins/Wox.Plugin.SystemPlugins.csproj +++ b/Wox.Plugin.SystemPlugins/Wox.Plugin.SystemPlugins.csproj @@ -86,6 +86,7 @@ ProgramSuffixes.xaml + @@ -102,7 +103,7 @@ - + diff --git a/Wox.Plugin/IPublicAPI.cs b/Wox.Plugin/IPublicAPI.cs index 64ff13ab08..5ae94e0a40 100644 --- a/Wox.Plugin/IPublicAPI.cs +++ b/Wox.Plugin/IPublicAPI.cs @@ -6,7 +6,12 @@ namespace Wox.Plugin { public interface IPublicAPI { - + /// + /// Push result to query window + /// + /// + /// + /// void PushResults(Query query,PluginMetadata plugin, List results); bool ShellRun(string cmd, bool runAsAdministrator = false); diff --git a/Wox.Plugin/PluginType.cs b/Wox.Plugin/PluginType.cs index a36cc50b27..d046b579f0 100644 --- a/Wox.Plugin/PluginType.cs +++ b/Wox.Plugin/PluginType.cs @@ -8,6 +8,6 @@ namespace Wox.Plugin public enum PluginType { System, - ThirdParty + User } } diff --git a/Wox/CommandArgs/ReloadPluginCommandArg.cs b/Wox/CommandArgs/ReloadPluginCommandArg.cs index 825f4439f6..f287e08c85 100644 --- a/Wox/CommandArgs/ReloadPluginCommandArg.cs +++ b/Wox/CommandArgs/ReloadPluginCommandArg.cs @@ -15,7 +15,7 @@ namespace Wox.CommandArgs public void Execute(IList args) { - PluginManager.Init(); + PluginManager.Init(App.Window); } } } diff --git a/Wox/Commands/BaseCommand.cs b/Wox/Commands/BaseCommand.cs deleted file mode 100644 index 8a5283e77a..0000000000 --- a/Wox/Commands/BaseCommand.cs +++ /dev/null @@ -1,18 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using Wox.Plugin; - -namespace Wox.Commands -{ - public abstract class BaseCommand - { - public abstract void Dispatch(Query query); - - protected void UpdateResultView(List results) - { - App.Window.OnUpdateResultView(results); - } - } -} diff --git a/Wox/Commands/CommandFactory.cs b/Wox/Commands/CommandFactory.cs deleted file mode 100644 index b7b2eb9c20..0000000000 --- a/Wox/Commands/CommandFactory.cs +++ /dev/null @@ -1,28 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using Wox.Core.Plugin; -using Wox.Helper; -using Wox.Plugin; - -namespace Wox.Commands -{ - internal static class CommandFactory - { - private static PluginCommand pluginCmd = new PluginCommand(); - private static SystemCommand systemCmd = new SystemCommand(); - - public static void DispatchCommand(Query query) - { - if (PluginManager.HitThirdpartyKeyword(query)) - { - pluginCmd.Dispatch(query); - } - else - { - systemCmd.Dispatch(query); - } - } - } -} diff --git a/Wox/Helper/PluginInstaller.cs b/Wox/Helper/PluginInstaller.cs index ed66f5eb24..5cc0a9568a 100644 --- a/Wox/Helper/PluginInstaller.cs +++ b/Wox/Helper/PluginInstaller.cs @@ -116,7 +116,7 @@ namespace Wox.Helper try { metadata = JsonConvert.DeserializeObject(File.ReadAllText(configPath)); - metadata.PluginType = PluginType.ThirdParty; + metadata.PluginType = PluginType.User; metadata.PluginDirectory = pluginDirectory; } catch (Exception) diff --git a/Wox/ImageLoader/ImageCacheStroage.cs b/Wox/ImageLoader/ImageCacheStroage.cs index 88526e3e45..a090d95cf9 100644 --- a/Wox/ImageLoader/ImageCacheStroage.cs +++ b/Wox/ImageLoader/ImageCacheStroage.cs @@ -9,8 +9,8 @@ namespace Wox.ImageLoader [Serializable] public class ImageCacheStroage : BinaryStorage { - public int counter = 0; - public const int maxCached = 200; + private int counter = 0; + private const int maxCached = 200; public Dictionary TopUsedImages = new Dictionary(); protected override string ConfigName diff --git a/Wox/MainWindow.xaml.cs b/Wox/MainWindow.xaml.cs index 3fe95171dc..79befea4d8 100644 --- a/Wox/MainWindow.xaml.cs +++ b/Wox/MainWindow.xaml.cs @@ -14,7 +14,6 @@ using WindowsInput; using WindowsInput.Native; using NHotkey; using NHotkey.Wpf; -using Wox.Commands; using Wox.Core.Plugin; using Wox.Helper; using Wox.Infrastructure; @@ -22,6 +21,7 @@ using Wox.Infrastructure.Hotkey; using Wox.Infrastructure.Storage; using Wox.Infrastructure.Storage.UserSettings; using Wox.Plugin; +using Wox.Storage; using Wox.Update; using Application = System.Windows.Application; using Brushes = System.Windows.Media.Brushes; @@ -127,7 +127,7 @@ namespace Wox public void ReloadPlugins() { - Dispatcher.Invoke(new Action(PluginManager.Init)); + Dispatcher.Invoke(new Action(()=> PluginManager.Init(this))); } public List GetAllPlugins() @@ -192,7 +192,7 @@ namespace Wox ThreadPool.QueueUserWorkItem(o => { Thread.Sleep(50); - PluginManager.Init(); + PluginManager.Init(this); }); ThreadPool.QueueUserWorkItem(o => { @@ -357,9 +357,9 @@ namespace Wox }, TimeSpan.FromMilliseconds(100), null); queryHasReturn = false; var q = new Query(lastQuery); - CommandFactory.DispatchCommand(q); + PluginManager.Query(q); BackToResultMode(); - if (PluginManager.HitThirdpartyKeyword(q)) + if (PluginManager.IsUserPluginQuery(q)) { Dispatcher.DelayInvoke("ShowProgressbar", originQuery => { diff --git a/Wox/SettingWindow.xaml.cs b/Wox/SettingWindow.xaml.cs index a356bdec9c..c8ae9cd0b3 100644 --- a/Wox/SettingWindow.xaml.cs +++ b/Wox/SettingWindow.xaml.cs @@ -195,7 +195,7 @@ namespace Wox new CollectionContainer { Collection = - PluginManager.AllPlugins.Where(o => o.Metadata.PluginType == PluginType.ThirdParty) + PluginManager.AllPlugins.Where(o => o.Metadata.PluginType == PluginType.User) } }; lbPlugins.ItemsSource = plugins; diff --git a/Wox.Infrastructure/Storage/UserSelectedRecordStorage.cs b/Wox/Storage/UserSelectedRecordStorage.cs similarity index 88% rename from Wox.Infrastructure/Storage/UserSelectedRecordStorage.cs rename to Wox/Storage/UserSelectedRecordStorage.cs index 0b21e320e7..2b6c4d217c 100644 --- a/Wox.Infrastructure/Storage/UserSelectedRecordStorage.cs +++ b/Wox/Storage/UserSelectedRecordStorage.cs @@ -1,12 +1,9 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; +using System.Collections.Generic; using Newtonsoft.Json; using Wox.Infrastructure.Storage; using Wox.Plugin; -namespace Wox.Infrastructure.Storage +namespace Wox.Storage { public class UserSelectedRecordStorage : JsonStrorage { diff --git a/Wox/Wox.csproj b/Wox/Wox.csproj index 66bb922123..dc218d536d 100644 --- a/Wox/Wox.csproj +++ b/Wox/Wox.csproj @@ -105,6 +105,7 @@ + NewVersionWindow.xaml @@ -124,10 +125,6 @@ - - - - From d9b28633825d075960723a134846e6f8af40ad7e Mon Sep 17 00:00:00 2001 From: qianlifeng Date: Sat, 27 Dec 2014 12:34:51 +0800 Subject: [PATCH 2/6] Refactoring. --- .../Plugin}/PluginInstaller.cs | 25 ++--- Wox.Core/Plugin/PluginManager.cs | 31 +++++- .../SystemPluginQueryDispatcher.cs | 2 +- Wox.Core/Wox.Core.csproj | 5 + Wox.Core/packages.config | 1 + .../UserSettings/UserSettingStorage.cs | 10 +- Wox.Plugin/PluginType.cs | 2 +- Wox/CommandArgs/InstallPluginCommandArg.cs | 3 +- Wox/Converters/AsyncConverter.cs | 38 ------- Wox/ImageLoader/ImageLoader.cs | 30 ++--- Wox/MainWindow.xaml.cs | 52 +-------- Wox/SettingWindow.xaml.cs | 17 ++- Wox/ThemeManager.cs | 104 ++++++++++++++++++ Wox/Wox.csproj | 12 +- 14 files changed, 184 insertions(+), 148 deletions(-) rename {Wox/Helper => Wox.Core/Plugin}/PluginInstaller.cs (90%) delete mode 100644 Wox/Converters/AsyncConverter.cs create mode 100644 Wox/ThemeManager.cs diff --git a/Wox/Helper/PluginInstaller.cs b/Wox.Core/Plugin/PluginInstaller.cs similarity index 90% rename from Wox/Helper/PluginInstaller.cs rename to Wox.Core/Plugin/PluginInstaller.cs index 5cc0a9568a..3388c8c621 100644 --- a/Wox/Helper/PluginInstaller.cs +++ b/Wox.Core/Plugin/PluginInstaller.cs @@ -1,18 +1,17 @@ using System; using System.Diagnostics; using System.IO; -using System.Linq; using System.Windows; +using System.Windows.Forms; using ICSharpCode.SharpZipLib.Zip; using Newtonsoft.Json; -using Wox.Core.Plugin; using Wox.Plugin; -namespace Wox.Helper +namespace Wox.Core.Plugin { - public class PluginInstaller + internal class PluginInstaller { - public static void Install(string path) + internal static void Install(string path) { if (File.Exists(path)) { @@ -37,11 +36,7 @@ namespace Wox.Helper return; } - string pluginFolerPath = Path.Combine(Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath), "Plugins"); - if (!Directory.Exists(pluginFolerPath)) - { - Directory.CreateDirectory(pluginFolerPath); - } + string pluginFolerPath = PluginManager.DefaultPluginDirectory; string newPluginName = plugin.Name .Replace("/", "_") @@ -66,9 +61,9 @@ namespace Wox.Helper plugin.Name, existingPlugin.Metadata.Version, plugin.Version, plugin.Author); } - MessageBoxResult result = MessageBox.Show(content, "Install plugin", - MessageBoxButton.YesNo, MessageBoxImage.Question); - if (result == MessageBoxResult.Yes) + DialogResult result = MessageBox.Show(content, "Install plugin", MessageBoxButtons.YesNo, + MessageBoxIcon.Question); + if (result == DialogResult.Yes) { if (existingPlugin != null && Directory.Exists(existingPlugin.Metadata.PluginDirectory)) { @@ -88,7 +83,7 @@ namespace Wox.Helper // Plugins.Init(); //} if (MessageBox.Show("You have installed plugin " + plugin.Name + " successfully.\r\n Restart Wox to take effect?", "Install plugin", - MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes) + MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { ProcessStartInfo Info = new ProcessStartInfo(); Info.Arguments = "/C ping 127.0.0.1 -n 1 && \"" + @@ -97,7 +92,7 @@ namespace Wox.Helper Info.CreateNoWindow = true; Info.FileName = "cmd.exe"; Process.Start(Info); - App.Window.CloseApp(); + PluginManager.API.CloseApp(); } } } diff --git a/Wox.Core/Plugin/PluginManager.cs b/Wox.Core/Plugin/PluginManager.cs index e3b0217869..fbe46db441 100644 --- a/Wox.Core/Plugin/PluginManager.cs +++ b/Wox.Core/Plugin/PluginManager.cs @@ -24,17 +24,31 @@ namespace Wox.Core.Plugin /// private static List pluginDirectories = new List(); + + /// + /// Default plugin directory + /// new plugin will be installed to this directory + /// + public static string DefaultPluginDirectory + { + get + { + string userProfilePath = Environment.GetEnvironmentVariable("USERPROFILE"); + if (userProfilePath != null) + { + return Path.Combine(Path.Combine(userProfilePath, ".Wox"), "Plugins"); + } + + return string.Empty; + } + } + static PluginManager() { + pluginDirectories.Add(DefaultPluginDirectory); pluginDirectories.Add( Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Plugins")); - string userProfilePath = Environment.GetEnvironmentVariable("USERPROFILE"); - if (userProfilePath != null) - { - pluginDirectories.Add(Path.Combine(Path.Combine(userProfilePath, ".Wox"), "Plugins")); - } - MakesurePluginDirectoriesExist(); } @@ -73,6 +87,11 @@ namespace Wox.Core.Plugin } } + public static void InstallPlugin(string path) + { + PluginInstaller.Install(path); + } + public static void Query(Query query) { QueryDispatcher.QueryDispatcher.Dispatch(query); diff --git a/Wox.Core/Plugin/QueryDispatcher/SystemPluginQueryDispatcher.cs b/Wox.Core/Plugin/QueryDispatcher/SystemPluginQueryDispatcher.cs index b72c2e0760..a20aa8ebf7 100644 --- a/Wox.Core/Plugin/QueryDispatcher/SystemPluginQueryDispatcher.cs +++ b/Wox.Core/Plugin/QueryDispatcher/SystemPluginQueryDispatcher.cs @@ -19,7 +19,7 @@ namespace Wox.Core.Plugin.QueryDispatcher //websearch mode queryPlugins = new List() { - allSytemPlugins.First(o => ((ISystemPlugin)o.Plugin).ID == "565B73353DBF4806919830B9202EE3BF") + allSytemPlugins.First(o => o.Metadata.ID == "565B73353DBF4806919830B9202EE3BF") }; } diff --git a/Wox.Core/Wox.Core.csproj b/Wox.Core/Wox.Core.csproj index ec5cf28ca2..237c191560 100644 --- a/Wox.Core/Wox.Core.csproj +++ b/Wox.Core/Wox.Core.csproj @@ -32,6 +32,10 @@ 4 + + False + ..\packages\SharpZipLib.0.86.0\lib\20\ICSharpCode.SharpZipLib.dll + False ..\packages\Newtonsoft.Json.6.0.7\lib\net35\Newtonsoft.Json.dll @@ -45,6 +49,7 @@ + diff --git a/Wox.Core/packages.config b/Wox.Core/packages.config index 4185726464..6311fc40d5 100644 --- a/Wox.Core/packages.config +++ b/Wox.Core/packages.config @@ -1,4 +1,5 @@  + \ No newline at end of file diff --git a/Wox.Infrastructure/Storage/UserSettings/UserSettingStorage.cs b/Wox.Infrastructure/Storage/UserSettings/UserSettingStorage.cs index 229e100a2a..beb156a4fd 100644 --- a/Wox.Infrastructure/Storage/UserSettings/UserSettingStorage.cs +++ b/Wox.Infrastructure/Storage/UserSettings/UserSettingStorage.cs @@ -174,7 +174,15 @@ namespace Wox.Infrastructure.Storage.UserSettings if (string.IsNullOrEmpty(storage.ProgramSuffixes)) { storage.ProgramSuffixes = "lnk;exe;appref-ms;bat"; - } + } + if (storage.QueryBoxFont == null) + { + storage.QueryBoxFont = FontFamily.GenericSansSerif.Name; + } + if (storage.ResultItemFont == null) + { + storage.ResultItemFont = FontFamily.GenericSansSerif.Name; + } } } diff --git a/Wox.Plugin/PluginType.cs b/Wox.Plugin/PluginType.cs index d046b579f0..b8ffc6e124 100644 --- a/Wox.Plugin/PluginType.cs +++ b/Wox.Plugin/PluginType.cs @@ -10,4 +10,4 @@ namespace Wox.Plugin System, User } -} +} \ No newline at end of file diff --git a/Wox/CommandArgs/InstallPluginCommandArg.cs b/Wox/CommandArgs/InstallPluginCommandArg.cs index 2c921008f3..e69d20a633 100644 --- a/Wox/CommandArgs/InstallPluginCommandArg.cs +++ b/Wox/CommandArgs/InstallPluginCommandArg.cs @@ -4,6 +4,7 @@ using System.IO; using System.Linq; using System.Text; using System.Windows; +using Wox.Core.Plugin; using Wox.Helper; namespace Wox.CommandArgs @@ -25,7 +26,7 @@ namespace Wox.CommandArgs MessageBox.Show("Plugin " + path + " didn't exist"); return; } - PluginInstaller.Install(path); + PluginManager.InstallPlugin(path); } } } diff --git a/Wox/Converters/AsyncConverter.cs b/Wox/Converters/AsyncConverter.cs deleted file mode 100644 index 5df3bc7b5f..0000000000 --- a/Wox/Converters/AsyncConverter.cs +++ /dev/null @@ -1,38 +0,0 @@ -using System; -using System.ComponentModel; -using System.Threading; -using System.Windows.Data; -using System.Windows.Threading; - -namespace Wox.Converters -{ - public class AsyncTask : INotifyPropertyChanged - { - public AsyncTask(Func valueFunc) - { - LoadValue(valueFunc); - } - - private void LoadValue(Func valueFunc) - { - var frame = new DispatcherFrame(); - ThreadPool.QueueUserWorkItem(delegate - { - - object returnValue = - AsyncValue = valueFunc(); - if (PropertyChanged != null) - PropertyChanged(this, new PropertyChangedEventArgs("AsyncValue")); - - }); - } - - public event PropertyChangedEventHandler PropertyChanged; - - public object AsyncValue - { - get; - set; - } - } -} diff --git a/Wox/ImageLoader/ImageLoader.cs b/Wox/ImageLoader/ImageLoader.cs index 5eccebb2b0..55b89da82c 100644 --- a/Wox/ImageLoader/ImageLoader.cs +++ b/Wox/ImageLoader/ImageLoader.cs @@ -13,7 +13,6 @@ namespace Wox.ImageLoader public class ImageLoader { private static readonly Dictionary imageCache = new Dictionary(); - private static object locker = new object(); private static readonly List imageExts = new List { @@ -54,23 +53,20 @@ namespace Wox.ImageLoader { //ImageCacheStroage.Instance.TopUsedImages can be changed during foreach, so we need to make a copy var imageList = new Dictionary(ImageCacheStroage.Instance.TopUsedImages); - using (new Timeit(string.Format("Preload {0} images",imageList.Count))) + using (new Timeit(string.Format("Preload {0} images", imageList.Count))) { foreach (var image in imageList) { - ImageSource img = Load(image.Key, false); - if (img != null) + if (!imageCache.ContainsKey(image.Key)) { - img.Freeze(); //to make it copy to UI thread - if (!imageCache.ContainsKey(image.Key)) + ImageSource img = Load(image.Key, false); + if (img != null) { - lock (locker) + img.Freeze(); //to make it copy to UI thread + if (!imageCache.ContainsKey(image.Key)) { - if (!imageCache.ContainsKey(image.Key)) - { - KeyValuePair copyedImg = image; - App.Window.Dispatcher.Invoke(new Action(() => imageCache.Add(copyedImg.Key, img))); - } + KeyValuePair copyedImg = image; + App.Window.Dispatcher.Invoke(new Action(() => imageCache.Add(copyedImg.Key, img))); } } } @@ -78,7 +74,7 @@ namespace Wox.ImageLoader } } - public static ImageSource Load(string path,bool addToCache = true) + public static ImageSource Load(string path, bool addToCache = true) { if (string.IsNullOrEmpty(path)) return null; if (addToCache) @@ -112,13 +108,7 @@ namespace Wox.ImageLoader { if (!imageCache.ContainsKey(path)) { - lock (locker) - { - if (!imageCache.ContainsKey(path)) - { - imageCache.Add(path, img); - } - } + imageCache.Add(path, img); } } diff --git a/Wox/MainWindow.xaml.cs b/Wox/MainWindow.xaml.cs index 79befea4d8..d192dd9f37 100644 --- a/Wox/MainWindow.xaml.cs +++ b/Wox/MainWindow.xaml.cs @@ -122,7 +122,7 @@ namespace Wox public void InstallPlugin(string path) { - Dispatcher.Invoke(new Action(() => PluginInstaller.Install(path))); + Dispatcher.Invoke(new Action(() => PluginManager.InstallPlugin(path))); } public void ReloadPlugins() @@ -172,14 +172,8 @@ namespace Wox pnlResult.RightMouseClickEvent += pnlResult_RightMouseClickEvent; ThreadPool.SetMaxThreads(30, 10); - try - { - SetTheme(UserSettingStorage.Instance.Theme); - } - catch (Exception) - { - SetTheme(UserSettingStorage.Instance.Theme = "Dark"); - } + ThemeManager.ChangeTheme(UserSettingStorage.Instance.Theme); + SetHotkey(UserSettingStorage.Instance.Hotkey, OnHotkey); SetCustomPluginHotkey(); @@ -676,44 +670,6 @@ namespace Wox } } - public void SetTheme(string themeName) - { - var dict = new ResourceDictionary - { - Source = new Uri(Path.Combine(Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath), "Themes\\" + themeName + ".xaml"), UriKind.Absolute) - }; - - - Style queryBoxStyle = dict["QueryBoxStyle"] as Style; - if (queryBoxStyle != null) - { - queryBoxStyle.Setters.Add(new Setter(TextBox.FontFamilyProperty, new FontFamily(UserSettingStorage.Instance.QueryBoxFont))); - queryBoxStyle.Setters.Add(new Setter(TextBox.FontStyleProperty, FontHelper.GetFontStyleFromInvariantStringOrNormal(UserSettingStorage.Instance.QueryBoxFontStyle))); - queryBoxStyle.Setters.Add(new Setter(TextBox.FontWeightProperty, FontHelper.GetFontWeightFromInvariantStringOrNormal(UserSettingStorage.Instance.QueryBoxFontWeight))); - queryBoxStyle.Setters.Add(new Setter(TextBox.FontStretchProperty, FontHelper.GetFontStretchFromInvariantStringOrNormal(UserSettingStorage.Instance.QueryBoxFontStretch))); - } - - Style resultItemStyle = dict["ItemTitleStyle"] as Style; - Style resultSubItemStyle = dict["ItemSubTitleStyle"] as Style; - Style resultItemSelectedStyle = dict["ItemTitleSelectedStyle"] as Style; - Style resultSubItemSelectedStyle = dict["ItemSubTitleSelectedStyle"] as Style; - if (resultItemStyle != null && resultSubItemStyle != null && resultSubItemSelectedStyle != null && resultItemSelectedStyle != null) - { - Setter fontFamily = new Setter(TextBlock.FontFamilyProperty, new FontFamily(UserSettingStorage.Instance.ResultItemFont)); - Setter fontStyle = new Setter(TextBlock.FontStyleProperty, FontHelper.GetFontStyleFromInvariantStringOrNormal(UserSettingStorage.Instance.ResultItemFontStyle)); - Setter fontWeight = new Setter(TextBlock.FontWeightProperty, FontHelper.GetFontWeightFromInvariantStringOrNormal(UserSettingStorage.Instance.ResultItemFontWeight)); - Setter fontStretch = new Setter(TextBlock.FontStretchProperty, FontHelper.GetFontStretchFromInvariantStringOrNormal(UserSettingStorage.Instance.ResultItemFontStretch)); - - Setter[] setters = new Setter[] { fontFamily, fontStyle, fontWeight, fontStretch }; - Array.ForEach(new Style[] { resultItemStyle, resultSubItemStyle, resultItemSelectedStyle, resultSubItemSelectedStyle }, o => Array.ForEach(setters, p => o.Setters.Add(p))); - } - - Application.Current.Resources.MergedDictionaries.Clear(); - Application.Current.Resources.MergedDictionaries.Add(dict); - - this.Opacity = this.AllowsTransparency ? UserSettingStorage.Instance.Opacity : 1; - } - public bool ShellRun(string cmd, bool runAsAdministrator = false) { try @@ -739,7 +695,7 @@ namespace Wox string[] files = (string[])e.Data.GetData(System.Windows.DataFormats.FileDrop); if (files[0].ToLower().EndsWith(".wox")) { - PluginInstaller.Install(files[0]); + PluginManager.InstallPlugin(files[0]); } else { diff --git a/Wox/SettingWindow.xaml.cs b/Wox/SettingWindow.xaml.cs index c8ae9cd0b3..a8107ce46e 100644 --- a/Wox/SettingWindow.xaml.cs +++ b/Wox/SettingWindow.xaml.cs @@ -172,8 +172,7 @@ namespace Wox } //PreviewPanel - App.Window.SetTheme(UserSettingStorage.Instance.Theme); - + ThemeManager.ChangeTheme(UserSettingStorage.Instance.Theme); #endregion #region Plugin @@ -366,7 +365,7 @@ namespace Wox private void ThemeComboBox_OnSelectionChanged(object sender, SelectionChangedEventArgs e) { string themeName = themeComboBox.SelectedItem.ToString(); - MainWindow.SetTheme(themeName); + ThemeManager.ChangeTheme(themeName); UserSettingStorage.Instance.Theme = themeName; UserSettingStorage.Instance.Save(); } @@ -379,7 +378,7 @@ namespace Wox this.cbQueryBoxFontFaces.SelectedItem = ((FontFamily)cbQueryBoxFont.SelectedItem).ChooseRegularFamilyTypeface(); UserSettingStorage.Instance.Save(); - App.Window.SetTheme(UserSettingStorage.Instance.Theme); + ThemeManager.ChangeTheme(UserSettingStorage.Instance.Theme); } private void CbQueryBoxFontFaces_OnSelectionChanged(object sender, SelectionChangedEventArgs e) @@ -399,7 +398,7 @@ namespace Wox UserSettingStorage.Instance.QueryBoxFontWeight = typeface.Weight.ToString(); UserSettingStorage.Instance.QueryBoxFontStyle = typeface.Style.ToString(); UserSettingStorage.Instance.Save(); - App.Window.SetTheme(UserSettingStorage.Instance.Theme); + ThemeManager.ChangeTheme(UserSettingStorage.Instance.Theme); } } @@ -411,7 +410,7 @@ namespace Wox this.cbResultItemFontFaces.SelectedItem = ((FontFamily)cbResultItemFont.SelectedItem).ChooseRegularFamilyTypeface(); UserSettingStorage.Instance.Save(); - App.Window.SetTheme(UserSettingStorage.Instance.Theme); + ThemeManager.ChangeTheme(UserSettingStorage.Instance.Theme); } private void CbResultItemFontFaces_OnSelectionChanged(object sender, SelectionChangedEventArgs e) @@ -422,8 +421,6 @@ namespace Wox { if (cbResultItemFontFaces.Items.Count > 0) cbResultItemFontFaces.SelectedIndex = 0; - - return; } else { @@ -431,7 +428,7 @@ namespace Wox UserSettingStorage.Instance.ResultItemFontWeight = typeface.Weight.ToString(); UserSettingStorage.Instance.ResultItemFontStyle = typeface.Style.ToString(); UserSettingStorage.Instance.Save(); - App.Window.SetTheme(UserSettingStorage.Instance.Theme); + ThemeManager.ChangeTheme(UserSettingStorage.Instance.Theme); } } @@ -445,7 +442,7 @@ namespace Wox else PreviewMainPanel.Opacity = 1; - App.Window.SetTheme(UserSettingStorage.Instance.Theme); + ThemeManager.ChangeTheme(UserSettingStorage.Instance.Theme); } #endregion diff --git a/Wox/ThemeManager.cs b/Wox/ThemeManager.cs new file mode 100644 index 0000000000..ce30c8161a --- /dev/null +++ b/Wox/ThemeManager.cs @@ -0,0 +1,104 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Media; +using Wox.Helper; +using Wox.Infrastructure.Storage.UserSettings; + +namespace Wox +{ + internal class ThemeManager + { + private static List themeDirectories = new List(); + + static ThemeManager() + { + themeDirectories.Add( + Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Themes")); + + string userProfilePath = Environment.GetEnvironmentVariable("USERPROFILE"); + if (userProfilePath != null) + { + themeDirectories.Add(Path.Combine(Path.Combine(userProfilePath, ".Wox"), "Themes")); + } + + MakesureThemeDirectoriesExist(); + } + + private static void MakesureThemeDirectoriesExist() + { + foreach (string pluginDirectory in themeDirectories) + { + if (!Directory.Exists(pluginDirectory)) + { + Directory.CreateDirectory(pluginDirectory); + } + } + } + + public static void ChangeTheme(string themeName) + { + string themePath = GetThemePath(themeName); + if (string.IsNullOrEmpty(themePath)) + { + themePath = GetThemePath("Dark"); + if (string.IsNullOrEmpty(themePath)) + { + throw new Exception("Change theme failed"); + } + } + + var dict = new ResourceDictionary + { + Source = new Uri(themePath, UriKind.Absolute) + }; + + Style queryBoxStyle = dict["QueryBoxStyle"] as Style; + if (queryBoxStyle != null) + { + queryBoxStyle.Setters.Add(new Setter(TextBox.FontFamilyProperty, new FontFamily(UserSettingStorage.Instance.QueryBoxFont))); + queryBoxStyle.Setters.Add(new Setter(TextBox.FontStyleProperty, FontHelper.GetFontStyleFromInvariantStringOrNormal(UserSettingStorage.Instance.QueryBoxFontStyle))); + queryBoxStyle.Setters.Add(new Setter(TextBox.FontWeightProperty, FontHelper.GetFontWeightFromInvariantStringOrNormal(UserSettingStorage.Instance.QueryBoxFontWeight))); + queryBoxStyle.Setters.Add(new Setter(TextBox.FontStretchProperty, FontHelper.GetFontStretchFromInvariantStringOrNormal(UserSettingStorage.Instance.QueryBoxFontStretch))); + } + + Style resultItemStyle = dict["ItemTitleStyle"] as Style; + Style resultSubItemStyle = dict["ItemSubTitleStyle"] as Style; + Style resultItemSelectedStyle = dict["ItemTitleSelectedStyle"] as Style; + Style resultSubItemSelectedStyle = dict["ItemSubTitleSelectedStyle"] as Style; + if (resultItemStyle != null && resultSubItemStyle != null && resultSubItemSelectedStyle != null && resultItemSelectedStyle != null) + { + Setter fontFamily = new Setter(TextBlock.FontFamilyProperty, new FontFamily(UserSettingStorage.Instance.ResultItemFont)); + Setter fontStyle = new Setter(TextBlock.FontStyleProperty, FontHelper.GetFontStyleFromInvariantStringOrNormal(UserSettingStorage.Instance.ResultItemFontStyle)); + Setter fontWeight = new Setter(TextBlock.FontWeightProperty, FontHelper.GetFontWeightFromInvariantStringOrNormal(UserSettingStorage.Instance.ResultItemFontWeight)); + Setter fontStretch = new Setter(TextBlock.FontStretchProperty, FontHelper.GetFontStretchFromInvariantStringOrNormal(UserSettingStorage.Instance.ResultItemFontStretch)); + + Setter[] setters = new Setter[] { fontFamily, fontStyle, fontWeight, fontStretch }; + Array.ForEach(new Style[] { resultItemStyle, resultSubItemStyle, resultItemSelectedStyle, resultSubItemSelectedStyle }, o => Array.ForEach(setters, p => o.Setters.Add(p))); + } + + Application.Current.Resources.MergedDictionaries.Clear(); + Application.Current.Resources.MergedDictionaries.Add(dict); + + } + + private static string GetThemePath(string themeName) + { + foreach (string themeDirectory in themeDirectories) + { + string path = Path.Combine(themeDirectory, themeName + ".xaml"); + if (File.Exists(path)) + { + return path; + } + } + + return string.Empty; + } + } +} diff --git a/Wox/Wox.csproj b/Wox/Wox.csproj index dc218d536d..b4e29e6d42 100644 --- a/Wox/Wox.csproj +++ b/Wox/Wox.csproj @@ -106,6 +106,7 @@ + NewVersionWindow.xaml @@ -125,7 +126,6 @@ - @@ -145,7 +145,6 @@ - HotkeyControl.xaml @@ -202,11 +201,6 @@ Designer MSBuild:Compile - - MSBuild:Compile - Designer - PreserveNewest - MSBuild:Compile Designer @@ -227,6 +221,10 @@ Designer PreserveNewest + + MSBuild:Compile + Designer + Designer MSBuild:Compile From 890397bae7061cdd8d05acefa4d1bd58c46eeb9e Mon Sep 17 00:00:00 2001 From: qianlifeng Date: Sun, 28 Dec 2014 15:17:58 +0800 Subject: [PATCH 3/6] Refactoring. Move plugin related work to Wox.Core --- Wox.Core/Exception/WoxCritialException.cs | 17 +++++++++++++++++ Wox.Core/Exception/WoxException.cs | 14 ++++++++++++++ .../Exception}/WoxHttpException.cs | 7 +------ .../Exception}/WoxJsonRPCException.cs | 2 +- Wox.Core/Plugin/CSharpPluginLoader.cs | 2 +- Wox.Core/Plugin/JsonRPCPlugin.cs | 5 ++--- Wox.Core/Plugin/JsonRPCPluginLoader.cs | 2 +- Wox.Core/Plugin/PluginConfig.cs | 4 ++-- Wox.Core/Plugin/PluginInstaller.cs | 8 ++++---- .../UserPluginQueryDispatcher.cs | 2 +- Wox.Core/Wox.Core.csproj | 4 ++++ Wox.Infrastructure/Exceptions/WoxException.cs | 13 ------------- Wox.Infrastructure/Wox.Infrastructure.csproj | 3 --- .../ControlPanel/ControlPanelItem.cs | 2 +- 14 files changed, 49 insertions(+), 36 deletions(-) create mode 100644 Wox.Core/Exception/WoxCritialException.cs create mode 100644 Wox.Core/Exception/WoxException.cs rename {Wox.Infrastructure/Exceptions => Wox.Core/Exception}/WoxHttpException.cs (51%) rename {Wox.Infrastructure/Exceptions => Wox.Core/Exception}/WoxJsonRPCException.cs (78%) delete mode 100644 Wox.Infrastructure/Exceptions/WoxException.cs diff --git a/Wox.Core/Exception/WoxCritialException.cs b/Wox.Core/Exception/WoxCritialException.cs new file mode 100644 index 0000000000..76aea3dea6 --- /dev/null +++ b/Wox.Core/Exception/WoxCritialException.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Wox.Core.Exception +{ + /// + /// Represent exceptions that wox can't handle and MUST close running Wox. + /// + public class WoxCritialException : WoxException + { + public WoxCritialException(string msg) : base(msg) + { + } + } +} diff --git a/Wox.Core/Exception/WoxException.cs b/Wox.Core/Exception/WoxException.cs new file mode 100644 index 0000000000..a5df7a3d34 --- /dev/null +++ b/Wox.Core/Exception/WoxException.cs @@ -0,0 +1,14 @@ +namespace Wox.Core.Exception +{ + /// + /// Base Wox Exceptions + /// + public class WoxException : System.Exception + { + public WoxException(string msg) + : base(msg) + { + + } + } +} diff --git a/Wox.Infrastructure/Exceptions/WoxHttpException.cs b/Wox.Core/Exception/WoxHttpException.cs similarity index 51% rename from Wox.Infrastructure/Exceptions/WoxHttpException.cs rename to Wox.Core/Exception/WoxHttpException.cs index 026d1d8ab9..55e3431a0e 100644 --- a/Wox.Infrastructure/Exceptions/WoxHttpException.cs +++ b/Wox.Core/Exception/WoxHttpException.cs @@ -1,9 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; - -namespace Wox.Infrastructure.Exceptions +namespace Wox.Core.Exception { public class WoxHttpException :WoxException { diff --git a/Wox.Infrastructure/Exceptions/WoxJsonRPCException.cs b/Wox.Core/Exception/WoxJsonRPCException.cs similarity index 78% rename from Wox.Infrastructure/Exceptions/WoxJsonRPCException.cs rename to Wox.Core/Exception/WoxJsonRPCException.cs index 0e984a3e6f..d3c0bfb0da 100644 --- a/Wox.Infrastructure/Exceptions/WoxJsonRPCException.cs +++ b/Wox.Core/Exception/WoxJsonRPCException.cs @@ -1,4 +1,4 @@ -namespace Wox.Infrastructure.Exceptions +namespace Wox.Core.Exception { public class WoxJsonRPCException : WoxException { diff --git a/Wox.Core/Plugin/CSharpPluginLoader.cs b/Wox.Core/Plugin/CSharpPluginLoader.cs index 9cfdc56734..5910a6bebf 100644 --- a/Wox.Core/Plugin/CSharpPluginLoader.cs +++ b/Wox.Core/Plugin/CSharpPluginLoader.cs @@ -44,7 +44,7 @@ namespace Wox.Core.Plugin plugins.Add(pair); } } - catch (Exception e) + catch (System.Exception e) { Log.Error(string.Format("Couldn't load plugin {0}: {1}", metadata.Name, e.Message)); #if (DEBUG) diff --git a/Wox.Core/Plugin/JsonRPCPlugin.cs b/Wox.Core/Plugin/JsonRPCPlugin.cs index 0809826300..fd0514af36 100644 --- a/Wox.Core/Plugin/JsonRPCPlugin.cs +++ b/Wox.Core/Plugin/JsonRPCPlugin.cs @@ -6,7 +6,6 @@ using System.Reflection; using System.Threading; using System.Windows.Forms; using Newtonsoft.Json; -using Wox.Infrastructure.Exceptions; using Wox.Infrastructure.Logger; using Wox.Plugin; @@ -73,7 +72,7 @@ namespace Wox.Core.Plugin } return results; } - catch (Exception e) + catch (System.Exception e) { Log.Error(e.Message); } @@ -90,7 +89,7 @@ namespace Wox.Core.Plugin { methodInfo.Invoke(PluginManager.API, parameters); } - catch (Exception) + catch (System.Exception) { #if (DEBUG) { diff --git a/Wox.Core/Plugin/JsonRPCPluginLoader.cs b/Wox.Core/Plugin/JsonRPCPluginLoader.cs index 234da6a25d..0bf35979c9 100644 --- a/Wox.Core/Plugin/JsonRPCPluginLoader.cs +++ b/Wox.Core/Plugin/JsonRPCPluginLoader.cs @@ -13,7 +13,7 @@ namespace Wox.Core.Plugin return jsonRPCPluginMetadatas.Select(metadata => new PluginPair() { - Plugin = jsonRPCPlugin, + Plugin = new T(), //every JsonRPC plugin should has its own plugin instance Metadata = metadata }).ToList(); } diff --git a/Wox.Core/Plugin/PluginConfig.cs b/Wox.Core/Plugin/PluginConfig.cs index 5d6c8137d4..6d2cba1488 100644 --- a/Wox.Core/Plugin/PluginConfig.cs +++ b/Wox.Core/Plugin/PluginConfig.cs @@ -4,7 +4,7 @@ using System.IO; using System.Linq; using System.Reflection; using Newtonsoft.Json; -using Wox.Infrastructure.Exceptions; +using Wox.Core.Exception; using Wox.Infrastructure.Logger; using Wox.Infrastructure.Storage.UserSettings; using Wox.Plugin; @@ -91,7 +91,7 @@ namespace Wox.Core.Plugin metadata.PluginType = PluginType.User; metadata.PluginDirectory = pluginDirectory; } - catch (Exception) + catch (System.Exception) { string error = string.Format("Parse plugin config {0} failed: json format is not valid", configPath); Log.Warn(error); diff --git a/Wox.Core/Plugin/PluginInstaller.cs b/Wox.Core/Plugin/PluginInstaller.cs index 3388c8c621..25e7d3b3c1 100644 --- a/Wox.Core/Plugin/PluginInstaller.cs +++ b/Wox.Core/Plugin/PluginInstaller.cs @@ -114,12 +114,12 @@ namespace Wox.Core.Plugin metadata.PluginType = PluginType.User; metadata.PluginDirectory = pluginDirectory; } - catch (Exception) + catch (System.Exception) { string error = string.Format("Parse plugin config {0} failed: json format is not valid", configPath); #if (DEBUG) { - throw new Exception(error); + throw new System.Exception(error); } #endif return null; @@ -132,7 +132,7 @@ namespace Wox.Core.Plugin metadata.Language); #if (DEBUG) { - throw new Exception(error); + throw new System.Exception(error); } #endif return null; @@ -143,7 +143,7 @@ namespace Wox.Core.Plugin metadata.ExecuteFilePath); #if (DEBUG) { - throw new Exception(error); + throw new System.Exception(error); } #endif return null; diff --git a/Wox.Core/Plugin/QueryDispatcher/UserPluginQueryDispatcher.cs b/Wox.Core/Plugin/QueryDispatcher/UserPluginQueryDispatcher.cs index 6c709e5bdc..3b96d2c3a8 100644 --- a/Wox.Core/Plugin/QueryDispatcher/UserPluginQueryDispatcher.cs +++ b/Wox.Core/Plugin/QueryDispatcher/UserPluginQueryDispatcher.cs @@ -30,7 +30,7 @@ namespace Wox.Core.Plugin.QueryDispatcher List results = userPlugin.Plugin.Query(query) ?? new List(); PluginManager.API.PushResults(query,userPlugin.Metadata,results); } - catch (Exception queryException) + catch (System.Exception queryException) { Log.Error(string.Format("Plugin {0} query failed: {1}", userPlugin.Metadata.Name, queryException.Message)); diff --git a/Wox.Core/Wox.Core.csproj b/Wox.Core/Wox.Core.csproj index 237c191560..1aacac6d10 100644 --- a/Wox.Core/Wox.Core.csproj +++ b/Wox.Core/Wox.Core.csproj @@ -49,6 +49,10 @@ + + + + diff --git a/Wox.Infrastructure/Exceptions/WoxException.cs b/Wox.Infrastructure/Exceptions/WoxException.cs deleted file mode 100644 index 581f36edb7..0000000000 --- a/Wox.Infrastructure/Exceptions/WoxException.cs +++ /dev/null @@ -1,13 +0,0 @@ -using System; - -namespace Wox.Infrastructure.Exceptions -{ - public class WoxException : Exception - { - public WoxException(string msg) - : base(msg) - { - - } - } -} diff --git a/Wox.Infrastructure/Wox.Infrastructure.csproj b/Wox.Infrastructure/Wox.Infrastructure.csproj index e3cfa6398a..7c84bf829c 100644 --- a/Wox.Infrastructure/Wox.Infrastructure.csproj +++ b/Wox.Infrastructure/Wox.Infrastructure.csproj @@ -56,9 +56,6 @@ - - - diff --git a/Wox.Plugin.SystemPlugins/ControlPanel/ControlPanelItem.cs b/Wox.Plugin.SystemPlugins/ControlPanel/ControlPanelItem.cs index 6d76e269a0..5781fa2910 100644 --- a/Wox.Plugin.SystemPlugins/ControlPanel/ControlPanelItem.cs +++ b/Wox.Plugin.SystemPlugins/ControlPanel/ControlPanelItem.cs @@ -22,4 +22,4 @@ namespace Wox.Plugin.SystemPlugins.ControlPanel GUID = newGUID; } } -} +} \ No newline at end of file From ba0bda6aa6645c3459b01ff4f0ece42656fc583f Mon Sep 17 00:00:00 2001 From: qianlifeng Date: Mon, 29 Dec 2014 21:55:27 +0800 Subject: [PATCH 4/6] Update the installer --- Deploy/Installer/Installer.iss | 14 ++++++++--- Wox.Core/Plugin/PluginManager.cs | 3 --- Wox.Core/{README.txt => README.md} | 1 + Wox.Core/Wox.Core.csproj | 2 +- Wox.Infrastructure/Storage/BaseStorage.cs | 29 ++++++++++++++++++----- Wox.Plugin/README.md | 5 ++++ Wox.Plugin/Wox.Plugin.csproj | 3 +++ Wox/App.config | 8 ++++++- Wox/Helper/WoxLogPathConverter.cs | 17 +++++++++++++ Wox/MainWindow.xaml.cs | 1 + Wox/ThemeManager.cs | 3 --- Wox/Wox.csproj | 1 + 12 files changed, 70 insertions(+), 17 deletions(-) rename Wox.Core/{README.txt => README.md} (94%) create mode 100644 Wox.Plugin/README.md create mode 100644 Wox/Helper/WoxLogPathConverter.cs diff --git a/Deploy/Installer/Installer.iss b/Deploy/Installer/Installer.iss index 21f08fd913..253572eef3 100644 --- a/Deploy/Installer/Installer.iss +++ b/Deploy/Installer/Installer.iss @@ -29,11 +29,13 @@ Name: english; MessagesFile: compiler:Default.isl Type: files; Name: "{commonstartup}\{#MyAppName}.lnk" [Tasks] -Name: desktopicon; Description: {cm:CreateDesktopIcon}; GroupDescription: {cm:AdditionalIcons}; Flags: unchecked +Name: desktopicon; Description: {cm:CreateDesktopIcon}; GroupDescription: {cm:AdditionalIcons}; Name: startupfolder; Description: Startup with Windows; [Files] -Source: {#MyAppPath}\*; DestDir: {app}; Flags: ignoreversion recursesubdirs +Source: {#MyAppPath}\*; Excludes: Plugins\*,Themes\*; DestDir: {app}; Flags: ignoreversion recursesubdirs +Source: {#MyAppPath}\Plugins\*; DestDir: {%USERPROFILE}\.Wox\Plugins; Flags: ignoreversion recursesubdirs +Source: {#MyAppPath}\Themes\*; DestDir: {%USERPROFILE}\.Wox\Themes; Flags: ignoreversion recursesubdirs [Icons] Name: {group}\{#MyAppName}; Filename: {app}\{#MyAppExeName} @@ -42,4 +44,10 @@ Name: {userdesktop}\{#MyAppName}; Filename: {app}\{#MyAppExeName}; Tasks: deskto Name: {userstartup}\{#MyAppName}; Filename: {app}\{#MyAppExeName}; Tasks: startupfolder [Run] -Filename: {app}\{#MyAppExeName}; Description: {cm:LaunchProgram,{#MyAppName}}; Flags: nowait postinstall skipifsilent +Filename: {app}\{#MyAppExeName}; Description: {cm:LaunchProgram,{#MyAppName}}; Flags: nowait postinstall skipifsilent unchecked + +[UninstallDelete] +Type: filesandordirs; Name: "{%USERPROFILE}\.Wox" + +[UninstallRun] +Filename: {sys}\taskkill.exe; Parameters: "/f /im Wox.exe"; Flags: skipifdoesntexist runhidden diff --git a/Wox.Core/Plugin/PluginManager.cs b/Wox.Core/Plugin/PluginManager.cs index fbe46db441..70d2918e26 100644 --- a/Wox.Core/Plugin/PluginManager.cs +++ b/Wox.Core/Plugin/PluginManager.cs @@ -46,9 +46,6 @@ namespace Wox.Core.Plugin static PluginManager() { pluginDirectories.Add(DefaultPluginDirectory); - pluginDirectories.Add( - Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Plugins")); - MakesurePluginDirectoriesExist(); } diff --git a/Wox.Core/README.txt b/Wox.Core/README.md similarity index 94% rename from Wox.Core/README.txt rename to Wox.Core/README.md index 5a0b695dc5..2c191d0856 100644 --- a/Wox.Core/README.txt +++ b/Wox.Core/README.md @@ -1,4 +1,5 @@ What does Wox.Core do? +===== * Handle Query * Loading Plugins (including system plugin and user plugin) \ No newline at end of file diff --git a/Wox.Core/Wox.Core.csproj b/Wox.Core/Wox.Core.csproj index 1aacac6d10..ce60a7f391 100644 --- a/Wox.Core/Wox.Core.csproj +++ b/Wox.Core/Wox.Core.csproj @@ -69,7 +69,7 @@ - + diff --git a/Wox.Infrastructure/Storage/BaseStorage.cs b/Wox.Infrastructure/Storage/BaseStorage.cs index d899cb6908..c72bf63c35 100644 --- a/Wox.Infrastructure/Storage/BaseStorage.cs +++ b/Wox.Infrastructure/Storage/BaseStorage.cs @@ -10,18 +10,35 @@ using Newtonsoft.Json; namespace Wox.Infrastructure.Storage { [Serializable] - public abstract class BaseStorage : IStorage where T : class,IStorage,new() + public abstract class BaseStorage : IStorage where T : class,IStorage, new() { - private readonly string configFolder = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "Config"); + private string configFolder; + + private string ConfigFolder + { + get + { + if (string.IsNullOrEmpty(configFolder)) + { + string userProfilePath = Environment.GetEnvironmentVariable("USERPROFILE"); + if (userProfilePath == null) + { + throw new ArgumentException("Environment variable USERPROFILE is empty"); + } + configFolder = Path.Combine(Path.Combine(userProfilePath, ".Wox"), "Config"); + } + return configFolder; + } + } protected string ConfigPath { get { - return Path.Combine(configFolder, ConfigName + FileSuffix); + return Path.Combine(ConfigFolder, ConfigName + FileSuffix); } } - + protected abstract string FileSuffix { get; } protected abstract string ConfigName { get; } @@ -72,9 +89,9 @@ namespace Wox.Infrastructure.Storage { if (!File.Exists(ConfigPath)) { - if (!Directory.Exists(configFolder)) + if (!Directory.Exists(ConfigFolder)) { - Directory.CreateDirectory(configFolder); + Directory.CreateDirectory(ConfigFolder); } File.Create(ConfigPath).Close(); } diff --git a/Wox.Plugin/README.md b/Wox.Plugin/README.md new file mode 100644 index 0000000000..1d5c7394da --- /dev/null +++ b/Wox.Plugin/README.md @@ -0,0 +1,5 @@ +What does Wox.Plugin do? +==== + +* Define base objects and interfaces for plugins +* Plugin Author who making C# plugin should reference this DLL via nuget \ No newline at end of file diff --git a/Wox.Plugin/Wox.Plugin.csproj b/Wox.Plugin/Wox.Plugin.csproj index abc3493ebb..d915ddd7bc 100644 --- a/Wox.Plugin/Wox.Plugin.csproj +++ b/Wox.Plugin/Wox.Plugin.csproj @@ -59,6 +59,9 @@ + + +