@@ -36,7 +36,7 @@ public Theme()
3636 {
3737 _themeDirectories . Add ( DirectoryPath ) ;
3838 _themeDirectories . Add ( UserDirectoryPath ) ;
39- MakesureThemeDirectoriesExist ( ) ;
39+ MakeSureThemeDirectoriesExist ( ) ;
4040
4141 var dicts = Application . Current . Resources . MergedDictionaries ;
4242 _oldResource = dicts . First ( d =>
@@ -55,20 +55,17 @@ public Theme()
5555 _oldTheme = Path . GetFileNameWithoutExtension ( _oldResource . Source . AbsolutePath ) ;
5656 }
5757
58- private void MakesureThemeDirectoriesExist ( )
58+ private void MakeSureThemeDirectoriesExist ( )
5959 {
60- foreach ( string dir in _themeDirectories )
60+ foreach ( var dir in _themeDirectories . Where ( dir => ! Directory . Exists ( dir ) ) )
6161 {
62- if ( ! Directory . Exists ( dir ) )
62+ try
6363 {
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 ) ;
7269 }
7370 }
7471 }
@@ -82,13 +79,14 @@ public bool ChangeTheme(string theme)
8279 {
8380 if ( string . IsNullOrEmpty ( path ) )
8481 throw new DirectoryNotFoundException ( "Theme path can't be found <{path}>" ) ;
85-
86- Settings . Theme = theme ;
87-
82+
8883 // reload all resources even if the theme itself hasn't changed in order to pickup changes
8984 // to things like fonts
90- UpdateResourceDictionary ( GetResourceDictionary ( ) ) ;
85+ UpdateResourceDictionary ( GetResourceDictionary ( theme ) ) ;
86+
87+ Settings . Theme = theme ;
9188
89+
9290 //always allow re-loading default theme, in case of failure of switching to a new theme from default theme
9391 if ( _oldTheme != theme || theme == defaultTheme )
9492 {
@@ -134,9 +132,9 @@ private void UpdateResourceDictionary(ResourceDictionary dictionaryToUpdate)
134132 _oldResource = dictionaryToUpdate ;
135133 }
136134
137- private ResourceDictionary CurrentThemeResourceDictionary ( )
135+ private ResourceDictionary GetThemeResourceDictionary ( string theme )
138136 {
139- var uri = GetThemePath ( Settings . Theme ) ;
137+ var uri = GetThemePath ( theme ) ;
140138 var dict = new ResourceDictionary
141139 {
142140 Source = new Uri ( uri , UriKind . Absolute )
@@ -145,10 +143,12 @@ private ResourceDictionary CurrentThemeResourceDictionary()
145143 return dict ;
146144 }
147145
148- public ResourceDictionary GetResourceDictionary ( )
146+ private ResourceDictionary CurrentThemeResourceDictionary ( ) => GetThemeResourceDictionary ( Settings . Theme ) ;
147+
148+ public ResourceDictionary GetResourceDictionary ( string theme )
149149 {
150- var dict = CurrentThemeResourceDictionary ( ) ;
151-
150+ var dict = GetThemeResourceDictionary ( theme ) ;
151+
152152 if ( dict [ "QueryBoxStyle" ] is Style queryBoxStyle &&
153153 dict [ "QuerySuggestionBoxStyle" ] is Style querySuggestionBoxStyle )
154154 {
@@ -200,6 +200,11 @@ public ResourceDictionary GetResourceDictionary()
200200 return dict ;
201201 }
202202
203+ private ResourceDictionary GetCurrentResourceDictionary ( )
204+ {
205+ return GetResourceDictionary ( Settings . Theme ) ;
206+ }
207+
203208 public List < string > LoadAvailableThemes ( )
204209 {
205210 List < string > themes = new List < string > ( ) ;
@@ -229,7 +234,7 @@ private string GetThemePath(string themeName)
229234
230235 public void AddDropShadowEffectToCurrentTheme ( )
231236 {
232- var dict = GetResourceDictionary ( ) ;
237+ var dict = GetCurrentResourceDictionary ( ) ;
233238
234239 var windowBorderStyle = dict [ "WindowBorderStyle" ] as Style ;
235240
@@ -273,7 +278,7 @@ public void AddDropShadowEffectToCurrentTheme()
273278
274279 public void RemoveDropShadowEffectFromCurrentTheme ( )
275280 {
276- var dict = CurrentThemeResourceDictionary ( ) ;
281+ var dict = GetCurrentResourceDictionary ( ) ;
277282 var windowBorderStyle = dict [ "WindowBorderStyle" ] as Style ;
278283
279284 var effectSetter = windowBorderStyle . Setters . FirstOrDefault ( setterBase => setterBase is Setter setter && setter . Property == Border . EffectProperty ) as Setter ;
0 commit comments