Refactoring CustomizedPluginConfig

1. Reorder the sequence of initialization of UserSettings.
2. Use dictionary for CustomizedPluginConfigs, so code logic like
`.FirstOrDefault(o => o.ID == id);` are removed
3. part of #389
This commit is contained in:
bao-qian 2016-03-25 01:22:24 +00:00
parent ef1a195036
commit c5d45c6b44
10 changed files with 79 additions and 86 deletions

View File

@ -14,9 +14,8 @@ namespace Wox.Plugin.PluginIndicator
var results = from keyword in PluginManager.NonGlobalPlugins.Keys var results = from keyword in PluginManager.NonGlobalPlugins.Keys
where keyword.StartsWith(query.Terms[0]) where keyword.StartsWith(query.Terms[0])
let metadata = PluginManager.NonGlobalPlugins[keyword].Metadata let metadata = PluginManager.NonGlobalPlugins[keyword].Metadata
let customizedPluginConfig = let customizedPluginConfig = UserSettingStorage.Instance.CustomizedPluginConfigs[metadata.ID]
UserSettingStorage.Instance.CustomizedPluginConfigs.FirstOrDefault(o => o.ID == metadata.ID) where !customizedPluginConfig.Disabled
where customizedPluginConfig == null || !customizedPluginConfig.Disabled
select new Result select new Result
{ {
Title = keyword, Title = keyword,

View File

@ -1,9 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq;
using Newtonsoft.Json; using Newtonsoft.Json;
using Wox.Core.UserSettings;
using Wox.Infrastructure.Exception; using Wox.Infrastructure.Exception;
using Wox.Infrastructure.Logger; using Wox.Infrastructure.Logger;
using Wox.Plugin; using Wox.Plugin;
@ -100,14 +98,6 @@ namespace Wox.Core.Plugin
return null; 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; return metadata;
} }
} }

View File

