From 8504d0168833f8bd6f24948e29ac4fd7cd3abcde Mon Sep 17 00:00:00 2001 From: bao-qian Date: Thu, 7 Jan 2016 04:47:28 +0000 Subject: [PATCH] Refactoring directory path for UI resource --- Wox.Core/Resource/Resource.cs | 15 ++++++++++++ Wox.Core/{UI => Resource}/ResourceMerger.cs | 10 ++------ Wox.Core/Theme/Theme.cs | 10 ++++---- Wox.Core/UI/IUIResource.cs | 12 ---------- Wox.Core/Wox.Core.csproj | 5 ++-- Wox.Core/i18n/IInternationalization.cs | 24 ------------------- Wox.Core/i18n/Internationalization.cs | 26 ++++++++++----------- 7 files changed, 37 insertions(+), 65 deletions(-) create mode 100644 Wox.Core/Resource/Resource.cs rename Wox.Core/{UI => Resource}/ResourceMerger.cs (81%) delete mode 100644 Wox.Core/UI/IUIResource.cs delete mode 100644 Wox.Core/i18n/IInternationalization.cs diff --git a/Wox.Core/Resource/Resource.cs b/Wox.Core/Resource/Resource.cs new file mode 100644 index 0000000000..3c03994e72 --- /dev/null +++ b/Wox.Core/Resource/Resource.cs @@ -0,0 +1,15 @@ +using System.IO; +using System.Windows; +using Wox.Infrastructure; + +namespace Wox.Core.UI +{ + public abstract class Resource + { + public string DirectoryName { get; protected set; } + + protected string DirectoryPath => Path.Combine(WoxDirectroy.Executable, DirectoryName); + + public abstract ResourceDictionary GetResourceDictionary(); + } +} diff --git a/Wox.Core/UI/ResourceMerger.cs b/Wox.Core/Resource/ResourceMerger.cs similarity index 81% rename from Wox.Core/UI/ResourceMerger.cs rename to Wox.Core/Resource/ResourceMerger.cs index 9ce07a97ee..255314e623 100644 --- a/Wox.Core/UI/ResourceMerger.cs +++ b/Wox.Core/Resource/ResourceMerger.cs @@ -25,18 +25,12 @@ namespace Wox.Core.UI } } - public static void UpdateResource(Theme.Theme t) + public static void UpdateResource(T t) where T : Resource { - RemoveResource(Theme.Theme.DirectoryName); + RemoveResource(t.DirectoryName); Application.Current.Resources.MergedDictionaries.Add(t.GetResourceDictionary()); } - public static void UpdateResources(Internationalization i) - { - RemoveResource(Internationalization.DirectoryName); - Application.Current.Resources.MergedDictionaries.Add(i.GetResourceDictionary()); - } - internal static void UpdatePluginLanguages() { RemoveResource(PluginManager.DirectoryName); diff --git a/Wox.Core/Theme/Theme.cs b/Wox.Core/Theme/Theme.cs index b54192bd11..28c32c90cb 100644 --- a/Wox.Core/Theme/Theme.cs +++ b/Wox.Core/Theme/Theme.cs @@ -14,14 +14,14 @@ using Wox.Infrastructure.Logger; namespace Wox.Core.Theme { - public class Theme : IUIResource + public class Theme : Resource { - public const string DirectoryName = "Themes"; private static List themeDirectories = new List(); - static Theme() + public Theme() { - themeDirectories.Add(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), DirectoryName)); + DirectoryName = "Themes"; + themeDirectories.Add(DirectoryPath); MakesureThemeDirectoriesExist(); } @@ -67,7 +67,7 @@ namespace Wox.Core.Theme } } - public ResourceDictionary GetResourceDictionary() + public override ResourceDictionary GetResourceDictionary() { var dict = new ResourceDictionary { diff --git a/Wox.Core/UI/IUIResource.cs b/Wox.Core/UI/IUIResource.cs deleted file mode 100644 index 99d89551a6..0000000000 --- a/Wox.Core/UI/IUIResource.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System.Windows; - -namespace Wox.Core.UI -{ - /// - /// Object implement this interface will have the ability to has its own UI styles - /// - public interface IUIResource - { - ResourceDictionary GetResourceDictionary(); - } -} diff --git a/Wox.Core/Wox.Core.csproj b/Wox.Core/Wox.Core.csproj index 6856a93632..e4f1687c79 100644 --- a/Wox.Core/Wox.Core.csproj +++ b/Wox.Core/Wox.Core.csproj @@ -66,13 +66,12 @@ - - - + + diff --git a/Wox.Core/i18n/IInternationalization.cs b/Wox.Core/i18n/IInternationalization.cs deleted file mode 100644 index 5ab83c1327..0000000000 --- a/Wox.Core/i18n/IInternationalization.cs +++ /dev/null @@ -1,24 +0,0 @@ -using System.Collections.Generic; - -namespace Wox.Core.i18n -{ - interface IInternationalization - { - List LoadAvailableLanguages(); - - string GetTranslation(string key); - - /// - /// Get language file for current user selected language - /// if couldn't find the current selected language file, it will first try to load en.xaml - /// if en.xaml couldn't find, return empty string - /// - /// - /// - string GetLanguageFile(string folder); - - void ChangeLanguage(Language language); - - void ChangeLanguage(string languageCode); - } -} diff --git a/Wox.Core/i18n/Internationalization.cs b/Wox.Core/i18n/Internationalization.cs index a8018ad057..fe19a33437 100644 --- a/Wox.Core/i18n/Internationalization.cs +++ b/Wox.Core/i18n/Internationalization.cs @@ -2,7 +2,6 @@ using System.Collections.Generic; using System.IO; using System.Linq; -using System.Reflection; using System.Windows; using Wox.Core.UI; using Wox.Core.UserSettings; @@ -12,23 +11,21 @@ using Wox.Plugin; namespace Wox.Core.i18n { - public class Internationalization : IInternationalization, IUIResource + public class Internationalization : Resource { - public const string DirectoryName = "Languages"; - private static readonly string DefaultDirectory = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), DirectoryName); - - static Internationalization() + public Internationalization() { + DirectoryName = "Languages"; MakesureThemeDirectoriesExist(); } - private static void MakesureThemeDirectoriesExist() + private void MakesureThemeDirectoriesExist() { - if (!Directory.Exists(DefaultDirectory)) + if (!Directory.Exists(DirectoryPath)) { try { - Directory.CreateDirectory(DefaultDirectory); + Directory.CreateDirectory(DirectoryPath); } catch (Exception e) { @@ -70,14 +67,16 @@ namespace Wox.Core.i18n UserSettingStorage.Instance.Language = language.LanguageCode; UserSettingStorage.Instance.Save(); - ResourceMerger.UpdateResources(this); + ResourceMerger.UpdateResource(this); } - public ResourceDictionary GetResourceDictionary() + + + public override ResourceDictionary GetResourceDictionary() { return new ResourceDictionary { - Source = new Uri(GetLanguageFile(DefaultDirectory), UriKind.Absolute) + Source = new Uri(GetLanguageFile(DirectoryPath), UriKind.Absolute) }; } @@ -124,7 +123,7 @@ namespace Wox.Core.i18n private string GetLanguagePath(Language language) { - string path = Path.Combine(DefaultDirectory, language.LanguageCode + ".xaml"); + string path = Path.Combine(DirectoryPath, language.LanguageCode + ".xaml"); if (File.Exists(path)) { return path; @@ -156,4 +155,5 @@ namespace Wox.Core.i18n } } + }