Skip to content

Run a virtual iPhone on an Apple Silicon Mac.

Swift

Move trackpad gestures with the turned panel

Jacky568643f649a2
diff --git a/VPhoneExecutable/VPhoneVirtualization/UI/VirtualMachine/VPhoneVirtualMachineView.swift b/VPhoneExecutable/VPhoneVirtualization/UI/VirtualMachine/VPhoneVirtualMachineView.swift
index 754adf58..2f3efd3d 100644
--- a/VPhoneExecutable/VPhoneVirtualization/UI/VirtualMachine/VPhoneVirtualMachineView.swift
+++ b/VPhoneExecutable/VPhoneVirtualization/UI/VirtualMachine/VPhoneVirtualMachineView.swift
@@ -256,6 +256,11 @@ class VPhoneVirtualMachineView: VZVirtualMachineView {
/// share a baseline: on hardware the vertical axis came out matching the
/// fingers while the horizontal axis was mirrored, hence the extra sign on
/// x. This is the one place the convention lives.
+ ///
+ /// The deltas are the window's; the finger moves on the panel, which the
+ /// window shows turned when the guest is sideways. Unturned, a vertical
+ /// swipe on a landscape guest dragged the finger across it, into the
+ /// panel's edge, where the re-anchor pressed it down again as a tap.
private func scrollDelta(of event: NSEvent) -> CGPoint {
var dx = -event.scrollingDeltaX
var dy = event.scrollingDeltaY
@@ -263,7 +268,15 @@ class VPhoneVirtualMachineView: VZVirtualMachineView {
dx = -dx
dy = -dy
}
- return CGPoint(x: dx * Self.scrollToTouchScale, y: dy * Self.scrollToTouchScale)
+ return screenOrientation.panelVector(
+ fromScreen: CGPoint(x: dx * Self.scrollToTouchScale, y: dy * Self.scrollToTouchScale),
+ )
+ }
+
+ /// How the window shows the panel: this view is turned with the guest's
+ /// interface, and keeps its portrait bounds.
+ private var screenOrientation: VPhoneDisplayOrientation {
+ VPhoneDisplayOrientation(viewRotation: frameCenterRotation)
}
/// Where to press the synthetic finger: the pointer, except for an upward
@@ -275,11 +288,18 @@ class VPhoneVirtualMachineView: VZVirtualMachineView {
/// is an ordinary drag, which is why unlocking worked with the pointer
/// parked on the bar and stopped as soon as it moved up a little. Snapping
/// only in that strip keeps the rest of the window usable for scrolling.
+ ///
+ /// Up and the bottom strip are the window's, which is the interface's: on
+ /// a sideways guest the Home indicator runs along a long side of the panel.
private func scrollStartPoint(for pointer: NSPoint, travel: CGPoint) -> NSPoint {
- guard travel.y > abs(travel.x),
- pointer.y <= bounds.height * Self.homeEdgeSnapFraction
+ let orientation = screenOrientation
+ let screenTravel = orientation.screenVector(fromPanel: travel)
+ let screenPointer = orientation.screenPoint(fromPanel: pointer, panel: bounds.size)
+ let screenHeight = orientation.displayedSize(panel: bounds.size).height
+ guard screenTravel.y > abs(screenTravel.x),
+ screenPointer.y <= screenHeight * Self.homeEdgeSnapFraction
else { return pointer }
- return NSPoint(x: pointer.x, y: 0)
+ return orientation.panelPoint(fromScreen: CGPoint(x: screenPointer.x, y: 0), panel: bounds.size)
}
/// Press the synthetic finger down at `point`, so the drag starts where the
@@ -420,11 +440,13 @@ class VPhoneVirtualMachineView: VZVirtualMachineView {
pinchTouchRadius = 0
}
+ /// The two fingers sit side by side as the window shows the panel.
private func sendPinchTouch(phase: Int) {
guard let center = pinchTouchCenter else { return }
+ let offset = screenOrientation.panelVector(fromScreen: CGPoint(x: pinchTouchRadius, y: 0))
let points = [
- clampedTouchPoint(NSPoint(x: center.x - pinchTouchRadius, y: center.y)),
- clampedTouchPoint(NSPoint(x: center.x + pinchTouchRadius, y: center.y)),
+ clampedTouchPoint(NSPoint(x: center.x - offset.x, y: center.y - offset.y)),
+ clampedTouchPoint(NSPoint(x: center.x + offset.x, y: center.y + offset.y)),
]
sendTouchEvent(
phase: phase, localPoints: points, swipeAim: 0,
@@ -648,8 +670,9 @@ class VPhoneVirtualMachineView: VZVirtualMachineView {
// MARK: - Back Gesture
- /// Replay the system back gesture: a drag from the left edge toward the
- /// centre.
+ /// Replay the system back gesture: a drag from the interface's left edge
+ /// toward the centre, which on a sideways guest is a long side of the
+ /// panel.
///
/// iOS has no back key. This is the gesture every app honours, so both the
/// Esc key and the Device menu's Back item come through here.
@@ -679,11 +702,17 @@ class VPhoneVirtualMachineView: VZVirtualMachineView {
self?.backGestureInFlight = false
}
+ // Laid out on the interface, y up, then carried onto the panel's
+ // pixels, y down.
+ let orientation = screenOrientation
+ let screen = orientation.displayedSize(panel: size)
+ let from = orientation.panelPoint(fromScreen: CGPoint(x: screen.width * 0.004, y: screen.height * 0.5), panel: size)
+ let to = orientation.panelPoint(fromScreen: CGPoint(x: screen.width * 0.85, y: screen.height * 0.5), panel: size)
injectSwipe(
- fromX: size.width * 0.004,
- fromY: size.height * 0.5,
- toX: size.width * 0.85,
- toY: size.height * 0.5,
+ fromX: from.x,
+ fromY: size.height - from.y,
+ toX: to.x,
+ toY: size.height - to.y,
screenWidth: Int(size.width),
screenHeight: Int(size.height),
durationMs: durationMS,
diff --git a/VPhoneKit/VPhoneCoreKit/VirtualMachine/VPhoneDisplayOrientation.swift b/VPhoneKit/VPhoneCoreKit/VirtualMachine/VPhoneDisplayOrientation.swift
index 9b17dcf4..f987f4d8 100644
--- a/VPhoneKit/VPhoneCoreKit/VirtualMachine/VPhoneDisplayOrientation.swift
+++ b/VPhoneKit/VPhoneCoreKit/VirtualMachine/VPhoneDisplayOrientation.swift
@@ -45,6 +45,55 @@ public enum VPhoneDisplayOrientation: Int, CaseIterable, Sendable {
isSideways ? CGSize(width: panel.height, height: panel.width) : panel
}
+ // MARK: - Screen and Panel
+
+ /// The orientation a view turned by `rotation` degrees counterclockwise
+ /// shows, to the nearest quarter turn: the inverse of `viewRotation`.
+ public init(viewRotation rotation: CGFloat) {
+ let quarters = Int((rotation / 90).rounded())
+ self = VPhoneDisplayOrientation(degrees: -quarters * 90) ?? .portrait
+ }
+
+ /// A direction as the window shows it, x right and y up, in the panel's
+ /// own portrait coordinates, also y up. A trackpad gesture or a swipe
+ /// described on screen goes through here before it moves a finger on the
+ /// panel, or a vertical swipe on a sideways panel runs across it.
+ public func panelVector(fromScreen vector: CGPoint) -> CGPoint {
+ switch self {
+ case .portrait: vector
+ case .landscapeRight: CGPoint(x: vector.y, y: -vector.x)
+ case .upsideDown: CGPoint(x: -vector.x, y: -vector.y)
+ case .landscapeLeft: CGPoint(x: -vector.y, y: vector.x)
+ }
+ }
+
+ /// A panel direction as the window shows it: the inverse of
+ /// `panelVector(fromScreen:)`.
+ public func screenVector(fromPanel vector: CGPoint) -> CGPoint {
+ switch self {
+ case .portrait: vector
+ case .landscapeRight: CGPoint(x: -vector.y, y: vector.x)
+ case .upsideDown: CGPoint(x: -vector.x, y: -vector.y)
+ case .landscapeLeft: CGPoint(x: vector.y, y: -vector.x)
+ }
+ }
+
+ /// A point on a `panel`-sized portrait panel, origin bottom left, as the
+ /// window shows it, in `displayedSize(panel:)`'s coordinates.
+ public func screenPoint(fromPanel point: CGPoint, panel: CGSize) -> CGPoint {
+ let screen = displayedSize(panel: panel)
+ let turned = screenVector(fromPanel: CGPoint(x: point.x - panel.width / 2, y: point.y - panel.height / 2))
+ return CGPoint(x: turned.x + screen.width / 2, y: turned.y + screen.height / 2)
+ }
+
+ /// The panel point the window shows at `point`: the inverse of
+ /// `screenPoint(fromPanel:panel:)`.
+ public func panelPoint(fromScreen point: CGPoint, panel: CGSize) -> CGPoint {
+ let screen = displayedSize(panel: panel)
+ let turned = panelVector(fromScreen: CGPoint(x: point.x - screen.width / 2, y: point.y - screen.height / 2))
+ return CGPoint(x: turned.x + panel.width / 2, y: turned.y + panel.height / 2)
+ }
+
// MARK: - Turning
/// The largest size with the panel's aspect ratio whose box, turned by
diff --git a/VPhoneKit/VPhoneCoreKitTests/VirtualMachine/DisplayOrientationTests.swift b/VPhoneKit/VPhoneCoreKitTests/VirtualMachine/DisplayOrientationTests.swift
index 7bd20adc..4a6a9dc1 100644
--- a/VPhoneKit/VPhoneCoreKitTests/VirtualMachine/DisplayOrientationTests.swift
+++ b/VPhoneKit/VPhoneCoreKitTests/VirtualMachine/DisplayOrientationTests.swift
@@ -45,6 +45,50 @@ struct DisplayOrientationTests {
#expect(VPhoneDisplayOrientation.landscapeRight.displayedSize(panel: Self.panel) == sideways)
}
+ // MARK: - Screen and Panel
+
+ @Test func `a view's turn reads back as its orientation`() {
+ for orientation in VPhoneDisplayOrientation.allCases {
+ #expect(VPhoneDisplayOrientation(viewRotation: orientation.viewRotation) == orientation)
+ }
+ // Mid-turn, the nearest quarter turn.
+ #expect(VPhoneDisplayOrientation(viewRotation: 80) == .landscapeRight)
+ #expect(VPhoneDisplayOrientation(viewRotation: -95) == .landscapeLeft)
+ #expect(VPhoneDisplayOrientation(viewRotation: 350) == .portrait)
+ }
+
+ /// Landscape right: the panel is turned a quarter turn counterclockwise,
+ /// so the interface's top is the panel's right edge.
+ @Test func `an upward swipe on a sideways panel runs toward its top edge`() {
+ let up = CGPoint(x: 0, y: 10)
+ #expect(VPhoneDisplayOrientation.portrait.panelVector(fromScreen: up) == up)
+ #expect(VPhoneDisplayOrientation.landscapeRight.panelVector(fromScreen: up) == CGPoint(x: 10, y: 0))
+ #expect(VPhoneDisplayOrientation.landscapeLeft.panelVector(fromScreen: up) == CGPoint(x: -10, y: 0))
+ #expect(VPhoneDisplayOrientation.upsideDown.panelVector(fromScreen: up) == CGPoint(x: 0, y: -10))
+ }
+
+ @Test func `screen and panel directions undo each other`() {
+ let vector = CGPoint(x: 3, y: -7)
+ for orientation in VPhoneDisplayOrientation.allCases {
+ #expect(orientation.screenVector(fromPanel: orientation.panelVector(fromScreen: vector)) == vector)
+ }
+ }
+
+ /// Landscape right shows the panel's right edge as the window's top, and
+ /// its bottom edge as the window's right.
+ @Test func `panel points land where the turned window shows them`() {
+ let panel = CGSize(width: 1032, height: 1376)
+ let landscape = VPhoneDisplayOrientation.landscapeRight
+ #expect(landscape.screenPoint(fromPanel: CGPoint(x: 1032, y: 688), panel: panel) == CGPoint(x: 688, y: 1032))
+ #expect(landscape.screenPoint(fromPanel: CGPoint(x: 516, y: 0), panel: panel) == CGPoint(x: 1376, y: 516))
+ #expect(landscape.panelPoint(fromScreen: CGPoint(x: 0, y: 516), panel: panel) == CGPoint(x: 516, y: 1376))
+ for orientation in VPhoneDisplayOrientation.allCases {
+ let point = CGPoint(x: 100, y: 300)
+ #expect(orientation.panelPoint(fromScreen: orientation.screenPoint(fromPanel: point, panel: panel), panel: panel) == point)
+ }
+ #expect(VPhoneDisplayOrientation.portrait.screenPoint(fromPanel: CGPoint(x: 5, y: 9), panel: panel) == CGPoint(x: 5, y: 9))
+ }
+
// MARK: - Turning
@Test func `the panel fills the window at rest`() {