SukiDialogBuilder.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using System;
  2. using System.Linq;
  3. using Avalonia.Controls;
  4. using Avalonia.Controls.Notifications;
  5. using SukiUI.ColorTheme;
  6. using SukiUI.Content;
  7. using SukiUI.Helpers;
  8. namespace SukiUI.Dialogs
  9. {
  10. public class SukiDialogBuilder
  11. {
  12. public ISukiDialogManager Manager { get; }
  13. public ISukiDialog Dialog { get; }
  14. public SukiDialogBuilder(ISukiDialogManager manager)
  15. {
  16. Manager = manager;
  17. Dialog = DialogPool.Get();
  18. Dialog.Manager = Manager;
  19. }
  20. public bool TryShow() => Manager.TryShowDialog(Dialog);
  21. public void SetTitle(string title) => Dialog.Title = title;
  22. public void SetShowCardBackground(bool show) => Dialog.ShowCardBackground = show;
  23. public void SetContent(object content) => Dialog.Content = content;
  24. public void SetViewModel(Func<ISukiDialog,object> viewModel)
  25. {
  26. Dialog.ViewModel = viewModel(Dialog);
  27. }
  28. public void SetType(NotificationType notificationType)
  29. {
  30. Dialog.Icon = notificationType switch
  31. {
  32. NotificationType.Information => Icons.InformationOutline,
  33. NotificationType.Success => Icons.Check,
  34. NotificationType.Warning => Icons.AlertOutline,
  35. NotificationType.Error => Icons.AlertOutline,
  36. _ => throw new ArgumentOutOfRangeException(nameof(notificationType), notificationType, null)
  37. };
  38. Dialog.IconColor = notificationType switch
  39. {
  40. NotificationType.Information => NotificationColor.InfoIconForeground,
  41. NotificationType.Success => NotificationColor.SuccessIconForeground,
  42. NotificationType.Warning => NotificationColor.WarningIconForeground,
  43. NotificationType.Error => NotificationColor.ErrorIconForeground,
  44. _ => throw new ArgumentOutOfRangeException(nameof(notificationType), notificationType, null)
  45. };
  46. }
  47. public void SetCanDismissWithBackgroundClick(bool canDismissWithBackgroundClick) =>
  48. Dialog.CanDismissWithBackgroundClick = canDismissWithBackgroundClick;
  49. public void SetOnDismissed(Action<ISukiDialog> onDismissed) => Dialog.OnDismissed = onDismissed;
  50. public void AddActionButton(object? buttonContent, Action<ISukiDialog> onClicked, bool dismissOnClick, string[] classes)
  51. {
  52. if(classes.Length == 0)
  53. classes = new[] { "Flat" };
  54. var btn = new Button { Content = buttonContent };
  55. foreach(var @class in classes)
  56. btn.Classes.Add(@class);
  57. btn.Click += (_,_) =>
  58. {
  59. onClicked(Dialog);
  60. if (!dismissOnClick) return;
  61. Manager.TryDismissDialog(Dialog);
  62. };
  63. Dialog.ActionButtons.Add(btn);
  64. }
  65. public class DismissDialog
  66. {
  67. public SukiDialogBuilder Builder { get; }
  68. public DismissDialog(SukiDialogBuilder builder)
  69. {
  70. Builder = builder;
  71. }
  72. }
  73. }
  74. }