InfoBadge.axaml.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using Avalonia;
  2. using Avalonia.Controls;
  3. using Avalonia.Controls.Notifications;
  4. using Avalonia.Controls.Primitives;
  5. using Avalonia.Interactivity;
  6. using Avalonia.Media;
  7. using SukiUI.ColorTheme;
  8. using SukiUI.Enums;
  9. namespace SukiUI.Controls;
  10. public class InfoBadge: HeaderedContentControl
  11. {
  12. private Border? _badgeContainer;
  13. public static readonly StyledProperty<NotificationType> AppearanceProperty =
  14. AvaloniaProperty.Register<InfoBadge, NotificationType>(nameof(Appearance), NotificationType.Information);
  15. public NotificationType Appearance
  16. {
  17. get => GetValue(AppearanceProperty);
  18. set
  19. {
  20. Background = value switch
  21. {
  22. NotificationType.Information => NotificationColor.InfoIconForeground,
  23. NotificationType.Success => NotificationColor.SuccessIconForeground,
  24. NotificationType.Warning => NotificationColor.WarningIconForeground,
  25. NotificationType.Error => NotificationColor.ErrorIconForeground,
  26. _ => NotificationColor.InfoIconForeground
  27. };
  28. SetValue(AppearanceProperty, value);
  29. }
  30. }
  31. public static readonly StyledProperty<CornerPosition> CornerPositionProperty = AvaloniaProperty.Register<InfoBadge, CornerPosition>(
  32. nameof(CornerPosition));
  33. public CornerPosition CornerPosition
  34. {
  35. get => GetValue(CornerPositionProperty);
  36. set => SetValue(CornerPositionProperty, value);
  37. }
  38. public static readonly StyledProperty<bool> IsDotProperty = AvaloniaProperty.Register<InfoBadge, bool>(
  39. nameof(IsDot), false);
  40. public bool IsDot
  41. {
  42. get => GetValue(IsDotProperty);
  43. set {
  44. UpdateBadgePosition();
  45. SetValue(IsDotProperty, value);
  46. }
  47. }
  48. public static readonly StyledProperty<int> OverflowProperty = AvaloniaProperty.Register<InfoBadge, int>(
  49. nameof(Overflow));
  50. public int Overflow
  51. {
  52. get => GetValue(OverflowProperty);
  53. set => SetValue(OverflowProperty, value);
  54. }
  55. static InfoBadge()
  56. {
  57. HeaderProperty.Changed.AddClassHandler<InfoBadge>((badge, _) => badge.UpdateBadgePosition());
  58. }
  59. protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
  60. {
  61. base.OnApplyTemplate(e);
  62. _badgeContainer = e.NameScope.Find<Border>("BadgeBorder");
  63. }
  64. protected override void OnLoaded(RoutedEventArgs e)
  65. {
  66. base.OnLoaded(e);
  67. UpdateBadgePosition();
  68. }
  69. protected override Size ArrangeOverride(Size finalSize)
  70. {
  71. UpdateBadgePosition();
  72. return base.ArrangeOverride(finalSize);
  73. }
  74. private void UpdateBadgePosition()
  75. {
  76. var verticalOffset = -1;
  77. if (CornerPosition is CornerPosition.BottomLeft or CornerPosition.BottomRight)
  78. {
  79. verticalOffset = 1;
  80. }
  81. var horizontalOffset = -1;
  82. if (CornerPosition is CornerPosition.TopRight or CornerPosition.BottomRight)
  83. {
  84. horizontalOffset = 1;
  85. }
  86. if (_badgeContainer is not null && Presenter?.Child is not null)
  87. {
  88. _badgeContainer.RenderTransform = new TransformGroup
  89. {
  90. Children = new Transforms
  91. {
  92. new TranslateTransform(horizontalOffset*_badgeContainer.Bounds.Width / 2,verticalOffset*_badgeContainer.Bounds.Height / 2)
  93. }
  94. };
  95. }
  96. }
  97. }