Skip to content

Add window introspection #269

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jun 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Changelog

## master

- Added: window introspection (#269)
- Added: `.sheet` introspection (#268)
- Added: `.fullScreenCover` introspection (#268)
- Added: `.popover` introspection (#268)
Expand Down
8 changes: 4 additions & 4 deletions Examples/Showcase/Showcase.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@

/* Begin PBXBuildFile section */
D53071F729983CEF00F1936C /* App.swift in Sources */ = {isa = PBXBuildFile; fileRef = D53071F629983CEF00F1936C /* App.swift */; };
D53071F929983CEF00F1936C /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D53071F829983CEF00F1936C /* ContentView.swift */; };
D53071F929983CEF00F1936C /* AppView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D53071F829983CEF00F1936C /* AppView.swift */; };
D5B829752999738200920EBD /* Helpers.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5B829742999738200920EBD /* Helpers.swift */; };
D5E3180329C132B6005847DC /* SwiftUIIntrospect in Frameworks */ = {isa = PBXBuildFile; productRef = D5E3180229C132B6005847DC /* SwiftUIIntrospect */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
D53071F329983CEF00F1936C /* Showcase.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Showcase.app; sourceTree = BUILT_PRODUCTS_DIR; };
D53071F629983CEF00F1936C /* App.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = App.swift; sourceTree = "<group>"; };
D53071F829983CEF00F1936C /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = "<group>"; };
D53071F829983CEF00F1936C /* AppView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppView.swift; sourceTree = "<group>"; };
D530720429983D9300F1936C /* Showcase.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = Showcase.entitlements; sourceTree = "<group>"; };
D5B829742999738200920EBD /* Helpers.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Helpers.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */
Expand Down Expand Up @@ -55,7 +55,7 @@
children = (
D530720429983D9300F1936C /* Showcase.entitlements */,
D53071F629983CEF00F1936C /* App.swift */,
D53071F829983CEF00F1936C /* ContentView.swift */,
D53071F829983CEF00F1936C /* AppView.swift */,
D5B829742999738200920EBD /* Helpers.swift */,
);
path = Showcase;
Expand Down Expand Up @@ -139,7 +139,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
D53071F929983CEF00F1936C /* ContentView.swift in Sources */,
D53071F929983CEF00F1936C /* AppView.swift in Sources */,
D5B829752999738200920EBD /* Helpers.swift in Sources */,
D53071F729983CEF00F1936C /* App.swift in Sources */,
);
Expand Down
4 changes: 2 additions & 2 deletions Examples/Showcase/Showcase/App.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ final class AppDelegate: UIResponder, UIApplicationDelegate {

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
window?.rootViewController = UIHostingController(rootView: ContentView())
window?.rootViewController = UIHostingController(rootView: AppView())
window?.makeKeyAndVisible()
return true
}
Expand All @@ -18,7 +18,7 @@ final class AppDelegate: UIResponder, UIApplicationDelegate {
struct App: SwiftUI.App {
var body: some Scene {
WindowGroup {
ContentView()
AppView()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
import SwiftUI
import SwiftUIIntrospect

struct AppView: View {
var body: some View {
ContentView()
#if os(iOS) || os(tvOS)
.introspect(.window, on: .iOS(.v13, .v14, .v15, .v16, .v17), .tvOS(.v13, .v14, .v15, .v16, .v17)) { window in
window.backgroundColor = .brown
}
#elseif os(macOS)
.introspect(.window, on: .macOS(.v10_15, .v11, .v12, .v13, .v14)) { window in
window.backgroundColor = .lightGray
}
#endif
}
}

struct ContentView: View {
@State var selection = 0

Expand Down
11 changes: 11 additions & 0 deletions Sources/Introspect.swift
Original file line number Diff line number Diff line change
Expand Up @@ -178,4 +178,15 @@ extension UIPresentationController: PlatformEntity {
@_spi(Internals)
public func isDescendant(of other: UIPresentationController) -> Bool { false }
}
#elseif canImport(AppKit)
extension NSWindow: PlatformEntity {
@_spi(Internals)
public var ancestor: NSWindow? { nil }

@_spi(Internals)
public var descendants: [NSWindow] { [] }

@_spi(Internals)
public func isDescendant(of other: NSWindow) -> Bool { false }
}
#endif
86 changes: 86 additions & 0 deletions Sources/ViewTypes/Window.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import SwiftUI

/// An abstract representation of a view's window in SwiftUI.
///
/// ### iOS
///
/// ```swift
/// struct ContentView: View {
/// var body: some View {
/// Text("Content")
/// .introspect(.window, on: .iOS(.v13, .v14, .v15, .v16, .v17)) {
/// print(type(of: $0)) // UIWindow
/// }
/// }
/// }
/// ```
///
/// ### tvOS
///
/// ```swift
/// struct ContentView: View {
/// var body: some View {
/// Text("Content")
/// .introspect(.window, on: .tvOS(.v13, .v14, .v15, .v16, .v17)) {
/// print(type(of: $0)) // UIWindow
/// }
/// }
/// }
/// ```
///
/// ### macOS
///
/// ```swift
/// struct ContentView: View {
/// var body: some View {
/// Text("Content")
/// .introspect(.window, on: .macOS(.v10_15, .v11, .v12, .v13, .v14)) {
/// print(type(of: $0)) // NSWindow
/// }
/// }
/// }
/// ```
///
public struct WindowType: IntrospectableViewType {}

extension IntrospectableViewType where Self == WindowType {
public static var window: Self { .init() }
}

#if canImport(UIKit)
extension iOSViewVersion<WindowType, UIWindow> {
public static let v13 = Self(for: .v13, selector: selector)
public static let v14 = Self(for: .v14, selector: selector)
public static let v15 = Self(for: .v15, selector: selector)
public static let v16 = Self(for: .v16, selector: selector)
public static let v17 = Self(for: .v17, selector: selector)

private static var selector: IntrospectionSelector<UIWindow> {
.from(UIView.self, selector: \.window)
}
}

extension tvOSViewVersion<WindowType, UIWindow> {
public static let v13 = Self(for: .v13, selector: selector)
public static let v14 = Self(for: .v14, selector: selector)
public static let v15 = Self(for: .v15, selector: selector)
public static let v16 = Self(for: .v16, selector: selector)
public static let v17 = Self(for: .v17, selector: selector)

private static var selector: IntrospectionSelector<UIWindow> {
.from(UIView.self, selector: \.window)
}
}
#elseif canImport(AppKit)
extension macOSViewVersion<WindowType, NSWindow> {
public static let v10_15 = Self(for: .v10_15, selector: selector)
public static let v11 = Self(for: .v11, selector: selector)
public static let v12 = Self(for: .v12, selector: selector)
public static let v13 = Self(for: .v13, selector: selector)
public static let v14 = Self(for: .v14, selector: selector)

private static var selector: IntrospectionSelector<NSWindow> {
.from(NSView.self, selector: \.window)
}
}
#endif
6 changes: 6 additions & 0 deletions Tests/Tests.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@
D50E2F892A2B9F6600BAFB03 /* TextFieldTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5B67B832A0D318F007D5D9B /* TextFieldTests.swift */; };
D50E2F8B2A2B9F6600BAFB03 /* SwiftUIIntrospect in Frameworks */ = {isa = PBXBuildFile; productRef = D50E2F5C2A2B9F6600BAFB03 /* SwiftUIIntrospect */; };
D50FFE8E2A17E2A400C32641 /* ScrollViewTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D50FFE8D2A17E2A400C32641 /* ScrollViewTests.swift */; };
D534D4DC2A4A596200218BFB /* WindowTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D534D4DB2A4A596200218BFB /* WindowTests.swift */; };
D534D4DD2A4A596200218BFB /* WindowTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D534D4DB2A4A596200218BFB /* WindowTests.swift */; };
D55F448D2A1FF209003381E4 /* ListTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D55F448C2A1FF209003381E4 /* ListTests.swift */; };
D568532C2A49DBB10039A99F /* SignInWithAppleButtonTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D568532B2A49DBB10039A99F /* SignInWithAppleButtonTests.swift */; };
D57506782A27BBBD00A628E4 /* PickerWithSegmentedStyleTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D57506772A27BBBD00A628E4 /* PickerWithSegmentedStyleTests.swift */; };
Expand Down Expand Up @@ -142,6 +144,7 @@
D50E2F572A2B9EFB00BAFB03 /* LegacyTestsHostApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LegacyTestsHostApp.swift; sourceTree = "<group>"; };
D50E2F902A2B9F6600BAFB03 /* LegacyTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = LegacyTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
D50FFE8D2A17E2A400C32641 /* ScrollViewTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ScrollViewTests.swift; sourceTree = "<group>"; };
D534D4DB2A4A596200218BFB /* WindowTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WindowTests.swift; sourceTree = "<group>"; };
D55F448C2A1FF209003381E4 /* ListTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ListTests.swift; sourceTree = "<group>"; };
D568532B2A49DBB10039A99F /* SignInWithAppleButtonTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SignInWithAppleButtonTests.swift; sourceTree = "<group>"; };
D57506772A27BBBD00A628E4 /* PickerWithSegmentedStyleTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PickerWithSegmentedStyleTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -288,6 +291,7 @@
D575068B2A27D40500A628E4 /* ToggleWithSwitchStyleTests.swift */,
D503B2AB2A49BFE300027F5F /* VideoPlayerTests.swift */,
D58119C52A227E930081F853 /* ViewTests.swift */,
D534D4DB2A4A596200218BFB /* WindowTests.swift */,
);
path = ViewTypes;
sourceTree = "<group>";
Expand Down Expand Up @@ -516,6 +520,7 @@
D50E2F632A2B9F6600BAFB03 /* TabViewTests.swift in Sources */,
D50E2F642A2B9F6600BAFB03 /* ListWithInsetStyleTests.swift in Sources */,
D50E2F652A2B9F6600BAFB03 /* PickerWithMenuStyleTests.swift in Sources */,
D534D4DD2A4A596200218BFB /* WindowTests.swift in Sources */,
D50E2F662A2B9F6600BAFB03 /* DatePickerWithWheelStyleTests.swift in Sources */,
D50E2F672A2B9F6600BAFB03 /* ListWithInsetGroupedStyleTests.swift in Sources */,
D5ADFAD32A4A4649009494FD /* SignInWithAppleButtonTests.swift in Sources */,
Expand Down Expand Up @@ -585,6 +590,7 @@
D568532C2A49DBB10039A99F /* SignInWithAppleButtonTests.swift in Sources */,
D575068A2A27CE7900A628E4 /* FormWithGroupedStyleTests.swift in Sources */,
D575067C2A27C24600A628E4 /* ListWithPlainStyleTests.swift in Sources */,
D534D4DC2A4A596200218BFB /* WindowTests.swift in Sources */,
D58119CA2A239BAC0081F853 /* TextEditorTests.swift in Sources */,
D57506842A27C8D400A628E4 /* ListWithSidebarStyleTests.swift in Sources */,
D575069E2A27F80E00A628E4 /* ProgressViewWithLinearStyleTests.swift in Sources */,
Expand Down
44 changes: 44 additions & 0 deletions Tests/Tests/ViewTypes/WindowTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import SwiftUI
import SwiftUIIntrospect
import XCTest

