FluentSukiToastBuilder.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using System;
  2. using Avalonia.Controls.Notifications;
  3. namespace SukiUI.Toasts
  4. {
  5. public static class FluentSukiToastBuilder
  6. {
  7. #region Content
  8. /// <summary>
  9. /// Creates a <see cref="SukiToastBuilder"/> instance that can be used to construct a <see cref="ISukiToast"/>
  10. /// </summary>
  11. public static SukiToastBuilder CreateToast(this ISukiToastManager manager) => new(manager);
  12. /// <summary>
  13. /// Creates a simple informational toast that can be dismissed by clicking and is otherwise dismissed after 3 seconds.
  14. /// This doesn't set the title/content and you should use <see cref="WithTitle"/> and <see cref="WithContent"/>
  15. /// </summary>
  16. public static SukiToastBuilder CreateSimpleInfoToast(this ISukiToastManager manager)
  17. {
  18. return new SukiToastBuilder(manager)
  19. .OfType(NotificationType.Information)
  20. .Dismiss().After(TimeSpan.FromSeconds(3))
  21. .Dismiss().ByClicking();
  22. }
  23. /// <summary>
  24. /// Gives the toast a title.
  25. /// </summary>
  26. public static SukiToastBuilder WithTitle(this SukiToastBuilder builder, string title)
  27. {
  28. builder.SetTitle(title);
  29. return builder;
  30. }
  31. /// <summary>
  32. /// Show a loading Toast.
  33. /// </summary>
  34. public static SukiToastBuilder WithLoadingState(this SukiToastBuilder builder, bool state)
  35. {
  36. builder.SetLoadingState(state);
  37. return builder;
  38. }
  39. /// <summary>
  40. /// Gives the toast some content. This can be a ViewModel if desired - View will be located via the default location strategy.
  41. /// </summary>
  42. public static SukiToastBuilder WithContent(this SukiToastBuilder builder, object? content)
  43. {
  44. builder.SetContent(content);
  45. return builder;
  46. }
  47. /// <summary>
  48. /// Sets the notification type - By default it is <see cref="NotificationType.Information"/>
  49. /// </summary>
  50. public static SukiToastBuilder OfType(this SukiToastBuilder builder, NotificationType type)
  51. {
  52. builder.SetType(type);
  53. return builder;
  54. }
  55. #endregion
  56. #region Dismissing
  57. /// <summary>
  58. /// Begins a dismiss statement for the toast - Follow this with something like <see cref="After"/>.
  59. /// </summary>
  60. public static SukiToastBuilder.DismissToast Dismiss(this SukiToastBuilder builder) => new(builder);
  61. /// <summary>
  62. /// Automatically dismisses the toast after the given amount of time.
  63. /// </summary>
  64. public static SukiToastBuilder After(this SukiToastBuilder.DismissToast dismiss, TimeSpan after)
  65. {
  66. dismiss.Builder.Delay(after, toast => dismiss.Builder.Manager.Dismiss(toast));
  67. return dismiss.Builder;
  68. }
  69. /// <summary>
  70. /// Allows the toast to be dismissed by clicking anywhere on the toast.
  71. /// </summary>
  72. public static SukiToastBuilder ByClicking(this SukiToastBuilder.DismissToast dismiss)
  73. {
  74. dismiss.Builder.SetCanDismissByClicking(true);
  75. return dismiss.Builder;
  76. }
  77. #endregion
  78. #region Actions
  79. /// <summary>
  80. /// The action provided will be called if the body of the toast is clicked.
  81. /// </summary>
  82. public static SukiToastBuilder OnClicked(this SukiToastBuilder builder, Action<ISukiToast> action)
  83. {
  84. builder.SetOnClicked(action);
  85. return builder;
  86. }
  87. /// <summary>
  88. /// The action provided will be called when the toast is dismissed for any reason, including clicking.
  89. /// </summary>
  90. public static SukiToastBuilder OnDismissed(this SukiToastBuilder builder, Action<ISukiToast> onDismissAction)
  91. {
  92. builder.SetOnDismiss(onDismissAction);
  93. return builder;
  94. }
  95. /// <summary>
  96. /// Adds an action button to the toast which will call the provided callback on click. Any number of buttons can be added to a toast.
  97. /// </summary>
  98. public static SukiToastBuilder WithActionButton(this SukiToastBuilder builder, object buttonContent, Action<ISukiToast> onClicked, bool dismissOnClick = false)
  99. {
  100. builder.AddActionButton(buttonContent, onClicked, dismissOnClick);
  101. return builder;
  102. }
  103. public static SukiToastBuilder WithActionButtonNormal(this SukiToastBuilder builder, object buttonContent, Action<ISukiToast> onClicked, bool dismissOnClick = false)
  104. {
  105. builder.AddActionButton(buttonContent, onClicked, dismissOnClick, false);
  106. return builder;
  107. }
  108. #endregion
  109. }
  110. }