From 4690d820ccfe6c6c9f04f1c772079bf53b59d5a8 Mon Sep 17 00:00:00 2001 From: Jack Youstra Date: Tue, 1 Nov 2022 14:47:15 -0700 Subject: [PATCH] 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 --- Source/Device.generated.swift | 10 ++++++++++ Source/Device.swift.gyb | 10 ++++++++++ Tests/Tests.swift | 12 ++++++++++++ 3 files changed, 32 insertions(+) diff --git a/Source/Device.generated.swift b/Source/Device.generated.swift index 942c9470..6c3a1ae3 100644 --- a/Source/Device.generated.swift +++ b/Source/Device.generated.swift @@ -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) diff --git a/Source/Device.swift.gyb b/Source/Device.swift.gyb index 2f5128e9..c1538693 100644 --- a/Source/Device.swift.gyb +++ b/Source/Device.swift.gyb @@ -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) diff --git a/Tests/Tests.swift b/Tests/Tests.swift index 9e53ca03..8e95478c 100644 --- a/Tests/Tests.swift +++ b/Tests/Tests.swift @@ -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)