This commit is contained in:
David Roman 2023-05-22 19:36:44 +01:00
parent c87bb0e45f
commit 8a7af525a9
No known key found for this signature in database
GPG Key ID: 7058646EEFCB70A7
1 changed files with 9 additions and 9 deletions

View File

@ -35,12 +35,12 @@ extension View {
selector: { (view: PlatformView) in selector: { (view: PlatformView) in
switch scope ?? defaultScope { switch scope ?? defaultScope {
case .receiver: case .receiver:
return view.findReceiver(ofType: PlatformSpecificView.self) return view.receiver(ofType: PlatformSpecificView.self)
case .ancestor: case .ancestor:
return view.findAncestor(ofType: PlatformSpecificView.self) return view.ancestor(ofType: PlatformSpecificView.self)
case .receiverOrAncestor: case .receiverOrAncestor:
return view.findReceiver(ofType: PlatformSpecificView.self) return view.receiver(ofType: PlatformSpecificView.self)
?? view.findAncestor(ofType: PlatformSpecificView.self) ?? view.ancestor(ofType: PlatformSpecificView.self)
} }
}, },
customize: customize customize: customize
@ -65,7 +65,7 @@ extension PlatformView {
AnySequence(sequence(first: self, next: \.superview).dropFirst()) AnySequence(sequence(first: self, next: \.superview).dropFirst())
} }
func findReceiver<PlatformSpecificView: PlatformView>( func receiver<PlatformSpecificView: PlatformView>(
ofType type: PlatformSpecificView.Type ofType type: PlatformSpecificView.Type
) -> PlatformSpecificView? { ) -> PlatformSpecificView? {
guard let hostingView = self.hostingView else { guard let hostingView = self.hostingView else {
@ -73,7 +73,7 @@ extension PlatformView {
} }
// for container in superviews { // for container in superviews {
let children = hostingView.recursivelyFindSubviews(ofType: PlatformSpecificView.self) let children = hostingView.allSubviews(ofType: PlatformSpecificView.self)
for child in children { for child in children {
guard guard
@ -93,17 +93,17 @@ extension PlatformView {
return nil return nil
} }
func recursivelyFindSubviews<PlatformSpecificView: PlatformView>( func allSubviews<PlatformSpecificView: PlatformView>(
ofType type: PlatformSpecificView.Type ofType type: PlatformSpecificView.Type
) -> [PlatformSpecificView] { ) -> [PlatformSpecificView] {
var result = self.subviews.compactMap { $0 as? PlatformSpecificView } var result = self.subviews.compactMap { $0 as? PlatformSpecificView }
for subview in self.subviews { for subview in self.subviews {
result.append(contentsOf: subview.recursivelyFindSubviews(ofType: type)) result.append(contentsOf: subview.allSubviews(ofType: type))
} }
return result return result
} }
func findAncestor<PlatformSpecificView: PlatformView>( func ancestor<PlatformSpecificView: PlatformView>(
ofType type: PlatformSpecificView.Type ofType type: PlatformSpecificView.Type
) -> PlatformSpecificView? { ) -> PlatformSpecificView? {
self.superviews.lazy.compactMap { $0 as? PlatformSpecificView }.first self.superviews.lazy.compactMap { $0 as? PlatformSpecificView }.first