1414
1515namespace H2MLauncher . UI . Services ;
1616
17+
1718public partial class CustomizationManager : ObservableObject
1819{
1920 internal const string DefaultResourceDirectory = "pack://application:,,,/H2MLauncher.UI;component" ;
@@ -93,7 +94,9 @@ public async Task LoadInitialValues()
9394 if ( customizationSettings ? . Themes is not null &&
9495 customizationSettings . Themes . Count > 0 )
9596 {
96- if ( ! File . Exists ( customizationSettings . Themes [ 0 ] ) )
97+ string selectedThemeFile = customizationSettings . Themes [ 0 ] ;
98+ bool isInternal = selectedThemeFile . StartsWith ( Constants . EmbeddedThemesAbsolutePath ) ;
99+ if ( isInternal ? ! Uri . TryCreate ( selectedThemeFile , UriKind . Absolute , out Uri ? themeResourceUri ) : ! File . Exists ( selectedThemeFile ) )
97100 {
98101 UpdateCustomizationSettings ( settings => settings with
99102 {
@@ -102,8 +105,8 @@ public async Task LoadInitialValues()
102105 }
103106 else
104107 {
105- LoadTheme ( customizationSettings . Themes [ 0 ] ) ;
106- }
108+ LoadTheme ( customizationSettings . Themes [ 0 ] ) ;
109+ }
107110 }
108111
109112 HotReloadThemes = customizationSettings ? . HotReloadThemes ?? false ;
@@ -248,15 +251,19 @@ public bool LoadTheme(string xamlPath)
248251 {
249252 string themeDirectory = Path . GetDirectoryName ( xamlPath ) ! + '\\ ' ;
250253
254+ bool isEmbedded = xamlPath . StartsWith ( "pack://application" ) ;
255+
251256 // load resource dictionary
252- using FileStream stream = new ( xamlPath , FileMode . Open , FileAccess . Read ) ;
257+ using Stream stream = isEmbedded
258+ ? Application . GetResourceStream ( new Uri ( xamlPath ) ) . Stream
259+ : new FileStream ( xamlPath , FileMode . Open , FileAccess . Read ) ;
253260
254261 ParserContext parserContext = new ( )
255262 {
256263 // Critical: this makes relative URIs work!
257- BaseUri = new Uri ( themeDirectory , UriKind . Absolute )
264+ BaseUri = new Uri ( isEmbedded ? xamlPath . Substring ( 0 , xamlPath . LastIndexOf ( '/' ) + 1 ) : themeDirectory , UriKind . Absolute )
258265 } ;
259-
266+
260267 ResourceDictionary resourceDictionary = ( ResourceDictionary ) XamlReader . Load ( stream , parserContext ) ;
261268
262269 // remove old one
@@ -295,19 +302,20 @@ public void ResetTheme()
295302 if ( Application . Current . Resources . MergedDictionaries . Count > 1 )
296303 {
297304 Application . Current . Resources . MergedDictionaries . RemoveAt ( 1 ) ;
305+ }
298306
299- // reset resource directory
300- Application . Current . Resources . Remove ( Constants . CurrentThemeDirectoryKey ) ;
301- CurrentThemeDirectory = DefaultResourceDirectory ;
302- CurrentThemeFile = null ;
307+ // reset resource directory
308+ Application . Current . Resources . Remove ( Constants . CurrentThemeDirectoryKey ) ;
309+ CurrentThemeDirectory = DefaultResourceDirectory ;
310+ CurrentThemeFile = null ;
303311
304- UpdateCustomizationSettings ( settings => settings with
305- {
306- Themes = [ ]
307- } ) ;
312+ UpdateCustomizationSettings ( settings => settings with
313+ {
314+ Themes = [ ]
315+ } ) ;
316+
317+ ThemeLoadingError = false ;
308318
309- ThemeLoadingError = false ;
310- }
311319 }
312320
313321 partial void OnBackgroundBlurChanged ( double value )
0 commit comments