Files
shotdeck/Sources/ShotdeckCore/Capture/ScreenCapturer.swift
Claude Fable 5 3fb9b504f4 feat(capture): choose what to capture — area, window, browser page or screen
Capture is now a decision, not a hidden setting. The popover shows a four-way scope control with the target named under it (800 x 600 on Display 1, the frontmost window, the front browser window, the whole display), and the window header carries the same choice beside the verbs. Picking Window or Browser hides Redline, dims the screen, names the app under the pointer and highlights its window; a click captures exactly that window, Escape cancels, and the desktop highlights the whole screen. New Session is one click: the current captures stay in their own folder, a fresh session opens, and the area picker comes straight up with the saved area pre-selected — Return keeps it, a drag replaces it. Settings is consolidated into one door (the popover gear, plus Cmd-,): what to capture, saved area, shortcut, ask-on-new-session, sending, session age limit, and a new Updates group with automatic install (on by default; installs only when nothing is unsent). The footer filler and the extra-actions menu are gone.
2026-11-01 11:47:58 +04:00

248 lines
9.7 KiB
Swift

import ScreenCaptureKit
import CoreGraphics
import ImageIO
import UniformTypeIdentifiers
import AppKit // NSScreen only, to read backingScaleFactor as a fallback
import Foundation
public struct CapturedImage: Sendable {
public let pngData: Data
public let pixelWidth: Int
public let pixelHeight: Int
public let scale: CGFloat
}
public struct ScreenCapturer: Sendable {
public init() {}
/// Does NOT prompt. Reports the current Screen Recording grant.
public static var isScreenRecordingGranted: Bool { CGPreflightScreenCaptureAccess() }
/// Captures whatever the given scope points at, resolving it right now.
public func capture(
_ scope: CaptureScope,
savedArea: CaptureRegion?
) async throws -> CapturedImage {
guard Self.isScreenRecordingGranted else {
throw ShotdeckError.screenRecordingNotGranted
}
let frontmostPID = NSWorkspace.shared.frontmostApplication?.processIdentifier ?? 0
let target = try CaptureTargetResolver.resolve(
scope: scope,
savedArea: savedArea,
frontmostPID: frontmostPID,
candidates: Self.onScreenWindows()
)
return try await capture(target)
}
/// Captures one resolved target. A window is captured window-only (so an overlapping
/// window never ends up in a review shot); a region or a display goes through the
/// display path.
public func capture(_ target: CaptureTarget) async throws -> CapturedImage {
guard Self.isScreenRecordingGranted else {
throw ShotdeckError.screenRecordingNotGranted
}
switch target {
case .region(let region):
return try await capture(region)
case .display(let displayID):
let bounds = CGDisplayBounds(displayID)
return try await capture(
CaptureRegion(
displayID: displayID, rect: bounds,
capturedScale: Self.backingScale(for: displayID)))
case .window(let windowID, let frame, let displayID):
return try await captureWindow(
windowID: windowID, frame: frame, displayID: displayID)
}
}
/// On-screen windows, front-most first, as plain data for the resolver.
public static func onScreenWindows() -> [WindowCandidate] {
guard let raw = CGWindowListCopyWindowInfo(
[.optionOnScreenOnly, .excludeDesktopElements], kCGNullWindowID
) as? [[String: Any]] else { return [] }
return raw.compactMap { entry in
guard let windowID = (entry[kCGWindowNumber as String] as? NSNumber)?.uint32Value,
let ownerPID = (entry[kCGWindowOwnerPID as String] as? NSNumber)?.int32Value,
let boundsDict = entry[kCGWindowBounds as String] as? [String: Any],
let layer = (entry[kCGWindowLayer as String] as? NSNumber)?.intValue
else { return nil }
let frame = CGRect(
x: boundsDict["X"] as? Double ?? 0,
y: boundsDict["Y"] as? Double ?? 0,
width: boundsDict["Width"] as? Double ?? 0,
height: boundsDict["Height"] as? Double ?? 0
)
return WindowCandidate(
windowID: windowID,
ownerBundleID: bundleIdentifier(forPID: ownerPID),
ownerPID: ownerPID,
frame: frame,
layer: layer
)
}
}
private static func bundleIdentifier(forPID pid: Int32) -> String? {
NSRunningApplication(processIdentifier: pid)?.bundleIdentifier
}
private static func backingScale(for displayID: UInt32) -> CGFloat {
guard let mode = CGDisplayCopyDisplayMode(displayID), mode.width > 0 else { return 2 }
return CGFloat(mode.pixelWidth) / CGFloat(mode.width)
}
/// Captures a single window through ScreenCaptureKit's window filter, at the display's
/// backing scale, without the window server compositing anything on top of it.
private func captureWindow(
windowID: UInt32,
frame: CGRect,
displayID: UInt32
) async throws -> CapturedImage {
let content: SCShareableContent
do {
content = try await SCShareableContent.excludingDesktopWindows(
false, onScreenWindowsOnly: true)
} catch {
throw ShotdeckError.captureFailed(underlying: error.localizedDescription)
}
guard let window = content.windows.first(where: { $0.windowID == windowID }) else {
throw ShotdeckError.noFrontWindow
}
let scale = Self.backingScale(for: displayID)
let filter = SCContentFilter(desktopIndependentWindow: window)
let configuration = SCStreamConfiguration()
configuration.width = Int((frame.width * scale).rounded())
configuration.height = Int((frame.height * scale).rounded())
configuration.showsCursor = false
configuration.scalesToFit = false
let cgImage: CGImage
do {
cgImage = try await SCScreenshotManager.captureImage(
contentFilter: filter, configuration: configuration)
} catch {
throw ShotdeckError.captureFailed(underlying: error.localizedDescription)
}
let data = NSMutableData()
guard let dest = CGImageDestinationCreateWithData(
data, UTType.png.identifier as CFString, 1, nil
) else {
throw ShotdeckError.captureFailed(underlying: "could not create PNG image destination")
}
CGImageDestinationAddImage(dest, cgImage, nil)
guard CGImageDestinationFinalize(dest) else {
throw ShotdeckError.captureFailed(underlying: "PNG encoding failed to finalize")
}
return CapturedImage(
pngData: data as Data,
pixelWidth: cgImage.width,
pixelHeight: cgImage.height,
scale: scale
)
}
/// Captures `region` at the display's true backing scale. Throws
/// `.screenRecordingNotGranted` when the grant is absent — never requests it.
public func capture(_ region: CaptureRegion) async throws -> CapturedImage {
guard Self.isScreenRecordingGranted else {
throw ShotdeckError.screenRecordingNotGranted
}
let content: SCShareableContent
do {
content = try await SCShareableContent.excludingDesktopWindows(
false,
onScreenWindowsOnly: true
)
} catch {
throw ShotdeckError.captureFailed(underlying: error.localizedDescription)
}
guard let display = content.displays.first(where: { $0.displayID == region.displayID }) else {
throw ShotdeckError.displayNoLongerConnected(displayID: region.displayID)
}
// Bounds/clamp: a resolution change often shrinks the display bounds by a few
// points without the region Ben picked being meaningfully wrong; 80% keeps a real
// recapture usable while a smaller overlap (e.g. a genuinely different display
// swapped in) still throws.
let rect: CGRect
if display.frame.contains(region.rect) {
rect = region.rect
} else {
let overlap = display.frame.intersection(region.rect)
let originalArea = region.rect.width * region.rect.height
let overlapArea = overlap.width * overlap.height
if originalArea > 0 && overlapArea / originalArea >= 0.8 {
rect = overlap
} else {
throw ShotdeckError.displayNoLongerConnected(displayID: region.displayID)
}
}
let localRect = CGRect(
x: rect.minX - display.frame.minX,
y: rect.minY - display.frame.minY,
width: rect.width,
height: rect.height
)
guard let mode = CGDisplayCopyDisplayMode(region.displayID) else {
throw ShotdeckError.captureFailed(
underlying: "no display mode for displayID \(region.displayID)"
)
}
guard mode.width > 0 else {
throw ShotdeckError.captureFailed(
underlying: "display mode width is 0 for displayID \(region.displayID)"
)
}
let scale = CGFloat(mode.pixelWidth) / CGFloat(mode.width)
let filter = SCContentFilter(display: display, excludingWindows: [])
let configuration = SCStreamConfiguration()
configuration.sourceRect = localRect
configuration.width = Int((localRect.width * scale).rounded())
configuration.height = Int((localRect.height * scale).rounded())
configuration.scalesToFit = false
configuration.showsCursor = false
let cgImage: CGImage
do {
cgImage = try await SCScreenshotManager.captureImage(
contentFilter: filter,
configuration: configuration
)
} catch {
throw ShotdeckError.captureFailed(underlying: error.localizedDescription)
}
let data = NSMutableData()
guard let dest = CGImageDestinationCreateWithData(
data,
UTType.png.identifier as CFString,
1,
nil
) else {
throw ShotdeckError.captureFailed(underlying: "could not create PNG image destination")
}
CGImageDestinationAddImage(dest, cgImage, nil)
guard CGImageDestinationFinalize(dest) else {
throw ShotdeckError.captureFailed(underlying: "PNG encoding failed to finalize")
}
return CapturedImage(
pngData: data as Data,
pixelWidth: cgImage.width,
pixelHeight: cgImage.height,
scale: scale
)
}
}