Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
26700c59f4 | ||
|
|
195c6a9b1f | ||
|
|
ecac7885dd |
+27
-13
@@ -119,33 +119,47 @@ enum LoginItem {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func runOCRSelfTest() {
|
func renderTestImage(_ text: String, fontSize: CGFloat = 56) -> CGImage? {
|
||||||
let text = "Kapture OCR test 1234, hello world"
|
let size = NSSize(width: 1900, height: 220)
|
||||||
let size = NSSize(width: 1600, height: 220)
|
|
||||||
guard let rep = NSBitmapImageRep(
|
guard let rep = NSBitmapImageRep(
|
||||||
bitmapDataPlanes: nil, pixelsWide: Int(size.width), pixelsHigh: Int(size.height),
|
bitmapDataPlanes: nil, pixelsWide: Int(size.width), pixelsHigh: Int(size.height),
|
||||||
bitsPerSample: 8, samplesPerPixel: 4, hasAlpha: true, isPlanar: false,
|
bitsPerSample: 8, samplesPerPixel: 4, hasAlpha: true, isPlanar: false,
|
||||||
colorSpaceName: .deviceRGB, bytesPerRow: 0, bitsPerPixel: 0
|
colorSpaceName: .deviceRGB, bytesPerRow: 0, bitsPerPixel: 0
|
||||||
) else { exit(1) }
|
) else { return nil }
|
||||||
|
|
||||||
NSGraphicsContext.saveGraphicsState()
|
NSGraphicsContext.saveGraphicsState()
|
||||||
NSGraphicsContext.current = NSGraphicsContext(bitmapImageRep: rep)
|
NSGraphicsContext.current = NSGraphicsContext(bitmapImageRep: rep)
|
||||||
NSColor.white.setFill()
|
NSColor.white.setFill()
|
||||||
NSRect(origin: .zero, size: size).fill()
|
NSRect(origin: .zero, size: size).fill()
|
||||||
let attrs: [NSAttributedString.Key: Any] = [
|
let attrs: [NSAttributedString.Key: Any] = [
|
||||||
.font: NSFont.boldSystemFont(ofSize: 56),
|
.font: NSFont.systemFont(ofSize: fontSize),
|
||||||
.foregroundColor: NSColor.black,
|
.foregroundColor: NSColor.black,
|
||||||
]
|
]
|
||||||
text.draw(at: NSPoint(x: 60, y: 75), withAttributes: attrs)
|
text.draw(at: NSPoint(x: 60, y: 75), withAttributes: attrs)
|
||||||
NSGraphicsContext.restoreGraphicsState()
|
NSGraphicsContext.restoreGraphicsState()
|
||||||
|
return rep.cgImage
|
||||||
|
}
|
||||||
|
|
||||||
guard let cgImage = rep.cgImage else { exit(1) }
|
func runOCRSelfTest() {
|
||||||
OCRService.shared.recognize(cgImage) { recognized in
|
let cases: [(expected: String, contains: String)] = [
|
||||||
print("expected: \(text)")
|
("Kapture OCR test 1234, hello world", "Kapture OCR test 1234"),
|
||||||
print("got: \(recognized)")
|
("你好世界,这是一个测试", "你好世界"),
|
||||||
let ok = recognized == text
|
]
|
||||||
print(ok ? "PASS" : "FAIL")
|
var index = 0
|
||||||
exit(ok ? 0 : 1)
|
|
||||||
|
func runNext() {
|
||||||
|
guard index < cases.count else { exit(0) }
|
||||||
|
let current = cases[index]
|
||||||
|
index += 1
|
||||||
|
guard let image = renderTestImage(current.expected) else { exit(1) }
|
||||||
|
OCRService.shared.recognize(image) { recognized in
|
||||||
|
print("case: \(current.expected)")
|
||||||
|
print("got: \(recognized)")
|
||||||
|
let ok = recognized.contains(current.contains)
|
||||||
|
print(ok ? "PASS" : "FAIL")
|
||||||
|
if !ok { exit(1) }
|
||||||
|
runNext()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
runNext()
|
||||||
RunLoop.main.run()
|
RunLoop.main.run()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -84,21 +84,26 @@ final class CaptureController: NSObject {
|
|||||||
}
|
}
|
||||||
isSelecting = true
|
isSelecting = true
|
||||||
previousApp = NSWorkspace.shared.frontmostApplication
|
previousApp = NSWorkspace.shared.frontmostApplication
|
||||||
// Activation is async on macOS 26; wait a tick so the app is active
|
// With the event tap driving input we must NOT activate: stealing
|
||||||
// before the overlays appear (otherwise clicks pass through).
|
// focus turns the target app into background mode (dimmed chrome),
|
||||||
NSApp.activate(ignoringOtherApps: true)
|
// and that is what would end up in the screenshot. Activate only on
|
||||||
CaptureController.log("begin: activate issued, active=\(NSApp.isActive)")
|
// the fallback path, where panel events are the only input.
|
||||||
|
let usesTap = AXIsProcessTrusted()
|
||||||
|
if !usesTap {
|
||||||
|
NSApp.activate(ignoringOtherApps: true)
|
||||||
|
}
|
||||||
|
CaptureController.log("begin: usesTap=\(usesTap) active=\(NSApp.isActive)")
|
||||||
DispatchQueue.main.asyncAfter(deadline: .now() + 0.05) { [weak self] in
|
DispatchQueue.main.asyncAfter(deadline: .now() + 0.05) { [weak self] in
|
||||||
guard let self, self.isSelecting else { return }
|
guard let self, self.isSelecting else { return }
|
||||||
CaptureController.log("begin: active=\(NSApp.isActive) — showing overlays")
|
|
||||||
self.overlays = NSScreen.screens.map { screen in
|
self.overlays = NSScreen.screens.map { screen in
|
||||||
let panel = OverlayWindow(screen: screen, controller: self)
|
let panel = OverlayWindow(screen: screen, controller: self)
|
||||||
(panel.contentView as? OverlayView)?.controller = self
|
(panel.contentView as? OverlayView)?.controller = self
|
||||||
return panel
|
return panel
|
||||||
}
|
}
|
||||||
self.overlays.forEach { $0.makeKeyAndOrderFront(nil) }
|
// orderFrontRegardless: show without taking key/focus.
|
||||||
|
self.overlays.forEach { $0.orderFrontRegardless() }
|
||||||
self.overlayIDs = self.overlays.map { $0.windowNumber }
|
self.overlayIDs = self.overlays.map { $0.windowNumber }
|
||||||
CaptureController.log("begin: \(self.overlays.count) overlays, ids=\(self.overlayIDs), active=\(NSApp.isActive)")
|
CaptureController.log("begin: \(self.overlays.count) overlays, ids=\(self.overlayIDs) — prior app stays focused")
|
||||||
self.installEventTap()
|
self.installEventTap()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,6 +12,11 @@ final class OCRService {
|
|||||||
request.recognitionLevel = .accurate
|
request.recognitionLevel = .accurate
|
||||||
request.usesLanguageCorrection = true
|
request.usesLanguageCorrection = true
|
||||||
request.recognitionLanguages = ["en-US", "zh-Hans", "zh-Hant", "ja-JP", "ko-KR"]
|
request.recognitionLanguages = ["en-US", "zh-Hans", "zh-Hant", "ja-JP", "ko-KR"]
|
||||||
|
// Without this, Vision defaults to the first listed language and
|
||||||
|
// renders CJK as Latin garbage (verified: 你好世界 -> "177WR").
|
||||||
|
if #available(macOS 13.0, *) {
|
||||||
|
request.automaticallyDetectsLanguage = true
|
||||||
|
}
|
||||||
|
|
||||||
let handler = VNImageRequestHandler(cgImage: cgImage, options: [:])
|
let handler = VNImageRequestHandler(cgImage: cgImage, options: [:])
|
||||||
do {
|
do {
|
||||||
|
|||||||
@@ -6,8 +6,11 @@ final class OverlayWindow: NSPanel {
|
|||||||
init(screen: NSScreen, controller: CaptureController) {
|
init(screen: NSScreen, controller: CaptureController) {
|
||||||
let content = OverlayView()
|
let content = OverlayView()
|
||||||
super.init(
|
super.init(
|
||||||
|
// Non-activating: the overlay must NOT steal focus, so the app
|
||||||
|
// being captured keeps its active appearance (undimmed chrome).
|
||||||
|
// Input comes from the CGEventTap, not panel events.
|
||||||
contentRect: screen.frame,
|
contentRect: screen.frame,
|
||||||
styleMask: [.borderless],
|
styleMask: [.borderless, .nonactivatingPanel],
|
||||||
backing: .buffered,
|
backing: .buffered,
|
||||||
defer: false
|
defer: false
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -195,14 +195,15 @@ final class CaptureModel: ObservableObject {
|
|||||||
return rep.cgImage ?? image
|
return rep.cgImage ?? image
|
||||||
}
|
}
|
||||||
|
|
||||||
func addStroke(_ points: [CGPoint]) {
|
/// `widthPx` is the stroke thickness in IMAGE PIXELS (the preview
|
||||||
|
/// converts the slider value so exports match what was drawn).
|
||||||
|
func addStroke(_ points: [CGPoint], widthPx: CGFloat) {
|
||||||
guard points.count > 1 else { return }
|
guard points.count > 1 else { return }
|
||||||
if tool == .redact {
|
if tool == .redact {
|
||||||
// Redaction: a filled black bar from start to end, as thick as the
|
// Redaction: a filled black bar from start to end.
|
||||||
// current stroke width (drawn as a rect for clean edges).
|
strokes.append(Stroke(points: points, width: max(widthPx, 12), color: .black, isRedaction: true))
|
||||||
strokes.append(Stroke(points: points, width: max(strokeWidth, 12), color: .black, isRedaction: true))
|
|
||||||
} else {
|
} else {
|
||||||
strokes.append(Stroke(points: points, width: strokeWidth, color: drawColor))
|
strokes.append(Stroke(points: points, width: widthPx, color: drawColor))
|
||||||
}
|
}
|
||||||
redoStack = []
|
redoStack = []
|
||||||
}
|
}
|
||||||
@@ -575,7 +576,8 @@ struct ScreenshotPane: View {
|
|||||||
for point in stroke.points.dropFirst() {
|
for point in stroke.points.dropFirst() {
|
||||||
path.addLine(to: CGPoint(x: origin.x + point.x * scale, y: origin.y + point.y * scale))
|
path.addLine(to: CGPoint(x: origin.x + point.x * scale, y: origin.y + point.y * scale))
|
||||||
}
|
}
|
||||||
context.stroke(path, with: .color(Color(nsColor: stroke.color)), lineWidth: stroke.width)
|
// stroke.width is in image pixels; scale to view points
|
||||||
|
context.stroke(path, with: .color(Color(nsColor: stroke.color)), lineWidth: stroke.width * scale)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -636,7 +638,7 @@ struct ScreenshotPane: View {
|
|||||||
.onEnded { _ in
|
.onEnded { _ in
|
||||||
if model.tool != .select {
|
if model.tool != .select {
|
||||||
if currentStroke.count > 1 {
|
if currentStroke.count > 1 {
|
||||||
model.addStroke(currentStroke)
|
model.addStroke(currentStroke, widthPx: model.strokeWidth / max(scale, 0.01))
|
||||||
}
|
}
|
||||||
currentStroke = []
|
currentStroke = []
|
||||||
} 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.8.7</string>
|
<string>1.9.2</string>
|
||||||
<key>CFBundleVersion</key>
|
<key>CFBundleVersion</key>
|
||||||
<string>87</string>
|
<string>92</string>
|
||||||
<key>LSUIElement</key>
|
<key>LSUIElement</key>
|
||||||
<true/>
|
<true/>
|
||||||
<key>NSHighResolutionCapable</key>
|
<key>NSHighResolutionCapable</key>
|
||||||
|
|||||||
Reference in New Issue
Block a user