From d506795e05087a8bdf21a418c392f308d4a2f31a Mon Sep 17 00:00:00 2001 From: Henrik Klee Date: Fri, 20 Nov 2026 02:34:55 +0800 Subject: [PATCH 1/4] simulator: a Scale menu, 100 % being pixel for pixel Scale > 50 %, 75 %, 100 % sizes the window so that one guest pixel takes that share of the Mac's own pixels (100 %: the guest's picture pixel for pixel, from the panel's live width and the screen's backing scale); Fit to Screen makes it as large as the screen allows. The window keeps its centre and its top on the screen; at 100 % an iPad runs off the bottom of a laptop's. Assisted-by: LLM --- simulator/main.swift | 51 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/simulator/main.swift b/simulator/main.swift index 4797aa47..113ef127 100644 --- a/simulator/main.swift +++ b/simulator/main.swift @@ -20,6 +20,8 @@ struct DeviceProfile { let darkFront: Bool // Window pixels per millimetre: both devices open about as tall. var scale: CGFloat { 720 / height } + // The panel's short side in pixels, until the guest's own picture says. + var panelPixelsWide: CGFloat { name == "iphone7" ? 750 : 1536 } static let margin: CGFloat = 24 // iPad (6th generation), Wi-Fi: 169.5 × 240 mm, 9.7-inch 4:3 display. @@ -115,6 +117,16 @@ final class SimulatorView: NSView { NSSize(width: heldSize.width * profile.scale + margin * 2, height: heldSize.height * profile.scale + margin * 2) } + // The window zoom (window points per canvas point) at which one guest + // pixel takes SHARE of a pixel of this Mac's screen: 1 is 100 %, the + // guest's picture pixel for pixel. The panel's width in pixels is the + // guest's own, or the profile's until it has drawn. + func zoom(pixelShare share: CGFloat, backingScale: CGFloat) -> CGFloat { + let live = display.map { CGFloat(min($0.width, $0.height)) } ?? 0 + let pixels = live > 0 ? live : profile.panelPixelsWide + return pixels * share / backingScale / (profile.display.width * profile.scale) + } + // Device millimetres (upright) to the held device's millimetres, and back. private func held(_ p: NSPoint) -> NSPoint { let w = profile.width, h = profile.height @@ -805,6 +817,22 @@ final class AppDelegate: NSObject, NSApplicationDelegate { appMenu.addItem(detach) } appItem.submenu = appMenu + // Scale: the guest's picture at a share of the Mac's own pixels + // (100 % is pixel for pixel), or as large as the screen allows. + let scaleItem = NSMenuItem() + menu.addItem(scaleItem) + let scaleMenu = NSMenu(title: "Scale") + for (title, share) in [("50%", 0.5), ("75%", 0.75), ("100%", 1.0)] { + let item = NSMenuItem(title: title, action: #selector(selectScale(_:)), keyEquivalent: "") + item.target = self + item.representedObject = share + scaleMenu.addItem(item) + } + scaleMenu.addItem(.separator()) + let fit = NSMenuItem(title: "Fit to Screen", action: #selector(selectScale(_:)), keyEquivalent: "") + fit.target = self + scaleMenu.addItem(fit) + scaleItem.submenu = scaleMenu app.mainMenu = menu window = SimulatorWindow(contentRect: NSRect(origin: .zero, size: view.canvas), styleMask: [.borderless], backing: .buffered, defer: false) window.title = "Orchard Simulator" @@ -876,6 +904,29 @@ final class AppDelegate: NSObject, NSApplicationDelegate { alert.runModal() } } + // The window at a share of the Mac's pixels, or fitted to the screen, + // about its own centre and kept on the screen. + @objc private func selectScale(_ item: NSMenuItem) { + guard let view = window.contentView as? SimulatorView else { return } + let screen = window.screen ?? NSScreen.main + let visible = screen?.visibleFrame ?? window.frame + let zoom: CGFloat + if let share = item.representedObject as? Double { + zoom = view.zoom(pixelShare: CGFloat(share), backingScale: window.backingScaleFactor) + } else { + zoom = min(visible.width / view.canvas.width, visible.height / view.canvas.height) + } + let size = NSSize(width: view.canvas.width * zoom, height: view.canvas.height * zoom) + var frame = NSRect(x: window.frame.midX - size.width / 2, y: window.frame.midY - size.height / 2, + width: size.width, height: size.height) + // Keep the top on the screen (where the menu bar is); larger than the + // screen, it runs off the bottom. + frame.origin.y = min(frame.origin.y, visible.maxY - frame.height) + frame.origin.x = max(visible.minX, min(frame.origin.x, visible.maxX - frame.width)) + window.setFrame(frame, display: true) + for entry in item.menu?.items ?? [] { entry.state = entry === item ? .on : .off } + } + // Turns the device in the window, frame and screen together, and tells the // VM's accelerometer; the guest turns its picture as a tablet does. @objc private func rotate(_ item: NSMenuItem) { From 1e6ee53747da8d94a2f5aac02468d5ad3d254aa9 Mon Sep 17 00:00:00 2001 From: Henrik Klee Date: Fri, 20 Nov 2026 04:00:21 +0800 Subject: [PATCH 2/4] simulator: the Scale menu follows the window and the device - The tick is worked out as the menu opens, from the window's zoom: after resizing the window off a scale, no scale is ticked. - Switching the device or turning it re-applies the chosen scale when the window was still at it, so 100 % stays pixel for pixel on the new panel and Fit to Screen fits again; resized off it, the window keeps its zoom. - A scale is reckoned from the profile's panel, not the guest's last picture, which is still the old device's right after a switch. Assisted-by: LLM --- simulator/main.swift | 58 ++++++++++++++++++++++++++++++++------------ 1 file changed, 43 insertions(+), 15 deletions(-) diff --git a/simulator/main.swift b/simulator/main.swift index 113ef127..97e34e74 100644 --- a/simulator/main.swift +++ b/simulator/main.swift @@ -120,11 +120,10 @@ final class SimulatorView: NSView { // The window zoom (window points per canvas point) at which one guest // pixel takes SHARE of a pixel of this Mac's screen: 1 is 100 %, the // guest's picture pixel for pixel. The panel's width in pixels is the - // guest's own, or the profile's until it has drawn. + // profile's: the VM runs with the profile's panel (a device switch + // restarts it), and the guest's last picture may still be the old one's. func zoom(pixelShare share: CGFloat, backingScale: CGFloat) -> CGFloat { - let live = display.map { CGFloat(min($0.width, $0.height)) } ?? 0 - let pixels = live > 0 ? live : profile.panelPixelsWide - return pixels * share / backingScale / (profile.display.width * profile.scale) + profile.panelPixelsWide * share / backingScale / (profile.display.width * profile.scale) } // Device millimetres (upright) to the held device's millimetres, and back. @@ -727,7 +726,7 @@ final class SimulatorWindow: NSWindow { override var canBecomeMain: Bool { true } } -final class AppDelegate: NSObject, NSApplicationDelegate { +final class AppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate { var window: NSWindow! func applicationDidFinishLaunching(_ notification: Notification) { let app = NSApplication.shared @@ -832,6 +831,7 @@ final class AppDelegate: NSObject, NSApplicationDelegate { let fit = NSMenuItem(title: "Fit to Screen", action: #selector(selectScale(_:)), keyEquivalent: "") fit.target = self scaleMenu.addItem(fit) + scaleMenu.delegate = self scaleItem.submenu = scaleMenu app.mainMenu = menu window = SimulatorWindow(contentRect: NSRect(origin: .zero, size: view.canvas), styleMask: [.borderless], backing: .buffered, defer: false) @@ -904,18 +904,44 @@ final class AppDelegate: NSObject, NSApplicationDelegate { alert.runModal() } } - // The window at a share of the Mac's pixels, or fitted to the screen, - // about its own centre and kept on the screen. + // The Scale menu's choice: a share of the Mac's pixels, or nil for Fit + // to Screen; none until one is picked. + private var scaleChoice: Double?? + + // The window zoom a Scale choice means for the device as it is now. + private func zoom(for choice: Double?, _ view: SimulatorView) -> CGFloat { + if let share = choice { return view.zoom(pixelShare: CGFloat(share), backingScale: window.backingScaleFactor) } + let visible = (window.screen ?? NSScreen.main)?.visibleFrame ?? window.frame + return min(visible.width / view.canvas.width, visible.height / view.canvas.height) + } + // Whether the window is at that choice now (not resized off it since). + private func isAt(_ choice: Double?, _ view: SimulatorView) -> Bool { + let target = zoom(for: choice, view) + return abs(window.frame.width / view.canvas.width - target) <= target * 0.01 + } + + // The tick goes on whichever scale the window is at as the menu opens: + // none after the window was resized to something else. + func menuNeedsUpdate(_ menu: NSMenu) { + guard let view = window.contentView as? SimulatorView else { return } + for entry in menu.items where entry.action == #selector(selectScale(_:)) { + entry.state = isAt(entry.representedObject as? Double, view) ? .on : .off + } + } + @objc private func selectScale(_ item: NSMenuItem) { guard let view = window.contentView as? SimulatorView else { return } + scaleChoice = .some(item.representedObject as? Double) + applyScale(view) + } + + // The window at a share of the Mac's pixels, or fitted to the screen, + // about its own centre and kept on the screen. + private func applyScale(_ view: SimulatorView) { + guard let choice = scaleChoice else { return } let screen = window.screen ?? NSScreen.main let visible = screen?.visibleFrame ?? window.frame - let zoom: CGFloat - if let share = item.representedObject as? Double { - zoom = view.zoom(pixelShare: CGFloat(share), backingScale: window.backingScaleFactor) - } else { - zoom = min(visible.width / view.canvas.width, visible.height / view.canvas.height) - } + let zoom = zoom(for: choice, view) let size = NSSize(width: view.canvas.width * zoom, height: view.canvas.height * zoom) var frame = NSRect(x: window.frame.midX - size.width / 2, y: window.frame.midY - size.height / 2, width: size.width, height: size.height) @@ -924,7 +950,6 @@ final class AppDelegate: NSObject, NSApplicationDelegate { frame.origin.y = min(frame.origin.y, visible.maxY - frame.height) frame.origin.x = max(visible.minX, min(frame.origin.x, visible.maxX - frame.width)) window.setFrame(frame, display: true) - for entry in item.menu?.items ?? [] { entry.state = entry === item ? .on : .off } } // Turns the device in the window, frame and screen together, and tells the @@ -975,11 +1000,14 @@ final class AppDelegate: NSObject, NSApplicationDelegate { } // Resizes the window to the view's new canvas at the same zoom, about the - // same centre. + // same centre; at a Scale choice still, the choice is applied to the new + // device or orientation instead (100 % stays pixel for pixel). private func refit(_ view: SimulatorView, _ change: () -> Void) { let zoom = window.frame.width / view.canvas.width let center = NSPoint(x: window.frame.midX, y: window.frame.midY) + let keepChoice = scaleChoice.map { isAt($0, view) } ?? false change() + if keepChoice { applyScale(view); return } let size = NSSize(width: view.canvas.width * zoom, height: view.canvas.height * zoom) window.setFrame(NSRect(x: center.x - size.width / 2, y: center.y - size.height / 2, width: size.width, height: size.height), display: true) } From 81daf17dbb66c7ae0ce9c68e19ac8d00763ae50b Mon Sep 17 00:00:00 2001 From: Henrik Klee Date: Fri, 20 Nov 2026 04:03:44 +0800 Subject: [PATCH 3/4] simulator: Hide (Cmd-H) and Hide Others (Cmd-Option-H) The app menu gets the Mac's Hide, Hide Others and Show All; as menu key equivalents they are the Mac's before Command reaches the guest as Super. Assisted-by: LLM --- simulator/main.swift | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/simulator/main.swift b/simulator/main.swift index 97e34e74..d5edf275 100644 --- a/simulator/main.swift +++ b/simulator/main.swift @@ -807,6 +807,14 @@ final class AppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate { mouse.tag = 0 appMenu.addItem(mouse) appMenu.addItem(.separator()) + // The Mac's own: a menu key equivalent is the Mac's before the guest + // sees Command as Super. + appMenu.addItem(withTitle: "Hide Orchard Simulator", action: #selector(NSApplication.hide(_:)), keyEquivalent: "h") + let hideOthers = NSMenuItem(title: "Hide Others", action: #selector(NSApplication.hideOtherApplications(_:)), keyEquivalent: "h") + hideOthers.keyEquivalentModifierMask = [.command, .option] + appMenu.addItem(hideOthers) + appMenu.addItem(withTitle: "Show All", action: #selector(NSApplication.unhideAllApplications(_:)), keyEquivalent: "") + appMenu.addItem(.separator()) appMenu.addItem(withTitle: "Quit Orchard Simulator", action: #selector(NSApplication.terminate(_:)), keyEquivalent: "q") if ProcessInfo.processInfo.environment["ORCHARD_VM_SCRIPT"] != nil { let detach = NSMenuItem(title: "Quit and Keep VM Running", action: #selector(quitKeepingVM), keyEquivalent: "q") From 0bdda14c6bd367ca921449b90b2ebb12a7da01e3 Mon Sep 17 00:00:00 2001 From: Henrik Klee Date: Fri, 20 Nov 2026 05:17:05 +0800 Subject: [PATCH 4/4] Changelog: the simulator's Scale menu and Hide shortcuts Assisted-by: LLM --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6562a1f0..e5c92115 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -27,6 +27,10 @@ is missing or empty. and the desktop follows through the same rotation-sensor path as a real tablet. The frame's buttons no longer take the keyboard away from the guest, and the screen comes back by itself after the VM restarts. +- Orchard Simulator: a Scale menu shows the guest at 50 %, 75 % or 100 % + of the Mac's pixels (100 % is pixel for pixel) or fitted to the screen, + and keeps the scale across device switches and rotation. Cmd+H and + Cmd+Option+H hide the simulator and other apps, as on any Mac app. ## [0.3.2] - 2026-11-19