From ae4390f607a4726777d6a810fc3f24354890d4f0 Mon Sep 17 00:00:00 2001 From: Wang Jie Date: Sun, 15 Jun 2025 00:05:35 +0800 Subject: [PATCH] Fix: use window ref instead of app ref to get bounds on macos --- window/win_sys.h | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/window/win_sys.h b/window/win_sys.h index d71ba11..2185ebb 100644 --- a/window/win_sys.h +++ b/window/win_sys.h @@ -3,7 +3,7 @@ // https://github.com/go-vgo/robotgo/blob/master/LICENSE // // Licensed under the Apache License, Version 2.0 +// http://www.apache.org/licenses/LICENSE-2.0> // // This file may not be copied, modified, or distributed // except according to those terms. @@ -24,33 +24,45 @@ Bounds get_bounds(uintptr pid, int8_t isPid){ AXValueRef axp = NULL; AXValueRef axs = NULL; AXUIElementRef AxID = AXUIElementCreateApplication(pid); + AXUIElementRef AxWin = NULL; + + // Get the window from the application + if (AXUIElementCopyAttributeValue(AxID, kAXFocusedWindowAttribute, (CFTypeRef *)&AxWin) + != kAXErrorSuccess || AxWin == NULL) { + // If no focused window, try to get the main window + if (AXUIElementCopyAttributeValue(AxID, kAXMainWindowAttribute, (CFTypeRef *)&AxWin) + != kAXErrorSuccess || AxWin == NULL) { + goto exit; + } + } // Determine the current point of the window - if (AXUIElementCopyAttributeValue(AxID, kAXPositionAttribute, (CFTypeRef*) &axp) - != kAXErrorSuccess || axp == NULL){ + if (AXUIElementCopyAttributeValue(AxWin, kAXPositionAttribute, (CFTypeRef*) &axp) + != kAXErrorSuccess || axp == NULL) { goto exit; } // Determine the current size of the window - if (AXUIElementCopyAttributeValue(AxID, kAXSizeAttribute, (CFTypeRef*) &axs) - != kAXErrorSuccess || axs == NULL){ + if (AXUIElementCopyAttributeValue(AxWin, kAXSizeAttribute, (CFTypeRef*) &axs) + != kAXErrorSuccess || axs == NULL) { goto exit; } CGPoint p; CGSize s; // Attempt to convert both values into atomic types - if (AXValueGetValue(axp, kAXValueCGPointType, &p) && - AXValueGetValue(axs, kAXValueCGSizeType, &s)){ + if (AXValueGetValue(axp, kAXValueCGPointType, &p) && + AXValueGetValue(axs, kAXValueCGSizeType, &s)) { bounds.X = p.x; bounds.Y = p.y; bounds.W = s.width; bounds.H = s.height; } - - // return bounds; + exit: if (axp != NULL) { CFRelease(axp); } if (axs != NULL) { CFRelease(axs); } + if (AxWin != NULL) { CFRelease(AxWin); } + if (AxID != NULL) { CFRelease(AxID); } return bounds; #elif defined(USE_X11) @@ -126,7 +138,7 @@ Bounds get_client(uintptr pid, int8_t isPid) { bounds.W = attr.width; bounds.H = attr.height; XCloseDisplay(rDisplay); - + return bounds; #elif defined(IS_WINDOWS) HWND hwnd = getHwnd(pid, isPid); @@ -148,4 +160,4 @@ Bounds get_client(uintptr pid, int8_t isPid) { return bounds; #endif -} \ No newline at end of file +}