Fix ResourceMerger

1. Fix duplication for loading resource.
2. Fix null reference when plugin is not loaded.
3. speedup.
This commit is contained in:
bao-qian 2015-11-02 02:49:38 +00:00
parent b5a8312167
commit 881e414043
5 changed files with 43 additions and 27 deletions

View File

@ -20,6 +20,7 @@ namespace Wox.Core.Plugin
/// </summary>
public static class PluginManager
{
public const string DirectoryName = "Plugins";
private static List<PluginMetadata> pluginMetadatas;
private static List<KeyValuePair<PluginPair, IInstantQuery>> instantSearches;
private static IEnumerable<PluginPair> exclusiveSearchPlugins;
@ -41,7 +42,7 @@ namespace Wox.Core.Plugin
public static string PluginDirectory
{
get { return Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Plugins"); }
get { return Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), DirectoryName); }
}
private static void SetupPluginDirectories()

View File

@ -14,11 +14,12 @@ namespace Wox.Core.Theme
{
public class Theme : IUIResource,ITheme
{
public const string DirectoryName = "Themes";
private static List<string> themeDirectories = new List<string>();
static Theme()
{
themeDirectories.Add(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Themes"));
themeDirectories.Add(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), DirectoryName));
MakesureThemeDirectoriesExist();
}
@ -55,7 +56,7 @@ namespace Wox.Core.Theme
UserSettingStorage.Instance.Theme = themeName;
UserSettingStorage.Instance.Save();
ResourceMerger.ApplyResources();
ResourceMerger.ApplyThemeResource();
}
public ResourceDictionary GetResourceDictionary()

View File

@ -1,20 +1,42 @@
using System;
using System.Linq;
using System.Windows;
using Wox.Core.i18n;
using Wox.Core.Plugin;
using Wox.Plugin;
namespace Wox.Core.UI
{
public class ResourceMerger
public static class ResourceMerger
{
internal static void ApplyResources()
private static void RemoveResource(string resourceDirectoryName)
{
Application.Current.Resources.MergedDictionaries.Clear();
ApplyPluginLanguages();
ApplyThemeAndLanguageResources();
var mergedDictionaries = Application.Current.Resources.MergedDictionaries;
foreach (var resource in mergedDictionaries)
{
int directoryPosition = resource.Source.Segments.Length - 2;
string currentDirectoryName = resource.Source.Segments[directoryPosition];
if (currentDirectoryName == resourceDirectoryName)
{
mergedDictionaries.Remove(resource);
break;
}
}
}
internal static void ApplyThemeAndLanguageResources()
public static void ApplyThemeResource()
{
RemoveResource(Theme.Theme.DirectoryName);
ApplyUIResources();
}
public static void ApplyLanguageResources()
{
RemoveResource(Internationalization.DirectoryName);
ApplyUIResources();
}
private static void ApplyUIResources()
{
var UIResources = AssemblyHelper.LoadInterfacesFromAppDomain<IUIResource>();
foreach (var uiResource in UIResources)
@ -25,6 +47,7 @@ namespace Wox.Core.UI
internal static void ApplyPluginLanguages()
{
RemoveResource(PluginManager.DirectoryName);
var pluginI18ns = AssemblyHelper.LoadInterfacesFromAppDomain<IPluginI18n>();
foreach (var pluginI18n in pluginI18ns)
{

View File

@ -14,7 +14,8 @@ namespace Wox.Core.i18n
{
public class Internationalization : IInternationalization, IUIResource
{
private static string DefaultLanguageDirectory = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Languages");
public const string DirectoryName = "Languages";
private static readonly string DefaultDirectory = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), DirectoryName);
static Internationalization()
{
@ -23,11 +24,11 @@ namespace Wox.Core.i18n
private static void MakesureThemeDirectoriesExist()
{
if (!Directory.Exists(DefaultLanguageDirectory))
if (!Directory.Exists(DefaultDirectory))
{
try
{
Directory.CreateDirectory(DefaultLanguageDirectory);
Directory.CreateDirectory(DefaultDirectory);
}
catch (System.Exception e)
{
@ -69,15 +70,14 @@ namespace Wox.Core.i18n
UserSettingStorage.Instance.Language = language.LanguageCode;
UserSettingStorage.Instance.Save();
ResourceMerger.ApplyResources();
UpdateAllPluginMetadataTranslations();
ResourceMerger.ApplyLanguageResources();
}
public ResourceDictionary GetResourceDictionary()
{
return new ResourceDictionary
{
Source = new Uri(GetLanguageFile(DefaultLanguageDirectory), UriKind.Absolute)
Source = new Uri(GetLanguageFile(DefaultDirectory), UriKind.Absolute)
};
}
@ -111,15 +111,6 @@ namespace Wox.Core.i18n
}
internal void UpdateAllPluginMetadataTranslations()
{
List<KeyValuePair<PluginPair, IPluginI18n>> plugins = AssemblyHelper.LoadPluginInterfaces<IPluginI18n>();
foreach (var plugin in plugins)
{
UpdatePluginMetadataTranslations(plugin.Key);
}
}
internal void UpdatePluginMetadataTranslations(PluginPair pluginPair)
{
var pluginI18n = pluginPair.Plugin as IPluginI18n;
@ -142,7 +133,7 @@ namespace Wox.Core.i18n
private string GetLanguagePath(Language language)
{
string path = Path.Combine(DefaultLanguageDirectory, language.LanguageCode + ".xaml");
string path = Path.Combine(DefaultDirectory, language.LanguageCode + ".xaml");
if (File.Exists(path))
{
return path;

View File

@ -5,8 +5,8 @@
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Themes/Dark.xaml" />
<ResourceDictionary Source="Languages/en.xaml" />
<ResourceDictionary Source="pack://application:,,,/Themes/Dark.xaml" />
<ResourceDictionary Source="pack://application:,,,/Languages/en.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>