Skip to content

Run a virtual iPhone on an Apple Silicon Mac.

Swift

Title the VM window with the VM name and keep Home visible

Lakr905196e7ed9f
diff --git a/VPhoneExecutable/VPhoneVirtualization/Resources/Localizable.xcstrings b/VPhoneExecutable/VPhoneVirtualization/Resources/Localizable.xcstrings
index 32c6f4a8..c31481bb 100644
--- a/VPhoneExecutable/VPhoneVirtualization/Resources/Localizable.xcstrings
+++ b/VPhoneExecutable/VPhoneVirtualization/Resources/Localizable.xcstrings
@@ -29504,108 +29504,6 @@
}
}
},
- "vphone — Connected" : {
- "localizations" : {
- "en" : {
- "stringUnit" : {
- "state" : "translated",
- "value" : "vphone — Connected"
- }
- },
- "ja" : {
- "stringUnit" : {
- "state" : "translated",
- "value" : "vphone — 接続済み"
- }
- },
- "ko" : {
- "stringUnit" : {
- "state" : "translated",
- "value" : "vphone — 연결됨"
- }
- },
- "vi" : {
- "stringUnit" : {
- "state" : "translated",
- "value" : "vphone — Đã kết nối"
- }
- },
- "zh-Hans" : {
- "stringUnit" : {
- "state" : "translated",
- "value" : "vphone — 已连接"
- }
- }
- }
- },
- "vphone — Disconnected" : {
- "localizations" : {
- "en" : {
- "stringUnit" : {
- "state" : "translated",
- "value" : "vphone — Disconnected"
- }
- },
- "ja" : {
- "stringUnit" : {
- "state" : "translated",
- "value" : "vphone — 未接続"
- }
- },
- "ko" : {
- "stringUnit" : {
- "state" : "translated",
- "value" : "vphone — 연결 끊김"
- }
- },
- "vi" : {
- "stringUnit" : {
- "state" : "translated",
- "value" : "vphone — Chưa kết nối"
- }
- },
- "zh-Hans" : {
- "stringUnit" : {
- "state" : "translated",
- "value" : "vphone — 未连接"
- }
- }
- }
- },
- "vphone — Starting…" : {
- "localizations" : {
- "en" : {
- "stringUnit" : {
- "state" : "translated",
- "value" : "vphone — Starting…"
- }
- },
- "ja" : {
- "stringUnit" : {
- "state" : "translated",
- "value" : "vphone — 起動中…"
- }
- },
- "ko" : {
- "stringUnit" : {
- "state" : "translated",
- "value" : "vphone — 시작 중…"
- }
- },
- "vi" : {
- "stringUnit" : {
- "state" : "translated",
- "value" : "vphone — Đang khởi động…"
- }
- },
- "zh-Hans" : {
- "stringUnit" : {
- "state" : "translated",
- "value" : "vphone — 启动中…"
- }
- }
- }
- },
"vphoned Agent" : {
"localizations" : {
"ja" : {
diff --git a/VPhoneExecutable/VPhoneVirtualization/UI/UserInterface/VPhoneVirtualMachineWindowController.swift b/VPhoneExecutable/VPhoneVirtualization/UI/UserInterface/VPhoneVirtualMachineWindowController.swift
index f08bb493..2fde4bf6 100644
--- a/VPhoneExecutable/VPhoneVirtualization/UI/UserInterface/VPhoneVirtualMachineWindowController.swift
+++ b/VPhoneExecutable/VPhoneVirtualization/UI/UserInterface/VPhoneVirtualMachineWindowController.swift
@@ -3,15 +3,13 @@ import Foundation
import Virtualization
@MainActor
-class VPhoneVirtualMachineWindowController: NSObject, NSToolbarDelegate {
+class VPhoneVirtualMachineWindowController: NSObject {
private var windowController: NSWindowController?
private weak var control: VPhoneGuestControl?
private weak var virtualMachineView: VPhoneVirtualMachineView?
private(set) var touchIDMonitor: VPhoneTouchIDMonitor?
- private var ecid: String?
private var menuKeyMonitor: Any?
-
- private nonisolated static let homeItemID = NSToolbarItem.Identifier("home")
+ private var homeButton: NSButton?
var captureView: VPhoneVirtualMachineView? {
virtualMachineView
@@ -24,11 +22,10 @@ class VPhoneVirtualMachineWindowController: NSObject, NSToolbarDelegate {
screenScale: Double,
keySender: VPhoneVirtualMachineKeySender,
control: VPhoneGuestControl,
- ecid: String?,
+ name: String,
sceneIdentifier: String,
) {
self.control = control
- self.ecid = ecid
let view = VPhoneVirtualMachineView()
view.virtualMachine = vm
@@ -55,8 +52,7 @@ class VPhoneVirtualMachineWindowController: NSObject, NSToolbarDelegate {
window.level = .normal
VPhoneAlert.hostWindow = window
window.contentAspectRatio = windowSize
- window.title = VPhoneLocalization.text("vphone — Starting…")
- window.subtitle = makeSubtitle(ip: nil)
+ window.title = name
window.contentView = vmView
// The scene belongs to the VM, not to the app: every VM directory keeps
@@ -69,12 +65,15 @@ class VPhoneVirtualMachineWindowController: NSObject, NSToolbarDelegate {
}
window.setFrameAutosaveName(sceneName)
- // Toolbar with unified style for two-line title
+ // An empty unified toolbar gives the title bar its full height. The Home
+ // button is a titlebar accessory rather than a toolbar item so that a
+ // narrow window truncates the title instead of moving it to overflow.
let toolbar = NSToolbar(identifier: "vphone-toolbar")
- toolbar.delegate = self
toolbar.displayMode = .iconOnly
window.toolbar = toolbar
window.toolbarStyle = .unified
+ window.addTitlebarAccessoryViewController(makeHomeAccessory())
+ updateHomeButton(connected: false)
let controller = NSWindowController(window: window)
controller.showWindow(nil)
@@ -100,58 +99,47 @@ class VPhoneVirtualMachineWindowController: NSObject, NSToolbarDelegate {
monitor.start(control: control, window: window)
touchIDMonitor = monitor
- // Poll vphoned status for title indicator
- _ = Timer.scheduledTimer(withTimeInterval: 2, repeats: true) {
- [weak self, weak window] _ in
+ // Poll vphoned status for the Home button
+ _ = Timer.scheduledTimer(withTimeInterval: 2, repeats: true) { [weak self] _ in
Task { @MainActor in
- guard let self, let window, let control = self.control else { return }
- window.title = VPhoneLocalization.text(
- control.isConnected ? "vphone — Connected" : "vphone — Disconnected",
- )
- window.subtitle = self.makeSubtitle(ip: control.isConnected ? control.guestIPAddress : nil)
+ guard let self, let control = self.control else { return }
+ self.updateHomeButton(connected: control.isConnected)
}
}
}
- private func makeSubtitle(ip: String?) -> String {
- switch (ecid, ip) {
- case let (ecid?, ip?): "\(ecid) — \(ip)"
- case (let ecid?, nil): ecid
- case (nil, let ip?): ip
- case (nil, nil): ""
- }
+ // MARK: - Home Button
+
+ private func makeHomeAccessory() -> NSTitlebarAccessoryViewController {
+ let button = NSButton(image: NSImage(), target: self, action: #selector(homePressed))
+ button.bezelStyle = .toolbar
+ button.toolTip = VPhoneLocalization.text("Home Button")
+ button.translatesAutoresizingMaskIntoConstraints = false
+ homeButton = button
+
+ let container = NSView()
+ container.addSubview(button)
+ NSLayoutConstraint.activate([
+ button.leadingAnchor.constraint(equalTo: container.leadingAnchor),
+ button.trailingAnchor.constraint(equalTo: container.trailingAnchor, constant: -12),
+ button.centerYAnchor.constraint(equalTo: container.centerYAnchor),
+ ])
+
+ let accessory = NSTitlebarAccessoryViewController()
+ accessory.view = container
+ accessory.layoutAttribute = .trailing
+ return accessory
}
- // MARK: - NSToolbarDelegate
-
- nonisolated func toolbar(
- _: NSToolbar,
- itemForItemIdentifier itemIdentifier: NSToolbarItem.Identifier,
- willBeInsertedIntoToolbar _: Bool,
- ) -> NSToolbarItem? {
- MainActor.assumeIsolated {
- if itemIdentifier == Self.homeItemID {
- let item = NSToolbarItem(itemIdentifier: itemIdentifier)
- item.label = VPhoneLocalization.text("Home")
- item.toolTip = VPhoneLocalization.text("Home Button")
- item.image = NSImage(
- systemSymbolName: "circle.circle",
- accessibilityDescription: VPhoneLocalization.text("Home"),
- )
- item.target = self
- item.action = #selector(homePressed)
- return item
- }
- return nil
- }
- }
-
- nonisolated func toolbarDefaultItemIdentifiers(_: NSToolbar) -> [NSToolbarItem.Identifier] {
- [.flexibleSpace, Self.homeItemID]
- }
-
- nonisolated func toolbarAllowedItemIdentifiers(_: NSToolbar) -> [NSToolbarItem.Identifier] {
- [Self.homeItemID, .flexibleSpace, .space]
+ /// The button presses Home through vphoned, so it is disabled and slashed
+ /// while vphoned is not connected.
+ private func updateHomeButton(connected: Bool) {
+ guard let homeButton else { return }
+ homeButton.isEnabled = connected
+ homeButton.image = NSImage(
+ systemSymbolName: connected ? "circle.circle" : "circle.slash",
+ accessibilityDescription: VPhoneLocalization.text("Home"),
+ )
}
// MARK: - Actions
diff --git a/VPhoneExecutable/VPhoneVirtualization/UI/VPhoneVirtualMachineAppDelegate.swift b/VPhoneExecutable/VPhoneVirtualization/UI/VPhoneVirtualMachineAppDelegate.swift
index f1705e5a..2bd16fc2 100644
--- a/VPhoneExecutable/VPhoneVirtualization/UI/VPhoneVirtualMachineAppDelegate.swift
+++ b/VPhoneExecutable/VPhoneVirtualization/UI/VPhoneVirtualMachineAppDelegate.swift
@@ -121,7 +121,7 @@ class VPhoneVirtualMachineAppDelegate: NSObject, NSApplicationDelegate {
screenScale: options.screenScale,
keySender: keySender,
control: control,
- ecid: vm.ecidHex,
+ name: VPhoneDockName.name(forConfig: options.configURL),
sceneIdentifier: options.configURL
.deletingLastPathComponent()
.standardizedFileURL