Skip to content

Commit 2c47f23

Browse files
committed
2 parents d202f6a + 5009db4 commit 2c47f23

File tree

4 files changed

+23
-40
lines changed

4 files changed

+23
-40
lines changed

example.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,5 @@ console.timeEnd("setBounds");
2121
console.log("[info]: Visible Windows List");
2222
windowManager.getWindows().forEach(window => {
2323
console.log('Title: '+window.getTitle(), '\n', 'Path: '+window.path);
24+
window.bringToTop();
2425
});

lib/macos.mm

Lines changed: 10 additions & 26 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) {
@@ -182,10 +162,14 @@ AXUIElementRef getAXWindow(int pid, int handle) {
182162
Napi::Boolean bringWindowToTop(const Napi::CallbackInfo &info) {
183163
Napi::Env env{info.Env()};
184164

185-
auto pid = info[0].As<Napi::Number>().Int32Value();
165+
auto handle = info[0].As<Napi::Number>().Int32Value();
166+
auto pid = info[1].As<Napi::Number>().Int32Value();
167+
186168
auto app = AXUIElementCreateApplication(pid);
169+
auto win = m[handle];
187170

188171
AXUIElementSetAttributeValue(app, kAXFrontmostAttribute, kCFBooleanTrue);
172+
AXUIElementSetAttributeValue(win, kAXMainAttribute, kCFBooleanTrue);
189173

190174
return Napi::Boolean::New(env, true);
191175
}

src/classes/window.ts

Lines changed: 11 additions & 13 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 {
@@ -134,7 +128,11 @@ export class Window {
134128

135129
bringToTop() {
136130
if (!addon) return;
137-
addon.bringWindowToTop(platform() === "darwin" ? this.processId : this.id);
131+
if (process.platform === 'darwin') {
132+
addon.bringWindowToTop(this.id, this.processId);
133+
} else {
134+
addon.bringWindowToTop(this.id);
135+
}
138136
}
139137

140138
redraw() {

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)