1
0

Group.swift 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /// A view that groups views together without affecting their layout (allowing
  2. /// modifiers to be applied to a whole group of views at once).
  3. public struct Group<Content: View>: View {
  4. public var body: Content
  5. /// Creates a group.
  6. ///
  7. /// - Parameter content: The content of the group.
  8. public init(@ViewBuilder content: () -> Content) {
  9. self.init(content: content())
  10. }
  11. init(content: Content) {
  12. body = content
  13. }
  14. public func asWidget<Backend: BaseAppBackend>(
  15. _ children: any ViewGraphNodeChildren,
  16. backend: Backend
  17. ) -> Backend.Widget {
  18. let container = backend.createContainer()
  19. for (index, child) in children.widgets(for: backend).enumerated() {
  20. backend.insert(child, into: container, at: index)
  21. }
  22. return container
  23. }
  24. public func computeLayout<Backend: BaseAppBackend>(
  25. _ widget: Backend.Widget,
  26. children: any ViewGraphNodeChildren,
  27. proposedSize: ProposedViewSize,
  28. environment: EnvironmentValues,
  29. backend: Backend
  30. ) -> ViewLayoutResult {
  31. if !(children is TupleViewChildren || children is EmptyViewChildren) {
  32. logger.warning(
  33. "Group will not function correctly with non-TupleView content",
  34. metadata: ["childrenType": "\(type(of: children))"]
  35. )
  36. }
  37. var cache = (children as? TupleViewChildren)?.stackLayoutCache ?? StackLayoutCache.initial
  38. let result = LayoutSystem.computeStackLayout(
  39. container: widget,
  40. children: layoutableChildren(backend: backend, children: children),
  41. cache: &cache,
  42. proposedSize: proposedSize,
  43. environment: environment,
  44. backend: backend,
  45. inheritStackLayoutParticipation: true
  46. )
  47. (children as? TupleViewChildren)?.stackLayoutCache = cache
  48. return result
  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. var cache = (children as? TupleViewChildren)?.stackLayoutCache ?? StackLayoutCache.initial
  58. LayoutSystem.commitStackLayout(
  59. container: widget,
  60. children: layoutableChildren(backend: backend, children: children),
  61. cache: &cache,
  62. layout: layout,
  63. environment: environment,
  64. backend: backend
  65. )
  66. (children as? TupleViewChildren)?.stackLayoutCache = cache
  67. }
  68. }