using System.Windows.Controls.Primitives; using System.Windows.Markup; namespace Melskin.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) return; var visible = Closable ?? 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 IconElement() { 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)); /// /// 获取或设置警报的图标。 /// 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 }