1
0

Alerts.swift 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. extension BackendFeatures {
  2. /// Backend methods for alerts.
  3. ///
  4. /// These are used by ``View/alert(_:actions:)``,
  5. /// ``View/alert(_:isPresented:actions:)``, ``EnvironmentValues/presentAlert``,
  6. /// and ``AlertScene``.
  7. @MainActor
  8. public protocol Alerts<Alert>: Core {
  9. /// The underlying alert type. Can be a wrapper or subclass.
  10. associatedtype Alert
  11. /// Creates an alert object (without showing it).
  12. ///
  13. /// Alerts contain a title, an optional body, and a set of action buttons.
  14. /// They prevent users from interacting with the parent window until
  15. /// dimissed.
  16. ///
  17. /// - Returns: An alert.
  18. func createAlert() -> Alert
  19. /// Updates the content and appearance of an alert.
  20. ///
  21. /// Can only be called once.
  22. ///
  23. /// - Parameters:
  24. /// - alert: The alert to update.
  25. /// - title: The title of the alert.
  26. /// - actionLabels: The labels of the alert's action buttons.
  27. /// - environment: The current environment.
  28. func updateAlert(
  29. _ alert: Alert,
  30. title: String,
  31. actionLabels: [String],
  32. environment: EnvironmentValues
  33. )
  34. /// Shows an alert as a modal on top of or within the given window.
  35. ///
  36. /// Users should be unable to interact with the parent window until the
  37. /// alert is dismissed.
  38. ///
  39. /// Must only be called once for any given alert.
  40. ///
  41. /// - Parameters:
  42. /// - alert: The alert to show.
  43. /// - window: The window to attach the alert to. If `nil`, the backend can
  44. /// either make the alert a whole app modal, a standalone window, or a
  45. /// modal for a window of its choosing.
  46. /// - handleResponse: The code to run when an action is selected. Receives
  47. /// the index of the chosen action (as per the `actionLabels` array).
  48. /// The alert will have already been hidden by the time this gets
  49. /// called.
  50. func showAlert(
  51. _ alert: Alert,
  52. window: Window?,
  53. responseHandler handleResponse: @escaping (Int) -> Void
  54. )
  55. /// Dismisses an alert programmatically without invoking the response
  56. /// handler.
  57. ///
  58. /// Must only be called after ``showAlert(_:window:responseHandler:)``.
  59. ///
  60. /// - Parameters:
  61. /// - alert: The alert to dismiss.
  62. /// - window: The window the alert is attached to, if any.
  63. func dismissAlert(_ alert: Alert, window: Window?)
  64. }
  65. }