1
0

ViewLayoutResult.swift 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /// The result of a call to ``View/computeLayout(_:children:proposedSize:environment:backend:)``.
  2. public struct ViewLayoutResult {
  3. /// The size that the view has chosen for itself based off of the proposed view size.
  4. public var size: ViewSize
  5. /// Whether the view participates in stack layouts when empty (i.e. has its own spacing).
  6. ///
  7. /// This will be removed once we properly support dynamic alignment and spacing.
  8. public var participateInStackLayoutsWhenEmpty: Bool
  9. /// The preference values produced by the view and its children.
  10. public var preferences: PreferenceValues
  11. public init(
  12. size: ViewSize,
  13. participateInStackLayoutsWhenEmpty: Bool = false,
  14. preferences: PreferenceValues
  15. ) {
  16. self.size = size
  17. self.participateInStackLayoutsWhenEmpty = participateInStackLayoutsWhenEmpty
  18. self.preferences = preferences
  19. }
  20. /// Creates a layout result by combining a parent view's sizing and its
  21. /// children's preference values.
  22. public init(
  23. size: ViewSize,
  24. childResults: [ViewLayoutResult],
  25. participateInStackLayoutsWhenEmpty: Bool = false,
  26. preferencesOverlay: PreferenceValues? = nil
  27. ) {
  28. self.size = size
  29. self.participateInStackLayoutsWhenEmpty = participateInStackLayoutsWhenEmpty
  30. preferences = PreferenceValues(
  31. merging: childResults.map(\.preferences)
  32. + [preferencesOverlay].compactMap { $0 }
  33. )
  34. }
  35. /// Creates the layout result of a leaf view (one with no children and no
  36. /// special preference behaviour). Uses ``PreferenceValues/default``.
  37. public static func leafView(size: ViewSize) -> Self {
  38. ViewLayoutResult(
  39. size: size,
  40. participateInStackLayoutsWhenEmpty: true,
  41. preferences: .default
  42. )
  43. }
  44. /// Whether the view should participate in stack layouts (i.e. get its own spacing).
  45. public var participatesInStackLayouts: Bool {
  46. size != .zero || participateInStackLayoutsWhenEmpty
  47. }
  48. }