mirror of
https://github.com/Flow-Launcher/Flow.Launcher.git
synced 2026-03-11 08:54:32 +00:00
error handling for theme loader
This commit is contained in:
parent
580c2b9e84
commit
0baf7744bc
3 changed files with 32 additions and 24 deletions
|
|
@ -6,6 +6,7 @@ using System.Runtime.InteropServices;
|
||||||
using System.Windows;
|
using System.Windows;
|
||||||
using System.Windows.Controls;
|
using System.Windows.Controls;
|
||||||
using System.Windows.Interop;
|
using System.Windows.Interop;
|
||||||
|
using System.Windows.Markup;
|
||||||
using System.Windows.Media;
|
using System.Windows.Media;
|
||||||
using Wox.Infrastructure;
|
using Wox.Infrastructure;
|
||||||
using Wox.Infrastructure.Logger;
|
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";
|
const string defaultTheme = "Dark";
|
||||||
bool valid;
|
|
||||||
|
|
||||||
string path = GetThemePath(theme);
|
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))
|
if (string.IsNullOrEmpty(path))
|
||||||
{
|
throw new DirectoryNotFoundException("Theme path can't be found <{path}>");
|
||||||
valid = false;
|
|
||||||
Log.Error($"|Theme.ChangeTheme|Default theme path can't be found <{path}>");
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
valid = true;
|
|
||||||
theme = dark;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
valid = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (valid)
|
|
||||||
{
|
|
||||||
Settings.Theme = theme;
|
Settings.Theme = theme;
|
||||||
|
|
||||||
var dicts = Application.Current.Resources.MergedDictionaries;
|
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);
|
dicts.Remove(_oldResource);
|
||||||
//fixme if something goes wrong here
|
|
||||||
var newResource = GetResourceDictionary();
|
var newResource = GetResourceDictionary();
|
||||||
dicts.Add(newResource);
|
dicts.Add(newResource);
|
||||||
_oldResource = newResource;
|
_oldResource = newResource;
|
||||||
_oldTheme = Path.GetFileNameWithoutExtension(_oldResource.Source.AbsolutePath);
|
_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()
|
public ResourceDictionary GetResourceDictionary()
|
||||||
|
|
|
||||||
|
|
@ -52,6 +52,8 @@
|
||||||
<system:String x:Key="resultItemFont">Result Item Font</system:String>
|
<system:String x:Key="resultItemFont">Result Item Font</system:String>
|
||||||
<system:String x:Key="windowMode">Window Mode</system:String>
|
<system:String x:Key="windowMode">Window Mode</system:String>
|
||||||
<system:String x:Key="opacity">Opacity</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-->
|
<!--Setting Hotkey-->
|
||||||
<system:String x:Key="hotkey">Hotkey</system:String>
|
<system:String x:Key="hotkey">Hotkey</system:String>
|
||||||
|
|
|
||||||
|
|
@ -52,6 +52,8 @@
|
||||||
<system:String x:Key="resultItemFont">结果项字体</system:String>
|
<system:String x:Key="resultItemFont">结果项字体</system:String>
|
||||||
<system:String x:Key="windowMode">窗口模式</system:String>
|
<system:String x:Key="windowMode">窗口模式</system:String>
|
||||||
<system:String x:Key="opacity">透明度</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>
|
<system:String x:Key="hotkey">热键</system:String>
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue