mirror of
https://github.com/insidegui/VirtualBuddy.git
synced 2026-12-02 01:29:39 +08:00
* Implement live configuration updates for shared folders * Allow display configuration changes on live VM * Improve live VM settings UI
492 lines
15 KiB
Swift
492 lines
15 KiB
Swift
//
|
|
// VMConfigurationView.swift
|
|
// VirtualBuddy
|
|
//
|
|
// Created by Guilherme Rambo on 17/07/22.
|
|
//
|
|
|
|
import SwiftUI
|
|
import VirtualCore
|
|
|
|
extension EnvironmentValues {
|
|
/// Type of guest that's currently being configured in a `VMConfigurationView`.
|
|
@Entry var configurationGuestType: VBGuestType = .mac
|
|
}
|
|
|
|
private struct ResolvedRestoreImageKey: EnvironmentKey {
|
|
static let defaultValue: ResolvedRestoreImage? = nil
|
|
}
|
|
|
|
extension EnvironmentValues {
|
|
var resolvedRestoreImage: ResolvedRestoreImage? {
|
|
get { self[ResolvedRestoreImageKey.self] }
|
|
set { self[ResolvedRestoreImageKey.self] = newValue }
|
|
}
|
|
}
|
|
|
|
enum CatalogFeatureID {
|
|
static let fileSharing = "file_sharing"
|
|
static let guestApp = "guest_app"
|
|
static let trackpad = "trackpad"
|
|
static let macKeyboard = "mac_keyboard"
|
|
static let stateRestoration = "state_restoration"
|
|
static let displayResize = "display_resize"
|
|
static let rosettaSharing = "rosetta_sharing"
|
|
static let provisioning = "provisioning"
|
|
static let usbPassthrough = "usb_passthrough"
|
|
}
|
|
|
|
extension ResolvedRestoreImage {
|
|
func feature(id: String) -> ResolvedVirtualizationFeature? {
|
|
features.first { $0.id == id }
|
|
}
|
|
}
|
|
|
|
extension ResolvedFeatureStatus {
|
|
var supportMessage: String? {
|
|
switch self {
|
|
case .supported:
|
|
return nil
|
|
case .warning(let title, let message), .unsupported(let title, let message):
|
|
return title ?? message
|
|
}
|
|
}
|
|
|
|
var supportMessageColor: Color {
|
|
switch self {
|
|
case .supported:
|
|
return .secondary
|
|
case .warning:
|
|
return .yellow
|
|
case .unsupported:
|
|
return .red
|
|
}
|
|
}
|
|
}
|
|
|
|
struct VMConfigurationView: View {
|
|
@EnvironmentObject private var viewModel: VMConfigurationViewModel
|
|
|
|
@Environment(VMTemplatesController.self) private var templatesController
|
|
|
|
var initialConfiguration: VBMacConfiguration
|
|
|
|
static var labelSpacing: CGFloat { 2 }
|
|
|
|
@AppStorage("config.general.collapsed")
|
|
private var generalCollapsed = true
|
|
|
|
@AppStorage("config.provisioning.collapsed")
|
|
private var provisioningCollapsed = true
|
|
|
|
@AppStorage("config.storage.collapsed")
|
|
private var storageCollapsed = true
|
|
|
|
@AppStorage("config.usbDevices.collapsed")
|
|
private var usbDevicesCollapsed = true
|
|
|
|
@AppStorage("config.display.collapsed")
|
|
private var displayCollapsed = true
|
|
|
|
@AppStorage("config.pointing.collapsed")
|
|
private var pointingCollapsed = true
|
|
|
|
@AppStorage("config.keyboard.collapsed")
|
|
private var keyboardCollapsed = true
|
|
|
|
@AppStorage("config.network.collapsed")
|
|
private var networkCollapsed = true
|
|
|
|
@AppStorage("config.sound.collapsed")
|
|
private var soundCollapsed = true
|
|
|
|
@AppStorage("config.sharing.collapsed")
|
|
private var sharingCollapsed = true
|
|
|
|
@AppStorage("config.guestApp.collapsed")
|
|
private var guestAppCollapsed = true
|
|
|
|
private var systemType: VBGuestType { viewModel.config.systemType }
|
|
|
|
private var showBootDiskSection: Bool { viewModel.context == .preInstall }
|
|
|
|
private var showPointingDeviceSection: Bool { systemType.supportsVirtualTrackpad }
|
|
|
|
private var showKeyboardDeviceSection: Bool { systemType.supportsKeyboardCustomization }
|
|
|
|
private var showDisplayPPISection: Bool { systemType.supportsDisplayPPI }
|
|
|
|
private var showGuestAppSection: Bool { systemType.supportsGuestApp }
|
|
|
|
private var showProvisioningSection: Bool { systemType.supportsProvisioning }
|
|
|
|
private var showTemplatePicker: Bool { templatesController.hasTemplates(for: viewModel.config.systemType) }
|
|
|
|
var body: some View {
|
|
VStack(alignment: .leading, spacing: 16) {
|
|
if viewModel.isLockedBySavedSession {
|
|
savedSessionNotice
|
|
}
|
|
|
|
Group {
|
|
if showTemplatePicker {
|
|
templatePicker
|
|
}
|
|
|
|
if showBootDiskSection {
|
|
bootDisk
|
|
}
|
|
|
|
general
|
|
|
|
if showProvisioningSection {
|
|
provisioning
|
|
}
|
|
|
|
storage
|
|
|
|
usbDevices
|
|
|
|
display
|
|
|
|
if showPointingDeviceSection {
|
|
pointingDevice
|
|
}
|
|
|
|
if showKeyboardDeviceSection {
|
|
keyboardDevice
|
|
}
|
|
|
|
network
|
|
|
|
sound
|
|
|
|
if showGuestAppSection {
|
|
guestApp
|
|
}
|
|
|
|
sharing
|
|
}
|
|
/// This is required so that contents that have long text content won't cause the width to expand automatically.
|
|
.frame(minWidth: 0, idealWidth: VMConfigurationSheet.minWidth)
|
|
.disabled(viewModel.isLockedBySavedSession)
|
|
}
|
|
.font(.system(size: 12))
|
|
.environment(\.vmConfigurationContext, viewModel.context)
|
|
.environment(\.configurationGuestType, viewModel.config.systemType)
|
|
.environment(\.resolvedRestoreImage, viewModel.resolvedRestoreImage)
|
|
}
|
|
|
|
@ViewBuilder
|
|
private var savedSessionNotice: some View {
|
|
VStack(alignment: .leading, spacing: 8) {
|
|
Label("Resume and shut down this VM to change its hardware.", systemImage: "lock.fill")
|
|
.font(.headline)
|
|
|
|
Text("This virtual machine has a saved session, which only works with the hardware it was saved with. You can also discard the saved session to unlock these settings.")
|
|
.foregroundStyle(.secondary)
|
|
|
|
Button("Discard Saved Session…", role: .destructive) {
|
|
Task { @MainActor in
|
|
guard await SavedSessionPrompts.confirmDiscard(name: viewModel.vmName, startsAfterwards: false, from: nil) else { return }
|
|
|
|
do {
|
|
try await viewModel.discardSavedSession()
|
|
} catch {
|
|
NSAlert(error: error).runModal()
|
|
}
|
|
}
|
|
}
|
|
}
|
|
.padding(12)
|
|
.frame(maxWidth: .infinity, alignment: .leading)
|
|
.background(.quaternary, in: RoundedRectangle(cornerRadius: 8, style: .continuous))
|
|
}
|
|
|
|
@ViewBuilder
|
|
private var templatePicker: some View {
|
|
ConfigurationSection(.constant(false)) {
|
|
VMConfigurationTemplatePicker(
|
|
controller: templatesController,
|
|
context: viewModel.context,
|
|
configuration: $viewModel.config
|
|
) { updatedConfiguration in
|
|
if let image = viewModel.config.hardware.storageDevices.first(where: { $0.isBootVolume })?.managedImage {
|
|
viewModel.updateBootStorageDevice(with: image)
|
|
}
|
|
}
|
|
} header: {
|
|
SummaryHeader("Copy Configuration", systemImage: "square.on.square")
|
|
}
|
|
}
|
|
|
|
@ViewBuilder
|
|
private var general: some View {
|
|
ConfigurationSection($generalCollapsed) {
|
|
HardwareConfigurationView(device: $viewModel.config.hardware)
|
|
} header: {
|
|
SummaryHeader(
|
|
"General",
|
|
systemImage: "memorychip",
|
|
summary: viewModel.config.generalSummary
|
|
)
|
|
}
|
|
}
|
|
|
|
@ViewBuilder
|
|
private var provisioning: some View {
|
|
ConfigurationSection($provisioningCollapsed) {
|
|
ProvisioningConfigurationView(
|
|
configuration: $viewModel.config,
|
|
contextForbidden: viewModel.context != .preInstall && viewModel.vm.hasBootedNonRecoveryAtLeastOnce
|
|
)
|
|
} header: {
|
|
SummaryHeader(
|
|
"Skip Setup Assistant",
|
|
systemImage: "person.crop.circle",
|
|
summary: viewModel.config.provisioningSummary
|
|
)
|
|
}
|
|
}
|
|
|
|
@ViewBuilder
|
|
private var bootDisk: some View {
|
|
ConfigurationSection(.constant(false), collapsingDisabled: true) {
|
|
if let image = (try? viewModel.vm.bootDevice)?.managedImage {
|
|
ManagedDiskImageEditor(image: image, isExistingDiskImage: false, isForBootVolume: true) { image in
|
|
viewModel.updateBootStorageDevice(with: image)
|
|
}
|
|
} else {
|
|
Text("Something went terribly wrong: VM doesn't have a boot storage device with a managed disk image.")
|
|
.foregroundColor(.red)
|
|
}
|
|
} header: {
|
|
SummaryHeader(
|
|
"Boot Disk",
|
|
systemImage: "wrench.and.screwdriver"
|
|
)
|
|
}
|
|
}
|
|
|
|
private var storageSummary: String {
|
|
if showBootDiskSection {
|
|
return viewModel.config.hardware.storageDevices.count == 1 ? "None" : viewModel.config.storageSummary
|
|
} else {
|
|
return viewModel.config.storageSummary
|
|
}
|
|
}
|
|
|
|
@ViewBuilder
|
|
private var storage: some View {
|
|
ConfigurationSection($storageCollapsed) {
|
|
StorageConfigurationView(hardware: $viewModel.config.hardware)
|
|
.environmentObject(viewModel)
|
|
} header: {
|
|
SummaryHeader(
|
|
showBootDiskSection ? "Additional Storage" : "Storage",
|
|
systemImage: "externaldrive",
|
|
summary: storageSummary
|
|
)
|
|
}
|
|
.contextMenu {
|
|
Button("Reset General Settings") {
|
|
viewModel.config.hardware.cpuCount = initialConfiguration.hardware.cpuCount
|
|
viewModel.config.hardware.memorySize = initialConfiguration.hardware.memorySize
|
|
}
|
|
}
|
|
}
|
|
|
|
@ViewBuilder
|
|
private var usbDevices: some View {
|
|
ConfigurationSection($usbDevicesCollapsed) {
|
|
USBDevicesConfigurationView(hardware: $viewModel.config.hardware)
|
|
} header: {
|
|
SummaryHeader(
|
|
"USB Devices",
|
|
systemImage: "cable.connector",
|
|
summary: viewModel.config.usbDevicesSummary
|
|
)
|
|
}
|
|
}
|
|
|
|
@ViewBuilder
|
|
private var display: some View {
|
|
ConfigurationSection($displayCollapsed) {
|
|
DisplayConfigurationView(
|
|
device: $viewModel.config.hardware.displayDevices[0],
|
|
selectedPreset: $viewModel.selectedDisplayPreset,
|
|
canChangePPI: showDisplayPPISection
|
|
)
|
|
} header: {
|
|
SummaryHeader("Display", systemImage: "display", summary: viewModel.config.displaySummary) {
|
|
DisplayConfigurationView(
|
|
device: $viewModel.config.hardware.displayDevices[0],
|
|
selectedPreset: $viewModel.selectedDisplayPreset,
|
|
canChangePPI: showDisplayPPISection
|
|
)
|
|
.presetPicker
|
|
.frame(width: 24)
|
|
}
|
|
}
|
|
}
|
|
|
|
@ViewBuilder
|
|
private var pointingDevice: some View {
|
|
ConfigurationSection($pointingCollapsed) {
|
|
PointingDeviceConfigurationView(hardware: $viewModel.config.hardware)
|
|
} header: {
|
|
SummaryHeader(
|
|
"Pointing Device",
|
|
systemImage: "cursorarrow",
|
|
summary: viewModel.config.pointingDeviceSummary
|
|
)
|
|
}
|
|
}
|
|
|
|
@ViewBuilder
|
|
private var keyboardDevice: some View {
|
|
ConfigurationSection($keyboardCollapsed) {
|
|
KeyboardDeviceConfigurationView(hardware: $viewModel.config.hardware)
|
|
} header: {
|
|
SummaryHeader(
|
|
"Keyboard Device",
|
|
systemImage: "keyboard",
|
|
summary: viewModel.config.keyboardDeviceSummary
|
|
)
|
|
}
|
|
}
|
|
|
|
@ViewBuilder
|
|
private var network: some View {
|
|
ConfigurationSection($networkCollapsed) {
|
|
NetworkConfigurationView(hardware: $viewModel.config.hardware)
|
|
} header: {
|
|
SummaryHeader(
|
|
"Network",
|
|
systemImage: "network",
|
|
summary: viewModel.config.networkSummary
|
|
)
|
|
}
|
|
}
|
|
|
|
@ViewBuilder
|
|
private var sound: some View {
|
|
ConfigurationSection($soundCollapsed) {
|
|
SoundConfigurationView(hardware: $viewModel.config.hardware)
|
|
} header: {
|
|
SummaryHeader(
|
|
"Sound",
|
|
systemImage: viewModel.config.hardware.soundDevices.isEmpty ? "speaker.slash" : "speaker.3",
|
|
summary: viewModel.config.soundSummary
|
|
)
|
|
}
|
|
}
|
|
|
|
@ViewBuilder
|
|
private var sharing: some View {
|
|
ConfigurationSection($sharingCollapsed) {
|
|
SharingConfigurationView(configuration: $viewModel.config)
|
|
} header: {
|
|
SummaryHeader(
|
|
"Sharing",
|
|
systemImage: "folder",
|
|
summary: viewModel.config.sharingSummary
|
|
)
|
|
}
|
|
}
|
|
|
|
@ViewBuilder
|
|
private var guestApp: some View {
|
|
ConfigurationSection($guestAppCollapsed) {
|
|
GuestAppConfigurationView(configuration: $viewModel.config)
|
|
} header: {
|
|
SummaryHeader(
|
|
"Guest App",
|
|
summary: viewModel.config.guestAppSummary(for: viewModel.resolvedRestoreImage?.version)
|
|
) {
|
|
Image(.guestSymbol)
|
|
.resizable()
|
|
.aspectRatio(contentMode: .fit)
|
|
.frame(width: 15)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
struct VMConfigurationTemplatePicker: View {
|
|
let controller: VMTemplatesController
|
|
let context: VMConfigurationContext
|
|
@Binding var configuration: VBMacConfiguration
|
|
var onApply: (_ configuration: VBMacConfiguration) -> ()
|
|
|
|
var templates: [VBConfigurationTemplate] {
|
|
switch configuration.systemType {
|
|
case .mac: controller.templatesForMacGuest
|
|
case .linux: controller.templatesForLinuxGuest
|
|
}
|
|
}
|
|
|
|
@State private var selectedTemplateID: VBConfigurationTemplate.ID?
|
|
@State private var buttonNeedsAttention = false
|
|
|
|
private var selectedTemplate: VBConfigurationTemplate? {
|
|
selectedTemplateID.flatMap { controller.template(id: $0) }
|
|
}
|
|
|
|
var body: some View {
|
|
HStack {
|
|
Picker("Copy configuration", selection: $selectedTemplateID) {
|
|
Text("Choose existing configuration…")
|
|
.tag(Optional<VBConfigurationTemplate.ID>.none)
|
|
|
|
ForEach(templates) { template in
|
|
Text(template.name)
|
|
.tag(Optional<VBConfigurationTemplate.ID>.some(template.id))
|
|
}
|
|
}
|
|
.labelsHidden()
|
|
|
|
Spacer()
|
|
|
|
Button("Apply") {
|
|
applySelection()
|
|
}
|
|
.modifier(AttentionBounceViewModifier(enabled: buttonNeedsAttention))
|
|
.disabled(selectedTemplate == nil)
|
|
}
|
|
.onChange(of: selectedTemplateID) { _, newValue in
|
|
buttonNeedsAttention = newValue != nil
|
|
}
|
|
}
|
|
|
|
private func applySelection() {
|
|
guard let selectedTemplate else { return }
|
|
|
|
do {
|
|
var updatedConfiguration = configuration
|
|
try updatedConfiguration.apply(
|
|
template: selectedTemplate,
|
|
includingStorageDevices: context == .preInstall
|
|
)
|
|
|
|
configuration = updatedConfiguration
|
|
|
|
onApply(updatedConfiguration)
|
|
|
|
buttonNeedsAttention = false
|
|
} catch {
|
|
NSApp.presentError(error)
|
|
}
|
|
}
|
|
}
|
|
|
|
// MARK: - Section Header
|
|
|
|
#if DEBUG
|
|
struct VMConfigurationView_Previews: PreviewProvider {
|
|
static var previews: some View {
|
|
VMConfigurationSheet_Previews.previews
|
|
}
|
|
}
|
|
#endif
|