Skip to content

Commit e0536ff

Browse files
steipetedavdroman
andauthored
Add support for NSplitView (#100)
Co-authored-by: David Roman <[email protected]>
1 parent a1a97a8 commit e0536ff

File tree

3 files changed

+47
-20
lines changed

3 files changed

+47
-20
lines changed

Introspect/ViewExtensions.swift

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,20 +45,20 @@ extension View {
4545

4646
/// Finds a `UISplitViewController` from a `SwiftUI.NavigationView` with style `DoubleColumnNavigationViewStyle`.
4747
public func introspectSplitViewController(customize: @escaping (UISplitViewController) -> ()) -> some View {
48-
inject(UIKitIntrospectionViewController(
49-
selector: { introspectionViewController in
50-
51-
// Search in ancestors
52-
if let splitViewController = introspectionViewController.splitViewController {
53-
return splitViewController
54-
}
55-
56-
// Search in siblings
57-
return Introspect.previousSibling(containing: UISplitViewController.self, from: introspectionViewController)
58-
},
59-
customize: customize
60-
))
61-
}
48+
inject(UIKitIntrospectionViewController(
49+
selector: { introspectionViewController in
50+
51+
// Search in ancestors
52+
if let splitViewController = introspectionViewController.splitViewController {
53+
return splitViewController
54+
}
55+
56+
// Search in siblings
57+
return Introspect.previousSibling(containing: UISplitViewController.self, from: introspectionViewController)
58+
},
59+
customize: customize
60+
))
61+
}
6262

6363
/// Finds the containing `UIViewController` of a SwiftUI view.
6464
public func introspectViewController(customize: @escaping (UIViewController) -> ()) -> some View {
@@ -202,6 +202,11 @@ extension View {
202202
customize: customize
203203
))
204204
}
205+
206+
/// Finds a `NSSplitViewController` from a `SwiftUI.NavigationView` with style `DoubleColumnNavigationViewStyle`.
207+
public func introspectSplitView(customize: @escaping (NSSplitView) -> ()) -> some View {
208+
return introspect(selector: TargetViewSelector.ancestorOrSiblingContaining, customize: customize)
209+
}
205210

206211
/// Finds a `NSTableView` from a `SwiftUI.List`, or `SwiftUI.List` child.
207212
public func introspectTableView(customize: @escaping (NSTableView) -> ()) -> some View {

IntrospectTests/AppKitTests.swift

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,20 @@ enum TestUtils {
2323
}
2424
}
2525

26+
private struct SplitNavigationTestView: View {
27+
let spy: () -> Void
28+
var body: some View {
29+
NavigationView {
30+
VStack {
31+
EmptyView()
32+
}
33+
}
34+
.introspectSplitView { splitView in
35+
self.spy()
36+
}
37+
}
38+
}
39+
2640
private struct ListTestView: View {
2741

2842
let spy1: () -> Void
@@ -242,7 +256,17 @@ private struct MapTestView: View {
242256
}
243257

244258
class AppKitTests: XCTestCase {
245-
259+
260+
func testSplitNavigation() {
261+
262+
let expectation = XCTestExpectation()
263+
let view = SplitNavigationTestView(spy: {
264+
expectation.fulfill()
265+
})
266+
TestUtils.present(view: view)
267+
wait(for: [expectation], timeout: TestUtils.Constants.timeout)
268+
}
269+
246270
func testList() {
247271
if #available(macOS 11, *) {
248272
return // TODO: verify whether List still uses NSTableView under the hood in macOS >=11

README.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ SwiftUI | UIKit | AppKit | Introspect
5151
--- | --- | --- | ---
5252
NavigationView (StackNavigationViewStyle) | UINavigationController | _N/A_ | `.introspectNavigationController()`
5353
NavigationView (DoubleColumnNavigationViewStyle) | UISplitViewController | _N/A_ | `.introspectSplitViewController()`
54+
NavigationView (DoubleColumnNavigationViewStyle) | _N/A_ | NSSplitView | `.introspectSplitView()`
5455
_Any embedded view_ | UIViewController | _N/A_ | `.introspectViewController()`
5556
ScrollView | UIScrollView | NSScrollView | `.introspectScrollView()`
5657
List | UITableView | NSTableView | `.introspectTableView()`
@@ -188,13 +189,10 @@ $ bundle exec pod trunk push .
188189

189190

190191
<!-- References -->
191-
[CircleCI_Status]: https://circleci.com/gh/siteline/SwiftUI-Introspect.svg?style=svg&circle-token=6f995f204d4d417d31f79e7257f6e1ecf430ae07
192192

193-
[CircleCI_URL]: https://circleci.com/gh/siteline/SwiftUI-Introspect
193+
[GithubCI_Status]: https://github.com/siteline/swiftui-introspect/actions/workflows/ci.yml/badge.svg?branch=master
194194

195-
[GithubCI_Status]: https://github.com/siteline/swiftui-introspect/actions/workflows/build-and-test.yml/badge.svg?branch=master
196-
197-
[GithubCI_URL]: https://github.com/siteline/SwiftUI-Introspect/actions/workflows/build-and-test.yml
195+
[GithubCI_URL]: https://github.com/siteline/SwiftUI-Introspect/actions/workflows/ci.yml
198196

199197
[Siteline_Badge]: https://badgen.net/badge/Built%20by/Siteline/blue?icon=https://uploads-ssl.webflow.com/5f4513afbbfc64c4777fcccf/5f525b122370d681879e170e_siteline-icon.svg
200198

0 commit comments

Comments
 (0)