Files
Shrlalgo.RvKits/NeuWPF/NeoUI/Controls/Alert.xaml.cs

239 lines
6.7 KiB
C#
Raw Normal View History

2025-08-12 23:08:54 +08:00
using System.Windows.Controls.Primitives;
using System.Windows.Markup;
2025-08-20 12:10:13 +08:00
namespace NeumUI.Controls;
2025-08-12 23:08:54 +08:00
/// <summary>
/// Alert component for feedback.
/// </summary>
[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));
/// <summary>
/// Occurs when closing the tag.
/// </summary>
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));
/// <summary>
/// Occurs when a Tag is closed and is no longer visible.
/// </summary>
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));
/// <summary>
/// Gets/sets whether to show as banner.
/// </summary>
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));
/// <summary>
/// Gets/sets whether alert can be closed.
/// </summary>
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));
/// <summary>
/// Gets/sets close text to show.
/// </summary>
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));
/// <summary>
/// Gets/sets additional content of alert.
/// </summary>
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));
/// <summary>
/// Gets/sets the icon type of the alert.
/// </summary>
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));
/// <summary>
/// Gets/sets content of alert.
/// </summary>
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));
/// <summary>
/// Gets/sets whether to show icon.
/// </summary>
public bool ShowIcon
{
get => (bool)GetValue(ShowIconProperty);
set => SetValue(ShowIconProperty, value);
}
public static readonly DependencyProperty TypeProperty =
2025-08-20 12:10:13 +08:00
DependencyProperty.Register(nameof(Type), typeof(AppearanceType), typeof(Alert), new PropertyMetadata(AppearanceType.Info));
2025-08-12 23:08:54 +08:00
/// <summary>
/// Gets/sets the type of alert.
/// </summary>
2025-08-20 12:10:13 +08:00
public AppearanceType Type
2025-08-12 23:08:54 +08:00
{
2025-08-20 12:10:13 +08:00
get => (AppearanceType)GetValue(TypeProperty);
2025-08-12 23:08:54 +08:00
set => SetValue(TypeProperty, value);
}
public static readonly DependencyProperty IconBrushProperty =
DependencyProperty.Register(nameof(IconBrush), typeof(Brush), typeof(Alert), new PropertyMetadata(null));
/// <summary>
/// Gets/sets the alert icon brush.
/// </summary>
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
}