mirror of
https://github.com/insidegui/VirtualBuddy.git
synced 2026-12-02 11:49:39 +08:00
* New host<>guest transport/protocol implementation * Implemented minimal legacy guest protocol to maintain clipboard functionality for guests that can't run the latest VirtualBuddyGuest app * Fix missing dependency * Change VMBridge Swift package to remote * Disable VirtualBuddyGuest clipboard sharing support on legacy OSes, leave macOS 13 support for shared folder mount
159 lines
5.3 KiB
Swift
159 lines
5.3 KiB
Swift
//
|
|
// VMConfigurationViewModel.swift
|
|
// VirtualUI
|
|
//
|
|
// Created by Guilherme Rambo on 18/07/22.
|
|
//
|
|
|
|
import SwiftUI
|
|
import Combine
|
|
import VirtualCore
|
|
|
|
public enum VMConfigurationContext: Int {
|
|
case preInstall
|
|
case postInstall
|
|
|
|
/// Whether the configuration should be saved continuously as it's changed by the user.
|
|
var shouldAutoSave: Bool {
|
|
switch self {
|
|
case .preInstall: true
|
|
case .postInstall: false
|
|
}
|
|
}
|
|
}
|
|
|
|
public final class VMConfigurationViewModel: ObservableObject {
|
|
|
|
@Published var config: VBMacConfiguration {
|
|
didSet {
|
|
/// Reset display preset when changing display settings.
|
|
/// This is so the warning goes away, if any warning is being shown.
|
|
if config.hardware.displayDevices != oldValue.hardware.displayDevices,
|
|
config.hardware.displayDevices.first != selectedDisplayPreset?.device
|
|
{
|
|
selectedDisplayPreset = nil
|
|
}
|
|
}
|
|
}
|
|
|
|
@Published public internal(set) var supportState: VBMacConfiguration.SupportState = .supported
|
|
|
|
@Published public internal(set) var resolvedRestoreImage: ResolvedRestoreImage? {
|
|
didSet {
|
|
applyResolvedFeatureDefaultsIfNeeded()
|
|
}
|
|
}
|
|
|
|
@Published var selectedDisplayPreset: VBDisplayPreset?
|
|
|
|
@Published private(set) var vm: VBVirtualMachine
|
|
|
|
public let context: VMConfigurationContext
|
|
|
|
private var cancellables = Set<AnyCancellable>()
|
|
|
|
public init(_ vm: VBVirtualMachine, context: VMConfigurationContext = .postInstall, resolvedRestoreImage: ResolvedRestoreImage? = nil) {
|
|
self.config = vm.configuration
|
|
self.vm = vm
|
|
self.context = context
|
|
self.resolvedRestoreImage = resolvedRestoreImage
|
|
|
|
applyResolvedFeatureDefaultsIfNeeded()
|
|
|
|
Task { await validate() }
|
|
|
|
/// Automatically save configuration as it changes when in a pre-install context.
|
|
/// In a post-install context, configuration is only saved when the user confirms it.
|
|
if context.shouldAutoSave {
|
|
$config
|
|
.removeDuplicates()
|
|
.debounce(for: .milliseconds(500), scheduler: DispatchQueue.main)
|
|
.sink { [weak self] config in
|
|
do {
|
|
self?.vm.configuration = config
|
|
try self?.vm.saveMetadata()
|
|
} catch {
|
|
assert(ProcessInfo.isSwiftUIPreview, "Unexpected metadata write failure: \(error)")
|
|
}
|
|
}
|
|
.store(in: &cancellables)
|
|
}
|
|
}
|
|
|
|
@discardableResult
|
|
public func validate() async -> VBMacConfiguration.SupportState {
|
|
let updatedState = await config.validate(for: vm, skipVirtualizationConfig: context == .preInstall)
|
|
|
|
await MainActor.run {
|
|
supportState = updatedState
|
|
}
|
|
|
|
return updatedState
|
|
}
|
|
|
|
public func createImage(for device: VBStorageDevice) async throws {
|
|
guard let image = device.managedImage else {
|
|
throw Failure("Only managed disk images can be created.")
|
|
}
|
|
|
|
let settings = DiskImageGenerator.ImageSettings(for: image, in: vm)
|
|
|
|
try await DiskImageGenerator.generateImage(with: settings)
|
|
}
|
|
|
|
public func updateBootStorageDevice(with image: VBManagedDiskImage) {
|
|
guard let idx = config.hardware.storageDevices.firstIndex(where: { $0.isBootVolume }) else {
|
|
fatalError("Missing boot device in VM configuration")
|
|
}
|
|
|
|
var device = config.hardware.storageDevices[idx]
|
|
device.backing = .managedImage(image)
|
|
config.hardware.addOrUpdate(device)
|
|
}
|
|
|
|
}
|
|
|
|
// MARK: - Feature Defaults
|
|
|
|
private extension VMConfigurationViewModel {
|
|
func applyResolvedFeatureDefaultsIfNeeded() {
|
|
guard context == .preInstall else { return }
|
|
guard let resolvedRestoreImage else { return }
|
|
|
|
var updated = config
|
|
|
|
if resolvedRestoreImage.version < GuestAppSupport.minimumSystemVersion
|
|
|| resolvedRestoreImage.feature(id: CatalogFeatureID.guestApp)?.status.isUnsupported == true {
|
|
updated.guestAdditionsEnabled = false
|
|
}
|
|
|
|
if resolvedRestoreImage.feature(id: CatalogFeatureID.trackpad)?.status.isUnsupported == true,
|
|
updated.hardware.pointingDevice.kind == .trackpad
|
|
{
|
|
updated.hardware.pointingDevice.kind = .mouse
|
|
}
|
|
|
|
if resolvedRestoreImage.feature(id: CatalogFeatureID.macKeyboard)?.status.isUnsupported == true,
|
|
updated.hardware.keyboardDevice.kind == .mac
|
|
{
|
|
updated.hardware.keyboardDevice.kind = .generic
|
|
}
|
|
|
|
if resolvedRestoreImage.feature(id: CatalogFeatureID.displayResize)?.status.isUnsupported == true {
|
|
updated.hardware.displayDevices = updated.hardware.displayDevices.map { device in
|
|
var updatedDevice = device
|
|
updatedDevice.automaticallyReconfiguresDisplay = false
|
|
return updatedDevice
|
|
}
|
|
}
|
|
|
|
if resolvedRestoreImage.feature(id: CatalogFeatureID.rosettaSharing)?.status.isUnsupported == true {
|
|
updated.rosettaSharingEnabled = false
|
|
}
|
|
|
|
if updated != config {
|
|
config = updated
|
|
}
|
|
}
|
|
}
|