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:
@@ -55,4 +55,20 @@ public struct CaptureRegion: Codable, Sendable, Equatable {
|
||||
|
||||
/// Persisted in UserDefaults under this key BY THE APP (WP-4a), never by this type.
|
||||
public static let defaultsKey = "ai.flowmaster.shotdeck.region"
|
||||
|
||||
/// 1-based position of this region's display in the current screen list; 1 when the
|
||||
/// display is no longer attached.
|
||||
public var displayNumber: Int {
|
||||
let match = NSScreen.screens.firstIndex { screen in
|
||||
let id = (screen.deviceDescription[NSDeviceDescriptionKey("NSScreenNumber")]
|
||||
as? NSNumber)?.uint32Value
|
||||
return id == displayID
|
||||
}
|
||||
return (match ?? 0) + 1
|
||||
}
|
||||
|
||||
/// "800 × 600 on Display 1" — the line the popover shows under the capture control.
|
||||
public var summary: String {
|
||||
String(format: "%.0f × %.0f on Display %d", rect.width, rect.height, displayNumber)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
import Foundation
|
||||
|
||||
/// What a capture takes a picture of.
|
||||
///
|
||||
/// The choice belongs to the session being captured into: it is stored once and copied
|
||||
/// to the next session, so a review run keeps capturing the same kind of thing until
|
||||
/// the user says otherwise. Choosing anything other than `.area` never clears the saved
|
||||
/// area — it comes back when Area is chosen again.
|
||||
public enum CaptureScope: String, Codable, Sendable, CaseIterable {
|
||||
case area
|
||||
case window
|
||||
case browser
|
||||
case screen
|
||||
|
||||
/// Label for the segmented control and the window's menu.
|
||||
public var displayName: String {
|
||||
switch self {
|
||||
case .area: return "Area"
|
||||
case .window: return "Window"
|
||||
case .browser: return "Browser"
|
||||
case .screen: return "Screen"
|
||||
}
|
||||
}
|
||||
|
||||
/// The longer menu item wording (window picker).
|
||||
public var menuTitle: String {
|
||||
switch self {
|
||||
case .area: return "Saved Area"
|
||||
case .window: return "Front Window"
|
||||
case .browser: return "Browser Page"
|
||||
case .screen: return "Whole Screen"
|
||||
}
|
||||
}
|
||||
|
||||
/// Sentence shown under the control: what pressing Capture will take.
|
||||
public func targetDescription(savedArea: CaptureRegion?) -> String {
|
||||
switch self {
|
||||
case .area:
|
||||
guard let savedArea else { return "No area saved yet — choose one first" }
|
||||
return savedArea.summary
|
||||
case .window:
|
||||
return "The frontmost window when you capture"
|
||||
case .browser:
|
||||
return "The frontmost browser window when you capture"
|
||||
case .screen:
|
||||
guard let savedArea else { return "The display with the frontmost window" }
|
||||
return "All of Display \(savedArea.displayNumber)"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Persistence for the capture choice. Stored separately from the chosen area so
|
||||
/// switching scope never destroys the area.
|
||||
public enum CaptureSettings {
|
||||
public static let scopeKey = "ai.flowmaster.shotdeck.captureScope"
|
||||
public static let askOnNewSessionKey = "ai.flowmaster.shotdeck.askScopeOnNewSession"
|
||||
|
||||
public static func scope(defaults: UserDefaults = .standard) -> CaptureScope {
|
||||
guard let raw = defaults.string(forKey: scopeKey),
|
||||
let scope = CaptureScope(rawValue: raw)
|
||||
else { return .area }
|
||||
return scope
|
||||
}
|
||||
|
||||
public static func setScope(_ scope: CaptureScope, defaults: UserDefaults = .standard) {
|
||||
defaults.set(scope.rawValue, forKey: scopeKey)
|
||||
}
|
||||
|
||||
/// Whether "New Session…" asks what to capture before starting. Off means it
|
||||
/// starts immediately with the current choice.
|
||||
public static func askOnNewSession(defaults: UserDefaults = .standard) -> Bool {
|
||||
guard defaults.object(forKey: askOnNewSessionKey) != nil else { return true }
|
||||
return defaults.bool(forKey: askOnNewSessionKey)
|
||||
}
|
||||
|
||||
public static func setAskOnNewSession(_ value: Bool, defaults: UserDefaults = .standard) {
|
||||
defaults.set(value, forKey: askOnNewSessionKey)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,125 @@
|
||||
import CoreGraphics
|
||||
import Foundation
|
||||
|
||||
/// One on-screen window as the capture resolver sees it. Kept as plain data so the
|
||||
/// choice logic is testable without a window server.
|
||||
public struct WindowCandidate: Sendable, Equatable {
|
||||
public let windowID: UInt32
|
||||
public let ownerBundleID: String?
|
||||
public let ownerPID: Int32
|
||||
public let frame: CGRect
|
||||
public let layer: Int
|
||||
|
||||
public init(
|
||||
windowID: UInt32,
|
||||
ownerBundleID: String?,
|
||||
ownerPID: Int32,
|
||||
frame: CGRect,
|
||||
layer: Int
|
||||
) {
|
||||
self.windowID = windowID
|
||||
self.ownerBundleID = ownerBundleID
|
||||
self.ownerPID = ownerPID
|
||||
self.frame = frame
|
||||
self.layer = layer
|
||||
}
|
||||
}
|
||||
|
||||
/// What a capture will take: one region of a display, one whole display, or one window.
|
||||
public enum CaptureTarget: Sendable, Equatable {
|
||||
case region(CaptureRegion)
|
||||
case display(displayID: UInt32)
|
||||
case window(windowID: UInt32, frame: CGRect, displayID: UInt32)
|
||||
}
|
||||
|
||||
/// Chooses the capture target for a scope from the windows that are on screen.
|
||||
///
|
||||
/// Candidates arrive front-to-back (the order the window server reports), which is what
|
||||
/// makes "the front window" well defined. Redline's own windows are never candidates,
|
||||
/// so choosing Window never captures the app's own panel.
|
||||
public enum CaptureTargetResolver {
|
||||
/// Bundles whose front window counts as a browser page.
|
||||
public static let browserBundleIDs: Set<String> = [
|
||||
"com.apple.Safari",
|
||||
"com.google.Chrome",
|
||||
"com.google.Chrome.canary",
|
||||
"com.microsoft.edgemac",
|
||||
"com.brave.Browser",
|
||||
"company.thebrowser.Browser",
|
||||
"org.mozilla.firefox",
|
||||
"com.vivaldi.Vivaldi",
|
||||
"com.operasoftware.Opera",
|
||||
]
|
||||
|
||||
/// Redline's own bundle identifier — never a capture target.
|
||||
public static let ownBundleID = "ai.flowmaster.shotdeck"
|
||||
|
||||
public static func resolve(
|
||||
scope: CaptureScope,
|
||||
savedArea: CaptureRegion?,
|
||||
frontmostPID: Int32,
|
||||
candidates: [WindowCandidate]
|
||||
) throws -> CaptureTarget {
|
||||
let usable = candidates.filter { candidate in
|
||||
candidate.layer == 0
|
||||
&& candidate.ownerBundleID != ownBundleID
|
||||
&& candidate.frame.width >= 40
|
||||
&& candidate.frame.height >= 40
|
||||
}
|
||||
|
||||
switch scope {
|
||||
case .area:
|
||||
guard let savedArea else { throw ShotdeckError.noRegionRemembered }
|
||||
return .region(savedArea)
|
||||
|
||||
case .screen:
|
||||
if let savedArea {
|
||||
return .display(displayID: savedArea.displayID)
|
||||
}
|
||||
guard let window = usable.first else { throw ShotdeckError.noFrontWindow }
|
||||
return .display(displayID: displayID(containing: window.frame))
|
||||
|
||||
case .window:
|
||||
let ownAppFirst = usable.first { $0.ownerPID == frontmostPID }
|
||||
guard let window = ownAppFirst ?? usable.first else {
|
||||
throw ShotdeckError.noFrontWindow
|
||||
}
|
||||
return .window(
|
||||
windowID: window.windowID,
|
||||
frame: window.frame,
|
||||
displayID: displayID(containing: window.frame)
|
||||
)
|
||||
|
||||
case .browser:
|
||||
guard let window = usable.first(where: {
|
||||
guard let bundle = $0.ownerBundleID else { return false }
|
||||
return browserBundleIDs.contains(bundle)
|
||||
}) else {
|
||||
throw ShotdeckError.noBrowserWindow
|
||||
}
|
||||
return .window(
|
||||
windowID: window.windowID,
|
||||
frame: window.frame,
|
||||
displayID: displayID(containing: window.frame)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/// The display whose bounds contain the window's centre; the main display when a
|
||||
/// window straddles two screens and belongs to neither cleanly.
|
||||
public static func displayID(containing frame: CGRect) -> UInt32 {
|
||||
let centre = CGPoint(x: frame.midX, y: frame.midY)
|
||||
var count: UInt32 = 0
|
||||
guard CGGetActiveDisplayList(0, nil, &count) == .success, count > 0 else {
|
||||
return CGMainDisplayID()
|
||||
}
|
||||
var displays = [CGDirectDisplayID](repeating: 0, count: Int(count))
|
||||
guard CGGetActiveDisplayList(count, &displays, &count) == .success else {
|
||||
return CGMainDisplayID()
|
||||
}
|
||||
for display in displays.prefix(Int(count)) where CGDisplayBounds(display).contains(centre) {
|
||||
return display
|
||||
}
|
||||
return CGMainDisplayID()
|
||||
}
|
||||
}
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user