Skip to content

Commit

Permalink
Add canvas detection (#303)
Browse files Browse the repository at this point in the history
* Add canvas detection

* Move isCanvas to include it on tvOS and watchOS.

* Remove additional whitespace.

* Add optional return value to isCanvas and return nil if the current instance isn't the current device.

* Fix broken test on non iOS/iPadOS platforms.

Co-authored-by: Zandor Smith <[email protected]>
  • Loading branch information
JackYoustra and Zandor300 authored Nov 1, 2022
1 parent 53301cc commit 4690d82
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
10 changes: 10 additions & 0 deletions Source/Device.generated.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1055,6 +1055,16 @@ public enum Device {
}
#endif

/// Returns whether the current device is a SwiftUI preview canvas
public var isCanvas: Bool? {
guard isCurrent else { return nil }
#if DEBUG
return ProcessInfo.processInfo.environment["XCODE_RUNNING_FOR_PREVIEWS"] == "1"
#else
return false
#endif
}

/// All real devices (i.e. all devices except for all simulators)
public static var allRealDevices: [Device] {
#if os(iOS)
Expand Down
10 changes: 10 additions & 0 deletions Source/Device.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,16 @@ public enum Device {
}
#endif

/// Returns whether the current device is a SwiftUI preview canvas
public var isCanvas: Bool? {
guard isCurrent else { return nil }
#if DEBUG
return ProcessInfo.processInfo.environment["XCODE_RUNNING_FOR_PREVIEWS"] == "1"
#else
return false
#endif
}

/// All real devices (i.e. all devices except for all simulators)
public static var allRealDevices: [Device] {
#if os(iOS)
Expand Down
12 changes: 12 additions & 0 deletions Tests/Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@ class DeviceKitTests: XCTestCase {
|| device.description.contains("TV"))
}

func testIsCanvas() {
#if os(iOS)
let otherDevice: Device = device == .iPhone14Pro ? .iPhone14ProMax : .iPhone14Pro
#elseif os(tvOS)
let otherDevice: Device = device == .appleTVHD ? .appleTV4K : .appleTVHD
#elseif os(watchOS)
let otherDevice: Device = device == .appleWatchUltra ? .appleWatchSeries8_41mm : .appleWatchUltra
#endif
XCTAssertEqual(otherDevice.isCanvas, nil)
XCTAssertEqual(device.isCanvas, false)
}

#if os(iOS) || os(tvOS)
func testDeviceCPU() {
#if os(iOS)
Expand Down

0 comments on commit 4690d82

Please sign in to comment.