From 0baf7744bcb92e87dff83f930e4ade54df784a5d Mon Sep 17 00:00:00 2001 From: jhdxr Date: Sun, 23 Dec 2018 01:53:13 +0800 Subject: [PATCH] error handling for theme loader --- Wox.Core/Resource/Theme.cs | 52 ++++++++++++++++++++------------------ Wox/Languages/en.xaml | 2 ++ Wox/Languages/zh-cn.xaml | 2 ++ 3 files changed, 32 insertions(+), 24 deletions(-) diff --git a/Wox.Core/Resource/Theme.cs b/Wox.Core/Resource/Theme.cs index 7343a7b908..4b8c37ccfd 100644 --- a/Wox.Core/Resource/Theme.cs +++ b/Wox.Core/Resource/Theme.cs @@ -6,6 +6,7 @@ using System.Runtime.InteropServices; using System.Windows; using System.Windows.Controls; using System.Windows.Interop; +using System.Windows.Markup; using System.Windows.Media; using Wox.Infrastructure; using Wox.Infrastructure.Logger; @@ -62,47 +63,50 @@ namespace Wox.Core.Resource } } - public void ChangeTheme(string theme) + public bool ChangeTheme(string theme) { - const string dark = "Dark"; - bool valid; + const string defaultTheme = "Dark"; string path = GetThemePath(theme); - if (string.IsNullOrEmpty(path)) + try { - Log.Error($"|Theme.ChangeTheme|Theme path can't be found <{path}>, use default dark theme"); - path = GetThemePath(dark); if (string.IsNullOrEmpty(path)) - { - valid = false; - Log.Error($"|Theme.ChangeTheme|Default theme path can't be found <{path}>"); - } - else - { - valid = true; - theme = dark; - } - } - else - { - valid = true; - } + throw new DirectoryNotFoundException("Theme path can't be found <{path}>"); - if (valid) - { Settings.Theme = theme; var dicts = Application.Current.Resources.MergedDictionaries; - if (_oldTheme != theme) + //always allow re-loading default theme, in case of failure of switching to a new theme from default theme + if (_oldTheme != theme || theme == defaultTheme) { dicts.Remove(_oldResource); - //fixme if something goes wrong here var newResource = GetResourceDictionary(); dicts.Add(newResource); _oldResource = newResource; _oldTheme = Path.GetFileNameWithoutExtension(_oldResource.Source.AbsolutePath); } } + catch (DirectoryNotFoundException e) + { + Log.Error($"|Theme.ChangeTheme|Theme <{theme}> path can't be found"); + if (theme != defaultTheme) + { + MessageBox.Show(string.Format(InternationalizationManager.Instance.GetTranslation("theme_load_failure_path_not_exists"), theme)); + ChangeTheme(defaultTheme); + } + return false; + } + catch (XamlParseException e) + { + Log.Error($"|Theme.ChangeTheme|Theme <{theme}> fail to parse"); + if (theme != defaultTheme) + { + MessageBox.Show(string.Format(InternationalizationManager.Instance.GetTranslation("theme_load_failure_parse_error"), theme)); + ChangeTheme(defaultTheme); + } + return false; + } + return true; } public ResourceDictionary GetResourceDictionary() diff --git a/Wox/Languages/en.xaml b/Wox/Languages/en.xaml index 7431d1da55..37f944d871 100644 --- a/Wox/Languages/en.xaml +++ b/Wox/Languages/en.xaml @@ -52,6 +52,8 @@ Result Item Font Window Mode Opacity + Theme {0} not exists, fallback to default theme + Fail to load theme {0}, fallback to default theme Hotkey diff --git a/Wox/Languages/zh-cn.xaml b/Wox/Languages/zh-cn.xaml index 9b3bff0c7c..5e9570d543 100644 --- a/Wox/Languages/zh-cn.xaml +++ b/Wox/Languages/zh-cn.xaml @@ -52,6 +52,8 @@ 结果项字体 窗口模式 透明度 + 无法找到主题 {0} ,切换为默认主题 + 无法加载主题 {0} ,切换为默认主题 热键