From 92b636ab751afe0cdc7ac9686126744dfcc2a840 Mon Sep 17 00:00:00 2001 From: bao-qian Date: Tue, 28 Feb 2017 00:12:53 +0000 Subject: [PATCH] Don't remove /Theme/Dark.xaml --- Wox.Core/Resource/Theme.cs | 40 ++++++++++++++++++++++++++++---------- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/Wox.Core/Resource/Theme.cs b/Wox.Core/Resource/Theme.cs index aa8eccef35..c78ff91d2b 100644 --- a/Wox.Core/Resource/Theme.cs +++ b/Wox.Core/Resource/Theme.cs @@ -16,14 +16,30 @@ namespace Wox.Core.Resource public class Theme { private readonly List _themeDirectories = new List(); + private ResourceDictionary _oldResource; + private string _oldTheme; public Settings Settings { get; set; } - private string DirectoryPath => Path.Combine(Constant.ProgramDirectory, DirectoryName); - private const string DirectoryName = "Themes"; + private const string Folder = "Themes"; + private const string Extension = ".xaml"; + private string DirectoryPath => Path.Combine(Constant.ProgramDirectory, Folder); public Theme() { _themeDirectories.Add(DirectoryPath); MakesureThemeDirectoriesExist(); + + var dicts = Application.Current.Resources.MergedDictionaries; + _oldResource = dicts.First(d => + { + var p = d.Source.AbsolutePath; + var dir = Path.GetDirectoryName(p).NonNull(); + var info = new DirectoryInfo(dir); + var f = info.Name; + var e = Path.GetExtension(p); + var found = f == Folder && e == Extension; + return found; + }); + _oldTheme = Path.GetFileNameWithoutExtension(_oldResource.Source.AbsolutePath); } private void MakesureThemeDirectoriesExist() @@ -75,12 +91,16 @@ namespace Wox.Core.Resource Settings.Theme = theme; var dicts = Application.Current.Resources.MergedDictionaries; - // OnStartup, there is nothing to remove - var oldResource = dicts.FirstOrDefault(d => d.Source.AbsolutePath == path) ?? new ResourceDictionary(); - dicts.Remove(oldResource); - var newResource = GetResourceDictionary(); - dicts.Add(newResource); - + Log.Debug($"|TEST1|{dicts.Select(d => d.Source.AbsolutePath).Formatted()}"); + if (_oldTheme != theme) + { + dicts.Remove(_oldResource); + var newResource = GetResourceDictionary(); + dicts.Add(newResource); + _oldResource = newResource; + _oldTheme = Path.GetFileNameWithoutExtension(_oldResource.Source.AbsolutePath); + } + Log.Debug($"|TEST2|{dicts.Select(d => d.Source.AbsolutePath).Formatted()}"); } } @@ -125,7 +145,7 @@ namespace Wox.Core.Resource { themes.AddRange( Directory.GetFiles(themeDirectory) - .Where(filePath => filePath.EndsWith(".xaml") && !filePath.EndsWith("Base.xaml")) + .Where(filePath => filePath.EndsWith(Extension) && !filePath.EndsWith("Base.xaml")) .ToList()); } return themes.OrderBy(o => o).ToList(); @@ -135,7 +155,7 @@ namespace Wox.Core.Resource { foreach (string themeDirectory in _themeDirectories) { - string path = Path.Combine(themeDirectory, themeName + ".xaml"); + string path = Path.Combine(themeDirectory, themeName + Extension); if (File.Exists(path)) { return path;