Refactoring.

This commit is contained in:
qianlifeng 2014-12-27 12:34:51 +08:00
parent ccc8d7e5cd
commit d9b2863382
14 changed files with 184 additions and 148 deletions

View file

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

View file

@ -24,17 +24,31 @@ namespace Wox.Core.Plugin
/// </summary>
private static List<string> pluginDirectories = new List<string>();
/// <summary>
/// Default plugin directory
/// new plugin will be installed to this directory
/// </summary>
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);

View file

@ -19,7 +19,7 @@ namespace Wox.Core.Plugin.QueryDispatcher
//websearch mode
queryPlugins = new List<PluginPair>()
{
allSytemPlugins.First(o => ((ISystemPlugin)o.Plugin).ID == "565B73353DBF4806919830B9202EE3BF")
allSytemPlugins.First(o => o.Metadata.ID == "565B73353DBF4806919830B9202EE3BF")
};
}

View file

@ -32,6 +32,10 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="ICSharpCode.SharpZipLib, Version=0.86.0.518, Culture=neutral, PublicKeyToken=1b03e6acf1164f73, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\SharpZipLib.0.86.0\lib\20\ICSharpCode.SharpZipLib.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\Newtonsoft.Json.6.0.7\lib\net35\Newtonsoft.Json.dll</HintPath>
@ -45,6 +49,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Plugin\PluginInstaller.cs" />
<Compile Include="Plugin\QueryDispatcher\IQueryDispatcher.cs" />
<Compile Include="Plugin\QueryDispatcher\QueryDispatcher.cs" />
<Compile Include="Plugin\QueryDispatcher\UserPluginQueryDispatcher.cs" />

View file

@ -1,4 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Newtonsoft.Json" version="6.0.7" targetFramework="net35" />
<package id="SharpZipLib" version="0.86.0" targetFramework="net35" />
</packages>

View file

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

View file

@ -10,4 +10,4 @@ namespace Wox.Plugin
System,
User
}
}
}

View file

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

View file

@ -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<object> valueFunc)
{
LoadValue(valueFunc);
}
private void LoadValue(Func<object> 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;
}
}
}

View file

@ -13,7 +13,6 @@ namespace Wox.ImageLoader
public class ImageLoader
{
private static readonly Dictionary<string, ImageSource> imageCache = new Dictionary<string, ImageSource>();
private static object locker = new object();
private static readonly List<string> imageExts = new List<string>
{
@ -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<string, int>(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<string, int> copyedImg = image;
App.Window.Dispatcher.Invoke(new Action(() => imageCache.Add(copyedImg.Key, img)));
}
KeyValuePair<string, int> 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);
}
}

View file

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

View file

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

104
Wox/ThemeManager.cs Normal file
View file

@ -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<string> themeDirectories = new List<string>();
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;
}
}
}

View file

@ -106,6 +106,7 @@
<ItemGroup>
<Compile Include="ImageLoader\ImageCacheStroage.cs" />
<Compile Include="Storage\UserSelectedRecordStorage.cs" />
<Compile Include="ThemeManager.cs" />
<Compile Include="Update\NewVersionWindow.xaml.cs">
<DependentUpon>NewVersionWindow.xaml</DependentUpon>
</Compile>
@ -125,7 +126,6 @@
<Compile Include="CommandArgs\PluginDebuggerCommandArg.cs" />
<Compile Include="CommandArgs\QueryCommandArg.cs" />
<Compile Include="CommandArgs\ReloadPluginCommandArg.cs" />
<Compile Include="Converters\AsyncConverter.cs" />
<Compile Include="Converters\ConvertorBase.cs" />
<Compile Include="Helper\DataWebRequestFactory.cs" />
<Compile Include="Helper\ErrorReporting\ErrorReporting.cs" />
@ -145,7 +145,6 @@
</Compile>
<Compile Include="Helper\DispatcherExtensions.cs" />
<Compile Include="Helper\DWMDropShadow.cs" />
<Compile Include="Helper\PluginInstaller.cs" />
<Compile Include="HotkeyControl.xaml.cs">
<DependentUpon>HotkeyControl.xaml</DependentUpon>
</Compile>
@ -202,11 +201,6 @@
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<None Include="Themes\Dark.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="Themes\Light.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
@ -227,6 +221,10 @@
<SubType>Designer</SubType>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<Page Include="Themes\Dark.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>
<Page Include="Update\NewVersionWindow.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>