SukiToastBuilder.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using System;
  2. using System.Threading.Tasks;
  3. using Avalonia;
  4. using Avalonia.Controls;
  5. using Avalonia.Controls.Notifications;
  6. using SukiUI.ColorTheme;
  7. using SukiUI.Content;
  8. using SukiUI.Helpers;
  9. namespace SukiUI.Toasts
  10. {
  11. public class SukiToastBuilder
  12. {
  13. public ISukiToastManager Manager { get; }
  14. public ISukiToast Toast { get; }
  15. public SukiToastBuilder(ISukiToastManager manager)
  16. {
  17. Manager = manager;
  18. Toast = ToastPool.Get();
  19. Toast.Manager = Manager;
  20. }
  21. public ISukiToast Queue()
  22. {
  23. Manager.Queue(Toast);
  24. return Toast;
  25. }
  26. public void SetTitle(string title) => Toast.Title = title;
  27. public void SetContent(object? content) => Toast.Content = content;
  28. public void SetCanDismissByClicking(bool canDismiss) => Toast.CanDismissByClicking = canDismiss;
  29. public void SetLoadingState(bool loading) => Toast.LoadingState = loading;
  30. public void SetType(NotificationType type)
  31. {
  32. Toast.Icon = type switch
  33. {
  34. NotificationType.Information => Icons.InformationOutline,
  35. NotificationType.Success => Icons.Check,
  36. NotificationType.Warning => Icons.AlertOutline,
  37. NotificationType.Error => Icons.AlertOutline,
  38. _ => throw new ArgumentOutOfRangeException(nameof(type), type, null)
  39. };
  40. Toast.Foreground = type switch
  41. {
  42. NotificationType.Information => NotificationColor.InfoIconForeground,
  43. NotificationType.Success => NotificationColor.SuccessIconForeground,
  44. NotificationType.Warning => NotificationColor.WarningIconForeground,
  45. NotificationType.Error => NotificationColor.ErrorIconForeground,
  46. _ => throw new ArgumentOutOfRangeException(nameof(type), type, null)
  47. };
  48. }
  49. public void Delay(TimeSpan delay, Action<ISukiToast> action)
  50. {
  51. Toast.DelayDismissAction = action;
  52. Task.Delay(delay).ContinueWith(_ =>
  53. {
  54. if (Toast.DelayDismissAction != action) return;
  55. Toast.DelayDismissAction.Invoke(Toast);
  56. },
  57. TaskScheduler.FromCurrentSynchronizationContext());
  58. }
  59. public void SetOnDismiss(Action<ISukiToast> action) => Toast.OnDismissed = action;
  60. public void SetOnClicked(Action<ISukiToast> action) => Toast.OnClicked = action;
  61. public void AddActionButton(object buttonContent, Action<ISukiToast> action, bool dismissOnClick, bool flatstyle = true)
  62. {
  63. Button btn = new Button()
  64. {
  65. Content = buttonContent,
  66. Classes = { flatstyle ?"Flat" : "Basic" },
  67. Margin = flatstyle ? new Thickness(14, 9, 0, 12) : new Thickness(14, -3, 0, 2)
  68. };
  69. btn.Click += (_, _) =>
  70. {
  71. action(Toast);
  72. if(dismissOnClick)
  73. Manager.Dismiss(Toast);
  74. };
  75. Toast.ActionButtons.Add(btn);
  76. }
  77. public class DismissToast
  78. {
  79. public SukiToastBuilder Builder { get; }
  80. public DismissToast(SukiToastBuilder builder)
  81. {
  82. Builder = builder;
  83. }
  84. }
  85. }
  86. }