Merge pull request #730 from cnwangjie/fix-window-mac
Some checks are pending
Go / test (macOS-latest) (push) Waiting to run
Go / test (windows-latest) (push) Waiting to run

Fix: use window ref instead of app ref to get bounds on macos
This commit is contained in:
Evans 2025-06-15 12:16:48 -07:00 committed by GitHub
commit dd208c6dd1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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)){
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)