-
-
Notifications
You must be signed in to change notification settings - Fork 176
Add feature to disable VM screenshots #577
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
gabefair
wants to merge
3
commits into
insidegui:main
Choose a base branch
from
gabefair:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
216 changes: 216 additions & 0 deletions
216
VirtualUI/Source/Session/Components/VMSnapshotHoverOverlay.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,216 @@ | ||
// | ||
// VMSnapshotHoverOverlay.swift | ||
// VirtualUI | ||
// | ||
// Created for VirtualBuddy VM snapshot hover functionality. | ||
// | ||
|
||
import SwiftUI | ||
import VirtualCore | ||
import AppKit | ||
import UniformTypeIdentifiers | ||
|
||
struct VMSnapshotHoverOverlay: View { | ||
@EnvironmentObject private var controller: VMController | ||
@State private var isHovered = false | ||
|
||
var body: some View { | ||
GeometryReader { geometry in | ||
ZStack { | ||
// Invisible full coverage area for hover detection | ||
Rectangle() | ||
.fill(Color.clear) | ||
.contentShape(Rectangle()) | ||
|
||
if isHovered { | ||
// Semi-transparent overlay | ||
Rectangle() | ||
.fill(Color.black.opacity(0.4)) | ||
.transition(.opacity) | ||
|
||
// Button container | ||
VStack(spacing: 16) { | ||
Spacer() | ||
|
||
HStack(spacing: 16) { | ||
// Save Image button | ||
Button { | ||
saveScreenshotToHost() | ||
} label: { | ||
HStack(spacing: 8) { | ||
Image(systemName: "square.and.arrow.down") | ||
Text("Save Image") | ||
} | ||
.font(.system(size: 14, weight: .medium)) | ||
.padding(.horizontal, 16) | ||
.padding(.vertical, 8) | ||
} | ||
.buttonStyle(OverlayButtonStyle()) | ||
|
||
// Disable Preview button | ||
Button { | ||
toggleScreenshotGeneration() | ||
} label: { | ||
HStack(spacing: 8) { | ||
Image(systemName: controller.virtualMachineModel.metadata.screenshotGenerationEnabled ? "eye.slash" : "eye") | ||
Text(controller.virtualMachineModel.metadata.screenshotGenerationEnabled ? "Disable Previews" : "Enable Previews") | ||
} | ||
.font(.system(size: 14, weight: .medium)) | ||
.padding(.horizontal, 16) | ||
.padding(.vertical, 8) | ||
} | ||
.buttonStyle(OverlayButtonStyle()) | ||
} | ||
|
||
Spacer() | ||
} | ||
} | ||
} | ||
} | ||
.onHover { hovering in | ||
withAnimation(.easeInOut(duration: 0.2)) { | ||
isHovered = hovering | ||
} | ||
} | ||
} | ||
|
||
private func saveScreenshotToHost() { | ||
guard let screenshot = controller.virtualMachineModel.screenshot else { | ||
NSAlert.runInformationAlert( | ||
title: "No Screenshot Available", | ||
message: "There is no screenshot available for this virtual machine." | ||
) | ||
return | ||
} | ||
|
||
let savePanel = NSSavePanel() | ||
savePanel.title = "Save VM Screenshot" | ||
savePanel.nameFieldStringValue = "\(controller.virtualMachineModel.name) Screenshot" | ||
savePanel.allowedContentTypes = [.png, .jpeg, .heic] | ||
savePanel.canCreateDirectories = true | ||
|
||
savePanel.begin { response in | ||
guard response == .OK, let url = savePanel.url else { return } | ||
|
||
// Convert to the appropriate format based on file extension | ||
let fileExtension = url.pathExtension.lowercased() | ||
let success: Bool | ||
|
||
switch fileExtension { | ||
case "png": | ||
success = screenshot.pngWrite(to: url) | ||
case "jpg", "jpeg": | ||
success = screenshot.jpegWrite(to: url, compressionFactor: 0.9) | ||
case "heic": | ||
success = (try? screenshot.vb_encodeHEIC(to: url)) != nil | ||
default: | ||
// Default to PNG | ||
success = screenshot.pngWrite(to: url.appendingPathExtension("png")) | ||
} | ||
|
||
if !success { | ||
NSAlert.runErrorAlert( | ||
title: "Save Failed", | ||
message: "Failed to save the screenshot to \(url.path)." | ||
) | ||
} | ||
} | ||
} | ||
|
||
private func toggleScreenshotGeneration() { | ||
let currentlyEnabled = controller.virtualMachineModel.metadata.screenshotGenerationEnabled | ||
controller.virtualMachineModel.metadata.screenshotGenerationEnabled = !currentlyEnabled | ||
|
||
// Force save the metadata | ||
do { | ||
try controller.virtualMachineModel.saveMetadata() | ||
} catch { | ||
NSAlert.runErrorAlert( | ||
title: "Settings Save Failed", | ||
message: "Failed to save the screenshot generation setting: \(error.localizedDescription)" | ||
) | ||
// Revert the change | ||
controller.virtualMachineModel.metadata.screenshotGenerationEnabled = currentlyEnabled | ||
} | ||
} | ||
} | ||
|
||
// Custom button style for overlay buttons | ||
private struct OverlayButtonStyle: ButtonStyle { | ||
func makeBody(configuration: Configuration) -> some View { | ||
configuration.label | ||
.background( | ||
RoundedRectangle(cornerRadius: 8) | ||
.fill(Material.thick) | ||
.shadow(color: .black.opacity(0.3), radius: 4, x: 0, y: 2) | ||
) | ||
.foregroundColor(.primary) | ||
.scaleEffect(configuration.isPressed ? 0.95 : 1.0) | ||
.animation(.easeInOut(duration: 0.1), value: configuration.isPressed) | ||
} | ||
} | ||
|
||
// Extensions for NSImage saving | ||
private extension NSImage { | ||
func pngWrite(to url: URL) -> Bool { | ||
guard let tiffRepresentation = tiffRepresentation, | ||
let bitmapImage = NSBitmapImageRep(data: tiffRepresentation), | ||
let pngData = bitmapImage.representation(using: .png, properties: [:]) else { | ||
return false | ||
} | ||
|
||
do { | ||
try pngData.write(to: url) | ||
return true | ||
} catch { | ||
return false | ||
} | ||
} | ||
|
||
func jpegWrite(to url: URL, compressionFactor: CGFloat = 0.9) -> Bool { | ||
guard let tiffRepresentation = tiffRepresentation, | ||
let bitmapImage = NSBitmapImageRep(data: tiffRepresentation), | ||
let jpegData = bitmapImage.representation(using: .jpeg, properties: [.compressionFactor: compressionFactor]) else { | ||
return false | ||
} | ||
|
||
do { | ||
try jpegData.write(to: url) | ||
return true | ||
} catch { | ||
return false | ||
} | ||
} | ||
} | ||
|
||
// Extensions for NSAlert convenience | ||
private extension NSAlert { | ||
static func runInformationAlert(title: String, message: String) { | ||
let alert = NSAlert() | ||
alert.messageText = title | ||
alert.informativeText = message | ||
alert.alertStyle = .informational | ||
alert.addButton(withTitle: "OK") | ||
alert.runModal() | ||
} | ||
|
||
static func runErrorAlert(title: String, message: String) { | ||
let alert = NSAlert() | ||
alert.messageText = title | ||
alert.informativeText = message | ||
alert.alertStyle = .critical | ||
alert.addButton(withTitle: "OK") | ||
alert.runModal() | ||
} | ||
} | ||
|
||
#if DEBUG | ||
struct VMSnapshotHoverOverlay_Previews: PreviewProvider { | ||
static var previews: some View { | ||
VMSnapshotHoverOverlay() | ||
.environmentObject(VMController.preview) | ||
.frame(width: 400, height: 300) | ||
.background(Color.blue.opacity(0.3)) | ||
} | ||
} | ||
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -139,25 +139,35 @@ public struct VirtualMachineSessionView: View { | |
} | ||
} | ||
.animation(.bouncy, value: controller.state) | ||
|
||
// Add hover overlay for screenshot functionality | ||
VMSnapshotHoverOverlay() | ||
.environmentObject(controller) | ||
} | ||
} | ||
|
||
private func startableStateView(with error: Error?) -> some View { | ||
VStack(spacing: 28) { | ||
if let error = error { | ||
Text("The machine has stopped due to an error: \(String(describing: error))") | ||
.multilineTextAlignment(.center) | ||
.foregroundStyle(.secondary) | ||
.lineLimit(nil) | ||
.font(.caption) | ||
ZStack { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Zstack is used to add layers ontop of the views (z-axis layers). In this case I am adding a hover over menu that needs to cover the entire VM preview area without changing it. |
||
VStack(spacing: 28) { | ||
if let error = error { | ||
Text("The machine has stopped due to an error: \(String(describing: error))") | ||
.multilineTextAlignment(.center) | ||
.foregroundStyle(.secondary) | ||
.lineLimit(nil) | ||
.font(.caption) | ||
} | ||
|
||
circularStartButton | ||
|
||
VMSessionConfigurationView() | ||
.environment(\.backgroundMaterial, Material.thin) | ||
.environmentObject(controller) | ||
.frame(maxWidth: 400) | ||
} | ||
|
||
circularStartButton | ||
|
||
VMSessionConfigurationView() | ||
.environment(\.backgroundMaterial, Material.thin) | ||
// Add hover overlay for screenshot functionality when VM is stopped/idle | ||
VMSnapshotHoverOverlay() | ||
.environmentObject(controller) | ||
.frame(maxWidth: 400) | ||
} | ||
} | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added for backwards compatibility