Skip to content

Add Coordinate Space API #171

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
Dec 7, 2024
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
//
// CoordinateSpace.swift
// OpenSwiftUICore
//
// Audited for iOS 18.0
// Status: Complete

/// A resolved coordinate space created by the coordinate space protocol.
///
/// You don't typically use `CoordinateSpace` directly. Instead, use the static
/// properties and functions of `CoordinateSpaceProtocol` such as `.global`,
/// `.local`, and `.named(_:)`.
public enum CoordinateSpace {
/// The global coordinate space at the root of the view hierarchy.
case global

/// The local coordinate space of the current view.
case local

/// A named reference to a view's local coordinate space.
case named(AnyHashable)

@_spi(ForOpenSwiftUIOnly)
public struct ID: Equatable, Sendable {
let value: UniqueID

public init() {
value = .init()

Check warning on line 28 in Sources/OpenSwiftUICore/Layout/CoordinateSpace/CoordinateSpace.swift

View check run for this annotation

Codecov / codecov/patch

Sources/OpenSwiftUICore/Layout/CoordinateSpace/CoordinateSpace.swift#L27-L28

Added lines #L27 - L28 were not covered by tests
}
}

@_spi(ForOpenSwiftUIOnly)
case id(CoordinateSpace.ID)

package static let root: CoordinateSpace = .global

package var canonical: CoordinateSpace { self }

Check warning on line 37 in Sources/OpenSwiftUICore/Layout/CoordinateSpace/CoordinateSpace.swift

View check run for this annotation

Codecov / codecov/patch

Sources/OpenSwiftUICore/Layout/CoordinateSpace/CoordinateSpace.swift#L37

Added line #L37 was not covered by tests

package enum Name: Equatable {
case name(AnyHashable)
case id(CoordinateSpace.ID)
package var space: CoordinateSpace {
switch self {
case let .name(name): .named(name)
case let .id(id): .id(id)

Check warning on line 45 in Sources/OpenSwiftUICore/Layout/CoordinateSpace/CoordinateSpace.swift

View check run for this annotation

Codecov / codecov/patch

Sources/OpenSwiftUICore/Layout/CoordinateSpace/CoordinateSpace.swift#L42-L45

Added lines #L42 - L45 were not covered by tests
}
}
}
}

@available(*, unavailable)
extension CoordinateSpace: Sendable {}

extension CoordinateSpace {
public var isGlobal: Bool { self == .global }

Check warning on line 55 in Sources/OpenSwiftUICore/Layout/CoordinateSpace/CoordinateSpace.swift

View check run for this annotation

Codecov / codecov/patch

Sources/OpenSwiftUICore/Layout/CoordinateSpace/CoordinateSpace.swift#L55

Added line #L55 was not covered by tests

public var isLocal: Bool { self == .local }

Check warning on line 57 in Sources/OpenSwiftUICore/Layout/CoordinateSpace/CoordinateSpace.swift

View check run for this annotation

Codecov / codecov/patch

Sources/OpenSwiftUICore/Layout/CoordinateSpace/CoordinateSpace.swift#L57

Added line #L57 was not covered by tests
}

