Files

272 lines
7.9 KiB
C#
Raw Permalink Normal View History

2025-08-12 23:08:54 +08:00
using System.Windows.Controls.Primitives;
using System.Windows.Markup;
2026-01-02 17:30:41 +08:00
namespace Melskin.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";
2025-08-20 12:10:35 +08:00
private ButtonBase? close;
2025-08-12 23:08:54 +08:00
#endregion
#region Events
2025-08-20 12:10:35 +08:00
/// <summary>
/// 当警告框即将关闭时触发的事件。
/// </summary>
2025-08-12 23:08:54 +08:00
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);
}
2025-08-24 13:49:55 +08:00
/// <summary>
/// 当警告框关闭后触发的事件。
/// </summary>
2025-08-12 23:08:54 +08:00
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
2025-08-20 12:10:35 +08:00
/// <summary>
/// 表示警报控件是否显示为横幅样式。
/// </summary>
2025-08-12 23:08:54 +08:00
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);
}
2025-08-20 12:10:35 +08:00
/// <summary>
/// 表示警报是否可以关闭的依赖属性。
/// 该属性允许设置或获取一个布尔值,用于控制警报组件是否显示关闭按钮。
/// </summary>
2025-08-12 23:08:54 +08:00
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)
{
2025-08-20 12:10:35 +08:00
(d as Alert)?.SetCloseButtonVisibility();
2025-08-12 23:08:54 +08:00
}
private void SetCloseButtonVisibility()
{
2025-08-20 12:10:35 +08:00
if (close == null) return;
var visible = Closable ?? CloseText != null;
close.Visibility = visible ? Visibility.Visible : Visibility.Collapsed;
2025-08-12 23:08:54 +08:00
}
2025-08-20 12:10:35 +08:00
/// <summary>
/// 用于获取或设置关闭按钮的文本内容。
/// </summary>
2025-08-12 23:08:54 +08:00
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)
{
2025-08-20 12:10:35 +08:00
(d as Alert)?.SetCloseButton();
2025-08-12 23:08:54 +08:00
}
private void SetCloseButton()
{
if (close != null)
{
2025-08-20 12:10:35 +08:00
close.Content = CloseText != null ? CloseText : new IconElement() { Symbol = Assets.MaterialSymbol.Close };
2025-08-12 23:08:54 +08:00
}
SetCloseButtonVisibility();
}
2025-08-20 12:10:35 +08:00
/// <summary>
/// 用于获取或设置警报的描述内容。
/// </summary>
2025-08-12 23:08:54 +08:00
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);
}
2025-08-20 12:10:35 +08:00
/// <summary>
/// 用于获取或设置警报图标名称的依赖属性。此属性允许自定义警报中显示的图标,通常用来表示警报的不同类型或状态。
/// </summary>
2025-08-12 23:08:54 +08:00
public static readonly DependencyProperty IconProperty =
DependencyProperty.Register(nameof(Icon), typeof(string), typeof(Alert), new PropertyMetadata(string.Empty));
/// <summary>
2025-08-20 12:10:35 +08:00
/// 获取或设置警报的图标。
2025-08-12 23:08:54 +08:00
/// </summary>
public string Icon
{
get => (string)GetValue(IconProperty);
set => SetValue(IconProperty, value);
}
2025-08-20 12:10:35 +08:00
/// <summary>
/// 用于获取或设置警告控件中显示的消息内容。
/// </summary>
2025-08-12 23:08:54 +08:00
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);
}
2025-08-20 12:10:35 +08:00
/// <summary>
/// 用于控制是否显示图标的依赖属性。
/// </summary>
2025-08-12 23:08:54 +08:00
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);
}
2025-08-20 12:10:35 +08:00
/// <summary>
/// 获取或设置警报的外观类型。
/// </summary>
2025-08-12 23:08:54 +08:00
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);
}
2025-08-20 12:10:35 +08:00
/// <summary>
/// 用于设置或获取图标颜色的画刷属性。
/// </summary>
2025-08-12 23:08:54 +08:00
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
2025-08-20 12:10:35 +08:00
/// <inheritdoc />
2025-08-12 23:08:54 +08:00
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
}