diff --git a/CPAP-Exporter.UI/CpapExporterThemeDetector.cs b/CPAP-Exporter.UI/CpapExporterThemeDetector.cs new file mode 100644 index 0000000..3b58cf6 --- /dev/null +++ b/CPAP-Exporter.UI/CpapExporterThemeDetector.cs @@ -0,0 +1,47 @@ +using System.Windows; + +namespace CascadePass.CPAPExporter +{ + public class CpapExporterThemeDetector : ThemeDetector + { + public CpapExporterThemeDetector() : base() + { + } + + public CpapExporterThemeDetector(IRegistryProvider registryProviderToUse) : base(registryProviderToUse) + { + } + + public override bool ApplyTheme() + { + if (Application.Current?.Dispatcher is null) + { + // This is probably a unit test. + return false; + } + + Application.Current.Dispatcher.Invoke(() => + { + + Application.Current.Resources.MergedDictionaries.Clear(); + + var currentTheme = this.GetThemeType(); + if (currentTheme == ThemeType.Dark) + { + Application.Current.Resources.MergedDictionaries.Add(new() { Source = new Uri("/Themes/Dark.xaml", UriKind.Relative) }); + } + else if (currentTheme == ThemeType.Light) + { + Application.Current.Resources.MergedDictionaries.Add(new() { Source = new Uri("/Themes/Light.xaml", UriKind.Relative) }); + } + + Application.Current.Resources.MergedDictionaries.Add(new() { Source = new Uri("/Fonts.xaml", UriKind.Relative) }); + Application.Current.Resources.MergedDictionaries.Add(new() { Source = new Uri("/Styles.xaml", UriKind.Relative) }); + + Application.Current.Resources.MergedDictionaries.Add(new() { Source = new Uri("pack://application:,,,/PresentationFramework.Fluent;component/Themes/Fluent.xaml", UriKind.Absolute) }); + }); + + return true; + } + } +} diff --git a/CPAP-Exporter.UI/ThemeDetector.cs b/CPAP-Exporter.UI/ThemeDetector.cs index bfa6830..04a3b4a 100644 --- a/CPAP-Exporter.UI/ThemeDetector.cs +++ b/CPAP-Exporter.UI/ThemeDetector.cs @@ -1,9 +1,4 @@ using Microsoft.Win32; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; using System.Windows; namespace CascadePass.CPAPExporter @@ -94,42 +89,6 @@ protected async void OnThemeChanged(object sender, EventArgs e) #endregion } - public class CpapExporterThemeDetector : ThemeDetector - { - public CpapExporterThemeDetector() : base() - { - } - - public CpapExporterThemeDetector(IRegistryProvider registryProviderToUse) : base(registryProviderToUse) - { - } - - public override bool ApplyTheme() - { - // Implement the logic to apply the theme based on the current system settings. - - Application.Current.Resources.MergedDictionaries.Clear(); - - var currentTheme = this.GetThemeType(); - if (currentTheme == ThemeType.Dark) - { - Application.Current.Resources.MergedDictionaries.Add(new() { Source = new Uri("/Themes/Dark.xaml", UriKind.Relative) }); - } - else if (currentTheme == ThemeType.Light) - { - Application.Current.Resources.MergedDictionaries.Add(new() { Source = new Uri("/Themes/Light.xaml", UriKind.Relative) }); - } - - Application.Current.Resources.MergedDictionaries.Add(new() { Source = new Uri("/Fonts.xaml", UriKind.Relative) }); - Application.Current.Resources.MergedDictionaries.Add(new() { Source = new Uri("/Styles.xaml", UriKind.Relative) }); - - Application.Current.Resources.MergedDictionaries.Add(new() { Source = new Uri("pack://application:,,,/PresentationFramework.Fluent;component/Themes/Fluent.xaml", UriKind.Absolute) }); - //base.ThemeChanged?.Invoke(this, EventArgs.Empty); - - return true; - } - } - /// /// Represents a type or category of theme, of which there can be many individual /// themes. Applications typically implement their own themes, sometimes many;