extension CoordinateSpace: Equatable, Hashable {
public func hash(into hasher: inout Hasher) {
switch self {
case .global:
hasher.combine(0)
case .local:
hasher.combine(1)
case let .named(anyHashable):
hasher.combine(2)
anyHashable.hash(into: &hasher)
case let .id(id):
hasher.combine(3)
hasher.combine(id.value)

Check warning on line 72 in Sources/OpenSwiftUICore/Layout/CoordinateSpace/CoordinateSpace.swift

View check run for this annotation

Codecov / codecov/patch

Sources/OpenSwiftUICore/Layout/CoordinateSpace/CoordinateSpace.swift#L61-L72

Added lines #L61 - L72 were not covered by tests
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//
// CoordinateSpaceProtocol.swift
// OpenSwiftUICore
//
// Audited for iOS 18.0
// Status: Complete

/// A frame of reference within the layout system.
///
/// All geometric properties of a view, including size, position, and
/// transform, are defined within the local coordinate space of the view's
/// parent. These values can be converted into other coordinate spaces
/// by passing types conforming to this protocol into functions such as
/// `GeometryProxy.frame(in:)`.
///
/// For example, a named coordinate space allows you to convert the frame
/// of a view into the local coordinate space of an ancestor view by defining
/// a named coordinate space using the `coordinateSpace(_:)` modifier, then
/// passing that same named coordinate space into the `frame(in:)` function.
///
/// VStack {
/// GeometryReader { geometryProxy in
/// let distanceFromTop = geometryProxy.frame(in: "container").origin.y
/// Text("This view is \(distanceFromTop) points from the top of the VStack")
/// }
/// .padding()
/// }
/// .coordinateSpace(.named("container"))
///
/// You don't typically create types conforming to this protocol yourself.
/// Instead, use the system-provided `.global`, `.local`, and `.named(_:)`
/// implementations.
public protocol CoordinateSpaceProtocol {
/// The resolved coordinate space.
var coordinateSpace: CoordinateSpace { get }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// GlobalCoordinateSpace.swift
// OpenSwiftUICore
//
// Audited for iOS 18.0
// Status: Complete

/// The global coordinate space at the root of the view hierarchy.
public struct GlobalCoordinateSpace: CoordinateSpaceProtocol {
public init() {}

Check warning on line 10 in Sources/OpenSwiftUICore/Layout/CoordinateSpace/GlobalCoordinateSpace.swift

View check run for this annotation

Codecov / codecov/patch

Sources/OpenSwiftUICore/Layout/CoordinateSpace/GlobalCoordinateSpace.swift#L10

Added line #L10 was not covered by tests

public var coordinateSpace: CoordinateSpace { .global }

Check warning on line 12 in Sources/OpenSwiftUICore/Layout/CoordinateSpace/GlobalCoordinateSpace.swift

View check run for this annotation

Codecov / codecov/patch

Sources/OpenSwiftUICore/Layout/CoordinateSpace/GlobalCoordinateSpace.swift#L12

Added line #L12 was not covered by tests
}

@available(*, unavailable)
extension GlobalCoordinateSpace: Sendable {}

extension CoordinateSpaceProtocol where Self == GlobalCoordinateSpace {
/// The global coordinate space at the root of the view hierarchy.
public static var global: GlobalCoordinateSpace { .init() }

Check warning on line 20 in Sources/OpenSwiftUICore/Layout/CoordinateSpace/GlobalCoordinateSpace.swift

View check run for this annotation

Codecov / codecov/patch

Sources/OpenSwiftUICore/Layout/CoordinateSpace/GlobalCoordinateSpace.swift#L20

Added line #L20 was not covered by tests
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// LocalCoordinateSpace.swift
// OpenSwiftUICore
//
// Audited for iOS 18.0
// Status: Complete

/// The local coordinate space of the current view.
public struct LocalCoordinateSpace: CoordinateSpaceProtocol {
public init() {}

Check warning on line 10 in Sources/OpenSwiftUICore/Layout/CoordinateSpace/LocalCoordinateSpace.swift

View check run for this annotation

Codecov / codecov/patch

Sources/OpenSwiftUICore/Layout/CoordinateSpace/LocalCoordinateSpace.swift#L10

Added line #L10 was not covered by tests

public var coordinateSpace: CoordinateSpace { .local }

Check warning on line 12 in Sources/OpenSwiftUICore/Layout/CoordinateSpace/LocalCoordinateSpace.swift

View check run for this annotation

Codecov / codecov/patch

Sources/OpenSwiftUICore/Layout/CoordinateSpace/LocalCoordinateSpace.swift#L12

Added line #L12 was not covered by tests
}

@available(*, unavailable)
extension LocalCoordinateSpace: Sendable {}

extension CoordinateSpaceProtocol where Self == LocalCoordinateSpace {
/// The local coordinate space of the current view.
public static var local: LocalCoordinateSpace { .init() }

Check warning on line 20 in Sources/OpenSwiftUICore/Layout/CoordinateSpace/LocalCoordinateSpace.swift

View check run for this annotation

Codecov / codecov/patch

Sources/OpenSwiftUICore/Layout/CoordinateSpace/LocalCoordinateSpace.swift#L20

Added line #L20 was not covered by tests
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//
// NamedCoordinateSpace.swift
// OpenSwiftUICore
//
// Audited for iOS 18.0
// Status: Complete

/// A named coordinate space.
///
/// Use the `coordinateSpace(_:)` modifier to assign a name to the local
/// coordinate space of a parent view. Child views can then refer to that
/// coordinate space using `.named(_:)`.
public struct NamedCoordinateSpace: CoordinateSpaceProtocol, Equatable {
package var name: CoordinateSpace.Name

package init(name: CoordinateSpace.Name) {
self.name = name

Check warning on line 17 in Sources/OpenSwiftUICore/Layout/CoordinateSpace/NamedCoordinateSpace.swift

View check run for this annotation

Codecov / codecov/patch

Sources/OpenSwiftUICore/Layout/CoordinateSpace/NamedCoordinateSpace.swift#L16-L17

Added lines #L16 - L17 were not covered by tests
}

public var coordinateSpace: CoordinateSpace { name.space }

Check warning on line 20 in Sources/OpenSwiftUICore/Layout/CoordinateSpace/NamedCoordinateSpace.swift

View check run for this annotation

Codecov / codecov/patch

Sources/OpenSwiftUICore/Layout/CoordinateSpace/NamedCoordinateSpace.swift#L20

Added line #L20 was not covered by tests
}

@available(*, unavailable)
extension NamedCoordinateSpace: Sendable {}

extension CoordinateSpaceProtocol where Self == NamedCoordinateSpace {
/// Creates a named coordinate space using the given value.
///
/// Use the `coordinateSpace(_:)` modifier to assign a name to the local
/// coordinate space of a parent view. Child views can then refer to that
/// coordinate space using `.named(_:)`.
///
/// - Parameter name: A unique value that identifies the coordinate space.
///
/// - Returns: A named coordinate space identified by the given value.
public static func named(_ name: some Hashable) -> NamedCoordinateSpace {
NamedCoordinateSpace(name: .name(name))

Check warning on line 37 in Sources/OpenSwiftUICore/Layout/CoordinateSpace/NamedCoordinateSpace.swift

View check run for this annotation

Codecov / codecov/patch

Sources/OpenSwiftUICore/Layout/CoordinateSpace/NamedCoordinateSpace.swift#L36-L37

Added lines #L36 - L37 were not covered by tests
}

package static func id(_ id: CoordinateSpace.ID) -> NamedCoordinateSpace {
NamedCoordinateSpace(name: .id(id))

Check warning on line 41 in Sources/OpenSwiftUICore/Layout/CoordinateSpace/NamedCoordinateSpace.swift

View check run for this annotation

Codecov / codecov/patch

Sources/OpenSwiftUICore/Layout/CoordinateSpace/NamedCoordinateSpace.swift#L40-L41

Added lines #L40 - L41 were not covered by tests
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//
// ScrollCoordinateSpace.swift
// OpenSwiftUICore
//
// Audited for iOS 18.0
// Status: Complete

package enum ScrollCoordinateSpace {
package static let vertical: CoordinateSpace.ID = .init()
package static let horizontal: CoordinateSpace.ID = .init()
package static let all: CoordinateSpace.ID = .init()
package static let content: CoordinateSpace.ID = .init()
}

extension CoordinateSpace {
package static var verticalScrollView: CoordinateSpace { .id(ScrollCoordinateSpace.vertical) }
package static var horizontalScrollView: CoordinateSpace { .id(ScrollCoordinateSpace.horizontal) }
package static var scrollView: CoordinateSpace { .id(ScrollCoordinateSpace.all) }
package static var scrollViewContent: CoordinateSpace { .id(ScrollCoordinateSpace.content) }

Check warning on line 19 in Sources/OpenSwiftUICore/Layout/CoordinateSpace/ScrollCoordinateSpace.swift

View check run for this annotation

Codecov / codecov/patch

Sources/OpenSwiftUICore/Layout/CoordinateSpace/ScrollCoordinateSpace.swift#L16-L19

Added lines #L16 - L19 were not covered by tests
}

extension CoordinateSpace.Name {
package static var verticalScrollView: CoordinateSpace.Name { .id(ScrollCoordinateSpace.vertical) }
package static var horizontalScrollView: CoordinateSpace.Name { .id(ScrollCoordinateSpace.horizontal) }
package static var scrollView: CoordinateSpace.Name { .id(ScrollCoordinateSpace.all) }
package static var scrollViewContent: CoordinateSpace.Name { .id(ScrollCoordinateSpace.content) }

Check warning on line 26 in Sources/OpenSwiftUICore/Layout/CoordinateSpace/ScrollCoordinateSpace.swift

View check run for this annotation

Codecov / codecov/patch

Sources/OpenSwiftUICore/Layout/CoordinateSpace/ScrollCoordinateSpace.swift#L23-L26

Added lines #L23 - L26 were not covered by tests
}

extension CoordinateSpaceProtocol where Self == NamedCoordinateSpace {
/// The named coordinate space that is added by the system for the innermost
/// containing scroll view that allows scrolling along the provided axis.
public static func scrollView(axis: Axis) -> Self {
switch axis {
case .horizontal: NamedCoordinateSpace(name: .horizontalScrollView)
case .vertical: NamedCoordinateSpace(name: .verticalScrollView)

Check warning on line 35 in Sources/OpenSwiftUICore/Layout/CoordinateSpace/ScrollCoordinateSpace.swift

View check run for this annotation

Codecov / codecov/patch

Sources/OpenSwiftUICore/Layout/CoordinateSpace/ScrollCoordinateSpace.swift#L32-L35

Added lines #L32 - L35 were not covered by tests
}
}

/// The named coordinate space that is added by the system for the innermost
/// containing scroll view.
public static var scrollView: NamedCoordinateSpace { NamedCoordinateSpace(name: .scrollView) }

Check warning on line 41 in Sources/OpenSwiftUICore/Layout/CoordinateSpace/ScrollCoordinateSpace.swift

View check run for this annotation

Codecov / codecov/patch

Sources/OpenSwiftUICore/Layout/CoordinateSpace/ScrollCoordinateSpace.swift#L41

Added line #L41 was not covered by tests

package static var scrollViewContent: NamedCoordinateSpace { NamedCoordinateSpace(name: .scrollViewContent) }

Check warning on line 43 in Sources/OpenSwiftUICore/Layout/CoordinateSpace/ScrollCoordinateSpace.swift

View check run for this annotation

Codecov / codecov/patch

Sources/OpenSwiftUICore/Layout/CoordinateSpace/ScrollCoordinateSpace.swift#L43

Added line #L43 was not covered by tests
}
Loading