Skip to content

Update Layout API #308

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
May 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Sources/OpenSwiftUI/Util/OpenSwiftUIGlue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ final public class OpenSwiftUIGlue: CoreGlue {
MakeDefaultLayoutComputerResult(value: graph.viewGraph().$defaultLayoutComputer)
}

override final public func makeLayoutView<L>(
root: _GraphValue<L>,
inputs: _ViewInputs,
body: (_Graph, _ViewInputs) -> _ViewListOutputs
) -> _ViewOutputs where L: Layout {
L.makeLayoutView(root: root, inputs: inputs, body: body)
}

// TODO
}

Expand Down
617 changes: 617 additions & 0 deletions Sources/OpenSwiftUICore/Layout/AnyLayout.swift

Large diffs are not rendered by default.

1,998 changes: 1,998 additions & 0 deletions Sources/OpenSwiftUICore/Layout/Layout.swift

Large diffs are not rendered by default.

112 changes: 110 additions & 2 deletions Sources/OpenSwiftUICore/Layout/UnaryLayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
// UnaryLayout.swift
// OpenSwiftUICore
//
// Audited for iOS 18.0
// Status: WIP
// Status: Blocked by makeStaticView and makeDynamicView

package import Foundation
package import OpenGraphShims

// MARK: - UnaryLayout [6.4.41]

package protocol UnaryLayout: Animatable, MultiViewModifier, PrimitiveViewModifier {
associatedtype PlacementContextType = PlacementContext
Expand Down Expand Up @@ -48,3 +50,109 @@ extension UnaryLayout {
child.spacing()
}
}

// MARK: - DerivedLayout [6.4.41]

package protocol DerivedLayout: Layout {
associatedtype Base: Layout where Cache == Base.Cache

var base: Base { get }
}

extension DerivedLayout {
public static var layoutProperties: LayoutProperties {
Base.layoutProperties
}

public func makeCache(subviews: Subviews) -> Base.Cache {
base.makeCache(subviews: subviews)
}

public func updateCache(_ cache: inout Base.Cache, subviews: Subviews) {
base.updateCache(&cache, subviews: subviews)
}

public func spacing(subviews: Subviews, cache: inout Base.Cache) -> ViewSpacing {
base.spacing(subviews: subviews, cache: &cache)
}

public func sizeThatFits(proposal: ProposedViewSize, subviews: Subviews, cache: inout Base.Cache) -> CGSize {
base.sizeThatFits(proposal: proposal, subviews: subviews, cache: &cache)
}

public func placeSubviews(in bounds: CGRect, proposal: ProposedViewSize, subviews: Subviews, cache: inout Base.Cache) {
base.placeSubviews(in: bounds, proposal: proposal, subviews: subviews, cache: &cache)
}

public func explicitAlignment(of guide: HorizontalAlignment, in bounds: CGRect, proposal: ProposedViewSize, subviews: Subviews, cache: inout Base.Cache) -> CGFloat? {
base.explicitAlignment(of: guide, in: bounds, proposal: proposal, subviews: subviews, cache: &cache)
}

public func explicitAlignment(of guide: VerticalAlignment, in bounds: CGRect, proposal: ProposedViewSize, subviews: Subviews, cache: inout Base.Cache) -> CGFloat? {
base.explicitAlignment(of: guide, in: bounds, proposal: proposal, subviews: subviews, cache: &cache)
}
}

// MARK: - Layout + Static/Dynamic View Creation [6.4.41] [WIP]

extension Layout {
package static func makeStaticView(
root: _GraphValue<Self>,
inputs: _ViewInputs,
properties: LayoutProperties,
list: any _ViewList_Elements
) -> _ViewOutputs {
preconditionFailure("TODO")
}

static func makeDynamicView(
root: _GraphValue<Self>,
inputs: _ViewInputs,
properties: LayoutProperties,
list: Attribute<any ViewList>
) -> _ViewOutputs {
preconditionFailure("TODO")
}
}

package struct LayoutChildGeometries: Rule, AsyncAttribute {
@Attribute
private var parentSize: ViewSize

@Attribute
private var parentPosition: PlatformPoint

@Attribute
private var layoutComputer: LayoutComputer

package init(parentSize: Attribute<ViewSize>, parentPosition: Attribute<PlatformPoint>, layoutComputer: Attribute<LayoutComputer>) {
_parentSize = parentSize
_parentPosition = parentPosition
_layoutComputer = layoutComputer
}

package var value: [ViewGeometry] {
layoutComputer.childGeometries(at: parentSize, origin: parentPosition)
}
}

extension StatefulRule where Value == LayoutComputer {
package mutating func updateLayoutComputer<L>(
layout: L,
environment: Attribute<EnvironmentValues>,
attributes: [LayoutProxyAttributes]
) where L: Layout {
let context = AnyRuleContext(attribute: AnyAttribute.current!)
layout.updateLayoutComputer(
rule: &self,
layoutContext: SizeAndSpacingContext(
context: context,
environment: environment
),
children: LayoutProxyCollection(
context: context,
attributes: attributes
)
)
}
}
137 changes: 0 additions & 137 deletions Sources/OpenSwiftUICore/Layout/View/ProposedViewSize.swift

This file was deleted.

37 changes: 0 additions & 37 deletions Sources/OpenSwiftUICore/Layout/View/ViewSizeCache.swift

This file was deleted.

Loading