Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
sentialx committed Dec 11, 2019
2 parents d202f6a + 5009db4 commit 2c47f23
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 40 deletions.
1 change: 1 addition & 0 deletions example.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ console.timeEnd("setBounds");
console.log("[info]: Visible Windows List");
windowManager.getWindows().forEach(window => {
console.log('Title: '+window.getTitle(), '\n', 'Path: '+window.path);
window.bringToTop();
});
36 changes: 10 additions & 26 deletions lib/macos.mm
Original file line number Diff line number Diff line change
Expand Up @@ -45,27 +45,17 @@ AXUIElementRef getAXWindow(int pid, int handle) {
CGWindowListOption listOptions = kCGWindowListOptionOnScreenOnly | kCGWindowListExcludeDesktopElements;
CFArrayRef windowList = CGWindowListCopyWindowInfo(listOptions, kCGNullWindowID);

std::vector<Napi::Object> vec;
std::vector<Napi::Number> vec;

for (NSDictionary *info in (NSArray *)windowList) {
auto obj = Napi::Object::New(env);

NSNumber *ownerPid = info[(id)kCGWindowOwnerPID];
NSNumber *windowNumber = info[(id)kCGWindowNumber];
auto app = [NSRunningApplication runningApplicationWithProcessIdentifier: [ownerPid intValue]];

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

obj.Set("id", [windowNumber intValue]);
obj.Set("processId", [ownerPid intValue]);
obj.Set("path", path);

if (m.find([windowNumber intValue]) == m.end()) {
m[[windowNumber intValue]] = getAXWindow([ownerPid intValue], [windowNumber intValue]);
}

if (path != "") {
vec.push_back(obj);
vec.push_back(Napi::Number::New(env, [windowNumber intValue]));
}
}

Expand All @@ -78,7 +68,7 @@ AXUIElementRef getAXWindow(int pid, int handle) {
return arr;
}

Napi::Object getActiveWindow(const Napi::CallbackInfo &info) {
Napi::Number getActiveWindow(const Napi::CallbackInfo &info) {
Napi::Env env{info.Env()};

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

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

auto obj = Napi::Object::New(env);

obj.Set("id", [windowNumber intValue]);
obj.Set("processId", [ownerPid intValue]);
obj.Set("path", [app.bundleURL.path UTF8String]);

if (m.find([windowNumber intValue]) == m.end()) {
m[[windowNumber intValue]] = getAXWindow([ownerPid intValue], [windowNumber intValue]);
}

return obj;
return Napi::Number::New(env, [windowNumber intValue]);
}

return Napi::Object::New(env);
return Napi::Number::New(env, 0);
}

Napi::Object getWindowInfo(const Napi::CallbackInfo &info) {
Expand Down Expand Up @@ -182,10 +162,14 @@ AXUIElementRef getAXWindow(int pid, int handle) {
Napi::Boolean bringWindowToTop(const Napi::CallbackInfo &info) {
Napi::Env env{info.Env()};

auto pid = info[0].As<Napi::Number>().Int32Value();
auto handle = info[0].As<Napi::Number>().Int32Value();
auto pid = info[1].As<Napi::Number>().Int32Value();

auto app = AXUIElementCreateApplication(pid);
auto win = m[handle];

AXUIElementSetAttributeValue(app, kAXFrontmostAttribute, kCFBooleanTrue);
AXUIElementSetAttributeValue(win, kAXMainAttribute, kCFBooleanTrue);

return Napi::Boolean::New(env, true);
}
Expand Down
24 changes: 11 additions & 13 deletions src/classes/window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ interface WindowInfo {
title?: string;
bounds?: Rectangle;
opacity?: number;
owner?: Window;
owner?: number;
}

export class Window {
Expand All @@ -31,19 +31,13 @@ export class Window {
public processId: number;
public path: string;

constructor(arg: number | WindowInfo) {
constructor(id: number) {
if (!addon) return;

if (typeof arg === "object") {
this.id = arg.id;
this.processId = arg.processId;
this.path = arg.path;
} else {
this.id = arg;
const { processId, path } = this.getInfo();
this.processId = processId;
this.path = path;
}
this.id = id;
const { processId, path } = this.getInfo();
this.processId = processId;
this.path = path;
}

getBounds(): Rectangle {
Expand Down Expand Up @@ -134,7 +128,11 @@ export class Window {

bringToTop() {
if (!addon) return;
addon.bringWindowToTop(platform() === "darwin" ? this.processId : this.id);
if (process.platform === 'darwin') {
addon.bringWindowToTop(this.id, this.processId);
} else {
addon.bringWindowToTop(this.id);
}
}

redraw() {
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class WindowManager extends EventEmitter {
}

requestAccessibility = () => {
if (platform() !== 'darwin') return false;
if (platform() !== 'darwin') return true;
return addon.requestAccessibility();
}

Expand Down

0 comments on commit 2c47f23

Please sign in to comment.