|
4 | 4 | #include <napi.h> |
5 | 5 | #include <string> |
6 | 6 | #include <iostream> |
| 7 | +#include <map> |
7 | 8 |
|
8 | 9 | extern "C" AXError _AXUIElementGetWindow(AXUIElementRef, CGWindowID* out); |
9 | 10 |
|
10 | 11 | NSDictionary* opts = @{static_cast<id> (kAXTrustedCheckOptionPrompt): @YES}; |
11 | 12 | BOOL a = AXIsProcessTrustedWithOptions(static_cast<CFDictionaryRef> (opts)); |
12 | 13 |
|
| 14 | +std::map<int, AXUIElementRef> m; |
| 15 | + |
| 16 | +AXUIElementRef getAXWindow(int pid, int handle) { |
| 17 | + auto app = AXUIElementCreateApplication(pid); |
| 18 | + |
| 19 | + NSArray *windows; |
| 20 | + AXUIElementCopyAttributeValues(app, kAXWindowsAttribute, 0, 100, (CFArrayRef *) &windows); |
| 21 | + |
| 22 | + for (id child in windows) { |
| 23 | + auto window = (AXUIElementRef) child; |
| 24 | + |
| 25 | + CGWindowID windowId; |
| 26 | + _AXUIElementGetWindow(window, &windowId); |
| 27 | + |
| 28 | + if (windowId == handle) { |
| 29 | + return window; |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + return NULL; |
| 34 | +} |
| 35 | + |
13 | 36 | Napi::Array getWindows(const Napi::CallbackInfo &info) { |
14 | 37 | Napi::Env env{info.Env()}; |
15 | 38 |
|
|
28 | 51 | obj.Set("id", [windowNumber intValue]); |
29 | 52 | obj.Set("processId", [ownerPid intValue]); |
30 | 53 | obj.Set("path", app ? [app.bundleURL.path UTF8String] : ""); |
| 54 | + m[[windowNumber intValue]] = getAXWindow([ownerPid intValue], [windowNumber intValue]); |
31 | 55 |
|
32 | 56 | vec.push_back(obj); |
33 | 57 | } |
|
60 | 84 | obj.Set("id", [windowNumber intValue]); |
61 | 85 | obj.Set("processId", [ownerPid intValue]); |
62 | 86 | obj.Set("path", [app.bundleURL.path UTF8String]); |
| 87 | + m[[windowNumber intValue]] = getAXWindow([ownerPid intValue], [windowNumber intValue]); |
63 | 88 |
|
64 | 89 | return obj; |
65 | 90 | } |
|
107 | 132 | return Napi::Object::New(env); |
108 | 133 | } |
109 | 134 |
|
110 | | -AXUIElementRef getAXWindow(int pid, int handle) { |
111 | | - auto app = AXUIElementCreateApplication(pid); |
112 | | - |
113 | | - NSArray *windows; |
114 | | - AXUIElementCopyAttributeValues(app, kAXWindowsAttribute, 0, 100, (CFArrayRef *) &windows); |
115 | | - |
116 | | - for (id child in windows) { |
117 | | - auto window = (AXUIElementRef) child; |
118 | | - |
119 | | - CGWindowID windowId; |
120 | | - _AXUIElementGetWindow(window, &windowId); |
121 | | - |
122 | | - if (windowId == handle) { |
123 | | - return window; |
124 | | - } |
125 | | - } |
126 | | - |
127 | | - return NULL; |
128 | | -} |
129 | | - |
130 | 135 | Napi::Boolean setWindowBounds(const Napi::CallbackInfo &info) { |
131 | 136 | Napi::Env env{info.Env()}; |
132 | 137 |
|
133 | 138 | auto handle = info[0].As<Napi::Number>().Int32Value(); |
134 | | - auto pid = info[1].As<Napi::Number>().Int32Value(); |
135 | | - auto bounds = info[2].As<Napi::Object>(); |
| 139 | + auto bounds = info[1].As<Napi::Object>(); |
136 | 140 |
|
137 | 141 | auto x = bounds.Get("x").As<Napi::Number>().DoubleValue(); |
138 | 142 | auto y = bounds.Get("y").As<Napi::Number>().DoubleValue(); |
139 | 143 | auto width = bounds.Get("width").As<Napi::Number>().DoubleValue(); |
140 | 144 | auto height = bounds.Get("height").As<Napi::Number>().DoubleValue(); |
141 | 145 |
|
142 | | - auto win = getAXWindow(pid, handle); |
| 146 | + auto win = m[handle]; |
143 | 147 |
|
144 | 148 | if (win) { |
145 | 149 | NSPoint point = NSMakePoint((CGFloat) x, (CGFloat) y); |
@@ -170,10 +174,9 @@ AXUIElementRef getAXWindow(int pid, int handle) { |
170 | 174 | Napi::Env env{info.Env()}; |
171 | 175 |
|
172 | 176 | auto handle = info[0].As<Napi::Number>().Int32Value(); |
173 | | - auto pid = info[1].As<Napi::Number>().Int32Value(); |
174 | | - auto toggle = info[2].As<Napi::Boolean>(); |
| 177 | + auto toggle = info[1].As<Napi::Boolean>(); |
175 | 178 |
|
176 | | - auto win = getAXWindow(pid, handle); |
| 179 | + auto win = m[handle]; |
177 | 180 |
|
178 | 181 | if (win) { |
179 | 182 | AXUIElementSetAttributeValue(win, kAXMinimizedAttribute, toggle ? kCFBooleanTrue : kCFBooleanFalse); |
|
0 commit comments