|
|
|
@@ -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,31 +117,31 @@ final class ScreenCapture {
|
|
|
|
|
private init() {}
|
|
|
|
|
|
|
|
|
|
@available(macOS 14.0, *)
|
|
|
|
|
private func crop(_ image: CGImage, to rect: NSRect, display: SCDisplay) -> CGImage? {
|
|
|
|
|
// Scale from the actual captured image dimensions, which reflect the
|
|
|
|
|
// stream's pixel output (retina => 2x, non-retina => 1x).
|
|
|
|
|
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
|
|
|
|
|
let cgRect = CGRect(
|
|
|
|
|
x: rect.minX,
|
|
|
|
|
y: rect.minY,
|
|
|
|
|
width: rect.width,
|
|
|
|
|
height: rect.height
|
|
|
|
|
)
|
|
|
|
|
guard scaleX > 0, scaleY > 0 else { return nil }
|
|
|
|
|
let pxRect = CGRect(
|
|
|
|
|
x: (cgRect.minX - display.frame.minX) * scaleX,
|
|
|
|
|
y: (cgRect.minY - display.frame.minY) * scaleY,
|
|
|
|
|
width: cgRect.width * scaleX,
|
|
|
|
|
height: cgRect.height * scaleY
|
|
|
|
|
)
|
|
|
|
|
// Guard against negative offsets when the selection reaches beyond the
|
|
|
|
|
// display's frame (e.g. secondary monitor coordinate quirks).
|
|
|
|
|
let safeRect = pxRect.intersection(CGRect(x: 0, y: 0, width: CGFloat(image.width), height: CGFloat(image.height)))
|
|
|
|
|
guard safeRect.width >= 1, safeRect.height >= 1 else { return nil }
|
|
|
|
|
guard let cropCtx = CGContext(
|
|
|
|
|
data: nil, width: Int(pxRect.width), height: Int(pxRect.height),
|
|
|
|
|
data: nil, width: Int(safeRect.width), height: Int(safeRect.height),
|
|
|
|
|
bitsPerComponent: 8, bytesPerRow: 0,
|
|
|
|
|
space: CGColorSpaceCreateDeviceRGB(),
|
|
|
|
|
bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue
|
|
|
|
|
) else { return nil }
|
|
|
|
|
cropCtx.interpolationQuality = .high
|
|
|
|
|
cropCtx.draw(image, in: CGRect(x: -pxRect.minX, y: -pxRect.minY, width: CGFloat(image.width), height: CGFloat(image.height)))
|
|
|
|
|
cropCtx.draw(image, in: CGRect(x: -safeRect.minX, y: -safeRect.minY, width: CGFloat(image.width), height: CGFloat(image.height)))
|
|
|
|
|
return cropCtx.makeImage()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -135,20 +167,34 @@ final class ScreenCapture {
|
|
|
|
|
let excluded = content.windows.filter { excludedSet.contains($0.windowID) }
|
|
|
|
|
let filter = SCContentFilter(display: display, excludingWindows: excluded)
|
|
|
|
|
let config = SCStreamConfiguration()
|
|
|
|
|
// width/height are in points. Use the display's backing scale so
|
|
|
|
|
// retina captures come out at full pixel resolution.
|
|
|
|
|
let backingScale = NSScreen.screens.first(where: { $0.frame.contains(
|
|
|
|
|
NSPoint(x: rect.midX, y: rect.midY)
|
|
|
|
|
) })?.backingScaleFactor ?? 2
|
|
|
|
|
config.width = Int(display.frame.width * backingScale)
|
|
|
|
|
config.height = Int(display.frame.height * backingScale)
|
|
|
|
|
// SCDisplay.width/height are in POINTS; SCStreamConfiguration
|
|
|
|
|
// 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.
|
|
|
|
|
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's Y axis is flipped relative to AppKit
|
|
|
|
|
// NSScreen frames. Convert the selection into SCDisplay space
|
|
|
|
|
// using the matching NSScreen's top edge, then crop there.
|
|
|
|
|
let scRect = CGRect(
|
|
|
|
|
x: rect.minX,
|
|
|
|
|
y: screen.frame.maxY - rect.maxY + display.frame.minY,
|
|
|
|
|
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 {
|
|
|
|
|