Skip to content

Commit 90e2a25

Browse files
committed
Add Anchor API implementation
1 parent 325b75e commit 90e2a25

File tree

2 files changed

+398
-4
lines changed

2 files changed

+398
-4
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
//
2+
// Anchor+Point.swift
3+
// OpenSwiftUICore
4+
//
5+
// Audited for 6.5.4
6+
// Status: Complete
7+
8+
public import OpenCoreGraphicsShims
9+
10+
extension CGPoint: AnchorProtocol {
11+
package static var defaultAnchor: CGPoint {
12+
.zero
13+
}
14+
15+
package func prepare(geometry: AnchorGeometry) -> CGPoint {
16+
var point = self
17+
point.convert(to: .global, transform: geometry.transform)
18+
return point
19+
}
20+
21+
package static func hashValue(_ value: CGPoint, into hasher: inout Hasher) {
22+
hasher.combine(value.x)
23+
hasher.combine(value.y)
24+
}
25+
}
26+
27+
extension UnitPoint: AnchorProtocol {
28+
package static var defaultAnchor: CGPoint {
29+
.zero
30+
}
31+
32+
package func prepare(geometry: AnchorGeometry) -> CGPoint {
33+
let point = `in`(geometry.size)
34+
return point.prepare(geometry: geometry)
35+
}
36+
37+
package static func hashValue(_ value: CGPoint, into hasher: inout Hasher) {
38+
hasher.combine(value.x)
39+
hasher.combine(value.y)
40+
}
41+
}
42+
43+
@available(OpenSwiftUI_v1_0, *)
44+
extension Anchor.Source where Value == CGPoint {
45+
public static func point(_ p: CGPoint) -> Anchor<Value>.Source {
46+
.init(anchor: p)
47+
}
48+
49+
public static func unitPoint(_ p: UnitPoint) -> Anchor<Value>.Source {
50+
.init(anchor: p)
51+
}
52+
53+
public static var topLeading: Anchor<CGPoint>.Source {
54+
unitPoint(.topLeading)
55+
}
56+
57+
public static var top: Anchor<CGPoint>.Source {
58+
unitPoint(.top)
59+
}
60+
61+
public static var topTrailing: Anchor<CGPoint>.Source {
62+
unitPoint(.topTrailing)
63+
}
64+
65+
public static var leading: Anchor<CGPoint>.Source {
66+
unitPoint(.leading)
67+
}
68+
69+
public static var center: Anchor<CGPoint>.Source {
70+
unitPoint(.center)
71+
}
72+
73+
public static var trailing: Anchor<CGPoint>.Source {
74+
unitPoint(.trailing)
75+
}
76+
77+
public static var bottomLeading: Anchor<CGPoint>.Source {
78+
unitPoint(.bottomLeading)
79+
}
80+
81+
public static var bottom: Anchor<CGPoint>.Source {
82+
unitPoint(.bottom)
83+
}
84+
85+
public static var bottomTrailing: Anchor<CGPoint>.Source {
86+
unitPoint(.bottomTrailing)
87+
}
88+
}

0 commit comments

Comments
 (0)