diff --git a/src/TheXamlGuy.NotificationFlyout.Shared.UI/Helpers/SystemPersonalisationHelper.cs b/src/TheXamlGuy.NotificationFlyout.Shared.UI/Helpers/SystemPersonalisationHelper.cs index 06aebf1..2e3aa95 100644 --- a/src/TheXamlGuy.NotificationFlyout.Shared.UI/Helpers/SystemPersonalisationHelper.cs +++ b/src/TheXamlGuy.NotificationFlyout.Shared.UI/Helpers/SystemPersonalisationHelper.cs @@ -7,6 +7,8 @@ namespace TheXamlGuy.NotificationFlyout.Shared.UI.Helpers { public class SystemPersonalisationHelper : IWndProcHandler { + private static readonly Lazy _current = new(() => new SystemPersonalisationHelper()); + private readonly UISettings _settings = new(); private readonly string PersonalizeKey = @"Software\Microsoft\Windows\CurrentVersion\Themes\Personalize"; private SystemTheme _currentTheme; @@ -22,10 +24,17 @@ namespace TheXamlGuy.NotificationFlyout.Shared.UI.Helpers public event EventHandler ThemeChanged; + public static SystemPersonalisationHelper Current => _current.Value; public bool IsColorPrevalence => GetIsColorPrevalence(); public SystemTheme Theme => GetTheme(); - public static SystemPersonalisationHelper Create() => new(); + public void Handle(uint message, IntPtr wParam, IntPtr lParam) + { + if (message == (int)WndProcMessages.WM_SETTINGCHANGE) + { + RaiseThemeChangedEvent(); + } + } private bool GetIsColorPrevalence() { @@ -53,13 +62,5 @@ namespace TheXamlGuy.NotificationFlyout.Shared.UI.Helpers ThemeChanged?.Invoke(this, new SystemPersonalisationChangedEventArgs(theme, isColorPrevalence)); } } - - public void Handle(uint message, IntPtr wParam, IntPtr lParam) - { - if (message == (int)WndProcMessages.WM_SETTINGCHANGE) - { - RaiseThemeChangedEvent(); - } - } } } \ No newline at end of file diff --git a/src/TheXamlGuy.NotificationFlyout.Uwp.UI.Controls/NotificationFlyoutPresenter/NotificationFlyoutPresenter.cs b/src/TheXamlGuy.NotificationFlyout.Uwp.UI.Controls/NotificationFlyoutPresenter/NotificationFlyoutPresenter.cs index e4e7bc8..4f91d95 100644 --- a/src/TheXamlGuy.NotificationFlyout.Uwp.UI.Controls/NotificationFlyoutPresenter/NotificationFlyoutPresenter.cs +++ b/src/TheXamlGuy.NotificationFlyout.Uwp.UI.Controls/NotificationFlyoutPresenter/NotificationFlyoutPresenter.cs @@ -1,9 +1,25 @@ -using Windows.UI.Xaml.Controls; +using TheXamlGuy.NotificationFlyout.Shared.UI.Helpers; +using Windows.UI.Xaml; +using Windows.UI.Xaml.Controls; namespace TheXamlGuy.NotificationFlyout.Uwp.UI.Controls { public class NotificationFlyoutPresenter : ContentControl { - public NotificationFlyoutPresenter() => DefaultStyleKey = typeof(NotificationFlyoutPresenter); + private SystemPersonalisationHelper _systemPersonalisationHelper; + + public NotificationFlyoutPresenter() + { + DefaultStyleKey = typeof(NotificationFlyoutPresenter); + + _systemPersonalisationHelper = SystemPersonalisationHelper.Current; + _systemPersonalisationHelper.ThemeChanged += OnThemeChanged; + } + + protected override void OnApplyTemplate() => UpdateThemeVisualStates(false); + + private void OnThemeChanged(object sender, SystemPersonalisationChangedEventArgs args) => UpdateThemeVisualStates(true); + + private void UpdateThemeVisualStates(bool useTransition) => VisualStateManager.GoToState(this, _systemPersonalisationHelper.IsColorPrevalence ? "ColorPrevalenceTheme" : "DefaultTheme", useTransition); } } \ No newline at end of file diff --git a/src/TheXamlGuy.NotificationFlyout.Wpf.UI.Controls/NotificationFlyout/NotificationFlyoutXamlHost.cs b/src/TheXamlGuy.NotificationFlyout.Wpf.UI.Controls/NotificationFlyout/NotificationFlyoutXamlHost.cs index 0cb63a0..b54fda3 100644 --- a/src/TheXamlGuy.NotificationFlyout.Wpf.UI.Controls/NotificationFlyout/NotificationFlyoutXamlHost.cs +++ b/src/TheXamlGuy.NotificationFlyout.Wpf.UI.Controls/NotificationFlyout/NotificationFlyoutXamlHost.cs @@ -145,7 +145,7 @@ namespace TheXamlGuy.NotificationFlyout.Wpf.UI.Controls _notificationIconHelper = NotificationIconHelper.Create(); _notificationIconHelper.IconInvoked += OnIconInvoked; - _systemPersonalisationHelper = SystemPersonalisationHelper.Create(); + _systemPersonalisationHelper = SystemPersonalisationHelper.Current; _systemPersonalisationHelper.ThemeChanged += OnThemeChanged; UpdateIcons();