Skip to content

Commit dc07e05

Browse files
committed
WIP
1 parent 7028879 commit dc07e05

File tree

2 files changed

+42
-38
lines changed

2 files changed

+42
-38
lines changed
Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
import SwiftUI
2-
import SwiftUIIntrospect
2+
@_spi(Internals) import SwiftUIIntrospect
33

44
struct UIViewRepresentableShowcase: View {
55
var body: some View {
66
VStack(spacing: 10) {
7-
Text(".introspect(.view, ...)")
8-
.lineLimit(1)
9-
.minimumScaleFactor(0.5)
10-
.font(.system(.subheadline, design: .monospaced))
7+
GenericViewRepresentable()
118
#if os(iOS) || os(tvOS) || os(visionOS)
129
.introspect(
1310
.view,
@@ -20,35 +17,6 @@ struct UIViewRepresentableShowcase: View {
2017
view.layer?.backgroundColor = NSColor.cyan.cgColor
2118
}
2219
#endif
23-
24-
Button("A button", action: {})
25-
.padding(5)
26-
#if os(iOS) || os(tvOS) || os(visionOS)
27-
.introspect(
28-
.view,
29-
on: .iOS(.v13, .v14, .v15, .v16, .v17, .v18, .v26), .tvOS(.v13, .v14, .v15, .v16, .v17, .v18, .v26), .visionOS(.v1, .v2, .v26)
30-
) { view in
31-
view.backgroundColor = .yellow
32-
}
33-
#elseif os(macOS)
34-
.introspect(.view, on: .macOS(.v10_15, .v11, .v12, .v13, .v14, .v15, .v26)) { view in
35-
view.layer?.backgroundColor = NSColor.yellow.cgColor
36-
}
37-
#endif
38-
39-
Image(systemName: "scribble")
40-
#if os(iOS) || os(tvOS) || os(visionOS)
41-
.introspect(
42-
.view,
43-
on: .iOS(.v13, .v14, .v15, .v16, .v17, .v18, .v26), .tvOS(.v13, .v14, .v15, .v16, .v17, .v18, .v26), .visionOS(.v1, .v2, .v26)
44-
) { view in
45-
view.backgroundColor = .blue
46-
}
47-
#elseif os(macOS)
48-
.introspect(.view, on: .macOS(.v10_15, .v11, .v12, .v13, .v14, .v15, .v26)) { view in
49-
view.layer?.backgroundColor = NSColor.blue.cgColor
50-
}
51-
#endif
5220
}
5321
.padding()
5422
#if os(iOS) || os(tvOS) || os(visionOS)
@@ -65,3 +33,35 @@ struct UIViewRepresentableShowcase: View {
6533
#endif
6634
}
6735
}
36+
37+
@MainActor
38+
struct GenericViewRepresentable: PlatformViewControllerRepresentable {
39+
#if canImport(UIKit)
40+
typealias UIViewControllerType = PlatformViewController
41+
#elseif canImport(AppKit)
42+
typealias NSViewControllerType = PlatformViewController
43+
#endif
44+
45+
func makePlatformViewController(context: Context) -> PlatformViewController {
46+
let controller = PlatformViewController(nibName: nil, bundle: nil)
47+
controller.view.translatesAutoresizingMaskIntoConstraints = false
48+
49+
let widthConstraint = controller.view.widthAnchor.constraint(greaterThanOrEqualToConstant: .greatestFiniteMagnitude)
50+
widthConstraint.priority = .defaultLow
51+
52+
let heightConstraint = controller.view.heightAnchor.constraint(greaterThanOrEqualToConstant: .greatestFiniteMagnitude)
53+
heightConstraint.priority = .defaultLow
54+
55+
NSLayoutConstraint.activate([widthConstraint, heightConstraint])
56+
57+
return controller
58+
}
59+
60+
func updatePlatformViewController(_ controller: PlatformViewController, context: Context) {
61+
// NO-OP
62+
}
63+
64+
static func dismantlePlatformViewController(_ controller: PlatformViewController, coordinator: Coordinator) {
65+
// NO-OP
66+
}
67+
}

Sources/PlatformView.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,16 @@ public typealias PlatformViewController = NSViewController
1414
#endif
1515

1616
#if canImport(UIKit)
17-
typealias _PlatformViewControllerRepresentable = UIViewControllerRepresentable
17+
@_spi(Internals)
18+
public typealias _PlatformViewControllerRepresentable = UIViewControllerRepresentable
1819
#elseif canImport(AppKit)
19-
typealias _PlatformViewControllerRepresentable = NSViewControllerRepresentable
20+
@_spi(Internals)
21+
public typealias _PlatformViewControllerRepresentable = NSViewControllerRepresentable
2022
#endif
2123

2224
@MainActor
23-
protocol PlatformViewControllerRepresentable: _PlatformViewControllerRepresentable {
25+
@_spi(Internals)
26+
public protocol PlatformViewControllerRepresentable: _PlatformViewControllerRepresentable {
2427
#if canImport(UIKit)
2528
typealias ViewController = UIViewControllerType
2629
#elseif canImport(AppKit)
@@ -32,7 +35,8 @@ protocol PlatformViewControllerRepresentable: _PlatformViewControllerRepresentab
3235
static func dismantlePlatformViewController(_ controller: ViewController, coordinator: Coordinator)
3336
}
3437

35-
extension PlatformViewControllerRepresentable {
38+
@_spi(Internals)
39+
public extension PlatformViewControllerRepresentable {
3640
#if canImport(UIKit)
3741
func makeUIViewController(context: Context) -> ViewController {
3842
makePlatformViewController(context: context)

0 commit comments

Comments
 (0)