mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-09 04:37:30 +02:00
Fix ResourceMerger
1. Fix duplication for loading resource. 2. Fix null reference when plugin is not loaded. 3. speedup.
This commit is contained in:
@@ -20,6 +20,7 @@ namespace Wox.Core.Plugin
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public static class PluginManager
|
public static class PluginManager
|
||||||
{
|
{
|
||||||
|
public const string DirectoryName = "Plugins";
|
||||||
private static List<PluginMetadata> pluginMetadatas;
|
private static List<PluginMetadata> pluginMetadatas;
|
||||||
private static List<KeyValuePair<PluginPair, IInstantQuery>> instantSearches;
|
private static List<KeyValuePair<PluginPair, IInstantQuery>> instantSearches;
|
||||||
private static IEnumerable<PluginPair> exclusiveSearchPlugins;
|
private static IEnumerable<PluginPair> exclusiveSearchPlugins;
|
||||||
@@ -41,7 +42,7 @@ namespace Wox.Core.Plugin
|
|||||||
|
|
||||||
public static string PluginDirectory
|
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()
|
private static void SetupPluginDirectories()
|
||||||
|
|||||||
@@ -14,11 +14,12 @@ namespace Wox.Core.Theme
|
|||||||
{
|
{
|
||||||
public class Theme : IUIResource,ITheme
|
public class Theme : IUIResource,ITheme
|
||||||
{
|
{
|
||||||
|
public const string DirectoryName = "Themes";
|
||||||
private static List<string> themeDirectories = new List<string>();
|
private static List<string> themeDirectories = new List<string>();
|
||||||
|
|
||||||
static Theme()
|
static Theme()
|
||||||
{
|
{
|
||||||
themeDirectories.Add(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Themes"));
|
themeDirectories.Add(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), DirectoryName));
|
||||||
MakesureThemeDirectoriesExist();
|
MakesureThemeDirectoriesExist();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -55,7 +56,7 @@ namespace Wox.Core.Theme
|
|||||||
UserSettingStorage.Instance.Theme = themeName;
|
UserSettingStorage.Instance.Theme = themeName;
|
||||||
UserSettingStorage.Instance.Save();
|
UserSettingStorage.Instance.Save();
|
||||||
|
|
||||||
ResourceMerger.ApplyResources();
|
ResourceMerger.ApplyThemeResource();
|
||||||
}
|
}
|
||||||
|
|
||||||
public ResourceDictionary GetResourceDictionary()
|
public ResourceDictionary GetResourceDictionary()
|
||||||
|
|||||||
@@ -1,20 +1,42 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Linq;
|
||||||
using System.Windows;
|
using System.Windows;
|
||||||
using Wox.Core.i18n;
|
using Wox.Core.i18n;
|
||||||
|
using Wox.Core.Plugin;
|
||||||
using Wox.Plugin;
|
using Wox.Plugin;
|
||||||
|
|
||||||
namespace Wox.Core.UI
|
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();
|
var mergedDictionaries = Application.Current.Resources.MergedDictionaries;
|
||||||
ApplyPluginLanguages();
|
foreach (var resource in mergedDictionaries)
|
||||||
ApplyThemeAndLanguageResources();
|
{
|
||||||
|
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>();
|
var UIResources = AssemblyHelper.LoadInterfacesFromAppDomain<IUIResource>();
|
||||||
foreach (var uiResource in UIResources)
|
foreach (var uiResource in UIResources)
|
||||||
@@ -25,6 +47,7 @@ namespace Wox.Core.UI
|
|||||||
|
|
||||||
internal static void ApplyPluginLanguages()
|
internal static void ApplyPluginLanguages()
|
||||||
{
|
{
|
||||||
|
RemoveResource(PluginManager.DirectoryName);
|
||||||
var pluginI18ns = AssemblyHelper.LoadInterfacesFromAppDomain<IPluginI18n>();
|
var pluginI18ns = AssemblyHelper.LoadInterfacesFromAppDomain<IPluginI18n>();
|
||||||
foreach (var pluginI18n in pluginI18ns)
|
foreach (var pluginI18n in pluginI18ns)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -14,7 +14,8 @@ namespace Wox.Core.i18n
|
|||||||
{
|
{
|
||||||
public class Internationalization : IInternationalization, IUIResource
|
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()
|
static Internationalization()
|
||||||
{
|
{
|
||||||
@@ -23,11 +24,11 @@ namespace Wox.Core.i18n
|
|||||||
|
|
||||||
private static void MakesureThemeDirectoriesExist()
|
private static void MakesureThemeDirectoriesExist()
|
||||||
{
|
{
|
||||||
if (!Directory.Exists(DefaultLanguageDirectory))
|
if (!Directory.Exists(DefaultDirectory))
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
Directory.CreateDirectory(DefaultLanguageDirectory);
|
Directory.CreateDirectory(DefaultDirectory);
|
||||||
}
|
}
|
||||||
catch (System.Exception e)
|
catch (System.Exception e)
|
||||||
{
|
{
|
||||||
@@ -69,15 +70,14 @@ namespace Wox.Core.i18n
|
|||||||
|
|
||||||
UserSettingStorage.Instance.Language = language.LanguageCode;
|
UserSettingStorage.Instance.Language = language.LanguageCode;
|
||||||
UserSettingStorage.Instance.Save();
|
UserSettingStorage.Instance.Save();
|
||||||
ResourceMerger.ApplyResources();
|
ResourceMerger.ApplyLanguageResources();
|
||||||
UpdateAllPluginMetadataTranslations();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public ResourceDictionary GetResourceDictionary()
|
public ResourceDictionary GetResourceDictionary()
|
||||||
{
|
{
|
||||||
return new ResourceDictionary
|
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)
|
internal void UpdatePluginMetadataTranslations(PluginPair pluginPair)
|
||||||
{
|
{
|
||||||
var pluginI18n = pluginPair.Plugin as IPluginI18n;
|
var pluginI18n = pluginPair.Plugin as IPluginI18n;
|
||||||
@@ -142,7 +133,7 @@ namespace Wox.Core.i18n
|
|||||||
|
|
||||||
private string GetLanguagePath(Language language)
|
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))
|
if (File.Exists(path))
|
||||||
{
|
{
|
||||||
return path;
|
return path;
|
||||||
|
|||||||
@@ -5,8 +5,8 @@
|
|||||||
<Application.Resources>
|
<Application.Resources>
|
||||||
<ResourceDictionary>
|
<ResourceDictionary>
|
||||||
<ResourceDictionary.MergedDictionaries>
|
<ResourceDictionary.MergedDictionaries>
|
||||||
<ResourceDictionary Source="Themes/Dark.xaml" />
|
<ResourceDictionary Source="pack://application:,,,/Themes/Dark.xaml" />
|
||||||
<ResourceDictionary Source="Languages/en.xaml" />
|
<ResourceDictionary Source="pack://application:,,,/Languages/en.xaml" />
|
||||||
</ResourceDictionary.MergedDictionaries>
|
</ResourceDictionary.MergedDictionaries>
|
||||||
</ResourceDictionary>
|
</ResourceDictionary>
|
||||||
</Application.Resources>
|
</Application.Resources>
|
||||||
|
|||||||
Reference in New Issue
Block a user