Skip to content

Commit d8de43f

Browse files
feat: support suspend/resume on macOS (electron#24293)
Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>
1 parent c48a6b4 commit d8de43f

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

shell/browser/api/electron_api_power_monitor_mac.mm

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,21 @@ - (id)init {
3232
selector:@selector(onScreenUnlocked:)
3333
name:@"com.apple.screenIsUnlocked"
3434
object:nil];
35+
36+
// A notification that the workspace posts before the machine goes to sleep.
37+
[[[NSWorkspace sharedWorkspace] notificationCenter]
38+
addObserver:self
39+
selector:@selector(isSuspending:)
40+
name:NSWorkspaceWillSleepNotification
41+
object:nil];
42+
43+
// A notification that the workspace posts when the machine wakes from
44+
// sleep.
45+
[[[NSWorkspace sharedWorkspace] notificationCenter]
46+
addObserver:self
47+
selector:@selector(isResuming:)
48+
name:NSWorkspaceDidWakeNotification
49+
object:nil];
3550
}
3651
return self;
3752
}
@@ -45,14 +60,26 @@ - (void)addEmitter:(electron::api::PowerMonitor*)monitor_ {
4560
self->emitters.push_back(monitor_);
4661
}
4762

63+
- (void)isSuspending:(NSNotification*)notify {
64+
for (auto* emitter : self->emitters) {
65+
emitter->Emit("suspend");
66+
}
67+
}
68+
69+
- (void)isResuming:(NSNotification*)notify {
70+
for (auto* emitter : self->emitters) {
71+
emitter->Emit("resume");
72+
}
73+
}
74+
4875
- (void)onScreenLocked:(NSNotification*)notification {
49-
for (auto*& emitter : self->emitters) {
76+
for (auto* emitter : self->emitters) {
5077
emitter->Emit("lock-screen");
5178
}
5279
}
5380

5481
- (void)onScreenUnlocked:(NSNotification*)notification {
55-
for (auto*& emitter : self->emitters) {
82+
for (auto* emitter : self->emitters) {
5683
emitter->Emit("unlock-screen");
5784
}
5885
}

0 commit comments

Comments
 (0)