|
|
|
@@ -10,6 +10,38 @@ final class CaptureController: NSObject {
|
|
|
|
|
private var previousApp: NSRunningApplication?
|
|
|
|
|
private var overlayIDs: [Int] = []
|
|
|
|
|
|
|
|
|
|
/// Diagnostic snapshot for "Copy Diagnostics" menu item.
|
|
|
|
|
func diagnostics() -> String {
|
|
|
|
|
var lines: [String] = []
|
|
|
|
|
lines.append("Kapture diagnostics")
|
|
|
|
|
for (i, s) in NSScreen.screens.enumerated() {
|
|
|
|
|
let scale = s.backingScaleFactor
|
|
|
|
|
let cgID = (s.deviceDescription[NSDeviceDescriptionKey("NSScreenNumber")] as? NSNumber)?.uint32Value ?? 0
|
|
|
|
|
lines.append("screen[\(i)]: frame=\(s.frame) scale=\(scale) backingSize=\(s.frame.width*scale)x\(s.frame.height*scale) cgID=\(cgID)")
|
|
|
|
|
}
|
|
|
|
|
if #available(macOS 14.0, *) {
|
|
|
|
|
Task {
|
|
|
|
|
if let content = try? await SCShareableContent.current {
|
|
|
|
|
for (i, d) in content.displays.enumerated() {
|
|
|
|
|
lines.append("SCDisplay[\(i)]: frame=\(d.frame) width=\(d.width)x\(d.height) id=\(d.displayID)")
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
lines.append("SCShareableContent: unavailable (no Screen Recording permission?)")
|
|
|
|
|
}
|
|
|
|
|
let text = lines.joined(separator: "\n")
|
|
|
|
|
let pb = NSPasteboard.general
|
|
|
|
|
pb.clearContents()
|
|
|
|
|
pb.setString(text, forType: .string)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
let text = lines.joined(separator: "\n")
|
|
|
|
|
let pb = NSPasteboard.general
|
|
|
|
|
pb.clearContents()
|
|
|
|
|
pb.setString(text, forType: .string)
|
|
|
|
|
}
|
|
|
|
|
return lines.joined(separator: "\n")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func begin() {
|
|
|
|
|
guard !isSelecting else { return }
|
|
|
|
|
guard PermissionManager.isGranted else {
|
|
|
|
@@ -85,19 +117,13 @@ final class ScreenCapture {
|
|
|
|
|
private init() {}
|
|
|
|
|
|
|
|
|
|
@available(macOS 14.0, *)
|
|
|
|
|
private func crop(_ image: CGImage, to rect: NSRect, display: SCDisplay) -> CGImage? {
|
|
|
|
|
// The captured image may be at any scale (retina 2x, secondary display
|
|
|
|
|
// 1x, or trimmed by ScreenCaptureKit). Derive the pixel scale from the
|
|
|
|
|
// image we actually got, relative to the display's point frame.
|
|
|
|
|
private func crop(_ image: CGImage, to cgRect: CGRect, display: SCDisplay) -> CGImage? {
|
|
|
|
|
// cgRect is already in SCDisplay space (Y flipped relative to AppKit).
|
|
|
|
|
// Derive the pixel scale from the image we actually got, relative to
|
|
|
|
|
// the display's point frame.
|
|
|
|
|
let scaleX = CGFloat(image.width) / display.frame.width
|
|
|
|
|
let scaleY = CGFloat(image.height) / display.frame.height
|
|
|
|
|
guard scaleX > 0, scaleY > 0 else { return nil }
|
|
|
|
|
let cgRect = CGRect(
|
|
|
|
|
x: rect.minX,
|
|
|
|
|
y: rect.minY,
|
|
|
|
|
width: rect.width,
|
|
|
|
|
height: rect.height
|
|
|
|
|
)
|
|
|
|
|
let pxRect = CGRect(
|
|
|
|
|
x: (cgRect.minX - display.frame.minX) * scaleX,
|
|
|
|
|
y: (cgRect.minY - display.frame.minY) * scaleY,
|
|
|
|
@@ -145,18 +171,30 @@ final class ScreenCapture {
|
|
|
|
|
// width/height are output PIXELS. Multiply by the backing scale
|
|
|
|
|
// of the screen containing the selection so retina displays are
|
|
|
|
|
// captured at full resolution and 1x displays stay 1x.
|
|
|
|
|
let backingScale = NSScreen.screens
|
|
|
|
|
.first(where: { $0.frame.contains(NSPoint(x: rect.midX, y: rect.midY)) })?
|
|
|
|
|
.backingScaleFactor ?? 1
|
|
|
|
|
guard let screen = NSScreen.screens.first(where: { $0.frame.contains(NSPoint(x: rect.midX, y: rect.midY)) }) else {
|
|
|
|
|
completion(nil)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
let backingScale = screen.backingScaleFactor
|
|
|
|
|
config.width = Int(CGFloat(display.width) * backingScale)
|
|
|
|
|
config.height = Int(CGFloat(display.height) * backingScale)
|
|
|
|
|
config.showsCursor = true
|
|
|
|
|
|
|
|
|
|
// ScreenCaptureKit uses a different origin than AppKit for
|
|
|
|
|
// screens stacked around the main display: per-display,
|
|
|
|
|
// SCDisplay y = appKitY - NSScreen.maxY + SCDisplay.maxY.
|
|
|
|
|
let scRect = CGRect(
|
|
|
|
|
x: rect.minX,
|
|
|
|
|
y: rect.minY - screen.frame.maxY + display.frame.maxY,
|
|
|
|
|
width: rect.width,
|
|
|
|
|
height: rect.height
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
guard let image = try? await SCScreenshotManager.captureImage(contentFilter: filter, configuration: config) else {
|
|
|
|
|
completion(nil)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
completion(self.crop(image, to: rect, display: display))
|
|
|
|
|
completion(self.crop(image, to: scRect, display: display))
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
guard let primary = NSScreen.screens.first(where: { $0.frame.origin == .zero }) ?? NSScreen.screens.first else {
|
|
|
|
|