Skip to content

Commit af5dc91

Browse files
committed
Add OpenSwiftUIBridge target
1 parent 3dd8e6e commit af5dc91

File tree

4 files changed

+123
-0
lines changed

4 files changed

+123
-0
lines changed

Package.swift

+10
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,13 @@ let openSwiftUIExtensionTarget = Target.target(
104104
],
105105
swiftSettings: sharedSwiftSettings
106106
)
107+
let openSwiftUIBridgeTarget = Target.target(
108+
name: "OpenSwiftUIBridge",
109+
dependencies: [
110+
"OpenSwiftUI",
111+
],
112+
swiftSettings: sharedSwiftSettings
113+
)
107114
let OpenSwiftUI_SPITestTarget = Target.testTarget(
108115
name: "OpenSwiftUI_SPITests",
109116
dependencies: [
@@ -153,6 +160,7 @@ let package = Package(
153160
.library(name: "OpenSwiftUI", targets: ["OpenSwiftUI", "OpenSwiftUIExtension"]),
154161
// FIXME: This will block xcodebuild build(iOS CI) somehow
155162
// .library(name: "OpenSwiftUI_SPI", targets: ["OpenSwiftUI_SPI"]),
163+
// .library(name: "OpenSwiftUIBridge", targets: ["OpenSwiftUIBridge"])
156164
],
157165
dependencies: [
158166
.package(url: "https://github.com/apple/swift-numerics.git", from: "1.0.2"),
@@ -179,7 +187,9 @@ let package = Package(
179187
.binaryTarget(name: "CoreServices", path: "PrivateFrameworks/CoreServices.xcframework"),
180188
openSwiftUICoreTarget,
181189
openSwiftUITarget,
190+
182191
openSwiftUIExtensionTarget,
192+
openSwiftUIBridgeTarget,
183193

184194
OpenSwiftUI_SPITestTarget,
185195
openSwiftUICoreTestTarget,
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//
2+
// Bridgeable.swift
3+
// OpenSwiftUIBridge
4+
5+
import SwiftUI
6+
import OpenSwiftUI
7+
8+
public protocol Bridgeable<Counterpart> {
9+
associatedtype Counterpart where Counterpart: Bridgeable, Counterpart.Counterpart == Self
10+
11+
init(_ counterpart: Counterpart)
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
//
2+
// Bridgeable+Color.swift
3+
// OpenSwiftUIBridge
4+
5+
#if canImport(SwiftUI)
6+
public import SwiftUI
7+
public import OpenSwiftUI
8+
9+
// MARK: Color + Bridgeable
10+
11+
extension SwiftUI.Color: Bridgeable {
12+
public typealias Counterpart = OpenSwiftUI.Color
13+
14+
public init(_ counterpart: Counterpart) {
15+
self.init(OpenSwiftUIColor2SwiftUIColorAdapter(base: counterpart))
16+
}
17+
}
18+
19+
extension OpenSwiftUI.Color: Bridgeable {
20+
public typealias Counterpart = SwiftUI.Color
21+
22+
public init(_ counterpart: Counterpart) {
23+
fatalError()
24+
}
25+
}
26+
27+
private struct OpenSwiftUIColor2SwiftUIColorAdapter: Hashable, SwiftUI.ShapeStyle {
28+
private let base: OpenSwiftUI.Color
29+
30+
init(base: OpenSwiftUI.Color) {
31+
self.base = base
32+
}
33+
34+
public typealias Resolved = SwiftUI.Color.Resolved
35+
36+
public func resolve(in environment: SwiftUI.EnvironmentValues) -> SwiftUI.Color.Resolved {
37+
.init(base.resolve(in: .init(environment)))
38+
}
39+
}
40+
41+
private struct SwiftUIColor2OpenSwiftUIColorAdapter: Hashable, OpenSwiftUI.ShapeStyle {
42+
private let base: SwiftUI.Color
43+
44+
init(base: SwiftUI.Color) {
45+
self.base = base
46+
}
47+
48+
public typealias Resolved = OpenSwiftUI.Color.Resolved
49+
50+
public func resolve(in environment: OpenSwiftUI.EnvironmentValues) -> OpenSwiftUI.Color.Resolved {
51+
.init(base.resolve(in: .init(environment)))
52+
}
53+
}
54+
55+
56+
// MARK: - Color.Resolved + Bridgeable
57+
58+
extension SwiftUI.Color.Resolved: Bridgeable {
59+
public typealias Counterpart = OpenSwiftUI.Color.Resolved
60+
61+
public init(_ counterpart: Counterpart) {
62+
self.init(colorSpace: .sRGBLinear, red: counterpart.linearRed, green: counterpart.linearGreen, blue: counterpart.linearBlue)
63+
}
64+
}
65+
66+
extension OpenSwiftUI.Color.Resolved: Bridgeable {
67+
public typealias Counterpart = SwiftUI.Color.Resolved
68+
69+
public init(_ counterpart: Counterpart) {
70+
self.init(colorSpace: .sRGBLinear, red: counterpart.linearRed, green: counterpart.linearGreen, blue: counterpart.linearBlue)
71+
}
72+
}
73+
74+
#endif
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//
2+
// SwiftUI.EnvironmentValues.swift
3+
// OpenSwiftUIBridge
4+
5+
#if canImport(SwiftUI)
6+
public import SwiftUI
7+
public import OpenSwiftUI
8+
9+
extension SwiftUI.EnvironmentValues: Bridgeable {
10+
public typealias Counterpart = OpenSwiftUI.EnvironmentValues
11+
12+
// FIXME: unsafe and not very robust
13+
public init(_ counterpart: Counterpart) {
14+
self = unsafeBitCast(counterpart, to: SwiftUI.EnvironmentValues.self)
15+
}
16+
}
17+
18+
extension OpenSwiftUI.EnvironmentValues: Bridgeable {
19+
public typealias Counterpart = SwiftUI.EnvironmentValues
20+
21+
// FIXME: unsafe and not very robust
22+
public init(_ counterpart: Counterpart) {
23+
self = unsafeBitCast(counterpart, to: OpenSwiftUI.EnvironmentValues.self)
24+
}
25+
}
26+
27+
#endif

0 commit comments

Comments
 (0)