Files
VirtualBuddy/VirtualUI/Source/Session/Configuration/VMSessionConfigurationView.swift
Guilherme Rambo 2a7cf4151b New virtual machine state preservation system (#780)
* Fix HostingWindowController allowing repeated requests to close while waiting for confirmation

* New virtual machine state preservation system with automatic save on close/quit and safe restoration

* Use visible directories for saved state storage for easier debugging in Finder

* Hide state restoration controls for unsupported Linux VMs

* Add app-level preference for what happens by default when attempting to close a running VM

* Use a custom sheet for VM close confirmation

* Improve VM close choice sheet

* Harden session storage against path traversal, fix race conditions in state coordination
2026-10-05 12:51:43 -03:00

117 lines
4.2 KiB
Swift
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// VMSessionConfigurationView.swift
// VirtualBuddy
//
// Created by Guilherme Rambo on 07/04/22.
//
import SwiftUI
import VirtualCore
struct VMSessionConfigurationView: View {
@EnvironmentObject var controller: VMController
@State private var isShowingVMSettings = false
private var vm: VBVirtualMachine { controller.virtualMachineModel }
private var resolvedRestoreImage: ResolvedRestoreImage? {
let catalog = SoftwareCatalog.current(for: vm.configuration.systemType)
if let remoteURL = vm.metadata.remoteInstallImageURL,
let resolved = catalog.resolvedRestoreImage(matching: remoteURL, guestType: vm.configuration.systemType) {
return resolved
}
if let localURL = vm.metadata.installImageURL,
let resolved = catalog.resolvedRestoreImage(matching: localURL, guestType: vm.configuration.systemType) {
return resolved
}
return nil
}
var body: some View {
SelfSizingGroupedForm(minHeight: 100) {
if let savedSession = controller.savedSession {
SavedSessionDetailsSection(descriptor: savedSession)
} else if let issue = saveEligibilityIssue {
Label {
Text("Save & Close isn’t available. \(issue.explanation)")
.font(.caption)
.foregroundStyle(.secondary)
} icon: {
Image(systemName: "info.circle")
}
}
/// The boot mode of a saved session can't change. Discarding the session is the way to boot differently.
Group {
if showInstallDeviceOption {
Toggle("Boot on install drive", isOn: $controller.options.bootOnInstallDevice)
}
if showRecoveryModeOption {
Toggle("Boot in recovery mode", isOn: $controller.options.bootInRecoveryMode)
.disabled(controller.options.bootInDFUMode)
}
if showDFUOption {
Toggle("Boot in DFU mode", isOn: $controller.options.bootInDFUMode)
.disabled(controller.options.bootInRecoveryMode)
}
}
.disabled(controller.savedSession != nil)
Toggle("Capture system keyboard shortcuts", isOn: $controller.virtualMachineModel.configuration.captureSystemKeys)
Button("Virtual Machine Settings…") {
isShowingVMSettings.toggle()
}
.frame(maxWidth: .infinity, alignment: .trailing)
}
.padding(-12)
.clipShape(shape)
.airMaterialBackground(visualEffect: .hudWindow, glassEffect: .clear, in: shape)
.sheet(isPresented: $isShowingVMSettings) {
VMConfigurationSheet(
configuration: $controller.virtualMachineModel.configuration
)
.environmentObject(makeConfigurationViewModel())
}
}
private func makeConfigurationViewModel() -> VMConfigurationViewModel {
let viewModel = VMConfigurationViewModel(vm, resolvedRestoreImage: resolvedRestoreImage)
viewModel.discardSavedSessionHandler = { [weak controller] in try await controller?.discardSavedSession() }
return viewModel
}
private var shape: RoundedRectangle {
RoundedRectangle(cornerRadius: 18)
}
private var showInstallDeviceOption: Bool { vm.configuration.systemType == .linux && vm.metadata.installImageURL != nil }
private var showRecoveryModeOption: Bool { vm.configuration.systemType == .mac }
private var showDFUOption: Bool { VBMacConfiguration.appBuildAllowsDFUMode && vm.configuration.systemType == .mac }
/// Why saving the session isn't available for this virtual machine, shown before the user depends on it.
private var saveEligibilityIssue: SavedSessionEligibility.Issue? {
guard controller.saveEligibility.isApplicable else { return nil }
return controller.saveEligibility.primaryIssue
}
}
#if DEBUG
#Preview("Glass") {
VirtualMachineSessionViewPreview()
}
#Preview("No Glass") {
VirtualMachineSessionViewPreview()
.environment(\.preview_overrideLiquidGlassSupported, false)
}
#endif