33using Avalonia . Markup . Xaml ;
44using Avalonia . Markup . Xaml . Styling ;
55using Avalonia . Styling ;
6+ using PleasantUI . Core ;
67
78namespace PleasantUI . ToolKit ;
89
@@ -13,6 +14,7 @@ public class PleasantUIToolKit : ResourceDictionary
1314{
1415 private static PleasantUIToolKit ? _instance ;
1516 private StyleInclude ? _vguiStyleInclude ;
17+ private bool _listenerAdded = false ;
1618
1719 /// <summary>
1820 /// Gets the singleton instance of PleasantUIToolKit
@@ -26,6 +28,109 @@ public PleasantUIToolKit()
2628 {
2729 _instance = this ;
2830 AvaloniaXamlLoader . Load ( this ) ;
31+
32+ TryAddListener ( ) ;
33+ }
34+
35+ private void TryAddListener ( )
36+ {
37+ if ( _listenerAdded ) return ;
38+
39+ if ( PleasantSettings . Current is not null )
40+ {
41+ PleasantSettings . Current . PropertyChanged += OnPleasantSettingsChanged ;
42+ _listenerAdded = true ;
43+ // Check if VGUI is already active
44+ UpdateVGUIStyle ( PleasantSettings . Current . Theme == "VGUI" ) ;
45+ System . Diagnostics . Debug . WriteLine ( "[PleasantUIToolKit] Listener added, checking VGUI status" ) ;
46+ }
47+ else
48+ {
49+ System . Diagnostics . Debug . WriteLine ( "[PleasantUIToolKit] PleasantSettings.Current is null, will retry later" ) ;
50+ // Retry after a delay
51+ System . Threading . Tasks . Task . Delay ( 100 ) . ContinueWith ( _ =>
52+ {
53+ Avalonia . Threading . Dispatcher . UIThread . Post ( TryAddListener ) ;
54+ } ) ;
55+ }
56+ }
57+
58+ private void OnPleasantSettingsChanged ( object ? sender , System . ComponentModel . PropertyChangedEventArgs e )
59+ {
60+ if ( e . PropertyName == nameof ( PleasantSettings . Current . Theme ) )
61+ {
62+ UpdateVGUIStyle ( PleasantSettings . Current ? . Theme == "VGUI" ) ;
63+ }
64+ }
65+
66+ /// <summary>
67+ /// Manually triggers VGUI style update (called from PleasantTheme via reflection)
68+ /// </summary>
69+ public static void UpdateVGUIStyleManually ( bool isVGUI )
70+ {
71+ System . Diagnostics . Debug . WriteLine ( $ "[PleasantUIToolKit] UpdateVGUIStyleManually called with isVGUI={ isVGUI } ") ;
72+
73+ if ( Avalonia . Application . Current ? . Resources is null )
74+ {
75+ System . Diagnostics . Debug . WriteLine ( "[PleasantUIToolKit] Application.Current.Resources is null" ) ;
76+ return ;
77+ }
78+
79+ System . Diagnostics . Debug . WriteLine ( $ "[PleasantUIToolKit] Application.Current.Resources.MergedDictionaries count: { Avalonia . Application . Current . Resources . MergedDictionaries . Count } ") ;
80+
81+ // Log all resources to debug
82+ int index = 0 ;
83+ foreach ( var resource in Avalonia . Application . Current . Resources . MergedDictionaries )
84+ {
85+ string resourceInfo = resource ? . GetType ( ) . FullName ?? "null" ;
86+ if ( resource is Avalonia . Markup . Xaml . Styling . ResourceInclude ri )
87+ {
88+ resourceInfo += $ " (Source: { ri . Source } )";
89+ }
90+ System . Diagnostics . Debug . WriteLine ( $ "[PleasantUIToolKit] Resource [{ index } ]: { resourceInfo } ") ;
91+ index ++ ;
92+ }
93+
94+ // Find PleasantUIToolKit in MergedDictionaries
95+ PleasantUIToolKit ? toolKit = null ;
96+ foreach ( var resource in Avalonia . Application . Current . Resources . MergedDictionaries )
97+ {
98+ if ( resource is PleasantUIToolKit tk )
99+ {
100+ toolKit = tk ;
101+ break ;
102+ }
103+ }
104+
105+ if ( toolKit is not null )
106+ {
107+ toolKit . UpdateVGUIStyle ( isVGUI ) ;
108+ }
109+ else
110+ {
111+ System . Diagnostics . Debug . WriteLine ( "[PleasantUIToolKit] PleasantUIToolKit not found in Application.Resources.MergedDictionaries" ) ;
112+ // Try to find it recursively in nested MergedDictionaries
113+ FindRecursive ( Avalonia . Application . Current . Resources , 0 ) ;
114+ }
115+ }
116+
117+ private static void FindRecursive ( IResourceDictionary dict , int depth )
118+ {
119+ string indent = new string ( ' ' , depth * 2 ) ;
120+ System . Diagnostics . Debug . WriteLine ( $ "{ indent } Checking dictionary: { dict ? . GetType ( ) . FullName } ") ;
121+
122+ if ( dict is PleasantUIToolKit )
123+ {
124+ System . Diagnostics . Debug . WriteLine ( $ "{ indent } FOUND PleasantUIToolKit!") ;
125+ }
126+
127+ foreach ( var resource in dict . MergedDictionaries )
128+ {
129+ if ( resource is IResourceDictionary rd )
130+ {
131+ FindRecursive ( rd , depth + 1 ) ;
132+ }
133+ }
29134 }
30135
31136 /// <summary>
@@ -35,6 +140,7 @@ public PleasantUIToolKit()
35140 public void UpdateVGUIStyle ( bool isVGUI )
36141 {
37142 System . Diagnostics . Debug . WriteLine ( $ "[PleasantUIToolKit] UpdateVGUIStyle called with isVGUI={ isVGUI } ") ;
143+ System . Diagnostics . Debug . WriteLine ( $ "[PleasantUIToolKit] MergedDictionaries count before: { MergedDictionaries . Count } ") ;
38144
39145 if ( isVGUI )
40146 {
@@ -48,7 +154,7 @@ public void UpdateVGUIStyle(bool isVGUI)
48154 Source = new Uri ( "avares://PleasantUI.ToolKit/Styling/VGUIControlThemes.axaml" )
49155 } ;
50156 MergedDictionaries . Add ( _vguiStyleInclude ) ;
51- System . Diagnostics . Debug . WriteLine ( "[PleasantUIToolKit] VGUIControlThemes.axaml added successfully" ) ;
157+ System . Diagnostics . Debug . WriteLine ( $ "[PleasantUIToolKit] VGUIControlThemes.axaml added successfully, MergedDictionaries count after: { MergedDictionaries . Count } ") ;
52158 }
53159 catch ( Exception ex )
54160 {
0 commit comments