Skip to content

Commit 0582091

Browse files
committed
feat: add refreshRate and isAsleep
1 parent c070036 commit 0582091

File tree

3 files changed

+32
-17
lines changed

3 files changed

+32
-17
lines changed

README.md

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ Display objects take the following format:
3333
* `width` Number - The width of the rectangle.
3434
* `height` Number - The height of the rectangle.
3535
* `internal` Boolean - `true` for an internal display and `false` for an external display.
36+
* `isAsleep` Boolean - Whether or not the display is sleeping.
37+
* `refreshRate` Number - Returns the refresh rate of the specified display.
3638

3739
### `displays.getAllDisplays()`
3840

@@ -46,13 +48,15 @@ console.log(allDisplays[0])
4648
/* Prints:
4749
[
4850
{
49-
id: 2077749241,
51+
id: 69734406,
5052
name: 'Built-in Retina Display',
51-
isMonochrome: false,
53+
refreshRate: 0,
5254
supportedWindowDepths: [
5355
264, 516, 520, 528,
5456
544, 0, 0, 0
5557
],
58+
isAsleep: false,
59+
isMonochrome: false,
5660
colorSpace: { name: 'Color LCD', componentCount: 3 },
5761
depth: 520,
5862
scaleFactor: 2,
@@ -76,13 +80,15 @@ const primary = displays.getPrimaryDisplay()
7680
console.log(primary)
7781
/* Prints:
7882
{
79-
id: 2077749241,
83+
id: 69734406,
8084
name: 'Built-in Retina Display',
81-
isMonochrome: false,
85+
refreshRate: 0,
8286
supportedWindowDepths: [
8387
264, 516, 520, 528,
8488
544, 0, 0, 0
8589
],
90+
isAsleep: false,
91+
isMonochrome: false,
8692
colorSpace: { name: 'Color LCD', componentCount: 3 },
8793
depth: 520,
8894
scaleFactor: 2,
@@ -107,13 +113,15 @@ const display = displays.getDisplayByID(2077749241)
107113
console.log(display)
108114
/* Prints:
109115
{
110-
id: 2077749241,
116+
id: 69734406,
111117
name: 'Built-in Retina Display',
112-
isMonochrome: false,
118+
refreshRate: 0,
113119
supportedWindowDepths: [
114120
264, 516, 520, 528,
115121
544, 0, 0, 0
116122
],
123+
isAsleep: false,
124+
isMonochrome: false,
117125
colorSpace: { name: 'Color LCD', componentCount: 3 },
118126
depth: 520,
119127
scaleFactor: 2,

displays.mm

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,16 @@
4141
return obj;
4242
}
4343

44-
// Creates an object containing all properties of an NSScreen.
44+
// Returns whether or not the display is monochrome.
45+
bool GetIsMonochrome() {
46+
CFStringRef app = CFSTR("com.apple.CoreGraphics");
47+
CFStringRef key = CFSTR("DisplayUseForcedGray");
48+
Boolean key_valid = false;
49+
50+
return CFPreferencesGetAppBooleanValue(key, app, &key_valid) ? true : false;
51+
}
52+
53+
// Creates an object containing all properties of an display.
4554
Napi::Object BuildDisplay(Napi::Env env, NSScreen *nsscreen) {
4655
Napi::Object display = Napi::Object::New(env);
4756

@@ -53,17 +62,13 @@
5362
display.Set("name", std::string([[nsscreen localizedName] UTF8String]));
5463
}
5564

56-
CFStringRef app = CFSTR("com.apple.CoreGraphics");
57-
CFStringRef key = CFSTR("DisplayUseForcedGray");
58-
Boolean key_valid = false;
59-
bool monochrome =
60-
CFPreferencesGetAppBooleanValue(key, app, &key_valid) ? true : false;
61-
display.Set("isMonochrome", monochrome);
62-
63-
Napi::Array window_depths =
64-
CArrayToNapiArray(env, [nsscreen supportedWindowDepths]);
65-
display.Set("supportedWindowDepths", window_depths);
65+
CGDisplayModeRef display_mode = CGDisplayCopyDisplayMode(display_id);
66+
display.Set("refreshRate", CGDisplayModeGetRefreshRate(display_mode));
6667

68+
display.Set("supportedWindowDepths",
69+
CArrayToNapiArray(env, [nsscreen supportedWindowDepths]));
70+
display.Set("isAsleep", CGDisplayIsAsleep(display_id) ? true : false);
71+
display.Set("isMonochrome", GetIsMonochrome());
6772
display.Set("colorSpace", NSColorSpaceToObject(env, [nsscreen colorSpace]));
6873
display.Set("depth", static_cast<int>([nsscreen depth]));
6974
display.Set("scaleFactor", [nsscreen backingScaleFactor]);

test/module.spec.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ describe('node-system-displays', () => {
1717
expect(display).to.have.property('isMonochrome').that.is.a('boolean')
1818
expect(display).to.have.property('depth').that.is.a('number')
1919
expect(display).to.have.property('internal').that.is.a('boolean')
20+
expect(display).to.have.property('isAsleep').that.is.a('boolean')
21+
expect(display).to.have.property('refreshRate').that.is.a('number')
2022

2123
expect(display).to.have.property('colorSpace').that.is.an('object')
2224
const { name, componentCount } = display.colorSpace

0 commit comments

Comments
 (0)