Skip to content

Commit 944bf89

Browse files
committed
Add ViewTransform convert API
1 parent 0cdefd1 commit 944bf89

File tree

1 file changed

+135
-3
lines changed

1 file changed

+135
-3
lines changed

Sources/OpenSwiftUICore/Layout/View/ViewTransform.swift

Lines changed: 135 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,23 @@ public struct ViewTransform: Equatable, CustomStringConvertible {
2525
package static func spaceToGlobal(_ space: CoordinateSpace) -> ViewTransform.Conversion {
2626
.spaceToSpace(space, .global)
2727
}
28+
29+
// FIXME
30+
@inline(__always)
31+
func normalized() -> Conversion {
32+
guard case let .spaceToSpace(space1, space2) = self else { return self }
33+
if case .local = space1 {
34+
return .localToSpace(space2)
35+
} else if case .local = space2 {
36+
return .spaceToLocal(space1)
37+
} else if .root == space1 {
38+
return .rootToSpace(space2)
39+
} else if .root == space2 {
40+
return .spaceToRoot(space1)
41+
} else {
42+
return self
43+
}
44+
}
2845
}
2946

3047
package enum Item: Equatable {
@@ -266,6 +283,50 @@ public struct ViewTransform: Equatable, CustomStringConvertible {
266283
forEach(inverted: false, body)
267284
}
268285

286+
private func spaceBeforeSpace(_ space1: CoordinateSpace, _ space2: CoordinateSpace) -> Bool {
287+
if case .global = space1 {
288+
return true
289+
} else if case .local = space1 {
290+
return false
291+
} else if case .global = space2 {
292+
return false
293+
} else if case .local = space2 {
294+
return true
295+
} else {
296+
forEach(inverted: false) { item, stop in
297+
// TODO
298+
}
299+
preconditionFailure("TODO")
300+
}
301+
}
302+
303+
package func convert(_ conversion: ViewTransform.Conversion, _ body: (ViewTransform.Item) -> Void) {
304+
guard !isEmpty else { return }
305+
preconditionFailure("TODO")
306+
}
307+
308+
package func convert(_ conversion: ViewTransform.Conversion, points: inout [CGPoint]) {
309+
guard !isEmpty else { return }
310+
preconditionFailure("TODO")
311+
}
312+
313+
package func convert(_ conversion: ViewTransform.Conversion, point: CGPoint) -> CGPoint {
314+
guard !isEmpty else { return point }
315+
preconditionFailure("TODO")
316+
}
317+
318+
package var containingScrollGeometry: ScrollGeometry? {
319+
preconditionFailure("TODO")
320+
}
321+
322+
package var nearestScrollGeometry: ScrollGeometry? {
323+
preconditionFailure("TODO")
324+
}
325+
326+
package func containingSizedCoordinateSpace(name: CoordinateSpace.Name) -> CGRect? {
327+
preconditionFailure("TODO")
328+
}
329+
269330
public var description: String {
270331
var descriptionArray = pendingTranslation == .zero ? [] : [String(describing: pendingTranslation)]
271332
var element = head
@@ -573,15 +634,37 @@ private class BufferedElement: AnyElement {
573634
// MARK: - ViewTransformable
574635

575636
private protocol ApplyViewTransform {
576-
mutating func convert(to space: CoordinateSpace, transform: ViewTransform)
637+
mutating func applyTransform(item: ViewTransform.Item)
638+
}
639+
640+
extension ApplyViewTransform {
641+
mutating func convert(to space: CoordinateSpace, transform: ViewTransform) {
642+
transform.convert(.localToSpace(space)) { item in
643+
applyTransform(item: item)
644+
}
645+
}
577646
}
578647

579648
package protocol ViewTransformable {
580649
mutating func convert(to space: CoordinateSpace, transform: ViewTransform)
581650
mutating func convert(from space: CoordinateSpace, transform: ViewTransform)
582651
}
583652

584-
extension CGPoint/*: ViewTransformable*/ {
653+
extension ViewTransformable where Self: ApplyViewTransform {
654+
mutating func convert(to space: CoordinateSpace, transform: ViewTransform) {
655+
transform.convert(.localToSpace(space)) { item in
656+
applyTransform(item: item)
657+
}
658+
}
659+
660+
mutating func convert(from space: CoordinateSpace, transform: ViewTransform) {
661+
transform.convert(.spaceToLocal(space)) { item in
662+
applyTransform(item: item)
663+
}
664+
}
665+
}
666+
667+
extension CGPoint: ApplyViewTransform, ViewTransformable {
585668
package mutating func applyTransform(item: ViewTransform.Item) {
586669
switch item {
587670
case let .translation(offset):
@@ -606,9 +689,17 @@ extension CGPoint/*: ViewTransformable*/ {
606689
break
607690
}
608691
}
692+
693+
package mutating func convert(to space: CoordinateSpace, transform: ViewTransform) {
694+
self = transform.convert(.localToSpace(space), point: self)
695+
}
696+
697+
package mutating func convert(from space: CoordinateSpace, transform: ViewTransform) {
698+
self = transform.convert(.spaceToLocal(space), point: self)
699+
}
609700
}
610701

611-
extension [CGPoint]/*: ViewTransformable*/ {
702+
extension [CGPoint]: ApplyViewTransform, ViewTransformable {
612703
package mutating func applyTransform(item: ViewTransform.Item) {
613704
switch item {
614705
case let .translation(offset):
@@ -626,4 +717,45 @@ extension [CGPoint]/*: ViewTransformable*/ {
626717
package mutating func apply(_ m: ProjectionTransform, inverse: Bool) {
627718
self = map { inverse ? $0.unapplying(m) : $0.applying(m) }
628719
}
720+
721+
package mutating func convert(to space: CoordinateSpace, transform: ViewTransform) {
722+
transform.convert(.localToSpace(space), points: &self)
723+
}
724+
725+
package mutating func convert(from space: CoordinateSpace, transform: ViewTransform) {
726+
transform.convert(.spaceToLocal(space), points: &self)
727+
}
629728
}
729+
730+
extension CGRect: ViewTransformable {
731+
package mutating func convert(to space: CoordinateSpace, transform: ViewTransform) {
732+
guard !isNull else { return }
733+
guard !isInfinite else { return }
734+
var points = cornerPoints
735+
points.convert(to: space, transform: transform)
736+
self = CGRect(cornerPoints: points)
737+
}
738+
739+
package mutating func convert(from space: CoordinateSpace, transform: ViewTransform) {
740+
guard !isNull else { return }
741+
guard !isInfinite else { return }
742+
var points = cornerPoints
743+
points.convert(from: space, transform: transform)
744+
self = CGRect(cornerPoints: points)
745+
}
746+
747+
package mutating func whileClippingToScrollViewsConvert(to space: CoordinateSpace, transform: ViewTransform) -> Bool {
748+
guard !isNull else { return true }
749+
guard !isInfinite else { return true }
750+
transform.convert(.localToSpace(space)) { item in
751+
// TODO
752+
}
753+
preconditionFailure("TODO")
754+
}
755+
}
756+
757+
// TODO: Path + ViewTransformable
758+
//extension Path: ViewTransformable {
759+
// package mutating func convert(to space: CoordinateSpace, transform: ViewTransform)
760+
// package mutating func convert(from space: CoordinateSpace, transform: ViewTransform)
761+
//}

0 commit comments

Comments
 (0)