Skip to content

Commit 23b08e5

Browse files
authored
Selector overrides (#239)
1 parent 94ab006 commit 23b08e5

File tree

2 files changed

+62
-41
lines changed

2 files changed

+62
-41
lines changed

Sources/IntrospectionSelector.swift

Lines changed: 60 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,77 @@
11
@_spi(Internals)
22
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 }) }
65

76
@_spi(Internals)
87
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)
3414
}
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
4434
}
4535

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
4841
}
4942

5043
func callAsFunction(
5144
_ controller: IntrospectionPlatformViewController,
5245
_ scope: IntrospectionScope,
5346
_ anchorID: IntrospectionAnchorID
5447
) -> 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
5676
}
5777
}

Sources/IntrospectionView.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import SwiftUI
22

3-
typealias IntrospectionAnchorID = UUID
3+
@_spi(Internals)
4+
public typealias IntrospectionAnchorID = UUID
45

56
/// ⚓️
67
struct IntrospectionAnchorView: PlatformViewControllerRepresentable {

0 commit comments

Comments
 (0)