-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add OpenSwiftUIBridge target * Update README.md * Add available check for Darwin OS
- Loading branch information
Showing
10 changed files
with
259 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// | ||
// Bridgeable.swift | ||
// OpenSwiftUIBridge | ||
|
||
import OpenSwiftUI | ||
|
||
/// A type that can be converted to and from its counterpart. | ||
public protocol Bridgeable<Counterpart> { | ||
associatedtype Counterpart where Counterpart: Bridgeable, Counterpart.Counterpart == Self | ||
|
||
init(_ counterpart: Counterpart) | ||
} | ||
|
||
extension Bridgeable { | ||
public var counterpart: Self.Counterpart { .init(self) } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// | ||
// SwiftUI.Color.swift | ||
// OpenSwiftUIBridge | ||
|
||
#if canImport(SwiftUI) | ||
public import SwiftUI | ||
public import OpenSwiftUI | ||
|
||
// MARK: Color + Bridgeable | ||
|
||
@available(iOS 17.0, macOS 14.0, tvOS 17.0, watchOS 10.0, *) | ||
extension SwiftUI.Color: Bridgeable { | ||
public typealias Counterpart = OpenSwiftUI.Color | ||
|
||
public init(_ counterpart: Counterpart) { | ||
self.init(OpenSwiftUIColor2SwiftUIColorAdapter(base: counterpart)) | ||
} | ||
} | ||
|
||
@available(iOS 17.0, macOS 14.0, tvOS 17.0, watchOS 10.0, *) | ||
extension OpenSwiftUI.Color: Bridgeable { | ||
public typealias Counterpart = SwiftUI.Color | ||
|
||
public init(_ counterpart: Counterpart) { | ||
self.init(SwiftUIColor2OpenSwiftUIColorAdapter(base: counterpart)) | ||
} | ||
} | ||
|
||
@available(iOS 17.0, macOS 14.0, tvOS 17.0, watchOS 10.0, *) | ||
private struct OpenSwiftUIColor2SwiftUIColorAdapter: Hashable, SwiftUI.ShapeStyle { | ||
private let base: OpenSwiftUI.Color | ||
|
||
init(base: OpenSwiftUI.Color) { | ||
self.base = base | ||
} | ||
|
||
public typealias Resolved = SwiftUI.Color.Resolved | ||
|
||
public func resolve(in environment: SwiftUI.EnvironmentValues) -> SwiftUI.Color.Resolved { | ||
.init(base.resolve(in: .init(environment))) | ||
} | ||
} | ||
|
||
@available(iOS 17.0, macOS 14.0, tvOS 17.0, watchOS 10.0, *) | ||
private struct SwiftUIColor2OpenSwiftUIColorAdapter: Hashable, OpenSwiftUI.ShapeStyle { | ||
private let base: SwiftUI.Color | ||
|
||
init(base: SwiftUI.Color) { | ||
self.base = base | ||
} | ||
|
||
public typealias Resolved = OpenSwiftUI.Color.Resolved | ||
|
||
public func resolve(in environment: OpenSwiftUI.EnvironmentValues) -> OpenSwiftUI.Color.Resolved { | ||
.init(base.resolve(in: .init(environment))) | ||
} | ||
} | ||
|
||
// MARK: - Color.Resolved + Bridgeable | ||
|
||
@available(iOS 17.0, macOS 14.0, tvOS 17.0, watchOS 10.0, *) | ||
extension SwiftUI.Color.Resolved: Bridgeable { | ||
public typealias Counterpart = OpenSwiftUI.Color.Resolved | ||
|
||
public init(_ counterpart: Counterpart) { | ||
self.init(colorSpace: .sRGBLinear, red: counterpart.linearRed, green: counterpart.linearGreen, blue: counterpart.linearBlue) | ||
} | ||
} | ||
|
||
@available(iOS 17.0, macOS 14.0, tvOS 17.0, watchOS 10.0, *) | ||
extension OpenSwiftUI.Color.Resolved: Bridgeable { | ||
public typealias Counterpart = SwiftUI.Color.Resolved | ||
|
||
public init(_ counterpart: Counterpart) { | ||
self.init(colorSpace: .sRGBLinear, red: counterpart.linearRed, green: counterpart.linearGreen, blue: counterpart.linearBlue) | ||
} | ||
} | ||
#endif |
28 changes: 28 additions & 0 deletions
28
Sources/OpenSwiftUIBridge/SwiftUI/SwiftUI.EnvironmentValues.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// | ||
// SwiftUI.EnvironmentValues.swift | ||
// OpenSwiftUIBridge | ||
|
||
#if canImport(SwiftUI) | ||
public import SwiftUI | ||
public import OpenSwiftUI | ||
|
||
// MARK: EnvironmentValues + Bridgeable | ||
|
||
extension SwiftUI.EnvironmentValues: Bridgeable { | ||
public typealias Counterpart = OpenSwiftUI.EnvironmentValues | ||
|
||
public init(_ counterpart: Counterpart) { | ||
// FIXME | ||
self.init() | ||
} | ||
} | ||
|
||
extension OpenSwiftUI.EnvironmentValues: Bridgeable { | ||
public typealias Counterpart = SwiftUI.EnvironmentValues | ||
|
||
public init(_ counterpart: Counterpart) { | ||
// FIXME | ||
self.init() | ||
} | ||
} | ||
#endif |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// | ||
// BridgeableTests.swift | ||
// OpenSwiftUIBridgeTests | ||
|
||
import Testing | ||
import OpenSwiftUIBridge | ||
|
||
struct BridgeableTests { | ||
@Test | ||
func example() throws { | ||
struct A1: Bridgeable, Equatable { | ||
typealias Counterpart = A2 | ||
var value: Int | ||
|
||
init(value: Int) { | ||
self.value = value | ||
} | ||
|
||
init(_ counterpart: A2) { | ||
self.value = counterpart.value | ||
} | ||
} | ||
|
||
struct A2: Bridgeable, Equatable { | ||
typealias Counterpart = A1 | ||
var value: Int | ||
|
||
init(value: Int) { | ||
self.value = value | ||
} | ||
|
||
init(_ counterpart: A1) { | ||
self.value = counterpart.value | ||
} | ||
} | ||
let a1 = A1(value: 3) | ||
let a2 = A2(value: 3) | ||
|
||
#expect(a1.counterpart == a2) | ||
#expect(a2.counterpart == a1) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
## OpenSwiftUIBridgeTests | ||
|
||
Test API of OpenSwiftUIBridge | ||
|
||
```swift | ||
import OpenSwiftUIBridge | ||
``` |
31 changes: 31 additions & 0 deletions
31
Tests/OpenSwiftUIBridgeTests/SwiftUI/SwiftUI.ColorTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// | ||
// SwiftUI.ColorTests.swift | ||
// OpenSwiftUIBridgeTests | ||
|
||
#if canImport(SwiftUI) | ||
import Testing | ||
import SwiftUI | ||
import OpenSwiftUI | ||
import OpenSwiftUIBridge | ||
|
||
struct SwiftUI_ColorTests { | ||
@Test | ||
@available(iOS 17.0, macOS 14.0, tvOS 17.0, watchOS 10.0, *) | ||
func color() throws { | ||
let swiftUIWhite = SwiftUI.Color.white | ||
let openSwiftUIWhite = OpenSwiftUI.Color.white | ||
#expect(swiftUIWhite.counterpart.resolve(in: .init()) == openSwiftUIWhite.resolve(in: .init())) | ||
#expect(openSwiftUIWhite.counterpart.resolve(in: .init()) == swiftUIWhite.resolve(in: .init())) | ||
} | ||
|
||
@Test | ||
@available(iOS 17.0, macOS 14.0, tvOS 17.0, watchOS 10.0, *) | ||
func resolved() throws { | ||
let swiftUIWhiteResolved = SwiftUI.Color.Resolved.init(red: 1, green: 1, blue: 1) | ||
let openSwiftUIWhiteResolved = OpenSwiftUI.Color.Resolved.init(red: 1, green: 1, blue: 1) | ||
|
||
#expect(swiftUIWhiteResolved.counterpart == openSwiftUIWhiteResolved) | ||
#expect(openSwiftUIWhiteResolved.counterpart == swiftUIWhiteResolved) | ||
} | ||
} | ||
#endif |
24 changes: 24 additions & 0 deletions
24
Tests/OpenSwiftUIBridgeTests/SwiftUI/SwiftUI.EnvironmentValues.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// | ||
// SwiftUI.EnvironmentValues.swift | ||
// OpenSwiftUIBridgeTests | ||
|
||
#if canImport(SwiftUI) | ||
import Testing | ||
import SwiftUI | ||
import OpenSwiftUI | ||
import OpenSwiftUIBridge | ||
|
||
struct SwiftUI_EnvironmentValues { | ||
@Test | ||
func example() throws { | ||
var swiftUIEnviromentValues = SwiftUI.EnvironmentValues() | ||
let openSwiftUIEnviromentValues = OpenSwiftUI.EnvironmentValues() | ||
#expect(swiftUIEnviromentValues.colorScheme == .light) | ||
#expect(openSwiftUIEnviromentValues.colorScheme == .light) | ||
swiftUIEnviromentValues.colorScheme = .dark | ||
withKnownIssue { | ||
#expect(swiftUIEnviromentValues.counterpart.colorScheme == .dark) | ||
} | ||
} | ||
} | ||
#endif |