Skip to content

Commit 4690d82

Browse files
Add canvas detection (#303)
* 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 <info@zsinfo.nl>
1 parent 53301cc commit 4690d82

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

Source/Device.generated.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,6 +1055,16 @@ public enum Device {
10551055
}
10561056
#endif
10571057

1058+
/// Returns whether the current device is a SwiftUI preview canvas
1059+
public var isCanvas: Bool? {
1060+
guard isCurrent else { return nil }
1061+
#if DEBUG
1062+
return ProcessInfo.processInfo.environment["XCODE_RUNNING_FOR_PREVIEWS"] == "1"
1063+
#else
1064+
return false
1065+
#endif
1066+
}
1067+
10581068
/// All real devices (i.e. all devices except for all simulators)
10591069
public static var allRealDevices: [Device] {
10601070
#if os(iOS)

Source/Device.swift.gyb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,16 @@ public enum Device {
680680
}
681681
#endif
682682

683+
/// Returns whether the current device is a SwiftUI preview canvas
684+
public var isCanvas: Bool? {
685+
guard isCurrent else { return nil }
686+
#if DEBUG
687+
return ProcessInfo.processInfo.environment["XCODE_RUNNING_FOR_PREVIEWS"] == "1"
688+
#else
689+
return false
690+
#endif
691+
}
692+
683693
/// All real devices (i.e. all devices except for all simulators)
684694
public static var allRealDevices: [Device] {
685695
#if os(iOS)

Tests/Tests.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,18 @@ class DeviceKitTests: XCTestCase {
3232
|| device.description.contains("TV"))
3333
}
3434

35+
func testIsCanvas() {
36+
#if os(iOS)
37+
let otherDevice: Device = device == .iPhone14Pro ? .iPhone14ProMax : .iPhone14Pro
38+
#elseif os(tvOS)
39+
let otherDevice: Device = device == .appleTVHD ? .appleTV4K : .appleTVHD
40+
#elseif os(watchOS)
41+
let otherDevice: Device = device == .appleWatchUltra ? .appleWatchSeries8_41mm : .appleWatchUltra
42+
#endif
43+
XCTAssertEqual(otherDevice.isCanvas, nil)
44+
XCTAssertEqual(device.isCanvas, false)
45+
}
46+
3547
#if os(iOS) || os(tvOS)
3648
func testDeviceCPU() {
3749
#if os(iOS)

0 commit comments

Comments
 (0)