Merge pull request #429 from klaudworks/fix/ocr-window-detection
Fix OCR capturing VoiceInk status overlay instead of frontmost app window
This commit is contained in:
commit
45dbd57722
@ -14,20 +14,52 @@ class ScreenCaptureService: ObservableObject {
|
|||||||
category: "aienhancement"
|
category: "aienhancement"
|
||||||
)
|
)
|
||||||
|
|
||||||
private func getActiveWindowInfo() -> (title: String, ownerName: String, windowID: CGWindowID)? {
|
private struct WindowCandidate {
|
||||||
let windowListInfo = CGWindowListCopyWindowInfo([.optionOnScreenOnly], kCGNullWindowID) as? [[String: Any]] ?? []
|
let title: String
|
||||||
|
let ownerName: String
|
||||||
|
let windowID: CGWindowID
|
||||||
|
let ownerPID: pid_t
|
||||||
|
let layer: Int32
|
||||||
|
}
|
||||||
|
|
||||||
if let frontWindow = windowListInfo.first(where: { info in
|
private func getActiveWindowInfo() -> (title: String, ownerName: String, windowID: CGWindowID)? {
|
||||||
let layer = info[kCGWindowLayer as String] as? Int32 ?? 0
|
let currentPID = ProcessInfo.processInfo.processIdentifier
|
||||||
return layer == 0
|
let frontmostPID = NSWorkspace.shared.frontmostApplication?.processIdentifier
|
||||||
}) {
|
let windowListInfo = CGWindowListCopyWindowInfo([.optionOnScreenOnly], kCGNullWindowID) as? [[String: Any]] ?? []
|
||||||
guard let windowID = frontWindow[kCGWindowNumber as String] as? CGWindowID,
|
|
||||||
let ownerName = frontWindow[kCGWindowOwnerName as String] as? String,
|
let candidates = windowListInfo.compactMap { info -> WindowCandidate? in
|
||||||
let title = frontWindow[kCGWindowName as String] as? String else {
|
guard let windowID = info[kCGWindowNumber as String] as? CGWindowID,
|
||||||
|
let ownerName = info[kCGWindowOwnerName as String] as? String,
|
||||||
|
let ownerPIDNumber = info[kCGWindowOwnerPID as String] as? NSNumber,
|
||||||
|
let layer = info[kCGWindowLayer as String] as? Int32 else {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return (title: title, ownerName: ownerName, windowID: windowID)
|
let rawTitle = (info[kCGWindowName as String] as? String)?.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||||
|
let resolvedTitle = rawTitle?.isEmpty == false ? rawTitle! : ownerName
|
||||||
|
|
||||||
|
return WindowCandidate(
|
||||||
|
title: resolvedTitle,
|
||||||
|
ownerName: ownerName,
|
||||||
|
windowID: windowID,
|
||||||
|
ownerPID: ownerPIDNumber.int32Value,
|
||||||
|
layer: layer
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
func isEligible(_ candidate: WindowCandidate) -> Bool {
|
||||||
|
guard candidate.layer == 0 else { return false }
|
||||||
|
guard candidate.ownerPID != currentPID else { return false }
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
if let frontmostPID = frontmostPID,
|
||||||
|
let focusedWindow = candidates.first(where: { isEligible($0) && $0.ownerPID == frontmostPID }) {
|
||||||
|
return (title: focusedWindow.title, ownerName: focusedWindow.ownerName, windowID: focusedWindow.windowID)
|
||||||
|
}
|
||||||
|
|
||||||
|
if let fallbackWindow = candidates.first(where: isEligible) {
|
||||||
|
return (title: fallbackWindow.title, ownerName: fallbackWindow.ownerName, windowID: fallbackWindow.windowID)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user