PowerToys/Wox.Core/UI/ResourceMerger.cs
2015-11-02 18:52:34 +00:00

54 lines
1.9 KiB
C#

using System;
using System.Linq;
using System.Windows;
using Wox.Core.i18n;
using Wox.Core.Plugin;
using Wox.Infrastructure;
using Wox.Plugin;
namespace Wox.Core.UI
{
public static class ResourceMerger
{
private static void RemoveResource(string resourceDirectoryName)
{
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;
}
}
}
public static void ApplyThemeResource(Theme.Theme t)
{
RemoveResource(Theme.Theme.DirectoryName);
Application.Current.Resources.MergedDictionaries.Add(t.GetResourceDictionary());
}
public static void ApplyLanguageResources(Internationalization i)
{
RemoveResource(Internationalization.DirectoryName);
Application.Current.Resources.MergedDictionaries.Add(i.GetResourceDictionary());
}
internal static void ApplyPluginLanguages()
{
RemoveResource(PluginManager.DirectoryName);
foreach (var languageFile in (PluginManager.GetPlugins<IPluginI18n>().
Select(plugin => InternationalizationManager.Instance.GetLanguageFile(((IPluginI18n)plugin.Plugin).GetLanguagesFolder())).
Where(file => !string.IsNullOrEmpty(file))))
{
Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary
{
Source = new Uri(languageFile, UriKind.Absolute)
});
}
}
}
}