|
1 | | -#if os(iOS) || os(macOS) || os(tvOS) |
| 1 | +#if os(iOS) || os(macOS) || os(tvOS) || os(visionOS) |
2 | 2 | #if os(macOS) |
3 | 3 | import Cocoa |
4 | 4 | #endif |
5 | 5 | import SceneKit |
6 | 6 | import SpriteKit |
7 | | - #if os(iOS) || os(tvOS) |
| 7 | + #if os(iOS) || os(tvOS) || os(visionOS) |
8 | 8 | import UIKit |
9 | 9 | #endif |
10 | | - #if os(iOS) || os(macOS) |
| 10 | + #if os(iOS) || os(macOS) || os(visionOS) |
11 | 11 | import WebKit |
12 | 12 | #endif |
13 | 13 |
|
14 | | - #if os(iOS) || os(tvOS) |
| 14 | + #if os(iOS) || os(tvOS) || os(visionOS) |
15 | 15 | public struct ViewImageConfig: Sendable { |
16 | 16 | public enum Orientation { |
17 | 17 | case landscape |
|
499 | 499 | size: .init(width: 3840, height: 2160), |
500 | 500 | traits: .init() |
501 | 501 | ) |
| 502 | + #elseif os(visionOS) |
| 503 | + public static let visionPro = ViewImageConfig( |
| 504 | + safeArea: .zero, |
| 505 | + size: .init(width: 2700, height: 1360), |
| 506 | + traits: .init() |
| 507 | + ) |
502 | 508 | #endif |
503 | 509 | } |
504 | 510 |
|
|
804 | 810 | ) |
805 | 811 | #elseif os(tvOS) |
806 | 812 | // TODO |
| 813 | + #elseif os(visionOS) |
| 814 | + public static let visionPro = UITraitCollection( |
| 815 | + traitsFrom: [ |
| 816 | + .init(userInterfaceIdiom: .vision), |
| 817 | + .init(horizontalSizeClass: .regular), |
| 818 | + .init(verticalSizeClass: .regular), |
| 819 | + .init(displayScale: 2.0), |
| 820 | + .init(userInterfaceStyle: .dark), |
| 821 | + .init(layoutDirection: .leftToRight), |
| 822 | + .init(forceTouchCapability: .unavailable), |
| 823 | + .init(preferredContentSizeCategory: .large), |
| 824 | + .init(accessibilityContrast: .normal), |
| 825 | + .init(userInterfaceLevel: .base), |
| 826 | + ] |
| 827 | + ) |
807 | 828 | #endif |
808 | 829 | } |
809 | 830 | #endif |
|
819 | 840 | imageView.frame = view.frame |
820 | 841 | #if os(macOS) |
821 | 842 | view.superview?.addSubview(imageView, positioned: .above, relativeTo: view) |
822 | | - #elseif os(iOS) || os(tvOS) |
| 843 | + #elseif os(iOS) || os(tvOS) || os(visionOS) |
823 | 844 | view.superview?.insertSubview(imageView, aboveSubview: view) |
824 | 845 | #endif |
825 | 846 | callback(imageView) |
|
850 | 871 | let cgImage = inWindow { skView.texture(from: skView.scene!)!.cgImage() } |
851 | 872 | #if os(macOS) |
852 | 873 | let image = Image(cgImage: cgImage, size: skView.bounds.size) |
853 | | - #elseif os(iOS) || os(tvOS) |
| 874 | + #elseif os(iOS) || os(tvOS) || os(visionOS) |
854 | 875 | let image = Image(cgImage: cgImage) |
855 | 876 | #endif |
856 | 877 | return Async(value: image) |
857 | 878 | } else { |
858 | 879 | fatalError("Taking SKView snapshots requires macOS 10.11 or greater") |
859 | 880 | } |
860 | 881 | } |
861 | | - #if os(iOS) || os(macOS) |
| 882 | + #if os(iOS) || os(macOS) || os(visionOS) |
862 | 883 | if let wkWebView = self as? WKWebView { |
863 | 884 | return Async<Image> { callback in |
864 | 885 | let work = { |
865 | | - if #available(iOS 11.0, macOS 10.13, *) { |
| 886 | + if #available(iOS 11.0, macOS 10.13, visionOS 1.0, *) { |
866 | 887 | inWindow { |
867 | 888 | guard wkWebView.frame.width != 0, wkWebView.frame.height != 0 else { |
868 | 889 | callback(Image()) |
869 | 890 | return |
870 | 891 | } |
871 | 892 | let configuration = WKSnapshotConfiguration() |
872 | | - if #available(iOS 13, macOS 10.15, *) { |
| 893 | + if #available(iOS 13, macOS 10.15, visionOS 1.0, *) { |
873 | 894 | configuration.afterScreenUpdates = false |
874 | 895 | } |
875 | 896 | wkWebView.takeSnapshot(with: configuration) { image, _ in |
|
879 | 900 | } else { |
880 | 901 | #if os(iOS) |
881 | 902 | fatalError("Taking WKWebView snapshots requires iOS 11.0 or greater") |
| 903 | + #elseif os(visionOS) |
| 904 | + fatalError("Taking WKWebView snapshots requires visionOS 1.0 or greater") |
882 | 905 | #elseif os(macOS) |
883 | 906 | fatalError("Taking WKWebView snapshots requires macOS 10.13 or greater") |
884 | 907 | #endif |
|
903 | 926 | #endif |
904 | 927 | return nil |
905 | 928 | } |
906 | | - #if os(iOS) || os(tvOS) |
| 929 | + #if os(iOS) || os(tvOS) || os(visionOS) |
907 | 930 | func asImage() -> Image { |
908 | 931 | let renderer = UIGraphicsImageRenderer(bounds: bounds) |
909 | 932 | return renderer.image { rendererContext in |
|
913 | 936 | #endif |
914 | 937 | } |
915 | 938 |
|
916 | | - #if os(iOS) || os(tvOS) |
| 939 | + #if os(iOS) || os(tvOS) || os(visionOS) |
917 | 940 | extension UIApplication { |
918 | 941 | static var sharedIfAvailable: UIApplication? { |
919 | 942 | let sharedSelector = NSSelectorFromString("sharedApplication") |
|
1011 | 1034 |
|
1012 | 1035 | func renderer(bounds: CGRect, for traits: UITraitCollection) -> UIGraphicsImageRenderer { |
1013 | 1036 | let renderer: UIGraphicsImageRenderer |
1014 | | - if #available(iOS 11.0, tvOS 11.0, *) { |
| 1037 | + if #available(iOS 11.0, tvOS 11.0, visionOS 1.0, *) { |
1015 | 1038 | renderer = UIGraphicsImageRenderer(bounds: bounds, format: .init(for: traits)) |
1016 | 1039 | } else { |
1017 | 1040 | renderer = UIGraphicsImageRenderer(bounds: bounds) |
|
1076 | 1099 |
|
1077 | 1100 | private func getKeyWindow() -> UIWindow? { |
1078 | 1101 | var window: UIWindow? |
1079 | | - if #available(iOS 13.0, *) { |
| 1102 | + if #available(iOS 13.0, visionOS 1.0, *) { |
1080 | 1103 | window = UIApplication.sharedIfAvailable?.windows.first { $0.isKeyWindow } |
1081 | 1104 | } else { |
1082 | | - window = UIApplication.sharedIfAvailable?.keyWindow |
| 1105 | + #if os(visionOS) |
| 1106 | + window = UIApplication.sharedIfAvailable?.windows.first { $0.isKeyWindow } |
| 1107 | + #else |
| 1108 | + window = UIApplication.sharedIfAvailable?.keyWindow |
| 1109 | + #endif |
1083 | 1110 | } |
1084 | 1111 | return window |
1085 | 1112 | } |
|
1112 | 1139 | fatalError("init(coder:) has not been implemented") |
1113 | 1140 | } |
1114 | 1141 |
|
1115 | | - @available(iOS 11.0, *) |
| 1142 | + @available(iOS 11.0, visionOS 1.0, *) |
1116 | 1143 | override var safeAreaInsets: UIEdgeInsets { |
1117 | 1144 | #if os(iOS) |
1118 | 1145 | let removeTopInset = |
|
0 commit comments