Allow placement to be updated at run time

This commit is contained in:
Daniel Clark
2021-02-27 20:27:56 +00:00
parent efc2f75d85
commit 04de82cda0
2 changed files with 32 additions and 19 deletions
@@ -30,7 +30,7 @@ namespace TheXamlGuy.NotificationFlyout.Uwp.UI.Controls
public static readonly DependencyProperty PlacementProperty = public static readonly DependencyProperty PlacementProperty =
DependencyProperty.Register(nameof(Placement), DependencyProperty.Register(nameof(Placement),
typeof(NotificationFlyoutPlacement), typeof(NotificationFlyout), typeof(NotificationFlyoutPlacement), typeof(NotificationFlyout),
new PropertyMetadata(NotificationFlyoutPlacement.Auto)); new PropertyMetadata(NotificationFlyoutPlacement.Auto, OnPlacementPropertyChanged));
public static readonly DependencyProperty TemplateSettingsProperty = public static readonly DependencyProperty TemplateSettingsProperty =
DependencyProperty.Register(nameof(TemplateSettings), DependencyProperty.Register(nameof(TemplateSettings),
@@ -59,10 +59,13 @@ namespace TheXamlGuy.NotificationFlyout.Uwp.UI.Controls
public event EventHandler<object> Opened; public event EventHandler<object> Opened;
internal event EventHandler IconSourceChanged; internal event EventHandler IconSourcePropertyChanged;
internal event EventHandler InteractedWith; internal event EventHandler InteractedWith;
internal event EventHandler PlacementPropertyChanged;
public ImageSource IconSource public ImageSource IconSource
{ {
get => (ImageSource)GetValue(IconSourceProperty); get => (ImageSource)GetValue(IconSourceProperty);
@@ -121,6 +124,14 @@ namespace TheXamlGuy.NotificationFlyout.Uwp.UI.Controls
internal static void SetApplication(INotificationFlyoutApplication application) => _applicationInstance = application; 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) internal void SetPlacement(double horizontalOffset, double verticalOffset, double workingAreaHeight, double workingAreaWidth, NotificationFlyoutTaskbarPlacement flyoutTaskbarPlacement)
{ {
if (_popup == null) if (_popup == null)
@@ -213,15 +224,6 @@ namespace TheXamlGuy.NotificationFlyout.Uwp.UI.Controls
ShowMode = FlyoutShowMode.Standard ShowMode = FlyoutShowMode.Standard
}); });
} }
internal void Close(bool shouldRespectIsLightDismissEnbabled)
{
if (!shouldRespectIsLightDismissEnbabled || IsLightDismissEnabled)
{
Close();
}
}
internal void UpdateTheme(bool isColorPrevalence) => VisualStateManager.GoToState(this, isColorPrevalence ? "ColorPrevalenceTheme" : "DefaultTheme", true); internal void UpdateTheme(bool isColorPrevalence) => VisualStateManager.GoToState(this, isColorPrevalence ? "ColorPrevalenceTheme" : "DefaultTheme", true);
protected override void OnApplyTemplate() protected override void OnApplyTemplate()
@@ -259,9 +261,17 @@ namespace TheXamlGuy.NotificationFlyout.Uwp.UI.Controls
sender?.OnIconPropertyChanged(); 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 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); private void OnPointerPressed(object sender, PointerRoutedEventArgs args) => InteractedWith?.Invoke(this, EventArgs.Empty);
@@ -49,8 +49,6 @@ namespace TheXamlGuy.NotificationFlyout.Wpf.UI.Controls
set => SetValue(FlyoutProperty, value); set => SetValue(FlyoutProperty, value);
} }
public void Exit() => _notificationFlyoutXamlHost.Close();
public void CloseFlyout(bool shouldRespectIsLightDismissEnbabled) public void CloseFlyout(bool shouldRespectIsLightDismissEnbabled)
{ {
if (Flyout == null) return; if (Flyout == null) return;
@@ -59,6 +57,7 @@ namespace TheXamlGuy.NotificationFlyout.Wpf.UI.Controls
public void CloseFlyout() => CloseFlyout(false); public void CloseFlyout() => CloseFlyout(false);
public void Exit() => _notificationFlyoutXamlHost.Close();
public void OpenAsWindow<TUIElement>() where TUIElement : Windows.UI.Xaml.UIElement public void OpenAsWindow<TUIElement>() where TUIElement : Windows.UI.Xaml.UIElement
{ {
var window = new XamlHost<TUIElement>(); var window = new XamlHost<TUIElement>();
@@ -80,6 +79,12 @@ namespace TheXamlGuy.NotificationFlyout.Wpf.UI.Controls
sender?.OnFlyoutPropertyChanged(); 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 OnFlyoutPropertyChanged() => PrepareFlyout();
private void OnIconInvoked(object sender, NotificationIconInvokedEventArgs args) 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 OnNotificationFlyoutXamlHostClosed(object sender, EventArgs args) => _notificationIconHelper?.Dispose();
private void OnNotificationFlyoutXamlHostDeactivated(object sender, EventArgs args) => CloseFlyout(true); private void OnNotificationFlyoutXamlHostDeactivated(object sender, EventArgs args) => CloseFlyout(true);
@@ -114,7 +115,8 @@ namespace TheXamlGuy.NotificationFlyout.Wpf.UI.Controls
private void PrepareFlyout() private void PrepareFlyout()
{ {
if (Flyout == null) return; if (Flyout == null) return;
Flyout.IconSourceChanged += OnFlyoutIconSourceChanged; Flyout.IconSourcePropertyChanged += OnFlyoutIconSourcePropertyChanged;
Flyout.PlacementPropertyChanged += OnFlyoutPlacementPropertyChanged;
Flyout.InteractedWith += OnFlyoutInteractedWith; Flyout.InteractedWith += OnFlyoutInteractedWith;
var content = _notificationFlyoutXamlHost.GetHostContent(); var content = _notificationFlyoutXamlHost.GetHostContent();
@@ -131,6 +133,7 @@ namespace TheXamlGuy.NotificationFlyout.Wpf.UI.Controls
_notificationFlyoutXamlHost = new TransparentXamlHost<ContentControl>(); _notificationFlyoutXamlHost = new TransparentXamlHost<ContentControl>();
_notificationFlyoutXamlHost.Closed += OnNotificationFlyoutXamlHostClosed; _notificationFlyoutXamlHost.Closed += OnNotificationFlyoutXamlHostClosed;
_notificationFlyoutXamlHost.Deactivated += OnNotificationFlyoutXamlHostDeactivated; _notificationFlyoutXamlHost.Deactivated += OnNotificationFlyoutXamlHostDeactivated;
var taskbarState = _taskbarHelper.GetCurrentState(); var taskbarState = _taskbarHelper.GetCurrentState();
_notificationFlyoutXamlHost.Left = taskbarState.Screen.WorkingArea.Left; _notificationFlyoutXamlHost.Left = taskbarState.Screen.WorkingArea.Left;
_notificationFlyoutXamlHost.Top = taskbarState.Screen.WorkingArea.Top; _notificationFlyoutXamlHost.Top = taskbarState.Screen.WorkingArea.Top;