This commit is contained in:
David Roman 2023-06-04 17:15:47 +01:00
parent 2ea5070688
commit 8254af4549
No known key found for this signature in database
GPG Key ID: 7058646EEFCB70A7
1 changed files with 32 additions and 41 deletions

View File

@ -1,14 +1,14 @@
@_spi(Internals)
public struct IntrospectionSelector<Target: PlatformEntity> {
private var receiverSelector: (IntrospectionPlatformViewController, IntrospectionAnchorID) -> Target?
private var ancestorSelector: (IntrospectionPlatformViewController, IntrospectionAnchorID) -> Target?
private var ancestorSelector: (IntrospectionPlatformViewController) -> Target?
@_spi(Internals)
public static var `default`: Self { .from(Target.self, selector: { $0 }) }
@_spi(Internals)
public func overrideAncestorSelector(
_ selector: @escaping (PlatformViewController, IntrospectionAnchorID) -> Target?
public func withAncestorSelector(
_ selector: @escaping (PlatformViewController) -> Target?
) -> Self {
var copy = self
copy.ancestorSelector = selector
@ -17,46 +17,22 @@ public struct IntrospectionSelector<Target: PlatformEntity> {
@_spi(Internals)
public static func from<Entry: PlatformEntity>(_ entryType: Entry.Type, selector: @escaping (Entry) -> Target?) -> Self {
.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
.init(
receiver: { controller, anchorID in
controller.as(Entry.self)?.receiver(ofType: Entry.self, anchorID: anchorID).flatMap(selector)
},
ancestor: { controller in
controller.as(Entry.self)?.ancestor(ofType: Entry.self).flatMap(selector)
}
if
scope.contains(.receiver),
let entry = entity.receiver(ofType: Entry.self, anchorID: anchorID),
let target = selector(entry)
{
return target
}
if
scope.contains(.ancestor),
let entry = entity.ancestor(ofType: Entry.self),
let target = selector(entry)
{
return target
}
return nil
}
)
}
init(_ selector: @escaping (IntrospectionPlatformViewController, IntrospectionScope, IntrospectionAnchorID) -> Target?) {
self.receiverSelector = { selector($0, .receiver, $1) }
self.ancestorSelector = { selector($0, .ancestor, $1) }
init(
receiver: @escaping (IntrospectionPlatformViewController, IntrospectionAnchorID) -> Target?,
ancestor: @escaping (IntrospectionPlatformViewController) -> Target?
) {
self.receiverSelector = receiver
self.ancestorSelector = ancestor
}
func callAsFunction(
@ -72,10 +48,25 @@ public struct IntrospectionSelector<Target: PlatformEntity> {
}
if
scope.contains(.ancestor),
let target = ancestorSelector(controller, anchorID)
let target = ancestorSelector(controller)
{
return target
}
return nil
}
}
extension PlatformViewController {
func `as`<Entity: PlatformEntity>(_ entityType: Entity.Type) -> (any PlatformEntity)? {
if Entity.Base.self == PlatformView.self {
#if canImport(UIKit)
return viewIfLoaded
#elseif canImport(AppKit)
return isViewLoaded ? view : nil
#endif
} else if Entity.Base.self == PlatformViewController.self {
return self
}
return nil
}
}