From 569f9a73a514abb40dc86a93b514d2613b86378e Mon Sep 17 00:00:00 2001 From: osy <50960678+osy@users.noreply.github.com> Date: Sun, 27 Jul 2025 18:49:41 -0700 Subject: [PATCH] vm: support disabling screenshots completely Resolves #6620 --- Platform/macOS/SettingsView.swift | 8 ++++++++ Services/UTMAppleVirtualMachine.swift | 8 +++++--- Services/UTMQemuVirtualMachine.swift | 4 +++- Services/UTMVirtualMachine.swift | 11 +++++++++-- 4 files changed, 25 insertions(+), 6 deletions(-) diff --git a/Platform/macOS/SettingsView.swift b/Platform/macOS/SettingsView.swift index 2f3df44b..b6b99d73 100644 --- a/Platform/macOS/SettingsView.swift +++ b/Platform/macOS/SettingsView.swift @@ -91,6 +91,7 @@ struct ApplicationSettingsView: View { } struct DisplaySettingsView: View { + @AppStorage("NoScreenshot") var isNoScreenshot = false @AppStorage("NoSaveScreenshot") var isNoSaveScreenshot = false @AppStorage("QEMURendererBackend") var qemuRendererBackend: UTMQEMURendererBackend = .qemuRendererBackendDefault @AppStorage("QEMURendererFPSLimit") var qemuRendererFpsLimit: Int = 0 @@ -98,9 +99,16 @@ struct DisplaySettingsView: View { var body: some View { Form { Section(header: Text("Display")) { + Toggle(isOn: $isNoScreenshot) { + Text("Disable VM screenshot") + }.help("No VM screenshots will be taken.") + .onChange(of: isNoScreenshot) { newValue in + isNoSaveScreenshot = newValue + } Toggle(isOn: $isNoSaveScreenshot) { Text("Do not save VM screenshot to disk") }.help("If enabled, any existing screenshot will be deleted the next time the VM is started.") + .disabled(isNoScreenshot) } Section(header: Text("QEMU Graphics Acceleration")) { diff --git a/Services/UTMAppleVirtualMachine.swift b/Services/UTMAppleVirtualMachine.swift index dfed3726..7b3a9942 100644 --- a/Services/UTMAppleVirtualMachine.swift +++ b/Services/UTMAppleVirtualMachine.swift @@ -326,9 +326,11 @@ final class UTMAppleVirtualMachine: UTMVirtualMachine { continuation.resume(throwing: UTMAppleVirtualMachineError.operationNotAvailable) return } - Task { @MainActor in - await self.takeScreenshot() - try? self.saveScreenshot() + if self.isScreenshotEnabled { + Task { @MainActor in + await self.takeScreenshot() + try? self.saveScreenshot() + } } apple.pause { result in continuation.resume(with: result) diff --git a/Services/UTMQemuVirtualMachine.swift b/Services/UTMQemuVirtualMachine.swift index 955f893f..2643ac49 100644 --- a/Services/UTMQemuVirtualMachine.swift +++ b/Services/UTMQemuVirtualMachine.swift @@ -538,7 +538,9 @@ extension UTMQemuVirtualMachine { guard let monitor = await monitor else { throw UTMQemuVirtualMachineError.invalidVmState } - await takeScreenshot() + if isScreenshotEnabled { + await takeScreenshot() + } try await monitor.qemuStop() } diff --git a/Services/UTMVirtualMachine.swift b/Services/UTMVirtualMachine.swift index f0c86638..0cfdad93 100644 --- a/Services/UTMVirtualMachine.swift +++ b/Services/UTMVirtualMachine.swift @@ -331,8 +331,12 @@ struct UTMVirtualMachineScreenshot { } extension UTMVirtualMachine { - private var isScreenshotSaveEnabled: Bool { - !UserDefaults.standard.bool(forKey: "NoSaveScreenshot") + nonisolated var isScreenshotEnabled: Bool { + !UserDefaults.standard.bool(forKey: "NoScreenshot") + } + + nonisolated private var isScreenshotSaveEnabled: Bool { + isScreenshotEnabled && !UserDefaults.standard.bool(forKey: "NoSaveScreenshot") } private var screenshotUrl: URL { @@ -349,6 +353,9 @@ extension UTMVirtualMachine { timer.invalidate() return } + guard self.isScreenshotEnabled else { + return + } if self.state == .started { Task { @MainActor in await self.takeScreenshot()