Skip to content

Commit e0db15f

Browse files
committed
merge
2 parents 6dc5f11 + 7fcc244 commit e0db15f

File tree

5 files changed

+43
-162
lines changed

5 files changed

+43
-162
lines changed

example.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,14 @@ console.time("getActiveWindow");
44
const window = windowManager.getActiveWindow();
55
console.timeEnd("getActiveWindow");
66

7-
console.log(window.path);
7+
console.time("getTitle");
8+
console.log(window.getTitle());
9+
console.timeEnd("getTitle");
10+
11+
console.time("getBounds");
12+
console.log(window.getBounds());
13+
console.timeEnd("getBounds");
14+
15+
console.time("setBounds");
16+
window.setBounds({ x: 0, y: 0 });
17+
console.timeEnd("setBounds");

lib/macos.mm

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,35 @@
44
#include <napi.h>
55
#include <string>
66
#include <iostream>
7+
#include <map>
78

89
extern "C" AXError _AXUIElementGetWindow(AXUIElementRef, CGWindowID* out);
910

1011
NSDictionary* opts = @{static_cast<id> (kAXTrustedCheckOptionPrompt): @YES};
1112
BOOL a = AXIsProcessTrustedWithOptions(static_cast<CFDictionaryRef> (opts));
1213

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+
1336
Napi::Array getWindows(const Napi::CallbackInfo &info) {
1437
Napi::Env env{info.Env()};
1538

@@ -28,6 +51,7 @@
2851
obj.Set("id", [windowNumber intValue]);
2952
obj.Set("processId", [ownerPid intValue]);
3053
obj.Set("path", app ? [app.bundleURL.path UTF8String] : "");
54+
m[[windowNumber intValue]] = getAXWindow([ownerPid intValue], [windowNumber intValue]);
3155

3256
vec.push_back(obj);
3357
}
@@ -60,6 +84,7 @@
6084
obj.Set("id", [windowNumber intValue]);
6185
obj.Set("processId", [ownerPid intValue]);
6286
obj.Set("path", [app.bundleURL.path UTF8String]);
87+
m[[windowNumber intValue]] = getAXWindow([ownerPid intValue], [windowNumber intValue]);
6388

6489
return obj;
6590
}
@@ -107,39 +132,18 @@
107132
return Napi::Object::New(env);
108133
}
109134

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-
130135
Napi::Boolean setWindowBounds(const Napi::CallbackInfo &info) {
131136
Napi::Env env{info.Env()};
132137

133138
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>();
136140

137141
auto x = bounds.Get("x").As<Napi::Number>().DoubleValue();
138142
auto y = bounds.Get("y").As<Napi::Number>().DoubleValue();
139143
auto width = bounds.Get("width").As<Napi::Number>().DoubleValue();
140144
auto height = bounds.Get("height").As<Napi::Number>().DoubleValue();
141145

142-
auto win = getAXWindow(pid, handle);
146+
auto win = m[handle];
143147

144148
if (win) {
145149
NSPoint point = NSMakePoint((CGFloat) x, (CGFloat) y);
@@ -170,10 +174,9 @@ AXUIElementRef getAXWindow(int pid, int handle) {
170174
Napi::Env env{info.Env()};
171175

172176
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>();
175178

176-
auto win = getAXWindow(pid, handle);
179+
auto win = m[handle];
177180

178181
if (win) {
179182
AXUIElementSetAttributeValue(win, kAXMinimizedAttribute, toggle ? kCFBooleanTrue : kCFBooleanFalse);

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "node-window-manager",
3-
"version": "1.0.1",
3+
"version": "1.0.2",
44
"description": "Manage windows in macOS, Windows and Linux",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

src/classes/window.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ interface WindowInfo {
2222
bounds?: Rectangle;
2323
opacity?: number;
2424
owner?: Window;
25+
axRef?: number;
2526
}
2627

2728
export class Window {
@@ -77,7 +78,7 @@ export class Window {
7778

7879
addon.setWindowBounds(this.id, newBounds);
7980
} else if (platform() === "darwin") {
80-
addon.setWindowBounds(this.id, this.processId, newBounds);
81+
addon.setWindowBounds(this.id, newBounds);
8182
}
8283
}
8384

src/constants/index.ts

Lines changed: 0 additions & 133 deletions
This file was deleted.

0 commit comments

Comments
 (0)