|
1 | | -import { Window } from "./classes/window"; |
2 | | -import { EventEmitter } from "events"; |
3 | | -import { platform } from "os"; |
4 | | -import { Monitor } from "./classes/monitor"; |
5 | | -import { EmptyMonitor } from "./classes/empty-monitor"; |
6 | | - |
7 | | -let addon: any; |
8 | | - |
9 | | -if (platform() === "win32" || platform() === "darwin") { |
10 | | - const ADDON_PATH = (process.env.NODE_ENV != "dev") ? "Release" : "Debug"; |
11 | | - addon = require(`../build/${ADDON_PATH}/addon.node`); |
12 | | -} |
13 | | - |
14 | | -let interval: any = null; |
15 | | - |
16 | | -let registeredEvents: string[] = []; |
17 | | - |
18 | | -class WindowManager extends EventEmitter { |
19 | | - constructor() { |
20 | | - super(); |
21 | | - |
22 | | - let lastId: number; |
23 | | - |
24 | | - if (!addon) return; |
25 | | - |
26 | | - this.on("newListener", event => { |
27 | | - if (registeredEvents.indexOf(event) !== -1) return; |
28 | | - |
29 | | - if (event === "window-activated") { |
30 | | - interval = setInterval(async () => { |
31 | | - const win = addon.getActiveWindow(); |
32 | | - |
33 | | - if (lastId !== win) { |
34 | | - lastId = win; |
35 | | - this.emit("window-activated", new Window(win)); |
36 | | - } |
37 | | - }, 50); |
38 | | - } else { |
39 | | - return; |
40 | | - } |
41 | | - |
42 | | - registeredEvents.push(event); |
43 | | - }); |
44 | | - |
45 | | - this.on("removeListener", event => { |
46 | | - if (this.listenerCount(event) > 0) return; |
47 | | - |
48 | | - if (event === "window-activated") { |
49 | | - clearInterval(interval); |
50 | | - } |
51 | | - |
52 | | - registeredEvents = registeredEvents.filter(x => x !== event); |
53 | | - }); |
54 | | - } |
55 | | - |
56 | | - requestAccessibility = () => { |
57 | | - if (!addon || !addon.requestAccessibility) return true; |
58 | | - return addon.requestAccessibility(); |
59 | | - } |
60 | | - |
61 | | - getActiveWindow = () => { |
62 | | - if (!addon) return; |
63 | | - return new Window(addon.getActiveWindow()); |
64 | | - }; |
65 | | - |
66 | | - getWindows = (): Window[] => { |
67 | | - if (!addon || !addon.getWindows) return []; |
68 | | - return addon.getWindows().map((win: any) => new Window(win)).filter((x: Window) => x.isWindow()); |
69 | | - }; |
70 | | - |
71 | | - getMonitors = (): Monitor[] => { |
72 | | - if (!addon || !addon.getMonitors) return []; |
73 | | - return addon.getMonitors().map((mon: any) => new Monitor(mon)); |
74 | | - }; |
75 | | - |
76 | | - getPrimaryMonitor = (): Monitor | EmptyMonitor => { |
77 | | - if (process.platform === 'win32') { |
78 | | - return this.getMonitors().find(x => x.isPrimary); |
79 | | - } else { |
80 | | - return new EmptyMonitor(); |
81 | | - } |
82 | | - } |
83 | | -} |
84 | | - |
85 | | -const windowManager = new WindowManager(); |
86 | | - |
87 | | -export { windowManager, Window, addon }; |
| 1 | +import { Window } from "./classes/window"; |
| 2 | +import { EventEmitter } from "events"; |
| 3 | +import { platform } from "os"; |
| 4 | +import { Monitor } from "./classes/monitor"; |
| 5 | +import { EmptyMonitor } from "./classes/empty-monitor"; |
| 6 | +import { resolve } from 'path'; |
| 7 | + |
| 8 | +let addon: any; |
| 9 | + |
| 10 | +if (platform() === "win32" || platform() === "darwin") { |
| 11 | + const ADDON_PATH = (process.env.NODE_ENV != "dev") ? "Release" : "Debug"; |
| 12 | + addon = require(`node-gyp-build`)(resolve(__dirname, '..')); |
| 13 | +} |
| 14 | + |
| 15 | +let interval: any = null; |
| 16 | + |
| 17 | +let registeredEvents: string[] = []; |
| 18 | + |
| 19 | +class WindowManager extends EventEmitter { |
| 20 | + constructor() { |
| 21 | + super(); |
| 22 | + |
| 23 | + let lastId: number; |
| 24 | + |
| 25 | + if (!addon) return; |
| 26 | + |
| 27 | + this.on("newListener", event => { |
| 28 | + if (registeredEvents.indexOf(event) !== -1) return; |
| 29 | + |
| 30 | + if (event === "window-activated") { |
| 31 | + interval = setInterval(async () => { |
| 32 | + const win = addon.getActiveWindow(); |
| 33 | + |
| 34 | + if (lastId !== win) { |
| 35 | + lastId = win; |
| 36 | + this.emit("window-activated", new Window(win)); |
| 37 | + } |
| 38 | + }, 50); |
| 39 | + } else { |
| 40 | + return; |
| 41 | + } |
| 42 | + |
| 43 | + registeredEvents.push(event); |
| 44 | + }); |
| 45 | + |
| 46 | + this.on("removeListener", event => { |
| 47 | + if (this.listenerCount(event) > 0) return; |
| 48 | + |
| 49 | + if (event === "window-activated") { |
| 50 | + clearInterval(interval); |
| 51 | + } |
| 52 | + |
| 53 | + registeredEvents = registeredEvents.filter(x => x !== event); |
| 54 | + }); |
| 55 | + } |
| 56 | + |
| 57 | + requestAccessibility = () => { |
| 58 | + if (!addon || !addon.requestAccessibility) return true; |
| 59 | + return addon.requestAccessibility(); |
| 60 | + } |
| 61 | + |
| 62 | + getActiveWindow = () => { |
| 63 | + if (!addon) return; |
| 64 | + return new Window(addon.getActiveWindow()); |
| 65 | + }; |
| 66 | + |
| 67 | + getWindows = (): Window[] => { |
| 68 | + if (!addon || !addon.getWindows) return []; |
| 69 | + return addon.getWindows().map((win: any) => new Window(win)).filter((x: Window) => x.isWindow()); |
| 70 | + }; |
| 71 | + |
| 72 | + getMonitors = (): Monitor[] => { |
| 73 | + if (!addon || !addon.getMonitors) return []; |
| 74 | + return addon.getMonitors().map((mon: any) => new Monitor(mon)); |
| 75 | + }; |
| 76 | + |
| 77 | + getPrimaryMonitor = (): Monitor | EmptyMonitor => { |
| 78 | + if (process.platform === 'win32') { |
| 79 | + return this.getMonitors().find(x => x.isPrimary); |
| 80 | + } else { |
| 81 | + return new EmptyMonitor(); |
| 82 | + } |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +const windowManager = new WindowManager(); |
| 87 | + |
| 88 | +export { windowManager, Window, addon }; |
0 commit comments