Skip to content

Run a virtual iPhone on an Apple Silicon Mac.

Swift

Draw the VM's name on its Dock tile

Jackyc7a864c9d31b
diff --git a/VPhoneExecutable/VPhoneVirtualization/UI/VPhoneDockName.swift b/VPhoneExecutable/VPhoneVirtualization/UI/VPhoneDockName.swift
index 4eecd89f..4dc51e13 100644
--- a/VPhoneExecutable/VPhoneVirtualization/UI/VPhoneDockName.swift
+++ b/VPhoneExecutable/VPhoneVirtualization/UI/VPhoneDockName.swift
@@ -1,12 +1,10 @@
import AppKit
import Foundation
-/// Names this process after the VM in the Dock, the app switcher and Force
-/// Quit, instead of "VPhone.bundle" for every guest.
+/// Tells one guest's process from another's in the menu bar and the Dock.
///
-/// Launch Services keeps the display name per running application and only
-/// exposes the setter privately; it is looked up at run time, and a missing
-/// symbol leaves the bundle name in place.
+/// Every guest is a `vphone-vm` inside the same `VPhone.bundle`, so by default
+/// they all look and read alike.
enum VPhoneDockName {
private typealias CurrentASN = @convention(c) () -> Unmanaged<CFTypeRef>?
private typealias SetInformationItem = @convention(c) (
@@ -16,8 +14,19 @@ enum VPhoneDockName {
/// `kLSDefaultSessionID`.
private static let defaultSession: Int32 = -2
- /// Call after the application has checked in with Launch Services, that
- /// is from `applicationDidFinishLaunching` on.
+ /// Names this process after the VM in Launch Services: the application
+ /// menu title and whatever else reads the running application's display
+ /// name.
+ ///
+ /// It does not reach the Dock's tooltip. A Dock process tile takes its
+ /// label from the bundle URL's localized name ("VPhone.bundle"), uses the
+ /// Launch Services name only when that lookup fails, and never reads it
+ /// again; `label(_:)` covers the Dock instead.
+ ///
+ /// The setter is private; it is looked up at run time, and a missing
+ /// symbol leaves the bundle name in place. Call after the application has
+ /// checked in with Launch Services, that is from
+ /// `applicationDidFinishLaunching` on.
@MainActor
static func set(_ name: String) {
guard !name.isEmpty,
@@ -32,8 +41,83 @@ enum VPhoneDockName {
_ = unsafeBitCast(setItem, to: SetInformationItem.self)(defaultSession, asn, key, name as CFString, nil)
}
+ /// Draws the VM's name on this process's Dock tile. The icon is the one
+ /// thing the Dock lets a process change about its own tile.
+ @MainActor
+ static func label(_ name: String) {
+ guard !name.isEmpty else { return }
+ let tile = NSApp.dockTile
+ tile.contentView = VPhoneDockTileView(
+ frame: NSRect(origin: .zero, size: tile.size),
+ icon: NSApp.applicationIconImage,
+ name: name,
+ )
+ tile.display()
+ }
+
/// The VM's name is its folder in the library, the one holding config.plist.
static func name(forConfig config: URL) -> String {
config.deletingLastPathComponent().lastPathComponent
}
}
+
+// MARK: - Dock Tile
+
+/// The application icon with the VM's name on a plate across its lower part.
+private final class VPhoneDockTileView: NSView {
+ private let icon: NSImage
+ private let name: String
+
+ init(frame: NSRect, icon: NSImage, name: String) {
+ self.icon = icon
+ self.name = name
+ super.init(frame: frame)
+ }
+
+ @available(*, unavailable)
+ required init?(coder _: NSCoder) {
+ fatalError("init(coder:) is not supported")
+ }
+
+ override func draw(_: NSRect) {
+ icon.draw(in: bounds)
+
+ let inset = bounds.width * 0.04
+ let plate = NSRect(
+ x: bounds.minX + inset,
+ y: bounds.minY + inset,
+ width: bounds.width - inset * 2,
+ height: bounds.height * 0.3,
+ )
+ NSColor.black.withAlphaComponent(0.75).setFill()
+ NSBezierPath(roundedRect: plate, xRadius: plate.height * 0.25, yRadius: plate.height * 0.25).fill()
+
+ // Shrink to fit before truncating: names in a library often share a
+ // prefix and differ only at the end.
+ let paragraph = NSMutableParagraphStyle()
+ paragraph.alignment = .center
+ paragraph.lineBreakMode = .byTruncatingMiddle
+ let textWidth = plate.width - plate.height * 0.3
+ var fontSize = plate.height * 0.6
+ var text = attributed(size: fontSize, paragraph: paragraph)
+ while text.size().width > textWidth, fontSize > plate.height * 0.36 {
+ fontSize -= 1
+ text = attributed(size: fontSize, paragraph: paragraph)
+ }
+ let textHeight = text.size().height
+ text.draw(in: NSRect(
+ x: plate.midX - textWidth / 2,
+ y: plate.midY - textHeight / 2,
+ width: textWidth,
+ height: textHeight,
+ ))
+ }
+
+ private func attributed(size: CGFloat, paragraph: NSParagraphStyle) -> NSAttributedString {
+ NSAttributedString(string: name, attributes: [
+ .font: NSFont.systemFont(ofSize: size, weight: .semibold),
+ .foregroundColor: NSColor.white,
+ .paragraphStyle: paragraph,
+ ])
+ }
+}
diff --git a/VPhoneExecutable/VPhoneVirtualization/UI/VPhoneVirtualMachineAppDelegate.swift b/VPhoneExecutable/VPhoneVirtualization/UI/VPhoneVirtualMachineAppDelegate.swift
index fd30390a..21685e18 100644
--- a/VPhoneExecutable/VPhoneVirtualization/UI/VPhoneVirtualMachineAppDelegate.swift
+++ b/VPhoneExecutable/VPhoneVirtualization/UI/VPhoneVirtualMachineAppDelegate.swift
@@ -26,7 +26,11 @@ class VPhoneVirtualMachineAppDelegate: NSObject, NSApplicationDelegate {
func applicationDidFinishLaunching(_: Notification) {
NSApp.setActivationPolicy(command.noGraphics ? .prohibited : .regular)
- VPhoneDockName.set(VPhoneDockName.name(forConfig: command.config))
+ let name = VPhoneDockName.name(forConfig: command.config)
+ VPhoneDockName.set(name)
+ if !command.noGraphics {
+ VPhoneDockName.label(name)
+ }
if !command.noGraphics {
VPhoneHostHotKeys.shared.recoverAfterCrash()