-
-
Save phillipcaudell/60d5e8c8fe1848b47576356a49719e46 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
public enum UserInterfaceIdiom: Hashable { | |
case unspecified | |
case phone | |
case pad | |
case mac | |
case carPlay | |
case tv | |
case vision | |
} | |
extension EnvironmentValues { | |
struct UserInterfaceIdiomKey: EnvironmentKey { | |
#if os(iOS) | |
static var defaultValue: UserInterfaceIdiom = UIDevice.current.userInterfaceIdiom.idiom | |
#elseif os(macOS) | |
static var defaultValue: UserInterfaceIdiom = .mac | |
#elseif os(tvOS) | |
static var defaultValue: UserInterfaceIdiom = .tv | |
#elseif os(visionOS) | |
static var defaultValue: UserInterfaceIdiom = .vision | |
#endif | |
} | |
public var userInterfaceIdiom: UserInterfaceIdiom { | |
get { self[UserInterfaceIdiomKey.self] } | |
} | |
} | |
#if os(iOS) | |
extension UIUserInterfaceIdiom { | |
var idiom: UserInterfaceIdiom { | |
switch self { | |
case .unspecified: | |
return .unspecified | |
case .phone: | |
return .phone | |
case .pad: | |
return .pad | |
case .tv: | |
return .tv | |
case .carPlay: | |
return .carPlay | |
case .mac: | |
return .mac | |
case .vision: | |
return .vision | |
@unknown default: | |
return .unspecified | |
} | |
} | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment