From 442fa3b7994afb83700f397a71ffc325e1775b6e Mon Sep 17 00:00:00 2001 From: David Roman <2538074+davdroman@users.noreply.github.com> Date: Fri, 2 Jun 2023 14:51:02 +0100 Subject: [PATCH] wip --- Sources/Introspect.swift | 2 +- Sources/IntrospectionView.swift | 77 +++++++++++++++++++++++++-------- Sources/PlatformView.swift | 30 ++++++------- 3 files changed, 74 insertions(+), 35 deletions(-) diff --git a/Sources/Introspect.swift b/Sources/Introspect.swift index 08012ba..6800812 100644 --- a/Sources/Introspect.swift +++ b/Sources/Introspect.swift @@ -93,7 +93,7 @@ extension View { extension PlatformView { fileprivate func receiver( ofType type: PlatformSpecificView.Type, - anchorID: IntrospectionAnchorView.ID + anchorID: UUID ) -> PlatformSpecificView? { let frontView = self guard diff --git a/Sources/IntrospectionView.swift b/Sources/IntrospectionView.swift index 2997007..b4fd66d 100644 --- a/Sources/IntrospectionView.swift +++ b/Sources/IntrospectionView.swift @@ -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: PlatformViewControllerRepresentable { + #if canImport(UIKit) + typealias UIViewControllerType = IntrospectionPlatformViewController + #elseif canImport(AppKit) + typealias NSViewControllerType = IntrospectionPlatformViewController + #endif + final class TargetCache { weak var target: Target? } diff --git a/Sources/PlatformView.swift b/Sources/PlatformView.swift index a89c412..0863186 100644 --- a/Sources/PlatformView.swift +++ b/Sources/PlatformView.swift @@ -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