Sheets.swift 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. extension BackendFeatures {
  2. /// Backend methods for sheets.
  3. ///
  4. /// These are used by ``View/sheet(isPresented:onDismiss:content:)``.
  5. @MainActor
  6. public protocol Sheets<Sheet>: Core {
  7. /// The underlying sheet type. Can be a wrapper or subclass.
  8. associatedtype Sheet
  9. /// Creates a sheet object (without showing it).
  10. ///
  11. /// Sheets contain view content. They prevent users from interacting with
  12. /// the parent window until dimissed and can optionally execute a callback
  13. /// on dismiss.
  14. ///
  15. /// - Parameter content: The content of the sheet.
  16. /// - Returns: A sheet containing `content`.
  17. func createSheet(content: Widget) -> Sheet
  18. /// Updates the content, appearance and behaviour of a sheet.
  19. ///
  20. /// - Parameters:
  21. /// - sheet: The sheet to update.
  22. /// - window: The root window that the sheet will be presented in. Used on
  23. /// platforms such as tvOS to compute layout constraints.
  24. ///
  25. /// The sheet shouldn't be attached to the window by `updateSheet`. That
  26. /// is handled by ``presentSheet(_:window:parentSheet:)`` which is
  27. /// guaranteed to be called exactly once (unlike `updateSheet` which
  28. /// gets called whenever preferences or sizing change).
  29. /// - environment: The environment that the sheet will be presented in.
  30. /// This differs from the environment passed to the sheet's content.
  31. /// - size: The size of the sheet.
  32. /// - onDismiss: An action to perform when the sheet gets dismissed by
  33. /// the user. Not triggered by programmatic dismissals, but _is_
  34. /// triggered by the implicit dismissals of nested sheets when their
  35. /// parent sheet is programmatically dismissed.
  36. /// - cornerRadius: The radius of the sheet. If `nil`, the platform
  37. /// default should be used. Not all backends can support this (e.g.
  38. /// macOS doesn't support custom window corner radii).
  39. /// - detents: An array of sizes that the sheet should snap to. This is
  40. /// generally only a thing on mobile where sheets can be dragged up
  41. /// and down.
  42. /// - dragIndicatorVisibility: Whether the drag indicator should be shown.
  43. /// Sheet drag indicators are generally only a thing on mobile, and
  44. /// usually appear as a small horizontal bar at the top of the sheet.
  45. /// - backgroundColor: The background color to use for the sheet. If
  46. /// `nil`, the platform's default sheet background style should be used.
  47. /// - interactiveDismissDisabled: Whether to disable user-driven sheet
  48. /// dismissal. On mobile this disables swiping to dismiss a sheet, and
  49. /// on desktop this usually disables dismissal shortcuts such as the
  50. /// escape key and/or removes system-provided close/cancel buttons from
  51. /// the sheet.
  52. func updateSheet(
  53. _ sheet: Sheet,
  54. window: Window,
  55. environment: EnvironmentValues,
  56. size: SIMD2<Int>,
  57. onDismiss: @escaping () -> Void,
  58. cornerRadius: Double?,
  59. detents: [PresentationDetent],
  60. dragIndicatorVisibility: Visibility,
  61. backgroundColor: Color.Resolved?,
  62. interactiveDismissDisabled: Bool
  63. )
  64. /// Presents a sheet as a modal on top of or within the given window.
  65. ///
  66. /// Sheets should disable interaction with all content below them until they
  67. /// get dismissed.
  68. ///
  69. /// `onDismiss` only gets called once the sheet has been closed.
  70. ///
  71. /// This method must only be called once for any given sheet.
  72. ///
  73. /// - Parameters:
  74. /// - sheet: The sheet to present.
  75. /// - window: The window to present the sheet on top of.
  76. /// - parentSheet: The sheet that the current sheet was presented from,
  77. /// if any.
  78. func presentSheet(
  79. _ sheet: Sheet,
  80. window: Window,
  81. parentSheet: Sheet?
  82. )
  83. /// Dismisses a sheet programmatically.
  84. ///
  85. /// Used by the ``View/sheet(isPresented:onDismiss:content:)`` modifier to
  86. /// close sheets.
  87. ///
  88. /// - Parameters:
  89. /// - sheet: The sheet to dismiss.
  90. /// - window: The window that the sheet was presented in.
  91. /// - parentSheet: The sheet that presented the current sheet, if any.
  92. func dismissSheet(_ sheet: Sheet, window: Window, parentSheet: Sheet?)
  93. /// Get the size of a sheet.
  94. ///
  95. /// - Parameter sheet: The sheet to get the size of.
  96. /// - Returns: The sheet's size.
  97. func size(ofSheet sheet: Sheet) -> SIMD2<Int>
  98. }
  99. }