Skip to content

Add _AlignmentLayout #307

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 1 commit 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
103 changes: 103 additions & 0 deletions Sources/OpenSwiftUICore/Layout/Alignment/AlignmentLayout.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
//
// AlignmentLayout.swift
// OpenSwiftUICore
//
// Status: Complete

package import Foundation

// MARK: - VAlignment [6.4.41]

/// An alignment in the vertical axis.
@frozen
public enum _VAlignment {
case top
case center
case bottom

package var value: CGFloat {
switch self {
case .top: 0.0
case .center: 0.5
case .bottom: 1.0
}
}
}

// MARK: - AlignmentLayout [6.4.41]

@frozen
public struct _AlignmentLayout: UnaryLayout {
public var horizontal: TextAlignment?

public var vertical: _VAlignment?

@inlinable
public init(
horizontal: TextAlignment? = nil,
vertical: _VAlignment? = nil
) {
self.horizontal = horizontal
self.vertical = vertical
}

package func placement(
of child: LayoutProxy,
in context: PlacementContext
) -> _Placement {
_Placement(
proposedSize: context.proposedSize,
aligning: UnitPoint(x: (horizontal ?? .center).value, y: (vertical ?? .center).value),
in: context.size
)
}

package func sizeThatFits(
in proposedSize: _ProposedSize,
context: SizeAndSpacingContext,
child: LayoutProxy
) -> CGSize {
guard horizontal != nil,
vertical != nil,
let width = proposedSize.width,
let height = proposedSize.height else {
let childSize = child.size(in: proposedSize)
let width = if horizontal != nil, let width = proposedSize.width {
width
} else {
childSize.width
}
let height = if vertical != nil, let height = proposedSize.height {
height
} else {
childSize.height
}
return CGSize(width: width, height: height)
}
return CGSize(width: width, height: height)
}

package func spacing(
in context: SizeAndSpacingContext,
child: LayoutProxy
) -> Spacing {
if _SemanticFeature_v3.isEnabled {
var spacing = child.layoutComputer.spacing()
spacing.reset(
AbsoluteEdge.Set(
[horizontal == nil ? [] : .horizontal, vertical == nil ? [] : .vertical],
layoutDirection: context.layoutDirection
)
)
return spacing
} else {
return child.layoutComputer.spacing()
}
}

public typealias AnimatableData = EmptyAnimatableData

public typealias Body = Never

package typealias PlacementContextType = PlacementContext
}
3 changes: 2 additions & 1 deletion Sources/OpenSwiftUICore/Layout/Alignment/TextAlignment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
// TextAlignment.swift
// OpenSwiftUICore
//
// Audited for iOS 18.0
// Status: Complete

package import Foundation

// MARK: - TextAlignment [6.0.87]

/// An alignment position for text along the horizontal axis.
@frozen
public enum TextAlignment: Hashable, CaseIterable {
Expand Down
24 changes: 0 additions & 24 deletions Sources/OpenSwiftUICore/Layout/Alignment/VAlignment.swift

This file was deleted.

22 changes: 15 additions & 7 deletions Sources/OpenSwiftUICore/Layout/Edge/Edge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,24 @@ public enum Edge: Int8, CaseIterable {
self.rawValue = rawValue
}

public static let top = Set(.top)
public static let leading = Set(.leading)
public static let bottom = Set(.bottom)
public static let trailing = Set(.trailing)
public static let top: Set = .init(.top) // 1

public static let leading: Set = .init(.leading) // 2

public static let bottom: Set = .init(.bottom) // 4

public static let trailing: Set = .init(.trailing) // 8

public static let all: Set = [.top, .leading, .bottom, .trailing]
public static let horizontal: Set = [.leading, .trailing]
public static let vertical: Set = [.top, .bottom]

public static let horizontal: Set = [.leading, .trailing] // 0xa

public static let vertical: Set = [.top, .bottom] // 0x5

/// Creates an instance containing just e
public init(_ e: Edge) { self.init(rawValue: 1 << e.rawValue) }
public init(_ e: Edge) {
rawValue = 1 << e.rawValue
}

package func contains(_ edge: Edge) -> Bool {
contains(.init(edge))
Expand Down
25 changes: 25 additions & 0 deletions Sources/OpenSwiftUICore/Layout/GeometryReader.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//
// GeometryReader.swift
// OpenSwiftUICore
//
// Status: WIP

extension UnaryLayout where Self.PlacementContextType == _PositionAwarePlacementContext {
package static func makeViewImpl(
modifier: _GraphValue<Self>,
inputs: _ViewInputs,
body: @escaping (_Graph, _ViewInputs) -> _ViewOutputs
) -> _ViewOutputs {
preconditionFailure("TODO")
}
}

extension UnaryLayout where Self.PlacementContextType == PlacementContext {
package static func makeViewImpl(
modifier: _GraphValue<Self>,
inputs: _ViewInputs,
body: @escaping (_Graph, _ViewInputs) -> _ViewOutputs
) -> _ViewOutputs {
preconditionFailure("TODO")
}
}
2 changes: 1 addition & 1 deletion Sources/OpenSwiftUICore/Layout/PlacementContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//
// Audited for iOS 18.0
// Status: Complete
// ID: BA60BF7120E939C5C25B2A488163D4AC
// ID: BA60BF7120E939C5C25B2A488163D4AC (SwiftUICore)

package import Foundation
package import OpenGraphShims
Expand Down