// Copyright (c) The Avalonia Project. All rights reserved. // Licensed under the MIT license. See licence.md file in the project root for full license information. using System; using System.Linq; using System.Reactive.Linq; using Avalonia.Interactivity; using Avalonia.LogicalTree; namespace Avalonia.Controls.Notifications { /// /// Control that represents and displays a notification. /// public class NotificationCard : ContentControl { private bool _isClosed; private bool _isClosing; /// /// Initializes a new instance of the class. /// public NotificationCard() { this.GetObservable(IsClosedProperty) .Subscribe(x => { if (!IsClosing && !IsClosed) { return; } RaiseEvent(new RoutedEventArgs(NotificationClosedEvent)); }); this.GetObservable(ContentProperty) .OfType() .Subscribe(x => { switch (x.Type) { case NotificationType.Error: PseudoClasses.Add(":error"); break; case NotificationType.Information: PseudoClasses.Add(":information"); break; case NotificationType.Success: PseudoClasses.Add(":success"); break; case NotificationType.Warning: PseudoClasses.Add(":warning"); break; } }); } /// /// Determines if the notification is already closing. /// public bool IsClosing { get { return _isClosing; } private set { SetAndRaise(IsClosingProperty, ref _isClosing, value); } } /// /// Defines the property. /// public static readonly DirectProperty IsClosingProperty = AvaloniaProperty.RegisterDirect(nameof(IsClosing), o => o.IsClosing); /// /// Determines if the notification is closed. /// public bool IsClosed { get { return _isClosed; } set { SetAndRaise(IsClosedProperty, ref _isClosed, value); } } /// /// Defines the property. /// public static readonly DirectProperty IsClosedProperty = AvaloniaProperty.RegisterDirect(nameof(IsClosed), o => o.IsClosed, (o, v) => o.IsClosed = v); /// /// Defines the event. /// public static readonly RoutedEvent NotificationClosedEvent = RoutedEvent.Register(nameof(NotificationClosed), RoutingStrategies.Bubble); /// /// Raised when the has closed. /// public event EventHandler NotificationClosed { add { AddHandler(NotificationClosedEvent, value); } remove { RemoveHandler(NotificationClosedEvent, value); } } public static bool GetCloseOnClick(Button obj) { return (bool)obj.GetValue(CloseOnClickProperty); } public static void SetCloseOnClick(Button obj, bool value) { obj.SetValue(CloseOnClickProperty, value); } /// /// Defines the CloseOnClick property. /// public static readonly AvaloniaProperty CloseOnClickProperty = AvaloniaProperty.RegisterAttached("CloseOnClick", typeof(NotificationCard), validate: CloseOnClickChanged); private static bool CloseOnClickChanged(Button button, bool value) { if (value) { button.Click += Button_Click; } else { button.Click -= Button_Click; } return true; } /// /// Called when a button inside the Notification is clicked. /// private static void Button_Click(object sender, RoutedEventArgs e) { var btn = sender as ILogical; var notification = btn.GetLogicalAncestors().OfType().FirstOrDefault(); notification?.Close(); } /// /// Closes the . /// public void Close() { if (IsClosing) { return; } IsClosing = true; } } }