Capture via ScreenCaptureKit, multi-display crop fix, app icon generation

This commit is contained in:
2026-08-10 22:20:53 +08:00
parent c64c7296e2
commit 9555ec7952
5 changed files with 167 additions and 26 deletions
+88 -25
View File
@@ -1,5 +1,6 @@
import AppKit
import CoreGraphics
import ScreenCaptureKit
final class CaptureController: NSObject {
static let shared = CaptureController()
@@ -7,6 +8,7 @@ final class CaptureController: NSObject {
private var overlays: [NSWindow] = []
private(set) var isSelecting = false
private var previousApp: NSRunningApplication?
private var overlayIDs: [Int] = []
func begin() {
guard !isSelecting else { return }
@@ -29,16 +31,18 @@ final class CaptureController: NSObject {
)
}
overlays.forEach { $0.makeKeyAndOrderFront(nil) }
overlayIDs = overlays.map { $0.windowNumber }
}
private func finishSelection(_ rect: NSRect) {
let ids = overlayIDs
hideOverlays()
guard rect.width >= 2, rect.height >= 2 else {
cancelSelection()
return
}
DispatchQueue.main.async { [self] in
capture(rect)
capture(rect, excludingWindowIDs: ids)
}
}
@@ -58,40 +62,99 @@ final class CaptureController: NSObject {
previousApp = nil
}
private func capture(_ rect: NSRect) {
guard let image = ScreenCapture.capture(rect) else {
isSelecting = false
restorePreviousApp()
Task { @MainActor in
ResultWindow.shared.show(image: ResultWindow.failureImage("Capture failed. Check Screen Recording permission."))
}
return
}
DispatchQueue.main.async { [weak self] in
self?.isSelecting = false
self?.restorePreviousApp()
Task { @MainActor in
ResultWindow.shared.show(image: image)
private func capture(_ rect: NSRect, excludingWindowIDs ids: [Int]) {
ScreenCapture.shared.capture(rect, excludingWindowIDs: ids) { [weak self] image in
DispatchQueue.main.async {
self?.isSelecting = false
self?.restorePreviousApp()
Task { @MainActor in
guard let image else {
ResultWindow.shared.show(image: ResultWindow.failureImage("Capture failed. Check Screen Recording permission."))
return
}
ResultWindow.shared.show(image: image)
}
}
}
}
}
enum ScreenCapture {
static func capture(_ rect: NSRect) -> CGImage? {
guard let primary = NSScreen.screens.first(where: { $0.frame.origin == .zero })
?? NSScreen.screens.first else { return nil }
final class ScreenCapture {
static let shared = ScreenCapture()
private init() {}
@available(macOS 14.0, *)
private func crop(_ image: CGImage, to rect: NSRect, display: SCDisplay) -> CGImage? {
let scaleX = CGFloat(display.width) / display.frame.width
let scaleY = CGFloat(display.height) / display.frame.height
let cgRect = CGRect(
x: rect.minX,
y: primary.frame.height - rect.maxY,
y: rect.minY,
width: rect.width,
height: rect.height
)
return CGWindowListCreateImage(
cgRect,
.optionOnScreenOnly,
kCGNullWindowID,
[.bestResolution, .boundsIgnoreFraming]
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 let cropCtx = CGContext(
data: nil, width: Int(pxRect.width), height: Int(pxRect.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)))
return cropCtx.makeImage()
}
@available(macOS 14.0, *)
private func displayContaining(_ rect: NSRect, in content: SCShareableContent) -> SCDisplay? {
let mid = NSPoint(x: rect.midX, y: rect.midY)
guard let screen = NSScreen.screens.first(where: { $0.frame.contains(mid) }) ?? NSScreen.screens.first else {
return nil
}
let cgID = (screen.deviceDescription[NSDeviceDescriptionKey("NSScreenNumber")] as? NSNumber)?.uint32Value ?? 0
return content.displays.first(where: { $0.displayID == cgID }) ?? content.displays.first
}
func capture(_ rect: NSRect, excludingWindowIDs excludedIDs: [Int], completion: @escaping (CGImage?) -> Void) {
if #available(macOS 14.0, *) {
Task {
guard let content = try? await SCShareableContent.current,
let display = displayContaining(rect, in: content) else {
completion(nil)
return
}
let excludedSet = Set(excludedIDs.map { CGWindowID($0) })
let excluded = content.windows.filter { excludedSet.contains($0.windowID) }
let filter = SCContentFilter(display: display, excludingWindows: excluded)
let config = SCStreamConfiguration()
config.width = Int(display.width)
config.height = Int(display.height)
config.showsCursor = true
guard let image = try? await SCScreenshotManager.captureImage(contentFilter: filter, configuration: config) else {
completion(nil)
return
}
completion(self.crop(image, to: rect, display: display))
}
} else {
guard let primary = NSScreen.screens.first(where: { $0.frame.origin == .zero }) ?? NSScreen.screens.first else {
completion(nil)
return
}
let cgRect = CGRect(
x: rect.minX,
y: primary.frame.height - rect.maxY,
width: rect.width,
height: rect.height
)
completion(CGWindowListCreateImage(cgRect, .optionOnScreenOnly, kCGNullWindowID, [.bestResolution, .boundsIgnoreFraming]))
}
}
}
+1 -1
View File
@@ -158,7 +158,7 @@ struct ResultView: View {
Spacer()
Button("Save PNG…") { model.savePNG() }
Button("Get Text") { model.runOCR() }
.disabled(model.isOCRRunning || model.extractedText != nil || model.mode == .text)
.disabled(model.isOCRRunning || model.extractedText != nil)
Button("Close") { onClose() }
}
.padding(.horizontal, 12)