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:
@@ -0,0 +1,252 @@
|
||||
import AppKit
|
||||
import ShotdeckCore
|
||||
|
||||
/// Hides Redline's own windows while the user is choosing what to capture, then puts
|
||||
/// them back. The popover would otherwise sit on top of the thing being picked and,
|
||||
/// worse, end up inside the screenshot.
|
||||
@MainActor
|
||||
enum RedlineWindows {
|
||||
private static var hidden: [NSWindow] = []
|
||||
|
||||
static func hideForPicking() {
|
||||
hidden = NSApp.windows.filter { window in
|
||||
window.isVisible && !(window is RegionPickerWindow)
|
||||
}
|
||||
for window in hidden {
|
||||
window.orderOut(nil)
|
||||
}
|
||||
}
|
||||
|
||||
static func restoreAfterPicking() {
|
||||
for window in hidden where !window.isVisible {
|
||||
window.orderFront(nil)
|
||||
}
|
||||
hidden = []
|
||||
}
|
||||
}
|
||||
|
||||
/// Highlights the window under the pointer and captures the one the user clicks —
|
||||
/// an app window, a browser window with its active tab, or the whole screen when the
|
||||
/// pointer is over the desktop.
|
||||
@MainActor
|
||||
public final class WindowPickerController {
|
||||
private var windows: [RegionPickerWindow] = []
|
||||
private var monitor: Any?
|
||||
private var hoverTimer: Timer?
|
||||
private var completion: (@MainActor (CaptureTarget?) -> Void)?
|
||||
private var candidates: [WindowCandidate] = []
|
||||
private var primaryHeight: CGFloat = 0
|
||||
private var hovered: WindowCandidate?
|
||||
private var hoveredScreen: NSScreen?
|
||||
|
||||
public init() {}
|
||||
|
||||
/// Presents the picker. The completion receives the chosen target, or nil when the
|
||||
/// user pressed Escape.
|
||||
public func pick(
|
||||
onlyBrowsers: Bool = false,
|
||||
completion: @escaping @MainActor (CaptureTarget?) -> Void
|
||||
) {
|
||||
if self.completion != nil {
|
||||
finish(target: nil)
|
||||
}
|
||||
self.completion = completion
|
||||
self.primaryHeight = NSScreen.screens.first?.frame.height ?? 0
|
||||
|
||||
let all = ScreenCapturer.onScreenWindows().filter { candidate in
|
||||
candidate.layer == 0
|
||||
&& candidate.ownerBundleID != CaptureTargetResolver.ownBundleID
|
||||
&& candidate.frame.width >= 40
|
||||
&& candidate.frame.height >= 40
|
||||
}
|
||||
self.candidates = onlyBrowsers
|
||||
? all.filter { candidate in
|
||||
guard let bundle = candidate.ownerBundleID else { return false }
|
||||
return CaptureTargetResolver.browserBundleIDs.contains(bundle)
|
||||
}
|
||||
: all
|
||||
|
||||
presentOverlays()
|
||||
}
|
||||
|
||||
// MARK: - Overlays
|
||||
|
||||
private func presentOverlays() {
|
||||
let screens = NSScreen.screens
|
||||
guard !screens.isEmpty else {
|
||||
finish(target: nil)
|
||||
return
|
||||
}
|
||||
windows = screens.map { RegionPickerWindow(screen: $0) }
|
||||
for window in windows {
|
||||
window.orderFrontRegardless()
|
||||
}
|
||||
let mouse = NSEvent.mouseLocation
|
||||
let keyWindow = windows.first { $0.coveringScreen.frame.contains(mouse) } ?? windows[0]
|
||||
keyWindow.makeKey()
|
||||
|
||||
// A timer drives the highlight from the real pointer position: mouse-moved
|
||||
// events are only delivered to the key window, and the overlay must track the
|
||||
// pointer even over another app's window.
|
||||
hoverTimer?.invalidate()
|
||||
hoverTimer = Timer.scheduledTimer(withTimeInterval: 1.0 / 60.0, repeats: true) {
|
||||
[weak self] _ in
|
||||
MainActor.assumeIsolated {
|
||||
self?.updateHover(at: NSEvent.mouseLocation)
|
||||
}
|
||||
}
|
||||
|
||||
monitor = NSEvent.addLocalMonitorForEvents(
|
||||
matching: [.mouseMoved, .leftMouseDown, .keyDown]
|
||||
) { [weak self] event in
|
||||
guard let self else { return event }
|
||||
let type = event.type
|
||||
let keyCode: UInt16 = (type == .keyDown) ? event.keyCode : 0
|
||||
let locationInWindow = event.locationInWindow
|
||||
let windowNumber = event.windowNumber
|
||||
let consume = MainActor.assumeIsolated {
|
||||
let location = self.globalAppKitLocation(
|
||||
locationInWindow: locationInWindow, windowNumber: windowNumber)
|
||||
return self.handle(type: type, keyCode: keyCode, location: location)
|
||||
}
|
||||
return consume ? nil : event
|
||||
}
|
||||
|
||||
updateHover(at: mouse)
|
||||
}
|
||||
|
||||
private func handle(type: NSEvent.EventType, keyCode: UInt16, location: NSPoint?) -> Bool {
|
||||
switch type {
|
||||
case .keyDown:
|
||||
if keyCode == 53 { // Escape
|
||||
finish(target: nil)
|
||||
return true
|
||||
}
|
||||
return false
|
||||
case .mouseMoved:
|
||||
updateHover(at: location ?? NSEvent.mouseLocation)
|
||||
return false
|
||||
case .leftMouseDown:
|
||||
if let target = targetUnderPointer(at: location ?? NSEvent.mouseLocation) {
|
||||
finish(target: target)
|
||||
}
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Hover
|
||||
|
||||
private func updateHover(at point: NSPoint) {
|
||||
let screen = NSScreen.screens.first { $0.frame.contains(point) }
|
||||
let candidate = candidate(at: point)
|
||||
hovered = candidate
|
||||
hoveredScreen = screen
|
||||
|
||||
let highlightRect = candidate.map { appKitRect($0.frame) } ?? screen?.frame
|
||||
let chip: String
|
||||
if let candidate {
|
||||
let app = appName(for: candidate)
|
||||
let title = windowTitle(for: candidate)
|
||||
chip = title.isEmpty ? app : "\(app) — \(title)"
|
||||
} else {
|
||||
chip = "Entire Screen"
|
||||
}
|
||||
|
||||
for window in windows {
|
||||
guard let windowScreen = windows.first(where: { $0 === window })?.coveringScreen
|
||||
else { continue }
|
||||
guard let highlightRect, windowScreen.frame.intersects(highlightRect) else {
|
||||
window.updateSelection(localRect: nil, sizeText: nil)
|
||||
continue
|
||||
}
|
||||
let local = CGRect(
|
||||
x: highlightRect.minX - windowScreen.frame.minX,
|
||||
y: highlightRect.minY - windowScreen.frame.minY,
|
||||
width: highlightRect.width,
|
||||
height: highlightRect.height
|
||||
)
|
||||
window.updateSelection(localRect: local, sizeText: chip)
|
||||
}
|
||||
}
|
||||
|
||||
private func targetUnderPointer(at point: NSPoint) -> CaptureTarget? {
|
||||
if let candidate = candidate(at: point) {
|
||||
return .window(
|
||||
windowID: candidate.windowID,
|
||||
frame: candidate.frame,
|
||||
displayID: CaptureTargetResolver.displayID(containing: candidate.frame)
|
||||
)
|
||||
}
|
||||
guard let screen = NSScreen.screens.first(where: { $0.frame.contains(point) }) else {
|
||||
return nil
|
||||
}
|
||||
return .display(displayID: displayID(for: screen))
|
||||
}
|
||||
|
||||
private func candidate(at point: NSPoint) -> WindowCandidate? {
|
||||
candidates.first { candidate in
|
||||
appKitRect(candidate.frame).contains(point)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Geometry and labels
|
||||
|
||||
private func appKitRect(_ cgRect: CGRect) -> CGRect {
|
||||
CGRect(
|
||||
x: cgRect.minX,
|
||||
y: primaryHeight - cgRect.minY - cgRect.height,
|
||||
width: cgRect.width,
|
||||
height: cgRect.height
|
||||
)
|
||||
}
|
||||
|
||||
private func displayID(for screen: NSScreen) -> UInt32 {
|
||||
(screen.deviceDescription[NSDeviceDescriptionKey("NSScreenNumber")] as? NSNumber)?
|
||||
.uint32Value ?? CGMainDisplayID()
|
||||
}
|
||||
|
||||
private func appName(for candidate: WindowCandidate) -> String {
|
||||
guard let bundle = candidate.ownerBundleID,
|
||||
let url = NSWorkspace.shared.urlForApplication(withBundleIdentifier: bundle)
|
||||
else { return "Window" }
|
||||
return FileManager.default.displayName(atPath: url.path)
|
||||
.replacingOccurrences(of: ".app", with: "")
|
||||
}
|
||||
|
||||
private func windowTitle(for candidate: WindowCandidate) -> String {
|
||||
guard let raw = CGWindowListCopyWindowInfo([.optionIncludingWindow], candidate.windowID)
|
||||
as? [[String: Any]], let first = raw.first
|
||||
else { return "" }
|
||||
return first[kCGWindowName as String] as? String ?? ""
|
||||
}
|
||||
|
||||
private func globalAppKitLocation(locationInWindow: NSPoint, windowNumber: Int) -> NSPoint? {
|
||||
if let window = windows.first(where: { $0.windowNumber == windowNumber }) {
|
||||
return window.convertPoint(toScreen: locationInWindow)
|
||||
}
|
||||
if windowNumber != 0, let window = NSApp.window(withWindowNumber: windowNumber) {
|
||||
return window.convertPoint(toScreen: locationInWindow)
|
||||
}
|
||||
return NSEvent.mouseLocation
|
||||
}
|
||||
|
||||
// MARK: - Finish
|
||||
|
||||
private func finish(target: CaptureTarget?) {
|
||||
hoverTimer?.invalidate()
|
||||
hoverTimer = nil
|
||||
if let monitor {
|
||||
NSEvent.removeMonitor(monitor)
|
||||
}
|
||||
monitor = nil
|
||||
for window in windows {
|
||||
window.orderOut(nil)
|
||||
}
|
||||
windows = []
|
||||
let completion = self.completion
|
||||
self.completion = nil
|
||||
completion?(target)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user