Skip to content

Commit b7a11f3

Browse files
committed
Fix CG type conflict
1 parent 616d8ea commit b7a11f3

File tree

6 files changed

+150
-10
lines changed

6 files changed

+150
-10
lines changed

Package.resolved

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,13 @@ let bridgeFramework = envStringValue("OPENSWIFTUI_BRIDGE_FRAMEWORK", default: "S
192192
// Workaround iOS CI build issue (We need to disable this on iOS CI)
193193
let supportMultiProducts: Bool = envBoolValue("SUPPORT_MULTI_PRODUCTS", default: true)
194194

195+
/// CGFloat and CGRect def in CFCGTypes.h will conflict with Foundation's CGSize/CGRect def on Linux.
196+
/// macOS: true -> no issue
197+
/// macOS: false -> use Swift implementation with OpenCoreGraphics Swift CGPath
198+
/// Linux: true + No CGPathRef support in ORBPath -> confilict with Foundation def
199+
/// Linux: false -> use Swift implementation with OpenCoreGraphics Swift CGPath
200+
let cfCGTypes = envBoolValue("CF_CGTYPES", default: buildForDarwinPlatform)
201+
195202
// MARK: - Shared Settings
196203

197204
var sharedCSettings: [CSetting] = [
@@ -373,6 +380,15 @@ if enableRuntimeConcurrencyCheck {
373380
sharedSwiftSettings.append(.define("OPENSWIFTUI_ENABLE_RUNTIME_CONCURRENCY_CHECK"))
374381
}
375382

383+
if cfCGTypes {
384+
sharedCSettings.append(.define("OPENSWIFTUI_CF_CGTYPES"))
385+
sharedCxxSettings.append(.define("OPENSWIFTUI_CF_CGTYPES"))
386+
sharedSwiftSettings.append(.define("OPENSWIFTUI_CF_CGTYPES"))
387+
sharedCSettings.append(.define("OPENRENDERBOX_CF_CGTYPES"))
388+
sharedCxxSettings.append(.define("OPENRENDERBOX_CF_CGTYPES"))
389+
sharedSwiftSettings.append(.define("OPENRENDERBOX_CF_CGTYPES"))
390+
}
391+
376392
// MARK: - Extension
377393

378394
extension Target {

Sources/OpenSwiftUICore/Shape/Path.swift

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,17 @@ public struct Path: Equatable, LosslessStringConvertible, @unchecked Sendable {
2424
@usableFromInline
2525
final package class PathBox: Equatable {
2626
private enum Kind: UInt8 {
27+
#if canImport(CoreGraphics) || !OPENSWIFTUI_CF_CGTYPES
2728
case cgPath
29+
#endif
2830
case rbPath
2931
case buffer
3032
}
3133

3234
private var kind: Kind
3335
private var data: PathData
3436

35-
#if canImport(CoreGraphics)
37+
#if canImport(CoreGraphics) || !OPENSWIFTUI_CF_CGTYPES
3638
@inline(__always)
3739
init(_ path: CGPath) {
3840
kind = .cgPath
@@ -48,7 +50,7 @@ public struct Path: Equatable, LosslessStringConvertible, @unchecked Sendable {
4850
private func prepareBuffer() {
4951
let path: ORBPath
5052
switch kind {
51-
#if canImport(CoreGraphics)
53+
#if canImport(CoreGraphics) || !OPENSWIFTUI_CF_CGTYPES
5254
case .cgPath:
5355
path = ORBPath(cgPath: data.cgPath.takeRetainedValue())
5456
#endif
@@ -130,7 +132,7 @@ public struct Path: Equatable, LosslessStringConvertible, @unchecked Sendable {
130132
return UnsafePointer(pointer)
131133
}()
132134

133-
#if canImport(CoreGraphics)
135+
#if canImport(CoreGraphics) || !OPENSWIFTUI_CF_CGTYPES
134136
@inline(__always)
135137
fileprivate var cgPath: CGPath {
136138
let rbPath: ORBPath
@@ -202,7 +204,7 @@ public struct Path: Equatable, LosslessStringConvertible, @unchecked Sendable {
202204
storage = .empty
203205
}
204206

205-
#if canImport(CoreGraphics)
207+
#if canImport(CoreGraphics) || !OPENSWIFTUI_CF_CGTYPES
206208
/// Creates a path from an immutable shape path.
207209
///
208210
/// - Parameter path: The immutable CoreGraphics path to initialize
@@ -396,9 +398,10 @@ public struct Path: Equatable, LosslessStringConvertible, @unchecked Sendable {
396398
#endif
397399
}
398400

399-
#if canImport(CoreGraphics)
401+
#if canImport(CoreGraphics) || !OPENSWIFTUI_CF_CGTYPES
400402
/// An immutable path representing the elements in the path.
401403
public var cgPath: CGPath {
404+
#if canImport(Darwin)
402405
switch storage {
403406
case .empty:
404407
CGPath(rect: .null, transform: nil)
@@ -413,6 +416,9 @@ public struct Path: Equatable, LosslessStringConvertible, @unchecked Sendable {
413416
case let .path(pathBox):
414417
pathBox.cgPath
415418
}
419+
#else
420+
_openSwiftUIPlatformUnimplementedFailure()
421+
#endif
416422
}
417423
#endif
418424

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
//
2+
// PathData.swift
3+
// OpenSwiftUICore
4+
5+
#if !OPENSWIFTUI_CF_CGTYPES
6+
7+
package import OpenCoreGraphicsShims
8+
package import OpenRenderBoxShims
9+
10+
// MARK: - PathData
11+
12+
/// A union-like structure matching the C PathData union layout.
13+
/// Size: 0x70 (112) bytes to match the buffer size.
14+
///
15+
/// C definition:
16+
///
17+
/// typedef union PathData {
18+
/// CGPathRef cgPath; // 8 bytes (pointer)
19+
/// ORBPath rbPath; // 16 bytes (2 pointers)
20+
/// uint8_t buffer[0x70]; // 112 bytes
21+
/// } PathData;
22+
package struct PathData {
23+
// 112 bytes of raw storage (0x70)
24+
package typealias Buffer = (
25+
UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8,
26+
UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8,
27+
UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8,
28+
UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8,
29+
UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8,
30+
UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8,
31+
UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8,
32+
UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8,
33+
UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8,
34+
UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8,
35+
UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8,
36+
UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8,
37+
UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8,
38+
UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8
39+
)
40+
41+
private var storage: Buffer = (
42+
0, 0, 0, 0, 0, 0, 0, 0,
43+
0, 0, 0, 0, 0, 0, 0, 0,
44+
0, 0, 0, 0, 0, 0, 0, 0,
45+
0, 0, 0, 0, 0, 0, 0, 0,
46+
0, 0, 0, 0, 0, 0, 0, 0,
47+
0, 0, 0, 0, 0, 0, 0, 0,
48+
0, 0, 0, 0, 0, 0, 0, 0,
49+
0, 0, 0, 0, 0, 0, 0, 0,
50+
0, 0, 0, 0, 0, 0, 0, 0,
51+
0, 0, 0, 0, 0, 0, 0, 0,
52+
0, 0, 0, 0, 0, 0, 0, 0,
53+
0, 0, 0, 0, 0, 0, 0, 0,
54+
0, 0, 0, 0, 0, 0, 0, 0,
55+
0, 0, 0, 0, 0, 0, 0, 0
56+
)
57+
58+
package init() {}
59+
60+
// MARK: - CGPath access
61+
62+
package init(cgPath: Unmanaged<CGPath>) {
63+
self.cgPath = cgPath
64+
}
65+
66+
package var cgPath: Unmanaged<CGPath> {
67+
get {
68+
withUnsafeBytes(of: storage) { buffer in
69+
buffer.load(as: Unmanaged<CGPath>.self)
70+
}
71+
}
72+
set {
73+
withUnsafeMutableBytes(of: &storage) { buffer in
74+
buffer.storeBytes(of: newValue, as: Unmanaged<CGPath>.self)
75+
}
76+
}
77+
}
78+
79+
// MARK: - ORBPath access
80+
81+
package init(rbPath: ORBPath) {
82+
self.rbPath = rbPath
83+
}
84+
85+
package var rbPath: ORBPath {
86+
get {
87+
withUnsafeBytes(of: storage) { buffer in
88+
buffer.load(as: ORBPath.self)
89+
}
90+
}
91+
set {
92+
withUnsafeMutableBytes(of: &storage) { buffer in
93+
buffer.storeBytes(of: newValue, as: ORBPath.self)
94+
}
95+
}
96+
}
97+
98+
// MARK: - Buffer access
99+
100+
package init(buffer: Buffer) {
101+
self.storage = buffer
102+
}
103+
104+
package var buffer: Buffer {
105+
get { storage }
106+
set { storage = newValue }
107+
}
108+
}
109+
110+
#endif

Sources/OpenSwiftUICore/Shape/RoundedCornerStyle.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,15 +176,19 @@ package struct FixedRoundedRect: Equatable {
176176
)
177177
}
178178

179-
#if canImport(CoreGraphics)
179+
#if canImport(CoreGraphics) || !OPENSWIFTUI_CF_CGTYPES
180180
package var cgPath: CGPath {
181+
#if canImport(Darwin)
181182
let clamped = clampedCornerSize
182183
return _CGPathCreateRoundedRect(
183184
rect,
184185
clamped.width,
185186
clamped.height,
186187
needsContinuousCorners ? .continuous : .circular
187188
)
189+
#else
190+
_openSwiftUIPlatformUnimplementedFailure()
191+
#endif
188192
}
189193
#endif
190194

Sources/OpenSwiftUI_SPI/Util/PathData.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// PathData.h
33
// OpenSwiftUI_SPI
44

5+
#if OPENSWIFTUI_CF_CGTYPES
6+
57
#ifndef PathData_h
68
#define PathData_h
79

@@ -38,3 +40,5 @@ OPENSWIFTUI_ASSUME_NONNULL_END
3840
OPENSWIFTUI_IMPLICIT_BRIDGING_DISABLED
3941

4042
#endif /* PathData_h */
43+
44+
#endif /* OPENSWIFTUI_CF_CGTYPES */

0 commit comments

Comments
 (0)