1
0

EmptyView.swift 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /// A placeholder view used by elementary ``View`` implementations which don't
  2. /// have bodies.
  3. ///
  4. /// Triggers a fatal error if rendered.
  5. public struct EmptyView: View, Sendable {
  6. /// The nonexistent body of an ``EmptyView``.
  7. ///
  8. /// - Warning: Do not access this property directly; it will trigger a fatal
  9. /// error at runtime.
  10. public var body: Never {
  11. return fatalError("Rendered EmptyView")
  12. }
  13. /// Creates an instance of ``EmptyView``.
  14. ///
  15. /// This will crash if used in a ``View`` that doesn't override the default
  16. /// widget creation code; it's not intended for regular use.
  17. public nonisolated init() {}
  18. public func children<Backend: BaseAppBackend>(
  19. backend: Backend,
  20. snapshots: [ViewGraphSnapshotter.NodeSnapshot]?,
  21. environment: EnvironmentValues
  22. ) -> any ViewGraphNodeChildren {
  23. return EmptyViewChildren()
  24. }
  25. public func layoutableChildren<Backend: BaseAppBackend>(
  26. backend: Backend,
  27. children: ViewGraphNodeChildren
  28. ) -> [LayoutSystem.LayoutableChild] {
  29. []
  30. }
  31. public func updateChildren<Backend: BaseAppBackend>(
  32. _ children: any ViewGraphNodeChildren,
  33. backend: Backend
  34. ) {}
  35. public func asWidget<Backend: BaseAppBackend>(
  36. _ children: any ViewGraphNodeChildren,
  37. backend: Backend
  38. ) -> Backend.Widget {
  39. backend.createContainer()
  40. }
  41. public func computeLayout<Backend: BaseAppBackend>(
  42. _ widget: Backend.Widget,
  43. children: any ViewGraphNodeChildren,
  44. proposedSize: ProposedViewSize,
  45. environment: EnvironmentValues,
  46. backend: Backend
  47. ) -> ViewLayoutResult {
  48. ViewLayoutResult(size: .zero, childResults: [], participateInStackLayoutsWhenEmpty: false)
  49. }
  50. public func commit<Backend: BaseAppBackend>(
  51. _ widget: Backend.Widget,
  52. children: any ViewGraphNodeChildren,
  53. layout: ViewLayoutResult,
  54. environment: EnvironmentValues,
  55. backend: Backend
  56. ) {}
  57. public var _asMenuItems: [MenuItem] { [] }
  58. }
  59. /// The children of a node with no children.
  60. public struct EmptyViewChildren: ViewGraphNodeChildren {
  61. public let widgets: [AnyWidget] = []
  62. public let erasedNodes: [ErasedViewGraphNode] = []
  63. /// Creates an empty collection of children for a node with no children.
  64. public init() {}
  65. }
  66. /// Used as the body of ``EmptyView`` to end the chain of view bodies.
  67. extension Never: View {
  68. public var body: Never {
  69. return fatalError("Rendered Never")
  70. }
  71. public var _asMenuItems: [MenuItem] { [] }
  72. }