diff --git a/Plugins/Wox.Plugin.PluginIndicator/PluginIndicator.cs b/Plugins/Wox.Plugin.PluginIndicator/PluginIndicator.cs index 63f0c2e186..1283c77639 100644 --- a/Plugins/Wox.Plugin.PluginIndicator/PluginIndicator.cs +++ b/Plugins/Wox.Plugin.PluginIndicator/PluginIndicator.cs @@ -14,9 +14,8 @@ namespace Wox.Plugin.PluginIndicator 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 + let customizedPluginConfig = UserSettingStorage.Instance.CustomizedPluginConfigs[metadata.ID] + where !customizedPluginConfig.Disabled select new Result { Title = keyword, diff --git a/Wox.Core/Plugin/PluginConfig.cs b/Wox.Core/Plugin/PluginConfig.cs index 0a8d286c1d..7d0ba4958e 100644 --- a/Wox.Core/Plugin/PluginConfig.cs +++ b/Wox.Core/Plugin/PluginConfig.cs @@ -1,9 +1,7 @@ using System; using System.Collections.Generic; using System.IO; -using System.Linq; using Newtonsoft.Json; -using Wox.Core.UserSettings; using Wox.Infrastructure.Exception; using Wox.Infrastructure.Logger; using Wox.Plugin; @@ -74,7 +72,7 @@ namespace Wox.Core.Plugin metadata = JsonConvert.DeserializeObject(File.ReadAllText(configPath)); metadata.PluginDirectory = pluginDirectory; // for plugins which doesn't has ActionKeywords key - metadata.ActionKeywords = metadata.ActionKeywords ?? new List {metadata.ActionKeyword}; + metadata.ActionKeywords = metadata.ActionKeywords ?? new List { metadata.ActionKeyword }; // for plugin still use old ActionKeyword metadata.ActionKeyword = metadata.ActionKeywords?[0]; } @@ -100,14 +98,6 @@ namespace Wox.Core.Plugin return null; } - //replace action keyword if user customized it. - var customizedPluginConfig = UserSettingStorage.Instance.CustomizedPluginConfigs.FirstOrDefault(o => o.ID == metadata.ID); - if (customizedPluginConfig?.ActionKeywords?.Count > 0) - { - metadata.ActionKeywords = customizedPluginConfig.ActionKeywords; - metadata.ActionKeyword = customizedPluginConfig.ActionKeywords[0]; - } - return metadata; } } diff --git a/Wox.Core/Plugin/PluginManager.cs b/Wox.Core/Plugin/PluginManager.cs index f3da6bb0ea..9388248810 100644 --- a/Wox.Core/Plugin/PluginManager.cs +++ b/Wox.Core/Plugin/PluginManager.cs @@ -156,8 +156,8 @@ namespace Wox.Core.Plugin foreach (var plugin in pluginPairs) { var customizedPluginConfig = UserSettingStorage.Instance. - CustomizedPluginConfigs.FirstOrDefault(o => o.ID == plugin.Metadata.ID); - if (customizedPluginConfig != null && customizedPluginConfig.Disabled) continue; + CustomizedPluginConfigs[plugin.Metadata.ID]; + if (customizedPluginConfig.Disabled) continue; if (IsInstantQueryPlugin(plugin)) { Stopwatch.Normal($"Instant QueryForPlugin for {plugin.Metadata.Name}", () => diff --git a/Wox.Core/UserSettings/CustomizedPluginConfig.cs b/Wox.Core/UserSettings/CustomizedPluginConfig.cs index bce1fd9bc8..c58bb96477 100644 --- a/Wox.Core/UserSettings/CustomizedPluginConfig.cs +++ b/Wox.Core/UserSettings/CustomizedPluginConfig.cs @@ -1,9 +1,8 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; namespace Wox.Core.UserSettings { - [Serializable] + public class CustomizedPluginConfig { public string ID { get; set; } diff --git a/Wox.Core/UserSettings/UserSettingStorage.cs b/Wox.Core/UserSettings/UserSettingStorage.cs index d5d54a48f3..b9a7f8ab7f 100644 --- a/Wox.Core/UserSettings/UserSettingStorage.cs +++ b/Wox.Core/UserSettings/UserSettingStorage.cs @@ -1,110 +1,80 @@ using System; using System.Collections.Generic; using System.Drawing; -using System.IO; using System.Linq; -using System.Reflection; -using Newtonsoft.Json; +using Wox.Core.Plugin; using Wox.Infrastructure.Storage; using Wox.Plugin; +using Newtonsoft.Json; namespace Wox.Core.UserSettings { public class UserSettingStorage : JsonStrorage { - [JsonProperty] public bool DontPromptUpdateMsg { get; set; } - [JsonProperty] public int ActivateTimes { get; set; } - - [JsonProperty] public bool EnableUpdateLog { get; set; } - [JsonProperty] public string Hotkey { get; set; } - [JsonProperty] public string Language { get; set; } - [JsonProperty] public string Theme { get; set; } - [JsonProperty] public string QueryBoxFont { get; set; } - [JsonProperty] public string QueryBoxFontStyle { get; set; } - [JsonProperty] public string QueryBoxFontWeight { get; set; } - [JsonProperty] public string QueryBoxFontStretch { get; set; } - [JsonProperty] public string ResultFont { get; set; } - [JsonProperty] public string ResultFontStyle { get; set; } - [JsonProperty] public string ResultFontWeight { get; set; } - [JsonProperty] public string ResultFontStretch { get; set; } - [JsonProperty] public double WindowLeft { get; set; } - [JsonProperty] public double WindowTop { get; set; } - public List CustomizedPluginConfigs { get; set; } + // Order defaults to 0 or -1, so 1 will let this property appear last + [JsonProperty(Order = 1)] + public Dictionary CustomizedPluginConfigs { get; set; } - [JsonProperty] public List CustomPluginHotkeys { get; set; } - [JsonProperty] public bool StartWoxOnSystemStartup { get; set; } [Obsolete] - [JsonProperty] public double Opacity { get; set; } [Obsolete] - [JsonProperty] public OpacityMode OpacityMode { get; set; } - [JsonProperty] public bool LeaveCmdOpen { get; set; } - [JsonProperty] public bool HideWhenDeactive { get; set; } - [JsonProperty] public bool RememberLastLaunchLocation { get; set; } - [JsonProperty] public bool IgnoreHotkeysOnFullscreen { get; set; } - [JsonProperty] public string ProxyServer { get; set; } - [JsonProperty] public bool ProxyEnabled { get; set; } - [JsonProperty] public int ProxyPort { get; set; } - [JsonProperty] public string ProxyUserName { get; set; } - [JsonProperty] public string ProxyPassword { get; set; } - [JsonProperty] public int MaxResultsToShow { get; set; } protected override string FileName { get; } = "Settings"; @@ -123,7 +93,7 @@ namespace Wox.Core.UserSettings DontPromptUpdateMsg = false; Theme = "Dark"; Language = "en"; - CustomizedPluginConfigs = new List(); + CustomizedPluginConfigs = new Dictionary(); Hotkey = "Alt + Space"; QueryBoxFont = FontFamily.GenericSansSerif.Name; ResultFont = FontFamily.GenericSansSerif.Name; @@ -139,10 +109,38 @@ namespace Wox.Core.UserSettings protected override void OnAfterLoad(UserSettingStorage storage) { + var metadatas = PluginManager.AllPlugins.Select(p => p.Metadata); if (storage.CustomizedPluginConfigs == null) { - storage.CustomizedPluginConfigs = new List(); + var configs = new Dictionary(); + foreach (var metadata in metadatas) + { + addPluginMetadata(configs, metadata); + } + storage.CustomizedPluginConfigs = configs; } + else + { + var configs = storage.CustomizedPluginConfigs; + foreach (var metadata in metadatas) + { + if (configs.ContainsKey(metadata.ID)) + { + var config = configs[metadata.ID]; + if (config.ActionKeywords?.Count > 0) + { + metadata.ActionKeywords = config.ActionKeywords; + metadata.ActionKeyword = config.ActionKeywords[0]; + } + } + else + { + addPluginMetadata(configs, metadata); + } + } + } + + if (storage.QueryBoxFont == null) { storage.QueryBoxFont = FontFamily.GenericSansSerif.Name; @@ -157,23 +155,22 @@ namespace Wox.Core.UserSettings } } + + private void addPluginMetadata(Dictionary configs, PluginMetadata metadata) + { + configs[metadata.ID] = new CustomizedPluginConfig + { + ID = metadata.ID, + Name = metadata.Name, + ActionKeywords = metadata.ActionKeywords, + Disabled = false + }; + } + public void UpdateActionKeyword(PluginMetadata metadata) { - var customizedPluginConfig = CustomizedPluginConfigs.FirstOrDefault(o => o.ID == metadata.ID); - if (customizedPluginConfig == null) - { - CustomizedPluginConfigs.Add(new CustomizedPluginConfig - { - Disabled = false, - ID = metadata.ID, - Name = metadata.Name, - ActionKeywords = metadata.ActionKeywords - }); - } - else - { - customizedPluginConfig.ActionKeywords = metadata.ActionKeywords; - } + var config = CustomizedPluginConfigs[metadata.ID]; + config.ActionKeywords = metadata.ActionKeywords; Save(); } } diff --git a/Wox.Plugin/Wox.Plugin.csproj b/Wox.Plugin/Wox.Plugin.csproj index 559f6cf463..fef71853ef 100644 --- a/Wox.Plugin/Wox.Plugin.csproj +++ b/Wox.Plugin/Wox.Plugin.csproj @@ -34,6 +34,10 @@ false + + ..\packages\Newtonsoft.Json.8.0.3\lib\net45\Newtonsoft.Json.dll + True + @@ -64,6 +68,7 @@ + diff --git a/Wox.Plugin/packages.config b/Wox.Plugin/packages.config new file mode 100644 index 0000000000..1975352b1b --- /dev/null +++ b/Wox.Plugin/packages.config @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/Wox/App.xaml.cs b/Wox/App.xaml.cs index a7d98d7348..4cbd7cea2d 100644 --- a/Wox/App.xaml.cs +++ b/Wox/App.xaml.cs @@ -8,9 +8,9 @@ using System.Threading; using System.Windows; using Wox.CommandArgs; using Wox.Core.Plugin; +using Wox.Core.UserSettings; using Wox.Helper; using Wox.Infrastructure; -using Wox.Plugin; using Wox.ViewModel; using Stopwatch = Wox.Infrastructure.Stopwatch; @@ -21,7 +21,7 @@ namespace Wox private const string Unique = "Wox_Unique_Application_Mutex"; public static MainWindow Window { get; private set; } - public static IPublicAPI API { get; private set; } + public static PublicAPIInstance API { get; private set; } [STAThread] public static void Main() @@ -50,11 +50,13 @@ namespace Wox MainViewModel mainVM = new MainViewModel(); API = new PublicAPIInstance(mainVM); Window = new MainWindow {DataContext = mainVM}; - NotifyIconManager notifyIconManager = new NotifyIconManager(API); - PluginManager.Init(API); CommandArgsFactory.Execute(e.Args.ToList()); + + // happlebao todo: the whole setting releated initialization should be put into seperate class/method + API.SetHotkey(UserSettingStorage.Instance.Hotkey, API.OnHotkey); + API.SetCustomPluginHotkey(); }); } diff --git a/Wox/PublicAPIInstance.cs b/Wox/PublicAPIInstance.cs index aec4094d35..1ab1cf0b3e 100644 --- a/Wox/PublicAPIInstance.cs +++ b/Wox/PublicAPIInstance.cs @@ -30,9 +30,6 @@ namespace Wox GlobalHotkey.Instance.hookedKeyboardCallback += KListener_hookedKeyboardCallback; WebRequest.RegisterPrefix("data", new DataWebRequestFactory()); - SetHotkey(UserSettingStorage.Instance.Hotkey, OnHotkey); - SetCustomPluginHotkey(); - MainVM.ListeningKeyPressed += (o, e) => { if(e.KeyEventArgs.Key == Key.Back) @@ -98,7 +95,7 @@ namespace Wox ShowWox(); } - public void ShowMsg(string title, string subTitle, string iconPath) + public void ShowMsg(string title, string subTitle = "", string iconPath = "") { Application.Current.Dispatcher.Invoke(() => { @@ -202,7 +199,7 @@ namespace Wox MainVM.OnTextBoxSelected(); } - public void SetHotkey(string hotkeyStr, EventHandler action) + internal void SetHotkey(string hotkeyStr, EventHandler action) { var hotkey = new HotkeyModel(hotkeyStr); SetHotkey(hotkey, action); @@ -244,7 +241,7 @@ namespace Wox return false; } - private void SetCustomPluginHotkey() + internal void SetCustomPluginHotkey() { if (UserSettingStorage.Instance.CustomPluginHotkeys == null) return; foreach (CustomPluginHotkey hotkey in UserSettingStorage.Instance.CustomPluginHotkeys) @@ -259,7 +256,7 @@ namespace Wox } } - private void OnHotkey(object sender, HotkeyEventArgs e) + protected internal void OnHotkey(object sender, HotkeyEventArgs e) { if (ShouldIgnoreHotkeys()) return; ToggleWox(); diff --git a/Wox/SettingWindow.xaml.cs b/Wox/SettingWindow.xaml.cs index 312060e406..f222e59035 100644 --- a/Wox/SettingWindow.xaml.cs +++ b/Wox/SettingWindow.xaml.cs @@ -567,7 +567,7 @@ namespace Wox pluginId = pair.Metadata.ID; pluginIcon.Source = ImageLoader.ImageLoader.Load(pair.Metadata.FullIcoPath); - var customizedPluginConfig = UserSettingStorage.Instance.CustomizedPluginConfigs.FirstOrDefault(o => o.ID == pluginId); + var customizedPluginConfig = UserSettingStorage.Instance.CustomizedPluginConfigs[pluginId]; cbDisablePlugin.IsChecked = customizedPluginConfig != null && customizedPluginConfig.Disabled; PluginContentPanel.Content = null; @@ -615,17 +615,17 @@ namespace Wox id = pair.Metadata.ID; name = pair.Metadata.Name; } - var customizedPluginConfig = UserSettingStorage.Instance.CustomizedPluginConfigs.FirstOrDefault(o => o.ID == id); + var customizedPluginConfig = UserSettingStorage.Instance.CustomizedPluginConfigs[id]; if (customizedPluginConfig == null) { // todo when this part will be invoked - UserSettingStorage.Instance.CustomizedPluginConfigs.Add(new CustomizedPluginConfig + UserSettingStorage.Instance.CustomizedPluginConfigs[id] = new CustomizedPluginConfig { Disabled = cbDisabled.IsChecked ?? true, ID = id, Name = name, ActionKeywords = null - }); + }; } else {