From f6ce6cafed2869e720b5854f9806a7a7d1555d13 Mon Sep 17 00:00:00 2001 From: "trop[bot]" <37223003+trop[bot]@users.noreply.github.com> Date: Mon, 29 Jun 2020 09:05:35 -0700 Subject: [PATCH] feat: support suspend/resume on macOS (#24294) Co-authored-by: Shelley Vohr --- .../api/electron_api_power_monitor_mac.mm | 31 +++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/shell/browser/api/electron_api_power_monitor_mac.mm b/shell/browser/api/electron_api_power_monitor_mac.mm index 7bad49285642b..f3830792d43bd 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 @@ - (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; } @@ -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"); } }