From 4290555a0d9dacc4ba46650cd2b1d5baca0e2467 Mon Sep 17 00:00:00 2001 From: Shelley Vohr Date: Wed, 24 Jun 2020 10:17:07 -0700 Subject: [PATCH] feat: support suspend/resume on macOS (#24254) --- docs/api/power-monitor.md | 4 +-- .../api/electron_api_power_monitor_mac.mm | 31 +++++++++++++++++-- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/docs/api/power-monitor.md b/docs/api/power-monitor.md index 1e94f1d25df..72c04d02ddb 100644 --- a/docs/api/power-monitor.md +++ b/docs/api/power-monitor.md @@ -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. diff --git a/shell/browser/api/electron_api_power_monitor_mac.mm b/shell/browser/api/electron_api_power_monitor_mac.mm index 7bad4928564..f3830792d43 100644 --- a/shell/browser/api/electron_api_power_monitor_mac.mm +++ b/shell/browser/api/electron_api_power_monitor_mac.mm @@ -32,6 +32,21 @@ 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; } @@ -45,14 +60,26 @@ 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"); } }