@@ -36,7 +36,7 @@ public Theme()
36
36
{
37
37
_themeDirectories . Add ( DirectoryPath ) ;
38
38
_themeDirectories . Add ( UserDirectoryPath ) ;
39
- MakesureThemeDirectoriesExist ( ) ;
39
+ MakeSureThemeDirectoriesExist ( ) ;
40
40
41
41
var dicts = Application . Current . Resources . MergedDictionaries ;
42
42
_oldResource = dicts . First ( d =>
@@ -55,20 +55,17 @@ public Theme()
55
55
_oldTheme = Path . GetFileNameWithoutExtension ( _oldResource . Source . AbsolutePath ) ;
56
56
}
57
57
58
- private void MakesureThemeDirectoriesExist ( )
58
+ private void MakeSureThemeDirectoriesExist ( )
59
59
{
60
- foreach ( string dir in _themeDirectories )
60
+ foreach ( var dir in _themeDirectories . Where ( dir => ! Directory . Exists ( dir ) ) )
61
61
{
62
- if ( ! Directory . Exists ( dir ) )
62
+ try
63
63
{
64
- try
65
- {
66
- Directory . CreateDirectory ( dir ) ;
67
- }
68
- catch ( Exception e )
69
- {
70
- Log . Exception ( $ "|Theme.MakesureThemeDirectoriesExist|Exception when create directory <{ dir } >", e ) ;
71
- }
64
+ Directory . CreateDirectory ( dir ) ;
65
+ }
66
+ catch ( Exception e )
67
+ {
68
+ Log . Exception ( $ "|Theme.MakesureThemeDirectoriesExist|Exception when create directory <{ dir } >", e ) ;
72
69
}
73
70
}
74
71
}
@@ -82,13 +79,14 @@ public bool ChangeTheme(string theme)
82
79
{
83
80
if ( string . IsNullOrEmpty ( path ) )
84
81
throw new DirectoryNotFoundException ( "Theme path can't be found <{path}>" ) ;
85
-
86
- Settings . Theme = theme ;
87
-
82
+
88
83
// reload all resources even if the theme itself hasn't changed in order to pickup changes
89
84
// to things like fonts
90
- UpdateResourceDictionary ( GetResourceDictionary ( ) ) ;
85
+ UpdateResourceDictionary ( GetResourceDictionary ( theme ) ) ;
86
+
87
+ Settings . Theme = theme ;
91
88
89
+
92
90
//always allow re-loading default theme, in case of failure of switching to a new theme from default theme
93
91
if ( _oldTheme != theme || theme == defaultTheme )
94
92
{
@@ -134,9 +132,9 @@ private void UpdateResourceDictionary(ResourceDictionary dictionaryToUpdate)
134
132
_oldResource = dictionaryToUpdate ;
135
133
}
136
134
137
- private ResourceDictionary CurrentThemeResourceDictionary ( )
135
+ private ResourceDictionary GetThemeResourceDictionary ( string theme )
138
136
{
139
- var uri = GetThemePath ( Settings . Theme ) ;
137
+ var uri = GetThemePath ( theme ) ;
140
138
var dict = new ResourceDictionary
141
139
{
142
140
Source = new Uri ( uri , UriKind . Absolute )
@@ -145,10 +143,12 @@ private ResourceDictionary CurrentThemeResourceDictionary()
145
143
return dict ;
146
144
}
147
145
148
- public ResourceDictionary GetResourceDictionary ( )
146
+ private ResourceDictionary CurrentThemeResourceDictionary ( ) => GetThemeResourceDictionary ( Settings . Theme ) ;
147
+
148
+ public ResourceDictionary GetResourceDictionary ( string theme )
149
149
{
150
- var dict = CurrentThemeResourceDictionary ( ) ;
151
-
150
+ var dict = GetThemeResourceDictionary ( theme ) ;
151
+
152
152
if ( dict [ "QueryBoxStyle" ] is Style queryBoxStyle &&
153
153
dict [ "QuerySuggestionBoxStyle" ] is Style querySuggestionBoxStyle )
154
154
{
@@ -200,6 +200,11 @@ public ResourceDictionary GetResourceDictionary()
200
200
return dict ;
201
201
}
202
202
203
+ private ResourceDictionary GetCurrentResourceDictionary ( )
204
+ {
205
+ return GetResourceDictionary ( Settings . Theme ) ;
206
+ }
207
+
203
208
public List < string > LoadAvailableThemes ( )
204
209
{
205
210
List < string > themes = new List < string > ( ) ;
@@ -229,7 +234,7 @@ private string GetThemePath(string themeName)
229
234
230
235
public void AddDropShadowEffectToCurrentTheme ( )
231
236
{
232
- var dict = GetResourceDictionary ( ) ;
237
+ var dict = GetCurrentResourceDictionary ( ) ;
233
238
234
239
var windowBorderStyle = dict [ "WindowBorderStyle" ] as Style ;
235
240
@@ -273,7 +278,7 @@ public void AddDropShadowEffectToCurrentTheme()
273
278
274
279
public void RemoveDropShadowEffectFromCurrentTheme ( )
275
280
{
276
- var dict = CurrentThemeResourceDictionary ( ) ;
281
+ var dict = GetCurrentResourceDictionary ( ) ;
277
282
var windowBorderStyle = dict [ "WindowBorderStyle" ] as Style ;
278
283
279
284
var effectSetter = windowBorderStyle . Setters . FirstOrDefault ( setterBase => setterBase is Setter setter && setter . Property == Border . EffectProperty ) as Setter ;
0 commit comments