Files
shotdeck/Sources/ShotdeckCore/Capture/CaptureTargetResolver.swift
T
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

126 lines
4.4 KiB
Swift

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()
}
}