|
1 | 1 | #include "../window_manager.h"
|
2 | 2 | #import <AppKit/AppKit.h>
|
| 3 | +#import <AppKit/NSAccessibility.h> |
3 | 4 | #import <ApplicationServices/ApplicationServices.h>
|
4 | 5 | #import <CoreGraphics/CGWindow.h>
|
5 | 6 | #import <Foundation/Foundation.h>
|
|
27 | 28 | return nullptr;
|
28 | 29 | }
|
29 | 30 |
|
| 31 | +AXUIElementRef getWindowByHandle(const WindowHandle windowHandle, pid_t *outPid) { |
| 32 | + // Collect list of on-screen windows |
| 33 | + CGWindowListOption listOptions = |
| 34 | + kCGWindowListOptionOnScreenOnly | kCGWindowListExcludeDesktopElements; |
| 35 | + CFArrayRef windowList = |
| 36 | + CGWindowListCopyWindowInfo(listOptions, kCGNullWindowID); |
| 37 | + |
| 38 | + AXUIElementRef targetWindow = nullptr; |
| 39 | + // Look for matching window |
| 40 | + for (NSDictionary *info in (NSArray *) windowList) { |
| 41 | + NSNumber *windowNumber = info[(id) kCGWindowNumber]; |
| 42 | + if ([windowNumber intValue] == windowHandle) { |
| 43 | + *outPid = [info[(id) kCGWindowOwnerPID] intValue]; |
| 44 | + AXUIElementRef app = AXUIElementCreateApplication(*outPid); |
| 45 | + |
| 46 | + CFArrayRef windowArray; |
| 47 | + AXError error = AXUIElementCopyAttributeValue(app, kAXWindowsAttribute, (CFTypeRef *) &windowArray); |
| 48 | + if (error == kAXErrorSuccess) { |
| 49 | + CFIndex count = CFArrayGetCount(windowArray); |
| 50 | + for (CFIndex i = 0; i < count; i++) { |
| 51 | + auto window = static_cast<AXUIElementRef>(CFArrayGetValueAtIndex(windowArray, i)); |
| 52 | + // Your logic here to match the window with the handle |
| 53 | + // Set targetWindow = window if it matches |
| 54 | + } |
| 55 | + CFRelease(windowArray); |
| 56 | + } |
| 57 | + CFRelease(app); |
| 58 | + break; |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + if (windowList) { |
| 63 | + CFRelease(windowList); |
| 64 | + } |
| 65 | + |
| 66 | + return targetWindow; |
| 67 | +} |
| 68 | + |
30 | 69 | WindowHandle getActiveWindow() {
|
31 | 70 | CGWindowListOption listOptions =
|
32 | 71 | kCGWindowListOptionOnScreenOnly | kCGWindowListExcludeDesktopElements;
|
@@ -278,8 +317,9 @@ bool moveWindow(const WindowHandle windowHandle, const MMPoint newOrigin) {
|
278 | 317 | if (error == kAXErrorSuccess) {
|
279 | 318 |
|
280 | 319 | // Create AXValue objects for position and size
|
| 320 | + CGPoint point = CGPointMake(newOrigin.x, newOrigin.y); |
281 | 321 | AXValueRef positionValue = AXValueCreate((AXValueType) kAXValueCGPointType,
|
282 |
| - (const void *) &newOrigin); |
| 322 | + (const void *) &point); |
283 | 323 |
|
284 | 324 | // Set new position and size
|
285 | 325 | AXUIElementSetAttributeValue(window, kAXPositionAttribute, positionValue);
|
|
0 commit comments