Browse Source

feat: add macOS fast user switching evt (#25321)

Shelley Vohr 4 years ago
parent
commit
6acf34fa4c
2 changed files with 32 additions and 0 deletions
  1. 8 0
      docs/api/power-monitor.md
  2. 24 0
      shell/browser/api/electron_api_power_monitor_mac.mm

+ 8 - 0
docs/api/power-monitor.md

@@ -39,6 +39,14 @@ Emitted when the system is about to lock the screen.
 
 Emitted as soon as the systems screen is unlocked.
 
+### Event: 'user-did-become-active' _macOS_
+
+Emitted when a login session is activated. See [documentation](https://developer.apple.com/documentation/appkit/nsworkspacesessiondidbecomeactivenotification?language=objc) for more information.
+
+### Event: 'user-did-resign-active' _macOS_
+
+Emitted when a login session is deactivated. See [documentation](https://developer.apple.com/documentation/appkit/nsworkspacesessiondidresignactivenotification?language=objc) for more information.
+
 ## Methods
 
 The `powerMonitor` module has the following methods:

+ 24 - 0
shell/browser/api/electron_api_power_monitor_mac.mm

@@ -45,6 +45,18 @@
                    selector:@selector(isResuming:)
                        name:NSWorkspaceDidWakeNotification
                      object:nil];
+    // A notification that the workspace posts when the user session becomes
+    // active.
+    [distCenter addObserver:self
+                   selector:@selector(onUserDidBecomeActive:)
+                       name:NSWorkspaceSessionDidBecomeActiveNotification
+                     object:nil];
+    // A notification that the workspace posts when the user session becomes
+    // inactive.
+    [distCenter addObserver:self
+                   selector:@selector(onUserDidResignActive:)
+                       name:NSWorkspaceSessionDidResignActiveNotification
+                     object:nil];
   }
   return self;
 }
@@ -82,6 +94,18 @@
   }
 }
 
+- (void)onUserDidBecomeActive:(NSNotification*)notification {
+  for (auto* emitter : self->emitters) {
+    emitter->Emit("user-did-become-active");
+  }
+}
+
+- (void)onUserDidResignActive:(NSNotification*)notification {
+  for (auto* emitter : self->emitters) {
+    emitter->Emit("user-did-resign-active");
+  }
+}
+
 @end
 
 namespace electron {