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 98f1d5cd9b
commit d6f9fddc94
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
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,

View File

@ -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<PluginMetadata>(File.ReadAllText(configPath));
metadata.PluginDirectory = pluginDirectory;
// for plugins which doesn't has ActionKeywords key
metadata.ActionKeywords = metadata.ActionKeywords ?? new List<string> {metadata.ActionKeyword};
metadata.ActionKeywords = metadata.ActionKeywords ?? new List<string> { 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;
}
}

View File

@ -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}", () =>

View File

@ -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; }

View File

@ -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<UserSettingStorage>
{
[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<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; }
[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<CustomizedPluginConfig>();
CustomizedPluginConfigs = new Dictionary<string, CustomizedPluginConfig>();
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<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)
{
storage.QueryBoxFont = FontFamily.GenericSansSerif.Name;
@ -157,23 +155,22 @@ namespace Wox.Core.UserSettings
}
}
private void addPluginMetadata(Dictionary<string, CustomizedPluginConfig> 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();
}
}

View File

@ -34,6 +34,10 @@
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<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="PresentationFramework" />
<Reference Include="System" />
@ -64,6 +68,7 @@
<Compile Include="ActionContext.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
<None Include="README.md" />
</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 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();
});
}

View File

@ -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<HotkeyEventArgs> action)
internal void SetHotkey(string hotkeyStr, EventHandler<HotkeyEventArgs> 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();

View File

@ -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
{