SukiToast.axaml.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using Avalonia;
  2. using Avalonia.Controls;
  3. using Avalonia.Controls.Primitives;
  4. using Avalonia.Input;
  5. using SukiUI.Models;
  6. using System;
  7. using System.Collections.ObjectModel;
  8. using System.Threading.Tasks;
  9. using System.Timers;
  10. using Avalonia.Controls.Notifications;
  11. using Avalonia.Interactivity;
  12. using Avalonia.Media;
  13. using SukiUI.ColorTheme;
  14. using SukiUI.Content;
  15. using SukiUI.Enums;
  16. using SukiUI.Toasts;
  17. namespace SukiUI.Controls;
  18. public class SukiToast : ContentControl, ISukiToast
  19. {
  20. protected override Type StyleKeyOverride => typeof(SukiToast);
  21. public ISukiToastManager? Manager { get; set; }
  22. public Action<ISukiToast>? OnDismissed { get; set; }
  23. public Action<ISukiToast>? OnClicked { get; set; }
  24. public static readonly StyledProperty<object?> IconProperty =
  25. AvaloniaProperty.Register<SukiToast, object?>(nameof(Icon));
  26. public object? Icon
  27. {
  28. get => GetValue(IconProperty);
  29. set => SetValue(IconProperty, value);
  30. }
  31. public static readonly StyledProperty<string> TitleProperty =
  32. AvaloniaProperty.Register<SukiToast, string>(nameof(Title));
  33. public string Title
  34. {
  35. get => GetValue(TitleProperty);
  36. set => SetValue(TitleProperty, value);
  37. }
  38. public static readonly StyledProperty<bool> LoadingStateProperty = AvaloniaProperty.Register<SukiToast, bool>(nameof(LoadingState));
  39. public bool LoadingState
  40. {
  41. get => GetValue(LoadingStateProperty);
  42. set => SetValue(LoadingStateProperty, value);
  43. }
  44. public static readonly StyledProperty<bool> CanDismissByClickingProperty = AvaloniaProperty.Register<SukiToast, bool>(nameof(CanDismissByClicking));
  45. public bool CanDismissByClicking
  46. {
  47. get => GetValue(CanDismissByClickingProperty);
  48. set => SetValue(CanDismissByClickingProperty, value);
  49. }
  50. public static readonly StyledProperty<ObservableCollection<object>> ActionButtonsProperty = AvaloniaProperty.Register<SukiToast,
  51. ObservableCollection<object>>(nameof(ActionButtons));
  52. public ObservableCollection<object> ActionButtons
  53. {
  54. get => GetValue(ActionButtonsProperty);
  55. set => SetValue(ActionButtonsProperty, value);
  56. }
  57. public Action<ISukiToast>? DelayDismissAction { get; set; }
  58. public SukiToast()
  59. {
  60. ActionButtons = new ObservableCollection<object>();
  61. }
  62. protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
  63. {
  64. base.OnApplyTemplate(e);
  65. e.NameScope.Get<Border>("PART_ToastCard").PointerPressed += ToastCardClickedHandler;
  66. }
  67. private void ToastCardClickedHandler(object o, PointerPressedEventArgs pointerPressedEventArgs)
  68. {
  69. OnClicked?.Invoke(this);
  70. if (!CanDismissByClicking) return;
  71. Manager.Dismiss(this);
  72. OnDismissed?.Invoke(this);
  73. }
  74. public void AnimateShow()
  75. {
  76. this.Animate(OpacityProperty, 0d, 1d, TimeSpan.FromMilliseconds(500));
  77. this.Animate(MarginProperty, new Thickness(0, 10, 0, -10), new Thickness(), TimeSpan.FromMilliseconds(500));
  78. }
  79. public void AnimateDismiss()
  80. {
  81. this.Animate(OpacityProperty, 1d, 0d, TimeSpan.FromMilliseconds(300));
  82. this.Animate(MarginProperty, new Thickness(), new Thickness(0, 50, 0, -50), TimeSpan.FromMilliseconds(300));
  83. }
  84. public ISukiToast ResetToDefault()
  85. {
  86. Title = string.Empty;
  87. Content = string.Empty;
  88. Icon = Icons.InformationOutline;
  89. Foreground = NotificationColor.InfoIconForeground;
  90. CanDismissByClicking = false;
  91. ActionButtons.Clear();
  92. OnDismissed = null;
  93. OnClicked = null;
  94. LoadingState = false;
  95. DelayDismissAction = null;
  96. DockPanel.SetDock(this, Dock.Bottom);
  97. return this;
  98. }
  99. }