TupleView.swift.gyb 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // This file was generated using gyb. Do not edit it directly. Edit
  2. // TupleView.swift.gyb instead.
  3. // swiftformat:options --allow-partial-wrapping true
  4. %{
  5. maximum_view_count = 20
  6. }%
  7. @MainActor
  8. private func layoutableChild<V: View>(
  9. node: AnyViewGraphNode<V>,
  10. view: V
  11. ) -> LayoutSystem.LayoutableChild {
  12. LayoutSystem.LayoutableChild(
  13. computeLayout: { proposedSize, environment in
  14. node.computeLayout(
  15. with: view,
  16. proposedSize: proposedSize,
  17. environment: environment
  18. )
  19. },
  20. commit: {
  21. node.commit()
  22. },
  23. tag: "\(type(of: view))"
  24. )
  25. }
  26. /// Just a vessel for some default implementations to compress this generated code.
  27. @MainActor
  28. protocol TupleView: TypeSafeView {}
  29. extension TupleView {
  30. @MainActor
  31. func asWidget<Backend: BaseAppBackend>(
  32. _ children: Children,
  33. backend: Backend
  34. ) -> Backend.Widget {
  35. let group = Group(content: self)
  36. return group.asWidget(children, backend: backend)
  37. }
  38. @MainActor
  39. func computeLayout<Backend: BaseAppBackend>(
  40. _ widget: Backend.Widget,
  41. children: Children,
  42. proposedSize: ProposedViewSize,
  43. environment: EnvironmentValues,
  44. backend: Backend
  45. ) -> ViewLayoutResult {
  46. let group = Group(content: self)
  47. return group.computeLayout(
  48. widget,
  49. children: children,
  50. proposedSize: proposedSize,
  51. environment: environment,
  52. backend: backend
  53. )
  54. }
  55. @MainActor
  56. func commit<Backend: BaseAppBackend>(
  57. _ widget: Backend.Widget,
  58. children: Children,
  59. layout: ViewLayoutResult,
  60. environment: EnvironmentValues,
  61. backend: Backend
  62. ) {
  63. let group = Group(content: self)
  64. group.commit(
  65. widget,
  66. children: children,
  67. layout: layout,
  68. environment: environment,
  69. backend: backend
  70. )
  71. }
  72. }
  73. % for i in range(maximum_view_count):
  74. %{
  75. view = "TupleView%d" % (i + 1)
  76. struct_parameters = ", ".join(["View%d: View" % (j) for j in range(i + 1)])
  77. properties = "\n ".join(["public var view%d: View%d" % (j, j) for j in range(i + 1)])
  78. init_parameters = ", ".join(["_ view%d: View%d" % (j, j) for j in range(i + 1)])
  79. init_body = "\n ".join(["self.view%d = view%d" % (j, j) for j in range(i + 1)])
  80. children_type_parameters = ", ".join(["View%d" % j for j in range(i + 1)])
  81. }%
  82. /// A view with exactly ${i + 1} children. Autogenerated as an alternative to Swift's not yet
  83. /// production ready variadic generics.
  84. ///
  85. /// Has the same behaviour as ``Group`` when rendered directly.
  86. public struct ${view}<
  87. ${struct_parameters}
  88. > {
  89. ${properties}
  90. public var body = EmptyView()
  91. /// Wraps ${i + 1} child views in a single container view.
  92. public init(
  93. ${init_parameters}
  94. ) {
  95. ${init_body}
  96. }
  97. }
  98. extension ${view}: View {
  99. public typealias Content = EmptyView
  100. public var _asMenuItems: [MenuItem] {
  101. var result: [MenuItem] = []
  102. % for j in range(i + 1):
  103. result += view${j}._asMenuItems
  104. % end
  105. return result
  106. }
  107. }
  108. extension ${view}: TupleView {
  109. typealias Children = TupleViewChildren${i + 1}<${children_type_parameters}>
  110. func children<Backend: BaseAppBackend>(
  111. backend: Backend,
  112. snapshots: [ViewGraphSnapshotter.NodeSnapshot]?,
  113. environment: EnvironmentValues
  114. ) -> Children {
  115. return Children(
  116. ${" ".join("view%d," % j for j in range(i + 1))}
  117. backend: backend, snapshots: snapshots, environment: environment
  118. )
  119. }
  120. func layoutableChildren<Backend: BaseAppBackend>(
  121. backend: Backend,
  122. children: Children
  123. ) -> [LayoutSystem.LayoutableChild] {
  124. [
  125. % for j in range(i + 1):
  126. layoutableChild(node: children.child${j}, view: view${j}),
  127. % end
  128. ]
  129. }
  130. }