This commit is contained in:
rabbitism
2024-06-03 15:43:26 +08:00
parent 68c55dd331
commit 899d818ba7
3 changed files with 6 additions and 11 deletions

View File

@@ -0,0 +1,70 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Metadata;
using Avalonia.Controls.Notifications;
using Avalonia.Controls.Primitives;
using Avalonia.Controls.Templates;
using Avalonia.Interactivity;
using Avalonia.Metadata;
using Irihi.Avalonia.Shared.Helpers;
namespace Ursa.Controls;
[PseudoClasses(PC_Icon)]
[TemplatePart(PART_CloseButton, typeof(Button))]
public class Banner: HeaderedContentControl
{
public const string PC_Icon = ":icon";
public const string PART_CloseButton = "PART_CloseButton";
private Button? _closeButton;
public static readonly StyledProperty<bool> CanCloseProperty = AvaloniaProperty.Register<Banner, bool>(
nameof(CanClose));
public bool CanClose
{
get => GetValue(CanCloseProperty);
set => SetValue(CanCloseProperty, value);
}
public static readonly StyledProperty<bool> ShowIconProperty = AvaloniaProperty.Register<Banner, bool>(
nameof(ShowIcon), true);
public bool ShowIcon
{
get => GetValue(ShowIconProperty);
set => SetValue(ShowIconProperty, value);
}
public static readonly StyledProperty<object?> IconProperty = AvaloniaProperty.Register<Banner, object?>(
nameof(Icon));
public object? Icon
{
get => GetValue(IconProperty);
set => SetValue(IconProperty, value);
}
public static readonly StyledProperty<NotificationType> TypeProperty = AvaloniaProperty.Register<Banner, NotificationType>(
nameof(Type));
public NotificationType Type
{
get => GetValue(TypeProperty);
set => SetValue(TypeProperty, value);
}
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
{
base.OnApplyTemplate(e);
Button.ClickEvent.RemoveHandler(OnCloseClick, _closeButton);
_closeButton = e.NameScope.Find<Button>(PART_CloseButton);
Button.ClickEvent.AddHandler(OnCloseClick, _closeButton);
}
private void OnCloseClick(object sender, RoutedEventArgs args)
{
IsVisible = false;
}
}