Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support suspend/resume on macOS #24254

Merged
merged 1 commit into from
Jun 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/api/power-monitor.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ Process: [Main](../glossary.md#main-process)

The `powerMonitor` module emits the following events:

### Event: 'suspend' _Linux_ _Windows_
### Event: 'suspend'

Emitted when the system is suspending.

### Event: 'resume' _Linux_ _Windows_
### Event: 'resume'

Emitted when system is resuming.

Expand Down
31 changes: 29 additions & 2 deletions shell/browser/api/electron_api_power_monitor_mac.mm
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,21 @@ - (id)init {
selector:@selector(onScreenUnlocked:)
name:@"com.apple.screenIsUnlocked"
object:nil];

// A notification that the workspace posts before the machine goes to sleep.
[[[NSWorkspace sharedWorkspace] notificationCenter]
addObserver:self
selector:@selector(isSuspending:)
name:NSWorkspaceWillSleepNotification
object:nil];

// A notification that the workspace posts when the machine wakes from
// sleep.
[[[NSWorkspace sharedWorkspace] notificationCenter]
addObserver:self
selector:@selector(isResuming:)
name:NSWorkspaceDidWakeNotification
object:nil];
}
return self;
}
Expand All @@ -45,14 +60,26 @@ - (void)addEmitter:(electron::api::PowerMonitor*)monitor_ {
self->emitters.push_back(monitor_);
}

- (void)isSuspending:(NSNotification*)notify {
for (auto* emitter : self->emitters) {
emitter->Emit("suspend");
}
}

- (void)isResuming:(NSNotification*)notify {
for (auto* emitter : self->emitters) {
emitter->Emit("resume");
}
}

- (void)onScreenLocked:(NSNotification*)notification {
for (auto*& emitter : self->emitters) {
for (auto* emitter : self->emitters) {
emitter->Emit("lock-screen");
}
}

- (void)onScreenUnlocked:(NSNotification*)notification {
for (auto*& emitter : self->emitters) {
for (auto* emitter : self->emitters) {
emitter->Emit("unlock-screen");
}
}
Expand Down