From 60de1a5fcd0b3e405cb4cc8a124c33ca419d3752 Mon Sep 17 00:00:00 2001 From: Guilherme Rambo Date: Sat, 7 Jun 2025 10:06:38 -0300 Subject: [PATCH] Use openCocoaWindow for all installer windows, fix confirm before closing, fix installation not cleaned up properly when cancelled --- VirtualBuddy/Bootstrap/VirtualBuddyApp.swift | 4 -- .../Restore/Installation/RestoreBackend.swift | 1 + .../SimulatedRestoreBackend.swift | 4 ++ .../VirtualizationRestoreBackend.swift | 36 +++++++++-- .../HostingWindowController.swift | 2 +- .../Installer/VMInstallationViewModel.swift | 62 +++++++++++++------ .../Installer/VMInstallationWizard.swift | 3 + VirtualUI/Source/Library/LibraryView.swift | 9 +-- 8 files changed, 89 insertions(+), 32 deletions(-) diff --git a/VirtualBuddy/Bootstrap/VirtualBuddyApp.swift b/VirtualBuddy/Bootstrap/VirtualBuddyApp.swift index d965e23..b09a530 100644 --- a/VirtualBuddy/Bootstrap/VirtualBuddyApp.swift +++ b/VirtualBuddy/Bootstrap/VirtualBuddyApp.swift @@ -62,9 +62,5 @@ struct VirtualBuddyApp: App { .environmentObject(settingsContainer) .frame(minWidth: 420, maxWidth: .infinity, minHeight: 370, maxHeight: .infinity) } - - WindowGroup(id: .vb_setupWindowID, for: URL.self) { urlBinding in - VMInstallationWizard(library: library, restoringAt: urlBinding.wrappedValue) - } } } diff --git a/VirtualCore/Source/Restore/Installation/RestoreBackend.swift b/VirtualCore/Source/Restore/Installation/RestoreBackend.swift index a64b800..afb4f83 100644 --- a/VirtualCore/Source/Restore/Installation/RestoreBackend.swift +++ b/VirtualCore/Source/Restore/Installation/RestoreBackend.swift @@ -7,4 +7,5 @@ public protocol RestoreBackend: AnyObject { init(model: VBVirtualMachine, restoringFromImageAt restoreImageFileURL: URL) var progress: Progress { get } func install() async throws + func cancel() async } diff --git a/VirtualCore/Source/Restore/Installation/SimulatedRestoreBackend.swift b/VirtualCore/Source/Restore/Installation/SimulatedRestoreBackend.swift index a4c6fd4..6c794ff 100644 --- a/VirtualCore/Source/Restore/Installation/SimulatedRestoreBackend.swift +++ b/VirtualCore/Source/Restore/Installation/SimulatedRestoreBackend.swift @@ -36,5 +36,9 @@ public final class SimulatedRestoreBackend: NSObject, RestoreBackend, @unchecked } } } + + public func cancel() async { + + } } #endif // DEBUG diff --git a/VirtualCore/Source/Restore/Installation/VirtualizationRestoreBackend.swift b/VirtualCore/Source/Restore/Installation/VirtualizationRestoreBackend.swift index 55414cd..d5e867e 100644 --- a/VirtualCore/Source/Restore/Installation/VirtualizationRestoreBackend.swift +++ b/VirtualCore/Source/Restore/Installation/VirtualizationRestoreBackend.swift @@ -5,6 +5,8 @@ import OSLog import Combine public final class VirtualizationRestoreBackend: RestoreBackend { + private let logger = Logger(subsystem: VirtualCoreConstants.subsystemName, category: "VirtualizationRestoreBackend") + public let model: VBVirtualMachine public let restoreImageFileURL: URL @@ -40,16 +42,40 @@ public final class VirtualizationRestoreBackend: RestoreBackend { createInternalProgressObservations(with: installer) defer { - UILog("Cleaning up installation") - - cancellables.removeAll() - _installer = nil - virtualMachineSubject.send(nil) + cleanup() } + try Task.checkCancellation() + try await installer.install() } + public func cancel() async { + logger.warning("Installation cancelled by client.") + + if let _installer, _installer.virtualMachine.canStop { + do { + logger.info("Stopping installation VM...") + + try await _installer.virtualMachine.stop() + + logger.info("Installation VM stopped.") + } catch { + logger.error("Error forcing installation VM stop - \(error, privacy: .public)") + } + } + + cleanup() + } + + private func cleanup() { + logger.debug("Cleaning up installation.") + + cancellables.removeAll() + _installer = nil + virtualMachineSubject.send(nil) + } + private func createInternalProgressObservations(with installer: VZMacOSInstaller) { installer.progress .publisher(for: \.totalUnitCount, options: [.initial, .new]) diff --git a/VirtualUI/Source/Components/HostingWindowController/HostingWindowController.swift b/VirtualUI/Source/Components/HostingWindowController/HostingWindowController.swift index b2e9088..cb67b3a 100644 --- a/VirtualUI/Source/Components/HostingWindowController/HostingWindowController.swift +++ b/VirtualUI/Source/Components/HostingWindowController/HostingWindowController.swift @@ -2,7 +2,7 @@ import Cocoa import SwiftUI import VirtualCore -let defaultHostingWindowStyleMask: NSWindow.StyleMask = [.titled, .closable, .fullSizeContentView] +let defaultHostingWindowStyleMask: NSWindow.StyleMask = [.titled, .miniaturizable, .resizable, .closable, .fullSizeContentView] public final class HostingWindowController: NSWindowController, NSWindowDelegate where Content: View { diff --git a/VirtualUI/Source/Installer/VMInstallationViewModel.swift b/VirtualUI/Source/Installer/VMInstallationViewModel.swift index a21f685..23e40a5 100644 --- a/VirtualUI/Source/Installer/VMInstallationViewModel.swift +++ b/VirtualUI/Source/Installer/VMInstallationViewModel.swift @@ -239,8 +239,6 @@ final class VMInstallationViewModel: ObservableObject, @unchecked Sendable { case .done: showNextButton = true buttonTitle = "Back to Library" - - cleanupInstallerArtifacts() } } @@ -396,7 +394,7 @@ final class VMInstallationViewModel: ObservableObject, @unchecked Sendable { private func startInstallation() async { switch machine?.configuration.systemType { case .mac: - await startMacInstallation() + startMacInstallation() case .linux: await startLinuxInstallation() case .none: @@ -450,7 +448,13 @@ final class VMInstallationViewModel: ObservableObject, @unchecked Sendable { @Published private(set) var virtualMachine: VZVirtualMachine? = nil - private func startMacInstallation() async { + private var installationTask: Task? + + private func startMacInstallation() { + installationTask = Task { await _runMacInstallation() } + } + + private func _runMacInstallation() async { guard let restoreURL = data.localRestoreImageURL else { state = .error("Missing local restore image URL") return @@ -479,23 +483,40 @@ final class VMInstallationViewModel: ObservableObject, @unchecked Sendable { } } - defer { - DispatchQueue.main.async { + @Sendable func cleanup() { + DispatchQueue.main.async { [weak self] in + guard let self else { return } self.stopPreventingAppTermination() self.library.loadMachines() + self.installationTask = nil + self.cleanupInstallerArtifacts() } } - do { - startPreventingAppTermination(forReason: "restoring virtual machine") + defer { cleanup() } - try await backend.install() + await withTaskCancellationHandler { + do { + startPreventingAppTermination(forReason: "restoring virtual machine") - self.machine?.metadata.installFinished = true + try await backend.install() - self.step = .done - } catch { - self.state = .error(error.localizedDescription) + try Task.checkCancellation() + + self.machine?.metadata.installFinished = true + + self.step = .done + + UILog("Installation task finished successfully") + } catch is CancellationError { + } catch { + UILog("Installation task finished with error \(error)") + + self.state = .error(error.localizedDescription) + } + } onCancel: { + UILog("Installation task cancelled") + cleanup() } } @@ -539,16 +560,21 @@ final class VMInstallationViewModel: ObservableObject, @unchecked Sendable { guard confirmed else { return false } - await MainActor.run { - self.downloader?.cancelDownload() - self.downloader = nil - self.installer = nil - } + await cancelInstallation() return true } } + func cancelInstallation() async { + UILog(#function) + + downloader?.cancelDownload() + downloader = nil + await installer?.cancel() + installationTask?.cancel() + } + } private extension VMInstallationViewModel.Step { diff --git a/VirtualUI/Source/Installer/VMInstallationWizard.swift b/VirtualUI/Source/Installer/VMInstallationWizard.swift index 8f4850d..eeb869c 100644 --- a/VirtualUI/Source/Installer/VMInstallationWizard.swift +++ b/VirtualUI/Source/Installer/VMInstallationWizard.swift @@ -120,6 +120,9 @@ public struct VMInstallationWizard: View { } .environment(\.containerPadding, Self.padding) .environment(\.maxContentWidth, effectiveMaxContentWidth) + .confirmBeforeClosingWindow { [weak viewModel] in + await viewModel?.confirmBeforeClosing() ?? true + } } private var dimBackground: Bool { diff --git a/VirtualUI/Source/Library/LibraryView.swift b/VirtualUI/Source/Library/LibraryView.swift index d8a34ac..be23b14 100644 --- a/VirtualUI/Source/Library/LibraryView.swift +++ b/VirtualUI/Source/Library/LibraryView.swift @@ -10,15 +10,14 @@ import VirtualCore public extension String { static let vb_libraryWindowID = "library" - static let vb_setupWindowID = "setup" } public struct LibraryView: View { @EnvironmentObject private var library: VMLibraryController @EnvironmentObject private var sessionManager: VirtualMachineSessionUIManager - @Environment(\.openWindow) - private var openWindow + @Environment(\.openCocoaWindow) + private var openCocoaWindow public init() { } @@ -93,7 +92,9 @@ public struct LibraryView: View { private var toolbarContents: some ToolbarContent { ToolbarItemGroup(placement: .primaryAction) { Button { - openWindow(id: .vb_setupWindowID) + openCocoaWindow { + VMInstallationWizard(library: library) + } } label: { Image(systemName: "plus") }