This commit is contained in:
David Roman 2023-06-02 14:51:02 +01:00
parent a84dfbfb28
commit 442fa3b799
No known key found for this signature in database
GPG Key ID: 7058646EEFCB70A7
3 changed files with 74 additions and 35 deletions

View File

@ -93,7 +93,7 @@ extension View {
extension PlatformView {
fileprivate func receiver<PlatformSpecificView: PlatformView>(
ofType type: PlatformSpecificView.Type,
anchorID: IntrospectionAnchorView.ID
anchorID: UUID
) -> PlatformSpecificView? {
let frontView = self
guard

View File

@ -1,44 +1,83 @@
import SwiftUI
///
struct IntrospectionAnchorView: PlatformViewRepresentable {
typealias ID = UUID
struct IntrospectionAnchorView: PlatformViewControllerRepresentable {
#if canImport(UIKit)
typealias UIViewControllerType = IntrospectionAnchorPlatformViewController
#elseif canImport(AppKit)
typealias NSViewControllerType = IntrospectionAnchorPlatformViewController
#endif
@Binding
private var observed: Void // workaround for state changes not triggering view updates
let id: ID
let id: UUID
init(id: ID) {
init(id: UUID) {
self._observed = .constant(())
self.id = id
}
func makePlatformViewController(context: Context) -> IntrospectionAnchorPlatformViewController {
IntrospectionAnchorPlatformViewController(id: id)
}
func updatePlatformViewController(_ controller: IntrospectionAnchorPlatformViewController, context: Context) {}
static func dismantlePlatformViewController(_ controller: IntrospectionAnchorPlatformViewController, coordinator: Coordinator) {}
}
final class IntrospectionAnchorPlatformViewController: PlatformViewController {
let id: UUID
init(id: UUID) {
self.id = id
super.init(nibName: nil, bundle: nil)
}
@available(*, unavailable)
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
#if canImport(UIKit)
func makeUIView(context: Context) -> UIView {
let view = UIView()
override func viewDidLoad() {
super.viewDidLoad()
view.tag = id.hashValue
return view
}
func updateUIView(_ controller: UIView, context: Context) {}
#elseif canImport(AppKit)
func makeNSView(context: Context) -> NSView {
final class TaggableView: NSView {
private var _tag: Int?
override var tag: Int {
get { _tag ?? super.tag }
set { _tag = newValue }
}
final class TaggableView: NSView {
private var _tag: Int
init(tag: Int) {
self._tag = tag
super.init(frame: .zero)
}
@available(*, unavailable)
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override var tag: Int {
get { _tag }
}
let view = TaggableView()
view.tag = id.hashValue
return view
}
func updateNSView(_ controller: NSView, context: Context) {}
override func loadView() {
let view = TaggableView(tag: id.hashValue)
self.view = view
}
#endif
}
struct IntrospectionView<Target: AnyObject>: PlatformViewControllerRepresentable {
#if canImport(UIKit)
typealias UIViewControllerType = IntrospectionPlatformViewController
#elseif canImport(AppKit)
typealias NSViewControllerType = IntrospectionPlatformViewController
#endif
final class TargetCache {
weak var target: Target?
}

View File

@ -6,12 +6,6 @@ public typealias PlatformView = UIView
public typealias PlatformView = NSView
#endif
#if canImport(UIKit)
typealias PlatformViewRepresentable = UIViewRepresentable
#elseif canImport(AppKit)
typealias PlatformViewRepresentable = NSViewRepresentable
#endif
#if canImport(UIKit)
public typealias PlatformViewController = UIViewController
#elseif canImport(AppKit)
@ -25,30 +19,36 @@ typealias _PlatformViewControllerRepresentable = NSViewControllerRepresentable
#endif
protocol PlatformViewControllerRepresentable: _PlatformViewControllerRepresentable {
func makePlatformViewController(context: Context) -> IntrospectionPlatformViewController
func updatePlatformViewController(_ controller: IntrospectionPlatformViewController, context: Context)
static func dismantlePlatformViewController(_ controller: IntrospectionPlatformViewController, coordinator: Coordinator)
#if canImport(UIKit)
typealias ViewController = UIViewControllerType
#elseif canImport(AppKit)
typealias ViewController = NSViewControllerType
#endif
func makePlatformViewController(context: Context) -> ViewController
func updatePlatformViewController(_ controller: ViewController, context: Context)
static func dismantlePlatformViewController(_ controller: ViewController, coordinator: Coordinator)
}
extension PlatformViewControllerRepresentable {
#if canImport(UIKit)
func makeUIViewController(context: Context) -> IntrospectionPlatformViewController {
func makeUIViewController(context: Context) -> ViewController {
makePlatformViewController(context: context)
}
func updateUIViewController(_ controller: IntrospectionPlatformViewController, context: Context) {
func updateUIViewController(_ controller: ViewController, context: Context) {
updatePlatformViewController(controller, context: context)
}
static func dismantleUIViewController(_ controller: IntrospectionPlatformViewController, coordinator: Coordinator) {
static func dismantleUIViewController(_ controller: ViewController, coordinator: Coordinator) {
dismantlePlatformViewController(controller, coordinator: coordinator)
}
#elseif canImport(AppKit)
func makeNSViewController(context: Context) -> IntrospectionPlatformViewController {
func makeNSViewController(context: Context) -> ViewController {
makePlatformViewController(context: context)
}
func updateNSViewController(_ controller: IntrospectionPlatformViewController, context: Context) {
func updateNSViewController(_ controller: ViewController, context: Context) {
updatePlatformViewController(controller, context: context)
}
static func dismantleNSViewController(_ controller: IntrospectionPlatformViewController, coordinator: Coordinator) {
static func dismantleNSViewController(_ controller: ViewController, coordinator: Coordinator) {
dismantlePlatformViewController(controller, coordinator: coordinator)
}
#endif