mirror of
https://github.com/insidegui/VirtualBuddy.git
synced 2026-12-05 23:09:22 +08:00
Use openCocoaWindow for all installer windows, fix confirm before closing, fix installation not cleaned up properly when cancelled
This commit is contained in:
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -36,5 +36,9 @@ public final class SimulatedRestoreBackend: NSObject, RestoreBackend, @unchecked
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public func cancel() async {
|
||||
|
||||
}
|
||||
}
|
||||
#endif // DEBUG
|
||||
|
||||
@@ -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])
|
||||
|
||||
@@ -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<Content>: NSWindowController, NSWindowDelegate where Content: View {
|
||||
|
||||
|
||||
@@ -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<Void, Never>?
|
||||
|
||||
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 {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user