Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ae054d70b9 | ||
|
|
3078713f8f | ||
|
|
265571d7e2 | ||
|
|
7633e81c52 | ||
|
|
83b127cf44 | ||
|
|
8332661504 | ||
|
|
832a18b06c | ||
|
|
6e5796e16d |
@@ -14,8 +14,9 @@ final class CaptureController: NSObject {
|
|||||||
// Selection state (driven by the event tap; AppKit global coordinates)
|
// Selection state (driven by the event tap; AppKit global coordinates)
|
||||||
private var marqueeStart: NSPoint?
|
private var marqueeStart: NSPoint?
|
||||||
private(set) var marqueeRect: NSRect?
|
private(set) var marqueeRect: NSRect?
|
||||||
private(set) var hoverFrame: NSRect?
|
private(set) var hoverWindow: (id: CGWindowID, frame: NSRect, pid: Int)?
|
||||||
private var trackedWindows: [(id: CGWindowID, frame: NSRect)] = []
|
var hoverFrame: NSRect? { hoverWindow?.frame }
|
||||||
|
private var trackedWindows: [(id: CGWindowID, frame: NSRect, pid: Int)] = []
|
||||||
private var lastWindowScan = Date.distantPast
|
private var lastWindowScan = Date.distantPast
|
||||||
private var eventTap: CFMachPort?
|
private var eventTap: CFMachPort?
|
||||||
|
|
||||||
@@ -102,14 +103,19 @@ final class CaptureController: NSObject {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A window was picked from hover — capture it with rounded corners.
|
/// A window was picked from hover — capture it directly (real rounded
|
||||||
private func finishWindowPick(_ rect: NSRect) {
|
/// corners come from the window's own alpha).
|
||||||
|
private func finishWindowPick(_ windowID: CGWindowID, _ rect: NSRect, ownedRisk: Bool) {
|
||||||
let ids = overlayIDs
|
let ids = overlayIDs
|
||||||
hideOverlays()
|
hideOverlays()
|
||||||
isSelecting = false
|
isSelecting = false
|
||||||
restorePreviousApp()
|
restorePreviousApp()
|
||||||
DispatchQueue.main.async { [self] in
|
DispatchQueue.main.async { [self] in
|
||||||
|
if ownedRisk {
|
||||||
capture(rect, excludingWindowIDs: ids, roundedCorners: true)
|
capture(rect, excludingWindowIDs: ids, roundedCorners: true)
|
||||||
|
} else {
|
||||||
|
capture(rect, excludingWindowIDs: ids, windowID: windowID)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -136,7 +142,7 @@ final class CaptureController: NSObject {
|
|||||||
overlays.forEach { $0.orderOut(nil) }
|
overlays.forEach { $0.orderOut(nil) }
|
||||||
overlays = []
|
overlays = []
|
||||||
marqueeRect = nil
|
marqueeRect = nil
|
||||||
hoverFrame = nil
|
hoverWindow = nil
|
||||||
marqueeStart = nil
|
marqueeStart = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -217,11 +223,12 @@ final class CaptureController: NSObject {
|
|||||||
) as? [[String: Any]] else { trackedWindows = []; return }
|
) as? [[String: Any]] else { trackedWindows = []; return }
|
||||||
|
|
||||||
let ownIDs = Set(overlayIDs)
|
let ownIDs = Set(overlayIDs)
|
||||||
var found: [(id: CGWindowID, frame: NSRect)] = []
|
var found: [(id: CGWindowID, frame: NSRect, pid: Int)] = []
|
||||||
for info in list {
|
for info in list {
|
||||||
guard let cid = info[kCGWindowNumber as String] as? CGWindowID,
|
guard let cid = info[kCGWindowNumber as String] as? CGWindowID,
|
||||||
let bounds = info[kCGWindowBounds as String] as? [String: CGFloat],
|
let bounds = info[kCGWindowBounds as String] as? [String: CGFloat],
|
||||||
let layer = info[kCGWindowLayer as String] as? Int,
|
let layer = info[kCGWindowLayer as String] as? Int,
|
||||||
|
let pid = info[kCGWindowOwnerPID as String] as? Int,
|
||||||
layer == 0
|
layer == 0
|
||||||
else { continue }
|
else { continue }
|
||||||
if ownIDs.contains(Int(cid)) { continue }
|
if ownIDs.contains(Int(cid)) { continue }
|
||||||
@@ -233,12 +240,7 @@ final class CaptureController: NSObject {
|
|||||||
)
|
)
|
||||||
guard cgRect.width >= 80, cgRect.height >= 60 else { continue }
|
guard cgRect.width >= 80, cgRect.height >= 60 else { continue }
|
||||||
let rect = Self.cgToAppKit(rect: cgRect)
|
let rect = Self.cgToAppKit(rect: cgRect)
|
||||||
// Skip windows filling an entire screen (fullscreen apps)
|
found.append((id: cid, frame: rect, pid: pid))
|
||||||
let coversWholeScreen = NSScreen.screens.contains { screen in
|
|
||||||
abs(screen.frame.width - rect.width) < 2 && abs(screen.frame.height - rect.height) < 2
|
|
||||||
}
|
|
||||||
guard !coversWholeScreen else { continue }
|
|
||||||
found.append((cid, rect))
|
|
||||||
}
|
}
|
||||||
trackedWindows = found
|
trackedWindows = found
|
||||||
}
|
}
|
||||||
@@ -251,10 +253,10 @@ final class CaptureController: NSObject {
|
|||||||
return NSRect(x: rect.origin.x, y: y, width: rect.width, height: rect.height)
|
return NSRect(x: rect.origin.x, y: y, width: rect.width, height: rect.height)
|
||||||
}
|
}
|
||||||
|
|
||||||
private func topmostWindowContaining(_ appKitPoint: NSPoint) -> NSRect? {
|
private func topmostWindowContaining(_ appKitPoint: NSPoint) -> (id: CGWindowID, frame: NSRect, pid: Int)? {
|
||||||
for (_, frame) in trackedWindows {
|
for entry in trackedWindows {
|
||||||
if frame.contains(appKitPoint) {
|
if entry.frame.contains(appKitPoint) {
|
||||||
return frame
|
return entry
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
@@ -265,11 +267,12 @@ final class CaptureController: NSObject {
|
|||||||
guard isSelecting else { return event }
|
guard isSelecting else { return event }
|
||||||
|
|
||||||
switch type {
|
switch type {
|
||||||
case .tapDisabledByUserInput:
|
case .tapDisabledByUserInput, .tapDisabledByTimeout:
|
||||||
CGEvent.tapEnable(tap: eventTap!, enable: true)
|
// A timed-out tap must be re-enabled; guard against a torn-down tap.
|
||||||
return event
|
if let tap = eventTap {
|
||||||
case .tapDisabledByTimeout:
|
CGEvent.tapEnable(tap: tap, enable: true)
|
||||||
CGEvent.tapEnable(tap: eventTap!, enable: true)
|
CaptureController.log("tap: re-enabled after \(type)")
|
||||||
|
}
|
||||||
return event
|
return event
|
||||||
case .keyDown:
|
case .keyDown:
|
||||||
if event.getIntegerValueField(.keyboardEventKeycode) == 53 {
|
if event.getIntegerValueField(.keyboardEventKeycode) == 53 {
|
||||||
@@ -286,9 +289,14 @@ final class CaptureController: NSObject {
|
|||||||
}
|
}
|
||||||
let appKitPoint = cgPointToAppKit(NSPoint(x: event.location.x, y: event.location.y))
|
let appKitPoint = cgPointToAppKit(NSPoint(x: event.location.x, y: event.location.y))
|
||||||
if type == .mouseMoved {
|
if type == .mouseMoved {
|
||||||
let frame = topmostWindowContaining(appKitPoint)
|
let window = topmostWindowContaining(appKitPoint)
|
||||||
if frame != hoverFrame {
|
if window?.frame != hoverFrame {
|
||||||
hoverFrame = frame
|
hoverWindow = window
|
||||||
|
if let w = window {
|
||||||
|
CaptureController.log("hover: picked id=\(w.id) frame=\(w.frame) (tracked=\(trackedWindows.count))")
|
||||||
|
} else {
|
||||||
|
CaptureController.log("hover: none (tracked=\(trackedWindows.count))")
|
||||||
|
}
|
||||||
renderOverlays()
|
renderOverlays()
|
||||||
}
|
}
|
||||||
return event
|
return event
|
||||||
@@ -302,7 +310,7 @@ final class CaptureController: NSObject {
|
|||||||
height: abs(appKitPoint.y - start.y)
|
height: abs(appKitPoint.y - start.y)
|
||||||
)
|
)
|
||||||
marqueeRect = rect
|
marqueeRect = rect
|
||||||
hoverFrame = nil
|
hoverWindow = nil
|
||||||
renderOverlays()
|
renderOverlays()
|
||||||
return nil
|
return nil
|
||||||
case .leftMouseDown:
|
case .leftMouseDown:
|
||||||
@@ -317,9 +325,18 @@ final class CaptureController: NSObject {
|
|||||||
if let rect = marqueeRect, dragDistance >= 4, rect.width >= 2, rect.height >= 2 {
|
if let rect = marqueeRect, dragDistance >= 4, rect.width >= 2, rect.height >= 2 {
|
||||||
CaptureController.log("tap: marquee complete \(rect)")
|
CaptureController.log("tap: marquee complete \(rect)")
|
||||||
finishSelection(rect)
|
finishSelection(rect)
|
||||||
} else if let hover = hoverFrame {
|
} else if let hover = hoverWindow {
|
||||||
CaptureController.log("tap: window pick \(hover)")
|
// An overlapping window from the same app means this is an
|
||||||
finishWindowPick(hover)
|
// owned popup — desktopIndependentWindow would composite the
|
||||||
|
// parent too, so capture the screen crop instead.
|
||||||
|
// Owned popups composite their parent even when the windows
|
||||||
|
// don't overlap, so ANY other same-app window on screen means
|
||||||
|
// crop (correct region) instead of the group composite.
|
||||||
|
let ownedRisk = trackedWindows.contains {
|
||||||
|
$0.id != hover.id && $0.pid == hover.pid
|
||||||
|
}
|
||||||
|
CaptureController.log("tap: window pick id=\(hover.id) pid=\(hover.pid) ownedRisk=\(ownedRisk) \(hover.frame)")
|
||||||
|
finishWindowPick(hover.id, hover.frame, ownedRisk: ownedRisk)
|
||||||
} else {
|
} else {
|
||||||
CaptureController.log("tap: click with no hover/marquee — cancel")
|
CaptureController.log("tap: click with no hover/marquee — cancel")
|
||||||
cancelSelection()
|
cancelSelection()
|
||||||
@@ -332,8 +349,8 @@ final class CaptureController: NSObject {
|
|||||||
|
|
||||||
// MARK: - Capture
|
// MARK: - Capture
|
||||||
|
|
||||||
private func capture(_ rect: NSRect, excludingWindowIDs ids: [Int], roundedCorners: Bool = false) {
|
private func capture(_ rect: NSRect, excludingWindowIDs ids: [Int], roundedCorners: Bool = false, windowID: CGWindowID? = nil) {
|
||||||
ScreenCapture.shared.capture(rect, excludingWindowIDs: ids, roundedCorners: roundedCorners) { [weak self] image in
|
ScreenCapture.shared.capture(rect, excludingWindowIDs: ids, roundedCorners: roundedCorners, windowID: windowID) { [weak self] image in
|
||||||
DispatchQueue.main.async {
|
DispatchQueue.main.async {
|
||||||
self?.isSelecting = false
|
self?.isSelecting = false
|
||||||
self?.restorePreviousApp()
|
self?.restorePreviousApp()
|
||||||
@@ -342,6 +359,7 @@ final class CaptureController: NSObject {
|
|||||||
ResultWindow.shared.show(image: ResultWindow.failureImage("Capture failed. Check Screen Recording permission."))
|
ResultWindow.shared.show(image: ResultWindow.failureImage("Capture failed. Check Screen Recording permission."))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
CaptureController.log("capture done: \(image.width)x\(image.height) — showing result window")
|
||||||
ResultWindow.shared.show(image: image)
|
ResultWindow.shared.show(image: image)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -417,7 +435,7 @@ final class ScreenCapture {
|
|||||||
return content.displays.first(where: { $0.displayID == cgID }) ?? content.displays.first
|
return content.displays.first(where: { $0.displayID == cgID }) ?? content.displays.first
|
||||||
}
|
}
|
||||||
|
|
||||||
func capture(_ rect: NSRect, excludingWindowIDs excludedIDs: [Int], roundedCorners: Bool = false, completion: @escaping (CGImage?) -> Void) {
|
func capture(_ rect: NSRect, excludingWindowIDs excludedIDs: [Int], roundedCorners: Bool = false, windowID: CGWindowID? = nil, completion: @escaping (CGImage?) -> Void) {
|
||||||
if #available(macOS 14.0, *) {
|
if #available(macOS 14.0, *) {
|
||||||
Task {
|
Task {
|
||||||
guard let content = try? await SCShareableContent.current,
|
guard let content = try? await SCShareableContent.current,
|
||||||
@@ -440,7 +458,7 @@ final class ScreenCapture {
|
|||||||
let backingScale = screen.backingScaleFactor
|
let backingScale = screen.backingScaleFactor
|
||||||
config.width = Int(CGFloat(display.width) * backingScale)
|
config.width = Int(CGFloat(display.width) * backingScale)
|
||||||
config.height = Int(CGFloat(display.height) * backingScale)
|
config.height = Int(CGFloat(display.height) * backingScale)
|
||||||
config.showsCursor = true
|
config.showsCursor = false
|
||||||
|
|
||||||
// ScreenCaptureKit uses a different origin than AppKit for
|
// ScreenCaptureKit uses a different origin than AppKit for
|
||||||
// screens stacked around the main display: per-display,
|
// screens stacked around the main display: per-display,
|
||||||
@@ -452,10 +470,42 @@ final class ScreenCapture {
|
|||||||
height: rect.height
|
height: rect.height
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Window pick: capture the window DIRECTLY — the image comes
|
||||||
|
// with the app's real rounded-corner alpha (no mask guessing).
|
||||||
|
if let windowID,
|
||||||
|
let scWindow = content.windows.first(where: { $0.windowID == windowID }) {
|
||||||
|
var windowConfig = SCStreamConfiguration()
|
||||||
|
windowConfig.width = Int(scWindow.frame.width * backingScale)
|
||||||
|
windowConfig.height = Int(scWindow.frame.height * backingScale)
|
||||||
|
windowConfig.showsCursor = false
|
||||||
|
if let windowImage = try? await SCScreenshotManager.captureImage(
|
||||||
|
contentFilter: SCContentFilter(desktopIndependentWindow: scWindow),
|
||||||
|
configuration: windowConfig
|
||||||
|
) {
|
||||||
|
let expectedW = CGFloat(scWindow.frame.width) * backingScale
|
||||||
|
let expectedH = CGFloat(scWindow.frame.height) * backingScale
|
||||||
|
let overflow = max(
|
||||||
|
CGFloat(windowImage.width) / max(expectedW, 1),
|
||||||
|
CGFloat(windowImage.height) / max(expectedH, 1)
|
||||||
|
)
|
||||||
|
if overflow > 1.35 {
|
||||||
|
// SCKit composited extra windows (owned popups etc.)
|
||||||
|
// — reject and use the screen crop instead.
|
||||||
|
CaptureController.log("capture: WINDOW id=\(windowID) overflow=\(overflow) (\(windowImage.width)x\(windowImage.height) vs \(Int(expectedW))x\(Int(expectedH))) — rejecting, using crop+mask")
|
||||||
|
} else {
|
||||||
|
CaptureController.log("capture: WINDOW id=\(windowID) image=\(windowImage.width)x\(windowImage.height) (native alpha corners)")
|
||||||
|
completion(windowImage)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
CaptureController.log("capture: window filter failed for id=\(windowID) — falling back to crop+mask")
|
||||||
|
}
|
||||||
|
|
||||||
guard let image = try? await SCScreenshotManager.captureImage(contentFilter: filter, configuration: config) else {
|
guard let image = try? await SCScreenshotManager.captureImage(contentFilter: filter, configuration: config) else {
|
||||||
completion(nil)
|
completion(nil)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
CaptureController.log("capture: rect=\(rect) screen=\(screen.frame) display=\(display.frame) backing=\(backingScale) image=\(image.width)x\(image.height) scRect=\(scRect)")
|
||||||
completion(self.crop(image, to: scRect, display: display, roundedCorners: roundedCorners))
|
completion(self.crop(image, to: scRect, display: display, roundedCorners: roundedCorners))
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
+2
-2
@@ -108,9 +108,9 @@ cat > "$APP/Contents/Info.plist" <<'PLIST'
|
|||||||
<key>CFBundleIconFile</key>
|
<key>CFBundleIconFile</key>
|
||||||
<string>Icon</string>
|
<string>Icon</string>
|
||||||
<key>CFBundleShortVersionString</key>
|
<key>CFBundleShortVersionString</key>
|
||||||
<string>1.7.0</string>
|
<string>1.8.3</string>
|
||||||
<key>CFBundleVersion</key>
|
<key>CFBundleVersion</key>
|
||||||
<string>70</string>
|
<string>83</string>
|
||||||
<key>LSUIElement</key>
|
<key>LSUIElement</key>
|
||||||
<true/>
|
<true/>
|
||||||
<key>NSHighResolutionCapable</key>
|
<key>NSHighResolutionCapable</key>
|
||||||
|
|||||||
Reference in New Issue
Block a user