Skip to content

Commit 8e99e97

Browse files
committed
refactor: improve theme update logic and clean up code
1 parent abd680a commit 8e99e97

File tree

2 files changed

+40
-31
lines changed

2 files changed

+40
-31
lines changed
Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
namespace RevitDevTool.Theme ;
1+
namespace RevitDevTool.Theme;
22

33
internal static class Win32DarkMode
44
{
5-
private const int DarkModeBefore20H1 = 19 ;
6-
private const int DarkMode = 20 ;
7-
5+
private const int DarkModeBefore20H1 = 19;
6+
private const int DarkMode = 20;
7+
88
[System.Runtime.InteropServices.DllImport("dwmapi.dll", CharSet = System.Runtime.InteropServices.CharSet.Unicode, SetLastError = true)]
99
private static extern int DwmSetWindowAttribute(IntPtr hwnd, int attr, ref int attrValue, int attrSize);
1010

1111
[System.Runtime.InteropServices.DllImport("uxtheme.dll", CharSet = System.Runtime.InteropServices.CharSet.Unicode, SetLastError = true)]
1212
private static extern int SetWindowTheme(IntPtr hWnd, string? pszSubAppName, string? pszSubIdList);
1313

14-
public static void SetImmersiveDarkMode( IntPtr hwnd, bool enable )
14+
public static void SetImmersiveDarkMode(IntPtr hwnd, bool isDark)
1515
{
16-
if ( hwnd == IntPtr.Zero ) return ;
17-
var useDark = enable ? 1 : 0 ;
18-
_ = DwmSetWindowAttribute( hwnd, DarkMode, ref useDark, sizeof( int ) ) ;
19-
_ = DwmSetWindowAttribute( hwnd, DarkModeBefore20H1, ref useDark, sizeof( int ) ) ;
20-
SetWindowTheme( hwnd, enable ? "DarkMode_Explorer" : "Explorer", null ) ;
16+
if (hwnd == IntPtr.Zero) return;
17+
var useDark = isDark ? 1 : 0;
18+
_ = SetWindowTheme(hwnd, isDark ? "DarkMode_Explorer" : "Explorer", null);
19+
_ = DwmSetWindowAttribute(hwnd, DarkMode, ref useDark, sizeof(int));
20+
_ = DwmSetWindowAttribute(hwnd, DarkModeBefore20H1, ref useDark, sizeof(int));
2121
}
2222
}

source/RevitDevTool/ViewModel/TraceLogViewModel.cs

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)