Repository navigation
fix(macos): detect the frontmost window via Quartz instead of NSWorkspace - #20
Conversation
…pace NSWorkspace.frontmostApplication() is updated through notifications on the main NSRunLoop. ScreenMind's capture loop has no run loop, so the value is read once and never changes: every capture was labeled with whatever app was in front at launch (usually Terminal). Read the live on-screen window list from Quartz instead. It is ordered front to back, so the first layer-0 window is the active one. This also gives the real window title (kCGWindowName, needs Screen Recording permission) instead of the app name, and the correct PID for the accessibility text extraction. Same fix for ScreenCapture._window_center_macos, which used the stale PID to choose the monitor when capture_active_monitor is on. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Move the frontmost-window bounds lookup out of ScreenCapture and into the platform layer, following the platform_support pattern: - PlatformAdapter.get_active_window_bounds() returns None by default (Windows/Linux unchanged). - MacOSAdapter implements it from the same _front_window() used for the app name and title, so monitor selection and app detection agree on which window is in front (same layer-0 and min-size rules). - ScreenCapture._window_center_macos() now just asks the adapter. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
my comments:
|
Problem
On macOS every capture gets the same app: the one in front when ScreenMind started (usually Terminal). The window title is also wrong. It is always the app name, never the real title.
NSWorkspace.frontmostApplication()updates through notifications on the mainNSRunLoop. ScreenMind's capture loop has no run loop, so the value never changes after the first read.Fix
CGWindowListCopyWindowInfo. It is ordered front to back, so the first layer-0 window is the active one.kCGWindowName) needs Screen Recording permission. Without it, we fall back to the app name.ScreenCapture._window_center_macos, which used the stale PID to choose the monitor whencapture_active_monitoris on. It now asks the platform layer: newPlatformAdapter.get_active_window_bounds()returnsNoneby default, so Windows and Linux don't change.MacOSAdapterimplements it from the same_front_window(), so monitor selection and app detection always agree on which window is in front.A side benefit: accessibility text extraction now gets the right PID too.
Testing
tests/test_macos_frontmost.pyfakes the Quartz module, so it runs on the Windows CI. It covers front-to-back order, skipping the menu bar and tiny windows, title fallback, changes between calls, window bounds, andScreenCaptureusing the adapter.Terminal. After it, each capture had the right app and the real window title.🤖 Generated with Claude Code