Skip to content

Commit 57821c3

Browse files
committed
feat: safer windows initialization
1 parent 0e3a73c commit 57821c3

File tree

3 files changed

+12
-38
lines changed

3 files changed

+12
-38
lines changed

lib/macos.mm

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -45,27 +45,17 @@ AXUIElementRef getAXWindow(int pid, int handle) {
4545
CGWindowListOption listOptions = kCGWindowListOptionOnScreenOnly | kCGWindowListExcludeDesktopElements;
4646
CFArrayRef windowList = CGWindowListCopyWindowInfo(listOptions, kCGNullWindowID);
4747

48-
std::vector<Napi::Object> vec;
48+
std::vector<Napi::Number> vec;
4949

5050
for (NSDictionary *info in (NSArray *)windowList) {
51-
auto obj = Napi::Object::New(env);
52-
5351
NSNumber *ownerPid = info[(id)kCGWindowOwnerPID];
5452
NSNumber *windowNumber = info[(id)kCGWindowNumber];
5553
auto app = [NSRunningApplication runningApplicationWithProcessIdentifier: [ownerPid intValue]];
5654

5755
auto path = app ? [app.bundleURL.path UTF8String] : "";
5856

59-
obj.Set("id", [windowNumber intValue]);
60-
obj.Set("processId", [ownerPid intValue]);
61-
obj.Set("path", path);
62-
63-
if (m.find([windowNumber intValue]) == m.end()) {
64-
m[[windowNumber intValue]] = getAXWindow([ownerPid intValue], [windowNumber intValue]);
65-
}
66-
6757
if (path != "") {
68-
vec.push_back(obj);
58+
vec.push_back(Napi::Number::New(env, [windowNumber intValue]));
6959
}
7060
}
7161

@@ -78,7 +68,7 @@ AXUIElementRef getAXWindow(int pid, int handle) {
7868
return arr;
7969
}
8070

81-
Napi::Object getActiveWindow(const Napi::CallbackInfo &info) {
71+
Napi::Number getActiveWindow(const Napi::CallbackInfo &info) {
8272
Napi::Env env{info.Env()};
8373

8474
CGWindowListOption listOptions = kCGWindowListOptionOnScreenOnly | kCGWindowListExcludeDesktopElements;
@@ -92,20 +82,10 @@ AXUIElementRef getAXWindow(int pid, int handle) {
9282

9383
if ([ownerPid intValue] != app.processIdentifier) continue;
9484

95-
auto obj = Napi::Object::New(env);
96-
97-
obj.Set("id", [windowNumber intValue]);
98-
obj.Set("processId", [ownerPid intValue]);
99-
obj.Set("path", [app.bundleURL.path UTF8String]);
100-
101-
if (m.find([windowNumber intValue]) == m.end()) {
102-
m[[windowNumber intValue]] = getAXWindow([ownerPid intValue], [windowNumber intValue]);
103-
}
104-
105-
return obj;
85+
return Napi::Number::New(env, [windowNumber intValue]);
10686
}
10787

108-
return Napi::Object::New(env);
88+
return Napi::Number::New(env, 0);
10989
}
11090

11191
Napi::Object getWindowInfo(const Napi::CallbackInfo &info) {

src/classes/window.ts

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ interface WindowInfo {
2222
title?: string;
2323
bounds?: Rectangle;
2424
opacity?: number;
25-
owner?: Window;
25+
owner?: number;
2626
}
2727

2828
export class Window {
@@ -31,19 +31,13 @@ export class Window {
3131
public processId: number;
3232
public path: string;
3333

34-
constructor(arg: number | WindowInfo) {
34+
constructor(id: number) {
3535
if (!addon) return;
3636

37-
if (typeof arg === "object") {
38-
this.id = arg.id;
39-
this.processId = arg.processId;
40-
this.path = arg.path;
41-
} else {
42-
this.id = arg;
43-
const { processId, path } = this.getInfo();
44-
this.processId = processId;
45-
this.path = path;
46-
}
37+
this.id = id;
38+
const { processId, path } = this.getInfo();
39+
this.processId = processId;
40+
this.path = path;
4741
}
4842

4943
getBounds(): Rectangle {

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class WindowManager extends EventEmitter {
5252
}
5353

5454
requestAccessibility = () => {
55-
if (platform() !== 'darwin') return false;
55+
if (platform() !== 'darwin') return true;
5656
return addon.requestAccessibility();
5757
}
5858

0 commit comments

Comments
 (0)