diff --git a/src/Avalonia.Controls/Notifications/INotificationManager.cs b/src/Avalonia.Controls/Notifications/INotificationManager.cs new file mode 100644 index 0000000000..e0b73d6293 --- /dev/null +++ b/src/Avalonia.Controls/Notifications/INotificationManager.cs @@ -0,0 +1,9 @@ +using System; + +namespace Avalonia.Controls.Notifications +{ + interface INotificationManager + { + void Show(object content, string areaName = "", TimeSpan? expirationTime = null, Action onClick = null, Action onClose = null); + } +} diff --git a/src/Avalonia.Controls/Notifications/Notification.cs b/src/Avalonia.Controls/Notifications/Notification.cs new file mode 100644 index 0000000000..f50458163b --- /dev/null +++ b/src/Avalonia.Controls/Notifications/Notification.cs @@ -0,0 +1,101 @@ +using System; +using System.Threading.Tasks; +using Avalonia.Controls.Primitives; +using Avalonia.Interactivity; + +namespace Avalonia.Controls.Notifications +{ + public class Notification : ContentControl + { + private TimeSpan _closingAnimationTime = TimeSpan.Zero; + + public bool IsClosing { get; set; } + + public static readonly RoutedEvent NotificationCloseInvokedEvent = EventManager.RegisterRoutedEvent( + "NotificationCloseInvoked", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(Notification)); + + public static readonly RoutedEvent NotificationClosedEvent = EventManager.RegisterRoutedEvent( + "NotificationClosed", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(Notification)); + + public event RoutedEventHandler NotificationCloseInvoked + { + add { AddHandler(NotificationCloseInvokedEvent, value); } + remove { RemoveHandler(NotificationCloseInvokedEvent, value); } + } + + public event RoutedEventHandler NotificationClosed + { + add { AddHandler(NotificationClosedEvent, value); } + remove { RemoveHandler(NotificationClosedEvent, value); } + } + + public static bool GetCloseOnClick(DependencyObject obj) + { + return (bool)obj.GetValue(CloseOnClickProperty); + } + + public static void SetCloseOnClick(DependencyObject obj, bool value) + { + obj.SetValue(CloseOnClickProperty, value); + } + + public static readonly DependencyProperty CloseOnClickProperty = + DependencyProperty.RegisterAttached("CloseOnClick", typeof(bool), typeof(Notification), new FrameworkPropertyMetadata(false, CloseOnClickChanged)); + + private static void CloseOnClickChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs) + { + var button = dependencyObject as Button; + if (button == null) + { + return; + } + + var value = (bool)dependencyPropertyChangedEventArgs.NewValue; + + if (value) + { + button.Click += (sender, args) => + { + var notification = VisualTreeHelperExtensions.GetParent(button); + notification?.Close(); + }; + } + } + + protected override void OnTemplateApplied(TemplateAppliedEventArgs e) + { + base.OnTemplateApplied(e); + var closeButton = this.FindControl