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.
This commit is contained in:
Claude Fable 5
2026-11-01 11:47:58 +04:00
parent 1ae8351b04
commit 3fb9b504f4
18 changed files with 1601 additions and 152 deletions
@@ -18,6 +18,135 @@ public struct ScreenCapturer: Sendable {
/// 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 {