using System.Windows.Controls.Primitives; using System.Windows.Markup; namespace NeumUI.Controls; /// /// Alert component for feedback. /// [ContentProperty("Message")] [TemplatePart(Name = PART_Close, Type = typeof(ButtonBase))] public class Alert : Control { #region Fields private const string PART_Close = "PART_Close"; private ButtonBase close; #endregion #region Events public static readonly RoutedEvent ClosingEvent = EventManager.RegisterRoutedEvent("Closing", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(Alert)); /// /// Occurs when closing the tag. /// public event RoutedEventHandler Closing { add => AddHandler(ClosingEvent, value); remove => RemoveHandler(ClosingEvent, value); } public static readonly RoutedEvent ClosedEvent = EventManager.RegisterRoutedEvent("Closed", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(Alert)); /// /// Occurs when a Tag is closed and is no longer visible. /// public event RoutedEventHandler Closed { add => AddHandler(ClosedEvent, value); remove => RemoveHandler(ClosedEvent, value); } #endregion #region Properties public static readonly DependencyProperty BannerProperty = DependencyProperty.Register(nameof(Banner), typeof(bool), typeof(Alert), new PropertyMetadata(false)); /// /// Gets/sets whether to show as banner. /// public bool Banner { get => (bool)GetValue(BannerProperty); set => SetValue(BannerProperty, value); } public static readonly DependencyProperty ClosableProperty = DependencyProperty.Register(nameof(Closable), typeof(bool?), typeof(Alert), new PropertyMetadata(null, OnClosableChanged)); /// /// Gets/sets whether alert can be closed. /// public bool? Closable { get => (bool?)GetValue(ClosableProperty); set => SetValue(ClosableProperty, value); } private static void OnClosableChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { (d as Alert).SetCloseButtonVisibility(); } private void SetCloseButtonVisibility() { if (close != null) { var visible = Closable.HasValue ? Closable.Value : CloseText != null; close.Visibility = visible ? Visibility.Visible : Visibility.Collapsed; } } public static readonly DependencyProperty CloseTextProperty = DependencyProperty.Register(nameof(CloseText), typeof(object), typeof(Alert), new PropertyMetadata(null, OnCloseTextChanged)); /// /// Gets/sets close text to show. /// public object CloseText { get => GetValue(CloseTextProperty); set => SetValue(CloseTextProperty, value); } private static void OnCloseTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { (d as Alert).SetCloseButton(); } private void SetCloseButton() { if (close != null) { close.Content = CloseText != null ? CloseText : new SymbolIcon() { Symbol = Assets.MaterialSymbol.Close }; } SetCloseButtonVisibility(); } public static readonly DependencyProperty DescriptionProperty = DependencyProperty.Register(nameof(Description), typeof(object), typeof(Alert), new PropertyMetadata(null)); /// /// Gets/sets additional content of alert. /// public object Description { get => GetValue(DescriptionProperty); set => SetValue(DescriptionProperty, value); } public static readonly DependencyProperty IconProperty = DependencyProperty.Register(nameof(Icon), typeof(string), typeof(Alert), new PropertyMetadata(string.Empty)); /// /// Gets/sets the icon type of the alert. /// public string Icon { get => (string)GetValue(IconProperty); set => SetValue(IconProperty, value); } public static readonly DependencyProperty MessageProperty = DependencyProperty.Register(nameof(Message), typeof(object), typeof(Alert), new PropertyMetadata(null)); /// /// Gets/sets content of alert. /// public object Message { get => GetValue(MessageProperty); set => SetValue(MessageProperty, value); } public static readonly DependencyProperty ShowIconProperty = DependencyProperty.Register(nameof(ShowIcon), typeof(bool), typeof(Alert), new PropertyMetadata(true)); /// /// Gets/sets whether to show icon. /// public bool ShowIcon { get => (bool)GetValue(ShowIconProperty); set => SetValue(ShowIconProperty, value); } public static readonly DependencyProperty TypeProperty = DependencyProperty.Register(nameof(Type), typeof(AppearanceType), typeof(Alert), new PropertyMetadata(AppearanceType.Info)); /// /// Gets/sets the type of alert. /// public AppearanceType Type { get => (AppearanceType)GetValue(TypeProperty); set => SetValue(TypeProperty, value); } public static readonly DependencyProperty IconBrushProperty = DependencyProperty.Register(nameof(IconBrush), typeof(Brush), typeof(Alert), new PropertyMetadata(null)); /// /// Gets/sets the alert icon brush. /// public Brush IconBrush { get => (Brush)GetValue(IconBrushProperty); set => SetValue(IconBrushProperty, value); } #endregion #region Constructors static Alert() { DefaultStyleKeyProperty.OverrideMetadata(typeof(Alert), new FrameworkPropertyMetadata(typeof(Alert))); } #endregion #region Overrides public override void OnApplyTemplate() { base.OnApplyTemplate(); close = GetTemplateChild(PART_Close) as ButtonBase; if (close != null) { Loaded -= OnLoaded; Loaded += OnLoaded; close.Click -= OnRaiseClosingEvent; close.Click += OnRaiseClosingEvent; } SetCloseButton(); } private void OnLoaded(object sender, RoutedEventArgs e) { Closing -= OnClosing; Closing += OnClosing; } private void OnRaiseClosingEvent(object sender, RoutedEventArgs e) { e.Handled = true; RaiseEvent(new RoutedEventArgs(ClosingEvent, this)); } private void OnClosing(object sender, RoutedEventArgs e) { SetCurrentValue(VisibilityProperty, Visibility.Collapsed); RaiseEvent(new RoutedEventArgs(ClosedEvent, this)); } #endregion }