|
| 1 | +import Foundation |
| 2 | +import CoreGraphics |
| 3 | + |
| 4 | +// Rotation between an app's interface space and the device's native (portrait-up) space, shared by |
| 5 | +// synthesized dispatch (forward) and capture (inverse). Rationale and measurements: ADR 0004 |
| 6 | +// "the coordinate space of a captured subtree" and contracts/fixtures/window-coordinate-space.json. |
| 7 | + |
| 8 | +public enum RunnerInterfaceOrientation { |
| 9 | + public static let unknown = 0 |
| 10 | + public static let portrait = 1 |
| 11 | + public static let portraitUpsideDown = 2 |
| 12 | + public static let landscapeRight = 3 |
| 13 | + public static let landscapeLeft = 4 |
| 14 | +} |
| 15 | + |
| 16 | +public enum CoordinateSpaceRotation { |
| 17 | + public static func native( |
| 18 | + point: CGPoint, |
| 19 | + in frame: CGRect, |
| 20 | + interfaceOrientation: Int |
| 21 | + ) -> CGPoint { |
| 22 | + let localX = Double(point.x) - Double(frame.minX) |
| 23 | + let localY = Double(point.y) - Double(frame.minY) |
| 24 | + let width = Double(frame.width) |
| 25 | + let height = Double(frame.height) |
| 26 | + switch interfaceOrientation { |
| 27 | + case RunnerInterfaceOrientation.landscapeRight: |
| 28 | + return CGPoint(x: height - localY, y: localX) |
| 29 | + case RunnerInterfaceOrientation.landscapeLeft: |
| 30 | + return CGPoint(x: localY, y: width - localX) |
| 31 | + case RunnerInterfaceOrientation.portraitUpsideDown: |
| 32 | + return CGPoint(x: width - localX, y: height - localY) |
| 33 | + default: |
| 34 | + return CGPoint(x: localX, y: localY) |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + public static func native(vector: CGVector, interfaceOrientation: Int) -> CGVector { |
| 39 | + switch interfaceOrientation { |
| 40 | + case RunnerInterfaceOrientation.landscapeRight: |
| 41 | + return CGVector(dx: -vector.dy, dy: vector.dx) |
| 42 | + case RunnerInterfaceOrientation.landscapeLeft: |
| 43 | + return CGVector(dx: vector.dy, dy: -vector.dx) |
| 44 | + case RunnerInterfaceOrientation.portraitUpsideDown: |
| 45 | + return CGVector(dx: -vector.dx, dy: -vector.dy) |
| 46 | + default: |
| 47 | + return vector |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + public static func oriented( |
| 52 | + point: CGPoint, |
| 53 | + in frame: CGRect, |
| 54 | + interfaceOrientation: Int |
| 55 | + ) -> CGPoint { |
| 56 | + let width = Double(frame.width) |
| 57 | + let height = Double(frame.height) |
| 58 | + let localX: Double |
| 59 | + let localY: Double |
| 60 | + switch interfaceOrientation { |
| 61 | + case RunnerInterfaceOrientation.landscapeRight: |
| 62 | + localX = Double(point.y) |
| 63 | + localY = height - Double(point.x) |
| 64 | + case RunnerInterfaceOrientation.landscapeLeft: |
| 65 | + localX = width - Double(point.y) |
| 66 | + localY = Double(point.x) |
| 67 | + case RunnerInterfaceOrientation.portraitUpsideDown: |
| 68 | + localX = width - Double(point.x) |
| 69 | + localY = height - Double(point.y) |
| 70 | + default: |
| 71 | + localX = Double(point.x) |
| 72 | + localY = Double(point.y) |
| 73 | + } |
| 74 | + return CGPoint(x: localX + Double(frame.minX), y: localY + Double(frame.minY)) |
| 75 | + } |
| 76 | + |
| 77 | + public static func oriented( |
| 78 | + rect: CGRect, |
| 79 | + in frame: CGRect, |
| 80 | + interfaceOrientation: Int |
| 81 | + ) -> CGRect { |
| 82 | + let leading = oriented( |
| 83 | + point: CGPoint(x: rect.minX, y: rect.minY), |
| 84 | + in: frame, |
| 85 | + interfaceOrientation: interfaceOrientation |
| 86 | + ) |
| 87 | + let trailing = oriented( |
| 88 | + point: CGPoint(x: rect.maxX, y: rect.maxY), |
| 89 | + in: frame, |
| 90 | + interfaceOrientation: interfaceOrientation |
| 91 | + ) |
| 92 | + return CGRect( |
| 93 | + x: min(leading.x, trailing.x), |
| 94 | + y: min(leading.y, trailing.y), |
| 95 | + width: abs(trailing.x - leading.x), |
| 96 | + height: abs(trailing.y - leading.y) |
| 97 | + ) |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +public enum SnapshotGeometrySpace: Equatable { |
| 102 | + case appOrientation |
| 103 | + case deviceNative(appFrame: CGRect, interfaceOrientation: Int) |
| 104 | + |
| 105 | + public static let quarterTurnTolerance: Double = 1 |
| 106 | + |
| 107 | + public func orientedFrame(of reportedFrame: CGRect) -> CGRect { |
| 108 | + switch self { |
| 109 | + case .appOrientation: |
| 110 | + return reportedFrame |
| 111 | + case .deviceNative(let appFrame, let interfaceOrientation): |
| 112 | + return CoordinateSpaceRotation.oriented( |
| 113 | + rect: reportedFrame, |
| 114 | + in: appFrame, |
| 115 | + interfaceOrientation: interfaceOrientation |
| 116 | + ) |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + public static func space( |
| 121 | + reportedBySurfaceHost isSurfaceHost: Bool, |
| 122 | + reportedFrame: CGRect, |
| 123 | + inheritedFrom inherited: SnapshotGeometrySpace, |
| 124 | + appFrame: CGRect, |
| 125 | + interfaceOrientation: Int |
| 126 | + ) -> SnapshotGeometrySpace { |
| 127 | + guard isSurfaceHost else { return inherited } |
| 128 | + guard namesQuarterTurn(interfaceOrientation) else { return .appOrientation } |
| 129 | + guard isQuarterTurned(reportedFrame, relativeTo: appFrame) else { return .appOrientation } |
| 130 | + return .deviceNative(appFrame: appFrame, interfaceOrientation: interfaceOrientation) |
| 131 | + } |
| 132 | + |
| 133 | + public static func namesQuarterTurn(_ interfaceOrientation: Int) -> Bool { |
| 134 | + interfaceOrientation == RunnerInterfaceOrientation.landscapeLeft |
| 135 | + || interfaceOrientation == RunnerInterfaceOrientation.landscapeRight |
| 136 | + } |
| 137 | + |
| 138 | + public static func isSurfaceHost(isWindow: Bool, parentIsWindow: Bool) -> Bool { |
| 139 | + isWindow || parentIsWindow |
| 140 | + } |
| 141 | + |
| 142 | + private static func isQuarterTurned(_ frame: CGRect, relativeTo appFrame: CGRect) -> Bool { |
| 143 | + guard isPlottable(frame), isPlottable(appFrame), |
| 144 | + abs(appFrame.width - appFrame.height) > quarterTurnTolerance |
| 145 | + else { |
| 146 | + return false |
| 147 | + } |
| 148 | + return abs(frame.width - appFrame.height) <= quarterTurnTolerance |
| 149 | + && abs(frame.height - appFrame.width) <= quarterTurnTolerance |
| 150 | + } |
| 151 | + |
| 152 | + private static func isPlottable(_ frame: CGRect) -> Bool { |
| 153 | + frame.origin.x.isFinite && frame.origin.y.isFinite |
| 154 | + && frame.width.isFinite && frame.height.isFinite |
| 155 | + && frame.width > 0 && frame.height > 0 |
| 156 | + } |
| 157 | +} |
0 commit comments