Skip to content

Instantly share code, notes, and snippets.

@phillipcaudell
Created February 19, 2024 09:39
Show Gist options
  • Save phillipcaudell/60d5e8c8fe1848b47576356a49719e46 to your computer and use it in GitHub Desktop.
Save phillipcaudell/60d5e8c8fe1848b47576356a49719e46 to your computer and use it in GitHub Desktop.
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