Skip to content

Commit

Permalink
Add Environment to Layout API
Browse files Browse the repository at this point in the history
  • Loading branch information
watt committed Apr 12, 2023
1 parent 21a3cd5 commit fed7d22
Show file tree
Hide file tree
Showing 25 changed files with 360 additions and 112 deletions.
8 changes: 6 additions & 2 deletions BlueprintUI/Sources/Element/ElementContent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -304,32 +304,36 @@ fileprivate struct SingleChildLayoutHost<WrappedLayout: SingleChildLayout>: Layo
func sizeThatFits(
proposal: SizeConstraint,
subelements: Subelements,
environment: Environment,
cache: inout Cache
) -> CGSize {
precondition(subelements.count == 1)
return wrapped.sizeThatFits(
proposal: proposal,
subelement: subelements[0],
environment: environment,
cache: &cache
)
}

func placeSubelements(
in size: CGSize,
subelements: Subelements,
environment: Environment,
cache: inout Cache
) {
precondition(subelements.count == 1)
wrapped.placeSubelement(
in: size,
subelement: subelements[0],
environment: environment,
cache: &cache
)
}

func makeCache(subelements: Subelements) -> Cache {
func makeCache(subelements: Subelements, environment: Environment) -> Cache {
precondition(subelements.count == 1)
return wrapped.makeCache(subelement: subelements[0])
return wrapped.makeCache(subelement: subelements[0], environment: environment)
}
}

Expand Down
6 changes: 4 additions & 2 deletions BlueprintUI/Sources/Element/LayoutStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,13 @@ extension LayoutStorage: CaffeinatedContentStorage {
let subelements = subelements(from: context.node, environment: context.environment)

var associatedCache = context.node.associatedCache {
layout.makeCache(subelements: subelements)
layout.makeCache(subelements: subelements, environment: environment)
}

let size = layout.sizeThatFits(
proposal: proposal,
subelements: subelements,
environment: context.environment,
cache: &associatedCache
)

Expand All @@ -174,12 +175,13 @@ extension LayoutStorage: CaffeinatedContentStorage {
let subelements = subelements(from: context.node, environment: context.environment)

var associatedCache = context.node.associatedCache {
layout.makeCache(subelements: subelements)
layout.makeCache(subelements: subelements, environment: environment)
}

layout.placeSubelements(
in: frame.size,
subelements: subelements,
environment: context.environment,
cache: &associatedCache
)

Expand Down
14 changes: 12 additions & 2 deletions BlueprintUI/Sources/Layout/Aligned.swift
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,21 @@ public struct Aligned: Element {
return attributes
}

func sizeThatFits(proposal: SizeConstraint, subelement: LayoutSubelement, cache: inout Cache) -> CGSize {
func sizeThatFits(
proposal: SizeConstraint,
subelement: Subelement,
environment: Environment,
cache: inout Cache
) -> CGSize {
subelement.sizeThatFits(proposal)
}

func placeSubelement(in size: CGSize, subelement: LayoutSubelement, cache: inout ()) {
func placeSubelement(
in size: CGSize,
subelement: Subelement,
environment: Environment,
cache: inout ()
) {
let x: CGFloat
let y: CGFloat

Expand Down
14 changes: 12 additions & 2 deletions BlueprintUI/Sources/Layout/ConstrainedAspectRatio.swift
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,12 @@ public struct ConstrainedAspectRatio: Element {
LayoutAttributes(size: size)
}

func sizeThatFits(proposal: SizeConstraint, subelement: LayoutSubelement, cache: inout Cache) -> CGSize {
func sizeThatFits(
proposal: SizeConstraint,
subelement: Subelement,
environment: Environment,
cache: inout Cache
) -> CGSize {
let contentSize = subelement.sizeThatFits(proposal)
return contentMode.constrain(
contentSize: contentSize,
Expand All @@ -168,7 +173,12 @@ public struct ConstrainedAspectRatio: Element {
)
}

func placeSubelement(in size: CGSize, subelement: LayoutSubelement, cache: inout ()) {
func placeSubelement(
in size: CGSize,
subelement: Subelement,
environment: Environment,
cache: inout ()
) {
subelement.place(filling: size)
}
}
Expand Down
14 changes: 12 additions & 2 deletions BlueprintUI/Sources/Layout/ConstrainedSize.swift
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,12 @@ extension ConstrainedSize {
LayoutAttributes(size: size)
}

func sizeThatFits(proposal: SizeConstraint, subelement: LayoutSubelement, cache: inout Cache) -> CGSize {
func sizeThatFits(
proposal: SizeConstraint,
subelement: Subelement,
environment: Environment,
cache: inout Cache
) -> CGSize {
if case let .absolute(width) = width, case let .absolute(height) = height {
return CGSize(width: width, height: height)
}
Expand All @@ -225,7 +230,12 @@ extension ConstrainedSize {
)
}

func placeSubelement(in size: CGSize, subelement: LayoutSubelement, cache: inout ()) {
func placeSubelement(
in size: CGSize,
subelement: Subelement,
environment: Environment,
cache: inout ()
) {
subelement.place(filling: size)
}
}
Expand Down
14 changes: 12 additions & 2 deletions BlueprintUI/Sources/Layout/EqualStack.swift
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,12 @@ extension EqualStack {
return result
}

func sizeThatFits(proposal: SizeConstraint, subelements: Subelements, cache: inout Cache) -> CGSize {
func sizeThatFits(
proposal: SizeConstraint,
subelements: Subelements,
environment: Environment,
cache: inout Cache
) -> CGSize {
guard subelements.count > 0 else { return .zero }

let totalSpacing = (spacing * CGFloat(subelements.count - 1))
Expand Down Expand Up @@ -192,7 +197,12 @@ extension EqualStack {
return totalSize
}

func placeSubelements(in size: CGSize, subelements: Subelements, cache: inout ()) {
func placeSubelements(
in size: CGSize,
subelements: Subelements,
environment: Environment,
cache: inout ()
) {
guard subelements.count > 0 else { return }

let totalSpacing = (spacing * CGFloat(subelements.count - 1))
Expand Down
14 changes: 12 additions & 2 deletions BlueprintUI/Sources/Layout/GridRow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,12 @@ extension GridRow {
}
}

func sizeThatFits(proposal: SizeConstraint, subelements: Subelements, cache: inout Cache) -> CGSize {
func sizeThatFits(
proposal: SizeConstraint,
subelements: Subelements,
environment: Environment,
cache: inout Cache
) -> CGSize {
guard subelements.count > 0 else {
return .zero
}
Expand All @@ -147,7 +152,12 @@ extension GridRow {
return size
}

func placeSubelements(in size: CGSize, subelements: Subelements, cache: inout ()) {
func placeSubelements(
in size: CGSize,
subelements: Subelements,
environment: Environment,
cache: inout ()
) {
guard subelements.count > 0 else {
return
}
Expand Down
14 changes: 12 additions & 2 deletions BlueprintUI/Sources/Layout/Hidden.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,21 @@ public struct Hidden: Element {
return attributes
}

func sizeThatFits(proposal: SizeConstraint, subelement: LayoutSubelement, cache: inout Cache) -> CGSize {
func sizeThatFits(
proposal: SizeConstraint,
subelement: Subelement,
environment: Environment,
cache: inout Cache
) -> CGSize {
subelement.sizeThatFits(proposal)
}

func placeSubelement(in size: CGSize, subelement: LayoutSubelement, cache: inout ()) {
func placeSubelement(
in size: CGSize,
subelement: Subelement,
environment: Environment,
cache: inout ()
) {
subelement.attributes.isHidden = isHidden
}
}
Expand Down
14 changes: 12 additions & 2 deletions BlueprintUI/Sources/Layout/Inset.swift
Original file line number Diff line number Diff line change
Expand Up @@ -159,13 +159,23 @@ extension Inset {
return LayoutAttributes(frame: frame)
}

func sizeThatFits(proposal: SizeConstraint, subelement: LayoutSubelement, cache: inout ()) -> CGSize {
func sizeThatFits(
proposal: SizeConstraint,
subelement: Subelement,
environment: Environment,
cache: inout ()
) -> CGSize {
let insetProposal = proposal.inset(by: edgeInsets)
let childSize = subelement.sizeThatFits(insetProposal)
return childSize + CGSize(width: left + right, height: top + bottom)
}

func placeSubelement(in size: CGSize, subelement: LayoutSubelement, cache: inout ()) {
func placeSubelement(
in size: CGSize,
subelement: Subelement,
environment: Environment,
cache: inout ()
) {
let insetSize = size.inset(by: edgeInsets)

subelement.place(
Expand Down
14 changes: 12 additions & 2 deletions BlueprintUI/Sources/Layout/Keyed.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,21 @@ public struct Keyed: Element {
LayoutAttributes(size: size)
}

func sizeThatFits(proposal: SizeConstraint, subelement: LayoutSubelement, cache: inout Cache) -> CGSize {
func sizeThatFits(
proposal: SizeConstraint,
subelement: Subelement,
environment: Environment,
cache: inout Cache
) -> CGSize {
subelement.sizeThatFits(proposal)
}

func placeSubelement(in size: CGSize, subelement: LayoutSubelement, cache: inout ()) {
func placeSubelement(
in size: CGSize,
subelement: Subelement,
environment: Environment,
cache: inout ()
) {
subelement.place(at: .zero, size: size)
}
}
Expand Down
Loading

0 comments on commit fed7d22

Please sign in to comment.