mirror of
https://github.com/microsoft/PowerToys.git
synced 2024-12-13 11:09:28 +08:00
error handling for theme loader
This commit is contained in:
parent
580c2b9e84
commit
0baf7744bc
@ -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()
|
||||
|
@ -52,6 +52,8 @@
|
||||
<system:String x:Key="resultItemFont">Result Item Font</system:String>
|
||||
<system:String x:Key="windowMode">Window Mode</system:String>
|
||||
<system:String x:Key="opacity">Opacity</system:String>
|
||||
<system:String x:Key="theme_load_failure_path_not_exists">Theme {0} not exists, fallback to default theme</system:String>
|
||||
<system:String x:Key="theme_load_failure_parse_error">Fail to load theme {0}, fallback to default theme</system:String>
|
||||
|
||||
<!--Setting Hotkey-->
|
||||
<system:String x:Key="hotkey">Hotkey</system:String>
|
||||
|
@ -52,6 +52,8 @@
|
||||
<system:String x:Key="resultItemFont">结果项字体</system:String>
|
||||
<system:String x:Key="windowMode">窗口模式</system:String>
|
||||
<system:String x:Key="opacity">透明度</system:String>
|
||||
<system:String x:Key="theme_load_failure_path_not_exists">无法找到主题 {0} ,切换为默认主题</system:String>
|
||||
<system:String x:Key="theme_load_failure_parse_error">无法加载主题 {0} ,切换为默认主题</system:String>
|
||||
|
||||
<!--设置,热键-->
|
||||
<system:String x:Key="hotkey">热键</system:String>
|
||||
|
Loading…
Reference in New Issue
Block a user