This commit is contained in:
David Roman 2023-06-03 13:48:25 +01:00
parent 459b65ee30
commit 0e0dc8e486
No known key found for this signature in database
GPG Key ID: 7058646EEFCB70A7
2 changed files with 25 additions and 22 deletions

View File

@ -1,12 +1,30 @@
@_spi(Internals) @_spi(Internals)
public struct IntrospectionSelector<Target: PlatformEntity> { public struct IntrospectionSelector<Target: PlatformEntity> {
private let selector: (any PlatformEntity, IntrospectionScope, IntrospectionAnchorID) -> Target? private let selector: (IntrospectionPlatformViewController, IntrospectionScope, IntrospectionAnchorID) -> Target?
static var `default`: Self { .from(Target.self, selector: { $0 }) } static var `default`: Self { .from(Target.self, selector: { $0 }) }
@_spi(Internals) @_spi(Internals)
public static func from<Entry: PlatformEntity>(_ entryType: Entry.Type, selector: @escaping (Entry) -> Target?) -> Self { public static func from<Entry: PlatformEntity>(_ entryType: Entry.Type, selector: @escaping (Entry) -> Target?) -> Self {
.init { entity, scope, anchorID in .init { controller, scope, anchorID in
guard let entity = { () -> (any PlatformEntity)? in
if Entry.Base.self == PlatformView.self {
#if canImport(UIKit)
if let introspectionView = controller.viewIfLoaded {
return introspectionView
}
#elseif canImport(AppKit)
if controller.isViewLoaded {
return controller.view
}
#endif
} else if Entry.Base.self == PlatformViewController.self {
return controller
}
return nil
}() else {
return nil
}
if if
scope.contains(.receiver), scope.contains(.receiver),
let entry = entity.receiver(ofType: Entry.self, anchorID: anchorID), let entry = entity.receiver(ofType: Entry.self, anchorID: anchorID),
@ -25,15 +43,15 @@ public struct IntrospectionSelector<Target: PlatformEntity> {
} }
} }
init(_ selector: @escaping (any PlatformEntity, IntrospectionScope, IntrospectionAnchorID) -> Target?) { init(_ selector: @escaping (IntrospectionPlatformViewController, IntrospectionScope, IntrospectionAnchorID) -> Target?) {
self.selector = selector self.selector = selector
} }
func callAsFunction( func callAsFunction(
_ entity: any PlatformEntity, _ controller: IntrospectionPlatformViewController,
_ scope: IntrospectionScope, _ scope: IntrospectionScope,
_ anchorID: IntrospectionAnchorID _ anchorID: IntrospectionAnchorID
) -> Target? { ) -> Target? {
selector(entity, scope, anchorID) selector(controller, scope, anchorID)
} }
} }

View File

@ -81,26 +81,11 @@ struct IntrospectionView<Target: PlatformEntity>: PlatformViewControllerRepresen
private let customize: (Target) -> Void private let customize: (Target) -> Void
init( init(
selector: @escaping (any PlatformEntity) -> Target?, selector: @escaping (IntrospectionPlatformViewController) -> Target?,
customize: @escaping (Target) -> Void customize: @escaping (Target) -> Void
) { ) {
self._observed = .constant(()) self._observed = .constant(())
self.selector = { introspectionController in self.selector = selector
if Target.Base.self == PlatformView.self {
#if canImport(UIKit)
if let introspectionView = introspectionController.viewIfLoaded {
return selector(introspectionView)
}
#elseif canImport(AppKit)
if introspectionController.isViewLoaded {
return selector(introspectionController.view)
}
#endif
} else if Target.Base.self == PlatformViewController.self {
return selector(introspectionController)
}
return nil
}
self.customize = customize self.customize = customize
} }