|
1 | 1 | @_spi(Internals) |
2 | 2 | public struct IntrospectionSelector<Target: PlatformEntity> { |
3 | | - private let selector: (IntrospectionPlatformViewController, IntrospectionScope, IntrospectionAnchorID) -> Target? |
4 | | - |
5 | | - static var `default`: Self { .from(Target.self, selector: { $0 }) } |
| 3 | + @_spi(Internals) |
| 4 | + public static var `default`: Self { .from(Target.self, selector: { $0 }) } |
6 | 5 |
|
7 | 6 | @_spi(Internals) |
8 | 7 | public static func from<Entry: PlatformEntity>(_ entryType: Entry.Type, selector: @escaping (Entry) -> Target?) -> Self { |
9 | | - .init { controller, scope, anchorID in |
10 | | - guard let entity = { () -> (any PlatformEntity)? in |
11 | | - if Entry.Base.self == PlatformView.self { |
12 | | - #if canImport(UIKit) |
13 | | - if let introspectionView = controller.viewIfLoaded { |
14 | | - return introspectionView |
15 | | - } |
16 | | - #elseif canImport(AppKit) |
17 | | - if controller.isViewLoaded { |
18 | | - return controller.view |
19 | | - } |
20 | | - #endif |
21 | | - } else if Entry.Base.self == PlatformViewController.self { |
22 | | - return controller |
23 | | - } |
24 | | - return nil |
25 | | - }() else { |
26 | | - return nil |
27 | | - } |
28 | | - if |
29 | | - scope.contains(.receiver), |
30 | | - let entry = entity.receiver(ofType: Entry.self, anchorID: anchorID), |
31 | | - let target = selector(entry) |
32 | | - { |
33 | | - return target |
| 8 | + .init( |
| 9 | + receiverSelector: { controller, anchorID in |
| 10 | + controller.as(Entry.self)?.receiver(ofType: Entry.self, anchorID: anchorID).flatMap(selector) |
| 11 | + }, |
| 12 | + ancestorSelector: { controller in |
| 13 | + controller.as(Entry.self)?.ancestor(ofType: Entry.self).flatMap(selector) |
34 | 14 | } |
35 | | - if |
36 | | - scope.contains(.ancestor), |
37 | | - let entry = entity.ancestor(ofType: Entry.self), |
38 | | - let target = selector(entry) |
39 | | - { |
40 | | - return target |
41 | | - } |
42 | | - return nil |
43 | | - } |
| 15 | + ) |
| 16 | + } |
| 17 | + |
| 18 | + private var receiverSelector: (IntrospectionPlatformViewController, IntrospectionAnchorID) -> Target? |
| 19 | + private var ancestorSelector: (IntrospectionPlatformViewController) -> Target? |
| 20 | + |
| 21 | + private init( |
| 22 | + receiverSelector: @escaping (IntrospectionPlatformViewController, IntrospectionAnchorID) -> Target?, |
| 23 | + ancestorSelector: @escaping (IntrospectionPlatformViewController) -> Target? |
| 24 | + ) { |
| 25 | + self.receiverSelector = receiverSelector |
| 26 | + self.ancestorSelector = ancestorSelector |
| 27 | + } |
| 28 | + |
| 29 | + @_spi(Internals) |
| 30 | + public func withReceiverSelector(_ selector: @escaping (PlatformViewController, IntrospectionAnchorID) -> Target?) -> Self { |
| 31 | + var copy = self |
| 32 | + copy.receiverSelector = selector |
| 33 | + return copy |
44 | 34 | } |
45 | 35 |
|
46 | | - init(_ selector: @escaping (IntrospectionPlatformViewController, IntrospectionScope, IntrospectionAnchorID) -> Target?) { |
47 | | - self.selector = selector |
| 36 | + @_spi(Internals) |
| 37 | + public func withAncestorSelector(_ selector: @escaping (PlatformViewController) -> Target?) -> Self { |
| 38 | + var copy = self |
| 39 | + copy.ancestorSelector = selector |
| 40 | + return copy |
48 | 41 | } |
49 | 42 |
|
50 | 43 | func callAsFunction( |
51 | 44 | _ controller: IntrospectionPlatformViewController, |
52 | 45 | _ scope: IntrospectionScope, |
53 | 46 | _ anchorID: IntrospectionAnchorID |
54 | 47 | ) -> Target? { |
55 | | - selector(controller, scope, anchorID) |
| 48 | + if |
| 49 | + scope.contains(.receiver), |
| 50 | + let target = receiverSelector(controller, anchorID) |
| 51 | + { |
| 52 | + return target |
| 53 | + } |
| 54 | + if |
| 55 | + scope.contains(.ancestor), |
| 56 | + let target = ancestorSelector(controller) |
| 57 | + { |
| 58 | + return target |
| 59 | + } |
| 60 | + return nil |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +extension PlatformViewController { |
| 65 | + func `as`<Entity: PlatformEntity>(_ entityType: Entity.Type) -> (any PlatformEntity)? { |
| 66 | + if Entity.Base.self == PlatformView.self { |
| 67 | + #if canImport(UIKit) |
| 68 | + return viewIfLoaded |
| 69 | + #elseif canImport(AppKit) |
| 70 | + return isViewLoaded ? view : nil |
| 71 | + #endif |
| 72 | + } else if Entity.Base.self == PlatformViewController.self { |
| 73 | + return self |
| 74 | + } |
| 75 | + return nil |
56 | 76 | } |
57 | 77 | } |
0 commit comments