InfoBar.axaml.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using Avalonia;
  2. using Avalonia.Controls;
  3. using Avalonia.Controls.Notifications;
  4. using Avalonia.Controls.Primitives;
  5. using Avalonia.Media;
  6. using SukiUI.ColorTheme;
  7. using SukiUI.Content;
  8. namespace SukiUI.Controls;
  9. public class InfoBar : ContentControl
  10. {
  11. public static readonly StyledProperty<NotificationType> SeverityProperty =
  12. AvaloniaProperty.Register<InfoBar, NotificationType>(nameof(Severity), NotificationType.Information);
  13. public NotificationType Severity
  14. {
  15. get => GetValue(SeverityProperty);
  16. set
  17. {
  18. Icon = value switch
  19. {
  20. NotificationType.Information => Icons.InformationOutline,
  21. NotificationType.Success => Icons.Check,
  22. NotificationType.Warning => Icons.AlertOutline,
  23. NotificationType.Error => Icons.AlertOutline,
  24. _ => Icons.InformationOutline
  25. };
  26. IconForeground = value switch
  27. {
  28. NotificationType.Information => NotificationColor.InfoIconForeground,
  29. NotificationType.Success => NotificationColor.SuccessIconForeground,
  30. NotificationType.Warning => NotificationColor.WarningIconForeground,
  31. NotificationType.Error => NotificationColor.ErrorIconForeground,
  32. _ => NotificationColor.InfoIconForeground
  33. };
  34. SetValue(SeverityProperty, value);
  35. }
  36. }
  37. public static readonly StyledProperty<object?> IconProperty =
  38. AvaloniaProperty.Register<InfoBar, object?>(nameof(Icon), Icons.InformationOutline);
  39. public object? Icon
  40. {
  41. get => GetValue(IconProperty);
  42. private set => SetValue(IconProperty, value);
  43. }
  44. public static readonly StyledProperty<IBrush?> IconForegroundProperty =
  45. AvaloniaProperty.Register<InfoBar, IBrush?>(nameof(IconForeground), NotificationColor.InfoIconForeground);
  46. public IBrush? IconForeground
  47. {
  48. get => GetValue(IconForegroundProperty);
  49. private set => SetValue(IconForegroundProperty, value);
  50. }
  51. public static readonly StyledProperty<bool> IsOpenProperty =
  52. AvaloniaProperty.Register<InfoBar, bool>(nameof(IsOpen), true);
  53. public bool IsOpen
  54. {
  55. get => GetValue(IsOpenProperty);
  56. set => SetValue(IsOpenProperty, value);
  57. }
  58. public static readonly StyledProperty<bool> IsClosableProperty =
  59. AvaloniaProperty.Register<InfoBar, bool>(nameof(IsClosable), true);
  60. public bool IsClosable
  61. {
  62. get => GetValue(IsClosableProperty);
  63. set => SetValue(IsClosableProperty, value);
  64. }
  65. public static readonly StyledProperty<bool> IsOpaqueProperty =
  66. AvaloniaProperty.Register<InfoBar, bool>(nameof(IsOpaque), false);
  67. public bool IsOpaque
  68. {
  69. get => GetValue(IsOpaqueProperty);
  70. set => SetValue(IsOpaqueProperty, value);
  71. }
  72. public static readonly StyledProperty<string> TitleProperty =
  73. AvaloniaProperty.Register<InfoBar, string>(nameof(Title), string.Empty);
  74. public string Title
  75. {
  76. get => GetValue(TitleProperty);
  77. set => SetValue(TitleProperty, value);
  78. }
  79. public static readonly StyledProperty<string> MessageProperty =
  80. AvaloniaProperty.Register<InfoBar, string>(nameof(Message), string.Empty);
  81. public string Message
  82. {
  83. get => GetValue(MessageProperty);
  84. set => SetValue(MessageProperty, value);
  85. }
  86. protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
  87. {
  88. base.OnApplyTemplate(e);
  89. e.NameScope.Get<Button>("PART_CloseButton").Click += (_, _) => { IsOpen = false;};
  90. }
  91. protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
  92. {
  93. base.OnAttachedToVisualTree(e);
  94. if (ContextMenu is null) return;
  95. }
  96. }