1
1
@_spi ( Internals)
2
2
public struct IntrospectionSelector < Target: PlatformEntity > {
3
3
private var receiverSelector : ( IntrospectionPlatformViewController , IntrospectionAnchorID ) -> Target ?
4
- private var ancestorSelector : ( IntrospectionPlatformViewController , IntrospectionAnchorID ) -> Target ?
4
+ private var ancestorSelector : ( IntrospectionPlatformViewController ) -> Target ?
5
5
6
6
@_spi ( Internals)
7
7
public static var `default` : Self { . from( Target . self, selector: { $0 } ) }
8
8
9
9
@_spi ( Internals)
10
- public func overrideAncestorSelector (
11
- _ selector: @escaping ( PlatformViewController , IntrospectionAnchorID ) -> Target ?
10
+ public func withAncestorSelector (
11
+ _ selector: @escaping ( PlatformViewController ) -> Target ?
12
12
) -> Self {
13
13
var copy = self
14
14
copy. ancestorSelector = selector
@@ -17,46 +17,22 @@ public struct IntrospectionSelector<Target: PlatformEntity> {
17
17
18
18
@_spi ( Internals)
19
19
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)
38
26
}
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
+ )
55
28
}
56
29
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
60
36
}
61
37
62
38
func callAsFunction(
@@ -72,10 +48,25 @@ public struct IntrospectionSelector<Target: PlatformEntity> {
72
48
}
73
49
if
74
50
scope. contains ( . ancestor) ,
75
- let target = ancestorSelector ( controller, anchorID )
51
+ let target = ancestorSelector ( controller)
76
52
{
77
53
return target
78
54
}
79
55
return nil
80
56
}
81
57
}
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