Skip to content

Commit 22c65ea

Browse files
committed
feat: EmptyMonitor object on non-Windows systems for better cross-platform compatibility
1 parent 799ba13 commit 22c65ea

File tree

7 files changed

+61
-4
lines changed

7 files changed

+61
-4
lines changed

docs/monitor.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
Control monitors.
44

5+
> NOTE: Monitors are supported only on `Windows`, but on `macOS` there's a stub object
6+
called `EmptyMonitor` for better cross-platform compatibility without checking whether
7+
a returned monitor is `undefined`.
8+
59
```typescript
610
import { windowManager } from 'node-window-manager';
711

@@ -21,22 +25,36 @@ const { height } = windowManager.getPrimaryWindow().getWorkArea();
2125

2226
#### monitor.getBounds() `Windows`
2327

28+
> NOTE: on macOS this method returns `{x: 0, y: 0, width: 0, height: 0}` for compatibility.
29+
2430
- Returns [`Rectangle`](rectangle.md)
2531

2632
#### monitor.getWorkArea() `Windows`
2733

34+
> NOTE: on macOS this method returns `{x: 0, y: 0, width: 0, height: 0}` for compatibility.
35+
2836
Gets monitor working area bounds.
2937

3038
- Returns [`Rectangle`](rectangle.md)
3139

3240
#### monitor.isPrimary() `Windows`
3341

42+
> NOTE: on macOS this method returns `false` for compatibility.
43+
3444
Whether the monitor is primary.
3545

3646
- Returns `boolean`
3747

3848
#### monitor.getScaleFactor() `Windows`
3949

50+
> NOTE: on macOS this method returns `1` for compatibility.
51+
4052
Gets monitor scale factor (DPI).
4153

4254
- Returns `number`
55+
56+
#### monitor.isValid() `Windows` `macOS`
57+
58+
Returns:
59+
- On `Windows`: `true`
60+
- On `macOS`: `false`, since it's just an `EmptyMonitor` object.

docs/window-manager.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,14 @@ The method is required to call before calling the following methods:
4040

4141
#### windowManager.getMonitors() `Windows`
4242

43+
> NOTE: on macOS this method returns `[]` for compatibility.
44+
4345
- Returns [`Monitor[]`](monitor.md)
4446

4547
#### windowManager.getPrimaryMonitor() `Windows`
4648

49+
> NOTE: on macOS this method returns an `EmptyMonitor` object for compatibility.
50+
4751
- Returns [`Monitor`](monitor.md)
4852

4953
### Events

docs/window.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ Returns `number` between 0 and 1.
7070

7171
#### win.getMonitor() `Windows`
7272

73+
> NOTE: on macOS this method returns an `EmptyMonitor` object for compatibility.
74+
7375
Gets monitor which the window belongs to.
7476

7577
Returns [`Monitor`](monitor.md)

src/classes/empty-monitor.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { IRectangle } from "../interfaces";
2+
3+
export class EmptyMonitor {
4+
getBounds(): IRectangle {
5+
return { x: 0, y: 0, width: 0, height: 0 };
6+
}
7+
8+
getWorkArea(): IRectangle {
9+
return { x: 0, y: 0, width: 0, height: 0 };
10+
}
11+
12+
isPrimary(): boolean {
13+
return false;
14+
}
15+
16+
getScaleFactor(): number {
17+
return 1;
18+
};
19+
20+
isValid(): boolean {
21+
return false;
22+
}
23+
}

src/classes/monitor.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,8 @@ export class Monitor {
3939

4040
return 1;
4141
};
42+
43+
isValid(): boolean {
44+
return addon && addon.getMonitorInfo;
45+
}
4246
}

src/classes/window.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { addon } from "..";
22
import extractFileIcon from 'extract-file-icon';
33
import { Monitor } from "./monitor";
44
import { IRectangle } from "../interfaces";
5+
import { EmptyMonitor } from "./empty-monitor";
56

67
export class Window {
78
public id: number;
@@ -59,8 +60,8 @@ export class Window {
5960
return addon.getWindowTitle(this.id);
6061
}
6162

62-
getMonitor(): Monitor {
63-
if (!addon || !addon.getMonitorFromWindow) return;
63+
getMonitor(): Monitor | EmptyMonitor {
64+
if (!addon || !addon.getMonitorFromWindow) return new EmptyMonitor();
6465
return new Monitor(addon.getMonitorFromWindow(this.id));
6566
}
6667

src/index.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Window } from "./classes/window";
22
import { EventEmitter } from "events";
33
import { platform } from "os";
44
import { Monitor } from "./classes/monitor";
5+
import { EmptyMonitor } from "./classes/empty-monitor";
56

67
let addon: any;
78

@@ -72,8 +73,12 @@ class WindowManager extends EventEmitter {
7273
return addon.getMonitors().map((mon: any) => new Monitor(mon));
7374
};
7475

75-
getPrimaryMonitor = (): Monitor => {
76-
return this.getMonitors().find(x => x.isPrimary);
76+
getPrimaryMonitor = (): Monitor | EmptyMonitor => {
77+
if (process.platform === 'win32') {
78+
return this.getMonitors().find(x => x.isPrimary);
79+
} else {
80+
return new EmptyMonitor();
81+
}
7782
}
7883
}
7984

0 commit comments

Comments
 (0)