TupleViewChildren.swift.gyb 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // This file was generated using gyb. Do not edit it directly. Edit
  2. // TupleViewChildren.swift.gyb instead.
  3. // swiftformat:options --allow-partial-wrapping true
  4. %{
  5. maximum_child_count = 20
  6. }%
  7. protocol TupleViewChildren: ViewGraphNodeChildren {
  8. @MainActor
  9. var stackLayoutCache: StackLayoutCache { get nonmutating set }
  10. }
  11. /// A helper function to shorten node initialisations to a single line. This
  12. /// helps compress the generated code a bit and minimise the number of additions
  13. /// and deletions caused by updating the generator.
  14. @MainActor
  15. private func node<V: View, Backend: BaseAppBackend>(
  16. for view: V,
  17. _ backend: Backend,
  18. _ snapshot: ViewGraphSnapshotter.NodeSnapshot?,
  19. _ environment: EnvironmentValues
  20. ) -> AnyViewGraphNode<V> {
  21. AnyViewGraphNode(
  22. for: view,
  23. backend: backend,
  24. snapshot: snapshot,
  25. environment: environment
  26. )
  27. }
  28. % for i in range(maximum_child_count):
  29. %{
  30. children = []
  31. for j in range(i + 1):
  32. children.append("Child%d" % j)
  33. struct_type_parameters = ", ".join(["%s: View" % child for child in children])
  34. variadic_type_parameters = ", ".join(children)
  35. }%
  36. /// A fixed-length strongly-typed collection of ${i + 1} child nodes. A counterpart to
  37. /// ``TupleView${i + 1}``.
  38. public class TupleViewChildren${i + 1}<
  39. ${struct_type_parameters}
  40. >: TupleViewChildren {
  41. public var widgets: [AnyWidget] {
  42. return [${", ".join("%s.widget" % child.lower() for child in children)}]
  43. }
  44. public var erasedNodes: [ErasedViewGraphNode] {
  45. return [
  46. % for child in children:
  47. ErasedViewGraphNode(wrapping: ${child.lower()}),
  48. % end
  49. ]
  50. }
  51. var stackLayoutCache = StackLayoutCache.initial
  52. % for child in children:
  53. /// ``AnyViewGraphNode`` is used instead of ``ViewGraphNode`` because otherwise the backend leaks into views.
  54. public var ${child.lower()}: AnyViewGraphNode<${child}>
  55. % end
  56. /// Creates the nodes for ${i + 1} child views.
  57. public init<Backend: BaseAppBackend>(
  58. ${" ".join("_ %s: %s," % (child.lower(), child) for child in children)}
  59. backend: Backend,
  60. snapshots: [ViewGraphSnapshotter.NodeSnapshot]?,
  61. environment: EnvironmentValues
  62. ) {
  63. let viewTypeNames = [
  64. ${", ".join("ViewGraphSnapshotter.name(of: %s.self)" % child for child in children)}
  65. ]
  66. let snapshots = ViewGraphSnapshotter.match(snapshots ?? [], to: viewTypeNames)
  67. % for j, child in enumerate(children):
  68. self.${child.lower()} = node(for: ${child.lower()}, backend, snapshots[${j}], environment)
  69. % end
  70. }
  71. }
  72. % end