1
0

Spacer.swift 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /// A flexible space that expands along the major axis of its containing
  2. /// stack layout, or on both axes if not contained in a stack.
  3. public struct Spacer: ElementaryView, View {
  4. /// The ideal length of a spacer.
  5. static let idealLength: Double = 8
  6. /// The minimum length this spacer can be shrunk to, along the axis of
  7. /// expansion.
  8. @_spi(Backends) public var minLength: Int?
  9. /// Creates a spacer with a given minimum length along its axis or axes
  10. /// of expansion.
  11. ///
  12. /// - Parameter minLength: The spacer's minimum length.
  13. public init(minLength: Int? = nil) {
  14. self.minLength = minLength
  15. }
  16. func asWidget<Backend: BaseAppBackend>(backend: Backend) -> Backend.Widget {
  17. return backend.createContainer()
  18. }
  19. func computeLayout<Backend: BaseAppBackend>(
  20. _ widget: Backend.Widget,
  21. proposedSize: ProposedViewSize,
  22. environment: EnvironmentValues,
  23. backend: Backend
  24. ) -> ViewLayoutResult {
  25. var size = ViewSize.zero
  26. let proposedLength = proposedSize[component: environment.layoutOrientation]
  27. size[component: environment.layoutOrientation] = max(
  28. Double(minLength ?? 0),
  29. proposedLength ?? Self.idealLength
  30. )
  31. return ViewLayoutResult(
  32. size: size,
  33. participateInStackLayoutsWhenEmpty: true,
  34. preferences: PreferenceValues.default
  35. .with(\.layoutPriority, -Double.infinity)
  36. )
  37. }
  38. func commit<Backend: BaseAppBackend>(
  39. _ widget: Backend.Widget,
  40. layout: ViewLayoutResult,
  41. environment: EnvironmentValues,
  42. backend: Backend
  43. ) {
  44. // Spacers are invisible so we don't have to update anything.
  45. }
  46. }