mirror of
https://github.com/ksyasuda/SubMiner.git
synced 2026-05-26 00:55:16 -07:00
Fix macOS overlay foreground handling and character-dictionary cache reuse (#68)
This commit is contained in:
@@ -7,7 +7,7 @@
|
||||
// It works with both bundled and unbundled mpv installations.
|
||||
//
|
||||
// Usage: swift get-mpv-window-macos.swift
|
||||
// Output: "x,y,width,height" or "not-found"
|
||||
// Output: "x,y,width,height,focused", "minimized", "active", "inactive", or "not-found"
|
||||
//
|
||||
|
||||
import Cocoa
|
||||
@@ -25,9 +25,16 @@ private struct WindowState {
|
||||
let focused: Bool
|
||||
}
|
||||
|
||||
private struct FrontmostApplicationState {
|
||||
let pid: pid_t
|
||||
let isMpv: Bool
|
||||
}
|
||||
|
||||
private enum WindowLookupResult {
|
||||
case visible(WindowState)
|
||||
case minimized
|
||||
case active
|
||||
case inactive
|
||||
}
|
||||
|
||||
private let targetMpvSocketPath: String? = {
|
||||
@@ -146,8 +153,41 @@ private func geometryFromAXWindow(_ axWindow: AXUIElement) -> WindowGeometry? {
|
||||
return geometry
|
||||
}
|
||||
|
||||
private func frontmostApplicationPid() -> pid_t? {
|
||||
NSWorkspace.shared.frontmostApplication?.processIdentifier
|
||||
private func frontmostApplicationState() -> FrontmostApplicationState? {
|
||||
guard let app = NSWorkspace.shared.frontmostApplication else {
|
||||
return nil
|
||||
}
|
||||
|
||||
return FrontmostApplicationState(
|
||||
pid: app.processIdentifier,
|
||||
isMpv: app.localizedName.map(normalizedMpvName) ?? false
|
||||
)
|
||||
}
|
||||
|
||||
private func isFocusedMpvWindow(ownerPid: pid_t, frontmost: FrontmostApplicationState?) -> Bool {
|
||||
guard let frontmost = frontmost else {
|
||||
return false
|
||||
}
|
||||
|
||||
if frontmost.pid == ownerPid {
|
||||
return true
|
||||
}
|
||||
|
||||
return frontmost.isMpv && windowHasTargetSocket(ownerPid)
|
||||
}
|
||||
|
||||
private func isFrontmostTargetMpv(_ frontmost: FrontmostApplicationState?) -> Bool {
|
||||
guard let frontmost = frontmost, frontmost.isMpv else {
|
||||
return false
|
||||
}
|
||||
|
||||
if windowHasTargetSocket(frontmost.pid) {
|
||||
return true
|
||||
}
|
||||
|
||||
// When macOS says mpv is frontmost but geometry APIs miss, keep the
|
||||
// overlay stable even if ps cannot expose the socket argument.
|
||||
return targetMpvSocketPath != nil
|
||||
}
|
||||
|
||||
private func windowStateFromAccessibilityAPI() -> WindowLookupResult? {
|
||||
@@ -158,7 +198,7 @@ private func windowStateFromAccessibilityAPI() -> WindowLookupResult? {
|
||||
return normalizedMpvName(name)
|
||||
}
|
||||
|
||||
let frontmostPid = frontmostApplicationPid()
|
||||
let frontmost = frontmostApplicationState()
|
||||
var foundMinimizedTargetWindow = false
|
||||
|
||||
for app in runningApps {
|
||||
@@ -198,7 +238,7 @@ private func windowStateFromAccessibilityAPI() -> WindowLookupResult? {
|
||||
return .visible(
|
||||
WindowState(
|
||||
geometry: geometry,
|
||||
focused: frontmostPid == windowPid
|
||||
focused: isFocusedMpvWindow(ownerPid: windowPid, frontmost: frontmost)
|
||||
)
|
||||
)
|
||||
}
|
||||
@@ -217,7 +257,7 @@ private func windowStateFromCoreGraphics() -> WindowState? {
|
||||
// Use on-screen layer-0 windows to avoid off-screen helpers/shadows.
|
||||
let options: CGWindowListOption = [.optionOnScreenOnly, .excludeDesktopElements]
|
||||
let windowList = CGWindowListCopyWindowInfo(options, kCGNullWindowID) as? [[String: Any]] ?? []
|
||||
let frontmostPid = frontmostApplicationPid()
|
||||
let frontmost = frontmostApplicationState()
|
||||
|
||||
for window in windowList {
|
||||
guard let ownerName = window[kCGWindowOwnerName as String] as? String,
|
||||
@@ -260,7 +300,7 @@ private func windowStateFromCoreGraphics() -> WindowState? {
|
||||
|
||||
return WindowState(
|
||||
geometry: geometry,
|
||||
focused: frontmostPid == ownerPid
|
||||
focused: isFocusedMpvWindow(ownerPid: ownerPid, frontmost: frontmost)
|
||||
)
|
||||
}
|
||||
|
||||
@@ -274,6 +314,13 @@ private let lookupResult: WindowLookupResult? = {
|
||||
if let cgWindow = windowStateFromCoreGraphics() {
|
||||
return .visible(cgWindow)
|
||||
}
|
||||
let frontmost = frontmostApplicationState()
|
||||
if isFrontmostTargetMpv(frontmost) {
|
||||
return .active
|
||||
}
|
||||
if frontmost != nil {
|
||||
return .inactive
|
||||
}
|
||||
return nil
|
||||
}()
|
||||
|
||||
@@ -285,6 +332,10 @@ if let result = lookupResult {
|
||||
)
|
||||
case .minimized:
|
||||
print("minimized")
|
||||
case .active:
|
||||
print("active")
|
||||
case .inactive:
|
||||
print("inactive")
|
||||
}
|
||||
} else {
|
||||
print("not-found")
|
||||
|
||||
Reference in New Issue
Block a user