@@ -32,13 +32,13 @@ internal partial class TraceLogViewModel : ObservableObject, IDisposable
3232
3333 public WindowsFormsHost LogTextBox { get ; }
3434
35- [ ObservableProperty ] [ NotifyCanExecuteChangedFor ( nameof ( OpenSettingsCommand ) ) ]
35+ [ ObservableProperty ]
36+ [ NotifyCanExecuteChangedFor ( nameof ( OpenSettingsCommand ) ) ]
3637 private bool _isSettingOpened ;
3738
3839 [ ObservableProperty ]
3940 private bool _isStarted = true ;
4041
41- [ ObservableProperty ]
4242 private bool _isSubcribe ;
4343
4444 [ ObservableProperty ]
@@ -127,19 +127,26 @@ private void ApplyTextBoxTheme()
127127 _winFormsTextBox . BackColor = isDark
128128 ? System . Drawing . Color . FromArgb ( 30 , 30 , 30 )
129129 : System . Drawing . Color . FromArgb ( 250 , 250 , 250 ) ;
130-
131- Win32DarkMode . SetImmersiveDarkMode ( _winFormsTextBox . Handle , isDark ) ;
130+
131+ if ( _winFormsTextBox . Handle != IntPtr . Zero )
132+ {
133+ Win32DarkMode . SetImmersiveDarkMode ( _winFormsTextBox . Handle , isDark ) ;
134+ }
132135 }
133136
134137 private void OnThemeChanged ( ApplicationTheme theme , System . Windows . Media . Color accent )
135138 {
136139 if ( _currentTheme == theme ) return ;
137-
140+ UpdateTheme ( theme , IsStarted ) ;
141+ }
142+
143+ private void UpdateTheme ( ApplicationTheme theme , bool shouldRestart )
144+ {
138145 LogTextBox . Dispatcher . Invoke ( ( ) =>
139146 {
140147 _currentTheme = theme ;
141148 ApplyTextBoxTheme ( ) ;
142- if ( IsStarted )
149+ if ( shouldRestart )
143150 {
144151 RestartLogging ( ) ;
145152 }
@@ -156,29 +163,27 @@ private void RestartLogging()
156163
157164 public void Subcribe ( )
158165 {
159- if ( IsSubcribe ) return ;
166+ if ( _isSubcribe ) return ;
160167
161- Debug . WriteLine ( "TraceLogViewModel Subscribe" ) ;
168+ _consoleRedirector ??= new ConsoleRedirector ( ) ;
162169
170+ UpdateTheme ( ThemeWatcher . GetRequiredTheme ( ) , true ) ;
163171 IsStarted = true ;
164- _consoleRedirector ??= new ConsoleRedirector ( ) ;
165172
166173 Context . UiApplication . Idling += _onIdlingHandler ;
167174 ApplicationThemeManager . Changed += _onThemeChangedHandler ;
168175
169- IsSubcribe = true ;
176+ _isSubcribe = true ;
170177 }
171178
172179 private void Unsubscribe ( )
173180 {
174- if ( ! IsSubcribe ) return ;
175-
176- Debug . WriteLine ( "TraceLogViewModel Unsubscribe" ) ;
177-
181+ if ( ! _isSubcribe ) return ;
182+
178183 Context . UiApplication . Idling -= _onIdlingHandler ;
179184 ApplicationThemeManager . Changed -= _onThemeChangedHandler ;
180-
181- IsSubcribe = false ;
185+
186+ _isSubcribe = false ;
182187 }
183188
184189 private void OnIdling ( object ? sender , IdlingEventArgs e )
@@ -206,13 +211,17 @@ public TraceLogViewModel()
206211 Child = _winFormsTextBox
207212 } ;
208213
214+ LogTextBox . Loaded += ( _ , _ ) =>
215+ {
216+ ApplyTextBoxTheme ( ) ;
217+ } ;
218+
209219 PresentationTraceSources . ResourceDictionarySource . Switch . Level = SourceLevels . Critical ;
210220 _levelSwitch = new LoggingLevelSwitch ( _logLevel ) ;
211221 _onThemeChangedHandler = OnThemeChanged ;
212222 _onIdlingHandler = OnIdling ;
213223 _currentTheme = ThemeWatcher . GetRequiredTheme ( ) ;
214224
215- ApplyTextBoxTheme ( ) ;
216225 Subcribe ( ) ;
217226 StartTracing ( ) ;
218227 }
@@ -238,22 +247,22 @@ private void OpenSettings()
238247 settingsWindow . Show ( ) ;
239248 settingsWindow . NavigationService . Navigate ( typeof ( GeneralSettingsView ) ) ;
240249 IsSettingOpened = true ;
241- settingsWindow . Closed += ( _ , _ ) => { IsSettingOpened = false ; } ;
250+ settingsWindow . Closed += ( _ , _ ) => { IsSettingOpened = false ; } ;
242251 }
243252
244253 private bool CanOpenSettings ( ) => ! IsSettingOpened ;
245254
246255 public void Dispose ( )
247256 {
248257 Debug . WriteLine ( "TraceLogViewModel Dispose" ) ;
249-
258+
250259 IsStarted = false ;
251260 StopTracing ( ) ;
252261 Unsubscribe ( ) ;
253-
262+
254263 _consoleRedirector ? . Dispose ( ) ;
255264 _consoleRedirector = null ;
256-
265+
257266 GC . SuppressFinalize ( this ) ;
258267 }
259268}
0 commit comments