@ -156,8 +156,8 @@ namespace Wox.Core.Plugin
foreach (var plugin in pluginPairs) foreach (var plugin in pluginPairs)
{ {
var customizedPluginConfig = UserSettingStorage.Instance. var customizedPluginConfig = UserSettingStorage.Instance.
CustomizedPluginConfigs.FirstOrDefault(o => o.ID == plugin.Metadata.ID); CustomizedPluginConfigs[plugin.Metadata.ID];
if (customizedPluginConfig != null && customizedPluginConfig.Disabled) continue; if (customizedPluginConfig.Disabled) continue;
if (IsInstantQueryPlugin(plugin)) if (IsInstantQueryPlugin(plugin))
{ {
Stopwatch.Normal($"Instant QueryForPlugin for {plugin.Metadata.Name}", () => Stopwatch.Normal($"Instant QueryForPlugin for {plugin.Metadata.Name}", () =>

View File

@ -1,9 +1,8 @@
using System; using System.Collections.Generic;
using System.Collections.Generic;
namespace Wox.Core.UserSettings namespace Wox.Core.UserSettings
{ {
[Serializable]
public class CustomizedPluginConfig public class CustomizedPluginConfig
{ {
public string ID { get; set; } public string ID { get; set; }

View File

@ -1,110 +1,80 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Drawing; using System.Drawing;
using System.IO;
using System.Linq; using System.Linq;
using System.Reflection; using Wox.Core.Plugin;
using Newtonsoft.Json;
using Wox.Infrastructure.Storage; using Wox.Infrastructure.Storage;
using Wox.Plugin; using Wox.Plugin;
using Newtonsoft.Json;
namespace Wox.Core.UserSettings namespace Wox.Core.UserSettings
{ {
public class UserSettingStorage : JsonStrorage<UserSettingStorage> public class UserSettingStorage : JsonStrorage<UserSettingStorage>
{ {
[JsonProperty]
public bool DontPromptUpdateMsg { get; set; } public bool DontPromptUpdateMsg { get; set; }
[JsonProperty]
public int ActivateTimes { get; set; } public int ActivateTimes { get; set; }
[JsonProperty]
public bool EnableUpdateLog { get; set; } public bool EnableUpdateLog { get; set; }
[JsonProperty]
public string Hotkey { get; set; } public string Hotkey { get; set; }
[JsonProperty]
public string Language { get; set; } public string Language { get; set; }
[JsonProperty]
public string Theme { get; set; } public string Theme { get; set; }
[JsonProperty]
public string QueryBoxFont { get; set; } public string QueryBoxFont { get; set; }
[JsonProperty]
public string QueryBoxFontStyle { get; set; } public string QueryBoxFontStyle { get; set; }
[JsonProperty]
public string QueryBoxFontWeight { get; set; } public string QueryBoxFontWeight { get; set; }
[JsonProperty]
public string QueryBoxFontStretch { get; set; } public string QueryBoxFontStretch { get; set; }
[JsonProperty]
public string ResultFont { get; set; } public string ResultFont { get; set; }
[JsonProperty]
public string ResultFontStyle { get; set; } public string ResultFontStyle { get; set; }
[JsonProperty]
public string ResultFontWeight { get; set; } public string ResultFontWeight { get; set; }
[JsonProperty]
public string ResultFontStretch { get; set; } public string ResultFontStretch { get; set; }
[JsonProperty]
public double WindowLeft { get; set; } public double WindowLeft { get; set; }
[JsonProperty]
public double WindowTop { get; set; } public double WindowTop { get; set; }
public List<CustomizedPluginConfig> CustomizedPluginConfigs { get; set; } // Order defaults to 0 or -1, so 1 will let this property appear last
[JsonProperty(Order = 1)]
public Dictionary<string, CustomizedPluginConfig> CustomizedPluginConfigs { get; set; }
[JsonProperty]
public List<CustomPluginHotkey> CustomPluginHotkeys { get; set; } public List<CustomPluginHotkey> CustomPluginHotkeys { get; set; }
[JsonProperty]
public bool StartWoxOnSystemStartup { get; set; } public bool StartWoxOnSystemStartup { get; set; }
[Obsolete] [Obsolete]
[JsonProperty]
public double Opacity { get; set; } public double Opacity { get; set; }
[Obsolete] [Obsolete]
[JsonProperty]
public OpacityMode OpacityMode { get; set; } public OpacityMode OpacityMode { get; set; }
[JsonProperty]
public bool LeaveCmdOpen { get; set; } public bool LeaveCmdOpen { get; set; }
[JsonProperty]
public bool HideWhenDeactive { get; set; } public bool HideWhenDeactive { get; set; }
[JsonProperty]
public bool RememberLastLaunchLocation { get; set; } public bool RememberLastLaunchLocation { get; set; }
[JsonProperty]
public bool IgnoreHotkeysOnFullscreen { get; set; } public bool IgnoreHotkeysOnFullscreen { get; set; }
[JsonProperty]
public string ProxyServer { get; set; } public string ProxyServer { get; set; }
[JsonProperty]
public bool ProxyEnabled { get; set; } public bool ProxyEnabled { get; set; }
[JsonProperty]
public int ProxyPort { get; set; } public int ProxyPort { get; set; }
[JsonProperty]
public string ProxyUserName { get; set; } public string ProxyUserName { get; set; }
[JsonProperty]
public string ProxyPassword { get; set; } public string ProxyPassword { get; set; }
[JsonProperty]
public int MaxResultsToShow { get; set; } public int MaxResultsToShow { get; set; }
protected override string FileName { get; } = "Settings"; protected override string FileName { get; } = "Settings";
@ -123,7 +93,7 @@ namespace Wox.Core.UserSettings
DontPromptUpdateMsg = false; DontPromptUpdateMsg = false;
Theme = "Dark"; Theme = "Dark";
Language = "en"; Language = "en";
CustomizedPluginConfigs = new List<CustomizedPluginConfig>(); CustomizedPluginConfigs = new Dictionary<string, CustomizedPluginConfig>();
Hotkey = "Alt + Space"; Hotkey = "Alt + Space";
QueryBoxFont = FontFamily.GenericSansSerif.Name; QueryBoxFont = FontFamily.GenericSansSerif.Name;
ResultFont = FontFamily.GenericSansSerif.Name; ResultFont = FontFamily.GenericSansSerif.Name;
@ -139,10 +109,38 @@ namespace Wox.Core.UserSettings
protected override void OnAfterLoad(UserSettingStorage storage) protected override void OnAfterLoad(UserSettingStorage storage)
{ {
var metadatas = PluginManager.AllPlugins.Select(p => p.Metadata);
if (storage.CustomizedPluginConfigs == null) if (storage.CustomizedPluginConfigs == null)
{ {
storage.CustomizedPluginConfigs = new List<CustomizedPluginConfig>(); var configs = new Dictionary<string, CustomizedPluginConfig>();
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) if (storage.QueryBoxFont == null)
{ {
storage.QueryBoxFont = FontFamily.GenericSansSerif.Name; storage.QueryBoxFont = FontFamily.GenericSansSerif.Name;
@ -157,23 +155,22 @@ namespace Wox.Core.UserSettings
} }
} }
public void UpdateActionKeyword(PluginMetadata metadata)
private void addPluginMetadata(Dictionary<string, CustomizedPluginConfig> configs, PluginMetadata metadata)
{ {
var customizedPluginConfig = CustomizedPluginConfigs.FirstOrDefault(o => o.ID == metadata.ID); configs[metadata.ID] = new CustomizedPluginConfig
if (customizedPluginConfig == null)
{ {
CustomizedPluginConfigs.Add(new CustomizedPluginConfig
{
Disabled = false,
ID = metadata.ID, ID = metadata.ID,
Name = metadata.Name, Name = metadata.Name,
ActionKeywords = metadata.ActionKeywords ActionKeywords = metadata.ActionKeywords,
}); Disabled = false
};
} }
else
public void UpdateActionKeyword(PluginMetadata metadata)
{ {
customizedPluginConfig.ActionKeywords = metadata.ActionKeywords; var config = CustomizedPluginConfigs[metadata.ID];
} config.ActionKeywords = metadata.ActionKeywords;
Save(); Save();
} }
} }

View File

@ -34,6 +34,10 @@
<Prefer32Bit>false</Prefer32Bit> <Prefer32Bit>false</Prefer32Bit>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="Newtonsoft.Json, Version=8.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.8.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="PresentationCore" /> <Reference Include="PresentationCore" />
<Reference Include="PresentationFramework" /> <Reference Include="PresentationFramework" />
<Reference Include="System" /> <Reference Include="System" />
@ -64,6 +68,7 @@
<Compile Include="ActionContext.cs" /> <Compile Include="ActionContext.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="packages.config" />
<None Include="README.md" /> <None Include="README.md" />
</ItemGroup> </ItemGroup>
<ItemGroup /> <ItemGroup />

View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Newtonsoft.Json" version="8.0.3" targetFramework="net452" />
</packages>

View File

@ -8,9 +8,9 @@ using System.Threading;
using System.Windows; using System.Windows;
using Wox.CommandArgs; using Wox.CommandArgs;
using Wox.Core.Plugin; using Wox.Core.Plugin;
using Wox.Core.UserSettings;
using Wox.Helper; using Wox.Helper;
using Wox.Infrastructure; using Wox.Infrastructure;
using Wox.Plugin;
using Wox.ViewModel; using Wox.ViewModel;
using Stopwatch = Wox.Infrastructure.Stopwatch; using Stopwatch = Wox.Infrastructure.Stopwatch;
@ -21,7 +21,7 @@ namespace Wox
private const string Unique = "Wox_Unique_Application_Mutex"; private const string Unique = "Wox_Unique_Application_Mutex";
public static MainWindow Window { get; private set; } public static MainWindow Window { get; private set; }
public static IPublicAPI API { get; private set; } public static PublicAPIInstance API { get; private set; }
[STAThread] [STAThread]
public static void Main() public static void Main()
@ -50,11 +50,13 @@ namespace Wox
MainViewModel mainVM = new MainViewModel(); MainViewModel mainVM = new MainViewModel();
API = new PublicAPIInstance(mainVM); API = new PublicAPIInstance(mainVM);
Window = new MainWindow {DataContext = mainVM}; Window = new MainWindow {DataContext = mainVM};
NotifyIconManager notifyIconManager = new NotifyIconManager(API); NotifyIconManager notifyIconManager = new NotifyIconManager(API);
PluginManager.Init(API); PluginManager.Init(API);
CommandArgsFactory.Execute(e.Args.ToList()); 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();
}); });
} }

View File

@ -30,9 +30,6 @@ namespace Wox
GlobalHotkey.Instance.hookedKeyboardCallback += KListener_hookedKeyboardCallback; GlobalHotkey.Instance.hookedKeyboardCallback += KListener_hookedKeyboardCallback;
WebRequest.RegisterPrefix("data", new DataWebRequestFactory()); WebRequest.RegisterPrefix("data", new DataWebRequestFactory());
SetHotkey(UserSettingStorage.Instance.Hotkey, OnHotkey);
SetCustomPluginHotkey();
MainVM.ListeningKeyPressed += (o, e) => { MainVM.ListeningKeyPressed += (o, e) => {
if(e.KeyEventArgs.Key == Key.Back) if(e.KeyEventArgs.Key == Key.Back)
@ -98,7 +95,7 @@ namespace Wox
ShowWox(); ShowWox();
} }
public void ShowMsg(string title, string subTitle, string iconPath) public void ShowMsg(string title, string subTitle = "", string iconPath = "")
{ {
Application.Current.Dispatcher.Invoke(() => Application.Current.Dispatcher.Invoke(() =>
{ {
@ -202,7 +199,7 @@ namespace Wox
MainVM.OnTextBoxSelected(); MainVM.OnTextBoxSelected();
} }
public void SetHotkey(string hotkeyStr, EventHandler<HotkeyEventArgs> action) internal void SetHotkey(string hotkeyStr, EventHandler<HotkeyEventArgs> action)
{ {
var hotkey = new HotkeyModel(hotkeyStr); var hotkey = new HotkeyModel(hotkeyStr);
SetHotkey(hotkey, action); SetHotkey(hotkey, action);
@ -244,7 +241,7 @@ namespace Wox
return false; return false;
} }
private void SetCustomPluginHotkey() internal void SetCustomPluginHotkey()
{ {
if (UserSettingStorage.Instance.CustomPluginHotkeys == null) return; if (UserSettingStorage.Instance.CustomPluginHotkeys == null) return;
foreach (CustomPluginHotkey hotkey in UserSettingStorage.Instance.CustomPluginHotkeys) 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; if (ShouldIgnoreHotkeys()) return;
ToggleWox(); ToggleWox();

View File

@ -567,7 +567,7 @@ namespace Wox
pluginId = pair.Metadata.ID; pluginId = pair.Metadata.ID;
pluginIcon.Source = ImageLoader.ImageLoader.Load(pair.Metadata.FullIcoPath); 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; cbDisablePlugin.IsChecked = customizedPluginConfig != null && customizedPluginConfig.Disabled;
PluginContentPanel.Content = null; PluginContentPanel.Content = null;
@ -615,17 +615,17 @@ namespace Wox
id = pair.Metadata.ID; id = pair.Metadata.ID;
name = pair.Metadata.Name; name = pair.Metadata.Name;
} }
var customizedPluginConfig = UserSettingStorage.Instance.CustomizedPluginConfigs.FirstOrDefault(o => o.ID == id); var customizedPluginConfig = UserSettingStorage.Instance.CustomizedPluginConfigs[id];
if (customizedPluginConfig == null) if (customizedPluginConfig == null)
{ {
// todo when this part will be invoked // todo when this part will be invoked
UserSettingStorage.Instance.CustomizedPluginConfigs.Add(new CustomizedPluginConfig UserSettingStorage.Instance.CustomizedPluginConfigs[id] = new CustomizedPluginConfig
{ {
Disabled = cbDisabled.IsChecked ?? true, Disabled = cbDisabled.IsChecked ?? true,
ID = id, ID = id,
Name = name, Name = name,
ActionKeywords = null ActionKeywords = null
}); };
} }
else else
{ {