final class WindowTests: XCTestCase {
#if canImport(UIKit)
typealias PlatformWindow = UIWindow
#elseif canImport(AppKit)
typealias PlatformWindow = NSWindow
#endif

func testWindow() {
XCTAssertViewIntrospection(of: PlatformWindow.self) { spies in
let spy0 = spies[0]
let spy1 = spies[1]
let spy2 = spies[2]

VStack {
Image(systemName: "scribble")
#if os(iOS) || os(tvOS)
.introspect(.window, on: .iOS(.v13, .v14, .v15, .v16, .v17), .tvOS(.v13, .v14, .v15, .v16, .v17), customize: spy0)
#elseif os(macOS)
.introspect(.window, on: .macOS(.v10_15, .v11, .v12, .v13, .v14), customize: spy0)
#endif

Text("Text")
#if os(iOS) || os(tvOS)
.introspect(.window, on: .iOS(.v13, .v14, .v15, .v16, .v17), .tvOS(.v13, .v14, .v15, .v16, .v17), customize: spy1)
#elseif os(macOS)
.introspect(.window, on: .macOS(.v10_15, .v11, .v12, .v13, .v14), customize: spy1)
#endif
}
#if os(iOS) || os(tvOS)
.introspect(.window, on: .iOS(.v13, .v14, .v15, .v16, .v17), .tvOS(.v13, .v14, .v15, .v16, .v17), customize: spy2)
#elseif os(macOS)
.introspect(.window, on: .macOS(.v10_15, .v11, .v12, .v13, .v14), customize: spy2)
#endif
} extraAssertions: {
XCTAssertIdentical($0[safe: 0], $0[safe: 1])
XCTAssertIdentical($0[safe: 0], $0[safe: 2])
XCTAssertIdentical($0[safe: 1], $0[safe: 2])
}
}
}
1 change: 1 addition & 0 deletions docs/SwiftUIIntrospect.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ Introspection
- [`Toggle` with `switch` style](https://swiftpackageindex.com/siteline/swiftui-introspect/master/documentation/swiftuiintrospect/togglewithswitchstyletype)
- [`VideoPlayer`](https://swiftpackageindex.com/siteline/swiftui-introspect/master/documentation/swiftuiintrospect/videoplayertype)
- [`View`](https://swiftpackageindex.com/siteline/swiftui-introspect/master/documentation/swiftuiintrospect/viewtype)
- [`Window`](https://swiftpackageindex.com/siteline/swiftui-introspect/master/documentation/swiftuiintrospect/windowtype)

**Missing an element?** Please [create an issue](https://github.com/timbersoftware/SwiftUI-Introspect/issues). As a temporary solution, you can [implement your own introspectable view type](#implement-your-own-view-type).

Expand Down