Files
VirtualBuddy/VirtualCore/Source/Virtualization/Helpers/VBDisplayDevice+LiveConfiguration.swift
Guilherme Rambo ff855c2808 Allow shared folders and display configuration changes on live VM (#806)
* Implement live configuration updates for shared folders

* Allow display configuration changes on live VM

* Improve live VM settings UI
2026-11-04 13:31:55 -03:00

38 lines
1.6 KiB
Swift

import Foundation
import Virtualization
extension VBDisplayDevice {
func validatedLiveDisplay(replacing current: VBDisplayDevice, guestType: VBGuestType) throws -> VBDisplayDevice {
guard id == current.id else { throw Failure("A live display's identity can't be changed.") }
if self == current { return current }
guard Self.displayWidthRange.contains(width), Self.displayHeightRange.contains(height),
!guestType.supportsDisplayPPI || Self.displayPPIRange.contains(pixelsPerInch) else {
throw Failure("The requested display dimensions or pixel density are outside the supported range.")
}
var updated = self
updated.name = current.name
if !guestType.supportsDisplayPPI { updated.pixelsPerInch = current.pixelsPerInch }
return updated
}
@available(macOS 14.0, *)
func runtimeDisplayConfiguration(for guestType: VBGuestType) -> VZGraphicsDisplayConfiguration {
switch guestType {
case .mac: vzDisplay
case .linux: vzScanout
}
}
}
extension VBMacConfiguration {
/// A saved display preference may change geometry, but cannot add, remove, or reorder displays.
func restoringLiveDisplays(_ displays: [VBDisplayDevice]) throws -> [VBDisplayDevice] {
guard displays.map(\.id) == hardware.displayDevices.map(\.id) else {
throw SavedSessionError.configurationChanged("The saved display identities are different.")
}
return try zip(displays, hardware.displayDevices).map {
try $0.validatedLiveDisplay(replacing: $1, guestType: systemType)
}
}
}