Files
Snaggle/GreenshotMac/Core/Capture.swift
T
2026-09-17 15:15:45 -05:00

78 lines
2.3 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Capture.swift
// GreenshotMac
//
// Equivalent of Greenshot's ICapture / ICaptureDetails.
import AppKit
enum CaptureMode: String {
case region
case fullscreen
case window
case lastRegion
case importedFile
}
/// Metadata that travels with a capture; used by the filename pattern engine.
struct CaptureDetails {
var captureMode: CaptureMode = .region
var dateTime: Date = Date()
/// Title of the captured window, or the screen name for a full screen capture.
var title: String = ""
/// Bundle identifier of the captured application, when known.
var applicationBundleID: String?
/// Application name of the captured window, when known.
var applicationName: String?
/// Screen rectangle the capture was taken from, in global (top-left origin) coordinates.
var screenBounds: CGRect = .zero
/// Scale factor of the display the capture came from.
var scaleFactor: CGFloat = 2.0
/// Set once the capture has been written to disk.
var filePath: String?
}
/// A screenshot plus its metadata.
final class Capture {
var image: CGImage
var details: CaptureDetails
init(image: CGImage, details: CaptureDetails) {
self.image = image
self.details = details
}
var pixelSize: CGSize {
CGSize(width: image.width, height: image.height)
}
/// Size in points, i.e. pixels divided by the capture's backing scale factor.
var pointSize: CGSize {
CGSize(width: CGFloat(image.width) / details.scaleFactor,
height: CGFloat(image.height) / details.scaleFactor)
}
var nsImage: NSImage {
NSImage(cgImage: image, size: pointSize)
}
}
enum CaptureError: LocalizedError {
case permissionDenied
case noContent
case cancelled
case captureFailed(String)
var errorDescription: String? {
switch self {
case .permissionDenied:
return "GreenshotMac needs Screen Recording permission. Open System Settings › Privacy & Security › Screen Recording and enable GreenshotMac."
case .noContent:
return "Nothing was available to capture."
case .cancelled:
return "Capture cancelled."
case .captureFailed(let message):
return message
}
}
}