Skip to content

Commit 8254af4

Browse files
committed
wip
1 parent 2ea5070 commit 8254af4

File tree

1 file changed

+32
-41
lines changed

1 file changed

+32
-41
lines changed

Sources/IntrospectionSelector.swift

Lines changed: 32 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
@_spi(Internals)
22
public struct IntrospectionSelector<Target: PlatformEntity> {
33
private var receiverSelector: (IntrospectionPlatformViewController, IntrospectionAnchorID) -> Target?
4-
private var ancestorSelector: (IntrospectionPlatformViewController, IntrospectionAnchorID) -> Target?
4+
private var ancestorSelector: (IntrospectionPlatformViewController) -> Target?
55

66
@_spi(Internals)
77
public static var `default`: Self { .from(Target.self, selector: { $0 }) }
88

99
@_spi(Internals)
10-
public func overrideAncestorSelector(
11-
_ selector: @escaping (PlatformViewController, IntrospectionAnchorID) -> Target?
10+
public func withAncestorSelector(
11+
_ selector: @escaping (PlatformViewController) -> Target?
1212
) -> Self {
1313
var copy = self
1414
copy.ancestorSelector = selector
@@ -17,46 +17,22 @@ public struct IntrospectionSelector<Target: PlatformEntity> {
1717

1818
@_spi(Internals)
1919
public static func from<Entry: PlatformEntity>(_ entryType: Entry.Type, selector: @escaping (Entry) -> Target?) -> Self {
20-
.init { controller, scope, anchorID in
21-
guard let entity = { () -> (any PlatformEntity)? in
22-
if Entry.Base.self == PlatformView.self {
23-
#if canImport(UIKit)
24-
if let introspectionView = controller.viewIfLoaded {
25-
return introspectionView
26-
}
27-
#elseif canImport(AppKit)
28-
if controller.isViewLoaded {
29-
return controller.view
30-
}
31-
#endif
32-
} else if Entry.Base.self == PlatformViewController.self {
33-
return controller
34-
}
35-
return nil
36-
}() else {
37-
return nil
20+
.init(
21+
receiver: { controller, anchorID in
22+
controller.as(Entry.self)?.receiver(ofType: Entry.self, anchorID: anchorID).flatMap(selector)
23+
},
24+
ancestor: { controller in
25+
controller.as(Entry.self)?.ancestor(ofType: Entry.self).flatMap(selector)
3826
}
39-
if
40-
scope.contains(.receiver),
41-
let entry = entity.receiver(ofType: Entry.self, anchorID: anchorID),
42-
let target = selector(entry)
43-
{
44-
return target
45-
}
46-
if
47-
scope.contains(.ancestor),
48-
let entry = entity.ancestor(ofType: Entry.self),
49-
let target = selector(entry)
50-
{
51-
return target
52-
}
53-
return nil
54-
}
27+
)
5528
}
5629

57-
init(_ selector: @escaping (IntrospectionPlatformViewController, IntrospectionScope, IntrospectionAnchorID) -> Target?) {
58-
self.receiverSelector = { selector($0, .receiver, $1) }
59-
self.ancestorSelector = { selector($0, .ancestor, $1) }
30+
init(
31+
receiver: @escaping (IntrospectionPlatformViewController, IntrospectionAnchorID) -> Target?,
32+
ancestor: @escaping (IntrospectionPlatformViewController) -> Target?
33+
) {
34+
self.receiverSelector = receiver
35+
self.ancestorSelector = ancestor
6036
}
6137

6238
func callAsFunction(
@@ -72,10 +48,25 @@ public struct IntrospectionSelector<Target: PlatformEntity> {
7248
}
7349
if
7450
scope.contains(.ancestor),
75-
let target = ancestorSelector(controller, anchorID)
51+
let target = ancestorSelector(controller)
7652
{
7753
return target
7854
}
7955
return nil
8056
}
8157
}
58+
59+
extension PlatformViewController {
60+
func `as`<Entity: PlatformEntity>(_ entityType: Entity.Type) -> (any PlatformEntity)? {
61+
if Entity.Base.self == PlatformView.self {
62+
#if canImport(UIKit)
63+
return viewIfLoaded
64+
#elseif canImport(AppKit)
65+
return isViewLoaded ? view : nil
66+
#endif
67+
} else if Entity.Base.self == PlatformViewController.self {
68+
return self
69+
}
70+
return nil
71+
}
72+
}

0 commit comments

Comments
 (0)