Skip to content

Commit

Permalink
Sync threads for theme changing
Browse files Browse the repository at this point in the history
  • Loading branch information
Nice3point committed Jan 29, 2025
1 parent 8003c58 commit 9f43346
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace RevitLookup.Abstractions.Services.Appearance;
public interface IThemeWatcherService
{
void Initialize();
void Watch();
void ApplyTheme();
void Watch(FrameworkElement frameworkElement);
void Unwatch();
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public void Initialize()
{
}

public void Watch()
public void ApplyTheme()
{
var theme = settingsService.GeneralSettings.Theme;
ApplicationThemeManager.Apply(theme, settingsService.GeneralSettings.Background);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ partial void OnThemeChanged(ApplicationTheme value)
if (!_initialized) return;

_settingsService.GeneralSettings.Theme = value;
_themeWatcherService.Watch();
_themeWatcherService.ApplyTheme();
}

partial void OnBackgroundChanged(WindowBackdropType value)
Expand Down
1 change: 1 addition & 0 deletions source/RevitLookup/Application.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ private static void EnableThemes()
{
var themeWatcherService = Host.GetService<IThemeWatcherService>();
themeWatcherService.Initialize();
themeWatcherService.ApplyTheme();
}

public static void EnableHardwareRendering()
Expand Down
41 changes: 26 additions & 15 deletions source/RevitLookup/Services/Appearance/ThemeWatcherService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,10 @@ public void Initialize()
Source = new Uri("pack://application:,,,/RevitLookup;component/Styles/App.Resources.xaml", UriKind.Absolute)
};

ApplicationThemeManager.Apply(settingsService.GeneralSettings.Theme, settingsService.GeneralSettings.Background);
ApplicationThemeManager.Changed += ApplicationThemeManagerOnChanged;
ApplicationThemeManager.Changed += OnApplicationThemeManagerChanged;
}

public void Watch()
public void ApplyTheme()
{
var theme = settingsService.GeneralSettings.Theme;
#if REVIT2024_OR_GREATER
Expand All @@ -61,11 +60,24 @@ public void Watch()

if (!_isWatching)
{
RevitShell.ActionEventHandler.Raise(_ => Context.UiApplication.ThemeChanged += OnRevitThemeChanged);
RevitShell.ActionEventHandler.Raise(application => application.ThemeChanged += OnRevitThemeChanged);
_isWatching = true;
}
}
#endif
var mainWindow = _observedElements.FirstOrDefault();
if (mainWindow is not null)
{
mainWindow.Dispatcher.Invoke(() => ApplyResources(theme));
}
else
{
ApplyResources(theme);
}
}

private void ApplyResources(ApplicationTheme theme)
{
ApplicationThemeManager.Apply(theme, settingsService.GeneralSettings.Background);
UpdateBackground(theme);
}
Expand All @@ -82,21 +94,12 @@ public void Unwatch()
#if REVIT2024_OR_GREATER
if (!_isWatching) return;

RevitShell.ActionEventHandler.Raise(_ => Context.UiApplication.ThemeChanged -= OnRevitThemeChanged);
RevitShell.ActionEventHandler.Raise(application => application.ThemeChanged -= OnRevitThemeChanged);
_isWatching = false;
#endif
}

#if REVIT2024_OR_GREATER
private void OnRevitThemeChanged(object? sender, ThemeChangedEventArgs args)
{
if (args.ThemeChangedType != ThemeType.UITheme) return;

var theme = GetRevitTheme();
ApplicationThemeManager.Apply(theme, settingsService.GeneralSettings.Background);
UpdateBackground(theme);
}

private static ApplicationTheme GetRevitTheme()
{
return UIThemeManager.CurrentTheme switch
Expand All @@ -106,9 +109,17 @@ private static ApplicationTheme GetRevitTheme()
_ => throw new ArgumentOutOfRangeException()
};
}

private void OnRevitThemeChanged(object? sender, ThemeChangedEventArgs args)
{
if (args.ThemeChangedType != ThemeType.UITheme) return;

ApplyTheme();
}

#endif

private void ApplicationThemeManagerOnChanged(ApplicationTheme applicationTheme, Color accent)
private void OnApplicationThemeManagerChanged(ApplicationTheme applicationTheme, Color accent)
{
foreach (var frameworkElement in _observedElements)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ partial void OnThemeChanged(ApplicationTheme value)
if (!_initialized) return;

_settingsService.GeneralSettings.Theme = value;
_themeWatcherService.Watch();
_themeWatcherService.ApplyTheme();
}

partial void OnThemeChanged(ApplicationTheme oldValue, ApplicationTheme newValue)
Expand Down

0 comments on commit 9f43346

Please sign in to comment.