From 04de82cda071dba99dc3d5416415f21670a16ad0 Mon Sep 17 00:00:00 2001 From: Daniel Clark Date: Sat, 27 Feb 2021 20:27:56 +0000 Subject: [PATCH] Allow placement to be updated at run time --- .../NotificationFlyout/NotificationFlyout.cs | 34 ++++++++++++------- .../NotificationFlyoutApplication.cs | 17 ++++++---- 2 files changed, 32 insertions(+), 19 deletions(-) diff --git a/src/TheXamlGuy.NotificationFlyout.Uwp.UI.Controls/NotificationFlyout/NotificationFlyout.cs b/src/TheXamlGuy.NotificationFlyout.Uwp.UI.Controls/NotificationFlyout/NotificationFlyout.cs index 13a188e..5023df9 100644 --- a/src/TheXamlGuy.NotificationFlyout.Uwp.UI.Controls/NotificationFlyout/NotificationFlyout.cs +++ b/src/TheXamlGuy.NotificationFlyout.Uwp.UI.Controls/NotificationFlyout/NotificationFlyout.cs @@ -30,7 +30,7 @@ namespace TheXamlGuy.NotificationFlyout.Uwp.UI.Controls public static readonly DependencyProperty PlacementProperty = DependencyProperty.Register(nameof(Placement), typeof(NotificationFlyoutPlacement), typeof(NotificationFlyout), - new PropertyMetadata(NotificationFlyoutPlacement.Auto)); + new PropertyMetadata(NotificationFlyoutPlacement.Auto, OnPlacementPropertyChanged)); public static readonly DependencyProperty TemplateSettingsProperty = DependencyProperty.Register(nameof(TemplateSettings), @@ -59,10 +59,13 @@ namespace TheXamlGuy.NotificationFlyout.Uwp.UI.Controls public event EventHandler Opened; - internal event EventHandler IconSourceChanged; + internal event EventHandler IconSourcePropertyChanged; internal event EventHandler InteractedWith; + internal event EventHandler PlacementPropertyChanged; + + public ImageSource IconSource { get => (ImageSource)GetValue(IconSourceProperty); @@ -121,6 +124,14 @@ namespace TheXamlGuy.NotificationFlyout.Uwp.UI.Controls internal static void SetApplication(INotificationFlyoutApplication application) => _applicationInstance = application; + internal void Close(bool shouldRespectIsLightDismissEnbabled) + { + if (!shouldRespectIsLightDismissEnbabled || IsLightDismissEnabled) + { + Close(); + } + } + internal void SetPlacement(double horizontalOffset, double verticalOffset, double workingAreaHeight, double workingAreaWidth, NotificationFlyoutTaskbarPlacement flyoutTaskbarPlacement) { if (_popup == null) @@ -213,15 +224,6 @@ namespace TheXamlGuy.NotificationFlyout.Uwp.UI.Controls ShowMode = FlyoutShowMode.Standard }); } - - internal void Close(bool shouldRespectIsLightDismissEnbabled) - { - if (!shouldRespectIsLightDismissEnbabled || IsLightDismissEnabled) - { - Close(); - } - } - internal void UpdateTheme(bool isColorPrevalence) => VisualStateManager.GoToState(this, isColorPrevalence ? "ColorPrevalenceTheme" : "DefaultTheme", true); protected override void OnApplyTemplate() @@ -259,9 +261,17 @@ namespace TheXamlGuy.NotificationFlyout.Uwp.UI.Controls sender?.OnIconPropertyChanged(); } + private static void OnPlacementPropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs args) + { + var sender = dependencyObject as NotificationFlyout; + sender?.OnPlacementPropertyChanged(); + } + private void OnGotFocus(object sender, RoutedEventArgs args) => InteractedWith?.Invoke(this, EventArgs.Empty); - private void OnIconPropertyChanged() => IconSourceChanged?.Invoke(this, EventArgs.Empty); + private void OnIconPropertyChanged() => IconSourcePropertyChanged?.Invoke(this, EventArgs.Empty); + + private void OnPlacementPropertyChanged() => PlacementPropertyChanged?.Invoke(this, EventArgs.Empty); private void OnPointerPressed(object sender, PointerRoutedEventArgs args) => InteractedWith?.Invoke(this, EventArgs.Empty); diff --git a/src/TheXamlGuy.NotificationFlyout.Wpf.UI.Controls/NotificationFlyout/NotificationFlyoutApplication.cs b/src/TheXamlGuy.NotificationFlyout.Wpf.UI.Controls/NotificationFlyout/NotificationFlyoutApplication.cs index aa533f4..c6cd1cf 100644 --- a/src/TheXamlGuy.NotificationFlyout.Wpf.UI.Controls/NotificationFlyout/NotificationFlyoutApplication.cs +++ b/src/TheXamlGuy.NotificationFlyout.Wpf.UI.Controls/NotificationFlyout/NotificationFlyoutApplication.cs @@ -49,8 +49,6 @@ namespace TheXamlGuy.NotificationFlyout.Wpf.UI.Controls set => SetValue(FlyoutProperty, value); } - public void Exit() => _notificationFlyoutXamlHost.Close(); - public void CloseFlyout(bool shouldRespectIsLightDismissEnbabled) { if (Flyout == null) return; @@ -59,6 +57,7 @@ namespace TheXamlGuy.NotificationFlyout.Wpf.UI.Controls public void CloseFlyout() => CloseFlyout(false); + public void Exit() => _notificationFlyoutXamlHost.Close(); public void OpenAsWindow() where TUIElement : Windows.UI.Xaml.UIElement { var window = new XamlHost(); @@ -80,6 +79,12 @@ namespace TheXamlGuy.NotificationFlyout.Wpf.UI.Controls sender?.OnFlyoutPropertyChanged(); } + private void OnFlyoutIconSourcePropertyChanged(object sender, EventArgs args) => UpdateIcons(); + + private void OnFlyoutInteractedWith(object sender, EventArgs args) => _notificationFlyoutXamlHost.Activate(); + + private void OnFlyoutPlacementPropertyChanged(object sender, EventArgs args) => UpdateFlyoutPlacement(); + private void OnFlyoutPropertyChanged() => PrepareFlyout(); private void OnIconInvoked(object sender, NotificationIconInvokedEventArgs args) @@ -95,10 +100,6 @@ namespace TheXamlGuy.NotificationFlyout.Wpf.UI.Controls } } - private void OnFlyoutIconSourceChanged(object sender, EventArgs args) => UpdateIcons(); - - private void OnFlyoutInteractedWith(object sender, EventArgs args) => _notificationFlyoutXamlHost.Activate(); - private void OnNotificationFlyoutXamlHostClosed(object sender, EventArgs args) => _notificationIconHelper?.Dispose(); private void OnNotificationFlyoutXamlHostDeactivated(object sender, EventArgs args) => CloseFlyout(true); @@ -114,7 +115,8 @@ namespace TheXamlGuy.NotificationFlyout.Wpf.UI.Controls private void PrepareFlyout() { if (Flyout == null) return; - Flyout.IconSourceChanged += OnFlyoutIconSourceChanged; + Flyout.IconSourcePropertyChanged += OnFlyoutIconSourcePropertyChanged; + Flyout.PlacementPropertyChanged += OnFlyoutPlacementPropertyChanged; Flyout.InteractedWith += OnFlyoutInteractedWith; var content = _notificationFlyoutXamlHost.GetHostContent(); @@ -131,6 +133,7 @@ namespace TheXamlGuy.NotificationFlyout.Wpf.UI.Controls _notificationFlyoutXamlHost = new TransparentXamlHost(); _notificationFlyoutXamlHost.Closed += OnNotificationFlyoutXamlHostClosed; _notificationFlyoutXamlHost.Deactivated += OnNotificationFlyoutXamlHostDeactivated; + var taskbarState = _taskbarHelper.GetCurrentState(); _notificationFlyoutXamlHost.Left = taskbarState.Screen.WorkingArea.Left; _notificationFlyoutXamlHost.Top = taskbarState.Screen.WorkingArea.Top;