Run a virtual iPhone on an Apple Silicon Mac.
Swift
fix(launchpad): confirm before closing window during machine creation
tylinuxe593029e2e80
diff --git a/VPhoneLaunchpad/VPhoneLaunchpad/Application/VPhoneLaunchpadApp.swift b/VPhoneLaunchpad/VPhoneLaunchpad/Application/VPhoneLaunchpadApp.swiftindex 1e81437e..1442c07b 100644--- a/VPhoneLaunchpad/VPhoneLaunchpad/Application/VPhoneLaunchpadApp.swift+++ b/VPhoneLaunchpad/VPhoneLaunchpad/Application/VPhoneLaunchpadApp.swift@@ -19,6 +19,7 @@ struct VPhoneLaunchpadApp: App {Window(Text(verbatim: "vphone-launchpad"), id: "main") {VPhoneLaunchpadRootView().environment(model)+ .confirmCloseDuringCreation(delegate).onAppear { delegate.model = model }}.windowToolbarStyle(.unified(showsTitle: false))@@ -52,12 +53,17 @@ struct VPhoneLaunchpadApp: App {final class VPhoneLaunchpadAppDelegate: NSObject, NSApplicationDelegate {weak var model: VPhoneLaunchpadModel?private let dockPolicy = VPhoneLaunchpadDockPolicy()+ /// Set once the user confirms closing the last window while a machine is+ /// being created, so the terminate path that follows does not ask again.+ private var confirmedClose = falsefunc applicationDidFinishLaunching(_: Notification) {dockPolicy.start()}- /// In menu bar mode the app stays behind in the menu bar.+ /// In menu bar mode the app stays behind in the menu bar. Closing the+ /// window while a machine is being created is refused in+ /// `windowShouldClose`, so this is not asked until the window is gone.func applicationShouldTerminateAfterLastWindowClosed(_: NSApplication) -> Bool {!VPhoneLaunchpadMenuBar.isEnabled}@@ -68,14 +74,105 @@ final class VPhoneLaunchpadAppDelegate: NSObject, NSApplicationDelegate {return .terminateNow}#endif+ if confirmedClose {+ return .terminateNow+ }guard let model, model.machines.hasActiveCreation else {return .terminateNow}+ return confirmStopCreation() ? .terminateNow : .terminateCancel+ }++ /// The close button, Close menu item and ⌘W all ask here, while the+ /// window is still up. Without the menu bar, closing the last window+ /// quits, so the alert says Quit. Cancel refuses the close. In menu bar+ /// mode closing only hides the window, so no confirmation is needed.+ func windowShouldClose(_ window: NSWindow) -> Bool {+ guard !VPhoneLaunchpadMenuBar.isEnabled, let model, model.machines.hasActiveCreation else {+ return true+ }+ guard confirmStopCreation() else {+ return false+ }+ confirmedClose = true+ return true+ }++ private func confirmStopCreation() -> Bool {let alert = NSAlert()alert.messageText = String(localized: "Stop Creating Machine?")- alert.informativeText = String(localized: "Quitting stops creating this machine. You can retry later from the step where it stopped.")+ alert.informativeText = String(+ localized: "Quitting stops creating this machine. You can retry later from the step where it stopped.",+ )alert.addButton(withTitle: String(localized: "Quit"))alert.addButton(withTitle: String(localized: "Cancel"))- return alert.runModal() == .alertFirstButtonReturn ? .terminateNow : .terminateCancel+ return alert.runModal() == .alertFirstButtonReturn+ }+}++/// Installed in the main window's content so that window, and no other, asks+/// before closing while a machine is being created. SwiftUI already owns the+/// window delegate, so the question is forwarded and every other message goes+/// to the delegate that was there.+private final class VPhoneLaunchpadWindowCloseHook: NSView, NSWindowDelegate {+ private weak var delegate: VPhoneLaunchpadAppDelegate?+ /// Read from `NSObject`'s nonisolated forwarding methods. AppKit calls+ /// those, and moves this view between windows, on the main thread.+ nonisolated(unsafe) private weak var forwarded: NSWindowDelegate?++ init(delegate: VPhoneLaunchpadAppDelegate) {+ self.delegate = delegate+ super.init(frame: .zero)+ }++ @available(*, unavailable)+ required init?(coder: NSCoder) {+ nil+ }++ override func viewDidMoveToWindow() {+ super.viewDidMoveToWindow()+ guard let window, window.delegate !== self else { return }+ forwarded = window.delegate+ window.delegate = self+ }++ func windowShouldClose(_ sender: NSWindow) -> Bool {+ if forwarded?.windowShouldClose?(sender) == false {+ return false+ }+ return delegate?.windowShouldClose(sender) ?? true}++ nonisolated override func responds(to selector: Selector!) -> Bool {+ super.responds(to: selector) || forwarded?.responds(to: selector) == true+ }++ nonisolated override func forwardingTarget(for selector: Selector!) -> Any? {+ if forwarded?.responds(to: selector) == true {+ return forwarded+ }+ return super.forwardingTarget(for: selector)+ }+}++extension View {+ /// Asks before this window closes while a machine is being created.+ func confirmCloseDuringCreation(_ delegate: VPhoneLaunchpadAppDelegate) -> some View {+ background {+ VPhoneLaunchpadWindowCloseInstaller(delegate: delegate)+ }+ }+}++private struct VPhoneLaunchpadWindowCloseInstaller: NSViewRepresentable {+ let delegate: VPhoneLaunchpadAppDelegate++ func makeNSView(context: Context) -> NSView {+ MainActor.assumeIsolated {+ VPhoneLaunchpadWindowCloseHook(delegate: delegate)+ }+ }++ func updateNSView(_ nsView: NSView, context: Context) {}}