From 88b42a3b3ac2b1e5dac99fe2ddf6e5fcfed4ff84 Mon Sep 17 00:00:00 2001 From: "trop[bot]" <37223003+trop[bot]@users.noreply.github.com> Date: Tue, 18 Apr 2023 14:58:32 +0000 Subject: [PATCH] feat: add did-resign-active event on app Co-authored-by: Shelley Vohr --- docs/api/app.md | 15 +++++++++++++-- shell/browser/api/electron_api_app.cc | 4 ++++ shell/browser/api/electron_api_app.h | 1 + shell/browser/browser.cc | 5 +++++ shell/browser/browser.h | 5 ++++- shell/browser/browser_observer.h | 4 +++- .../browser/mac/electron_application_delegate.mm | 4 ++++ 7 files changed, 34 insertions(+), 4 deletions(-) diff --git a/docs/api/app.md b/docs/api/app.md index 2150ad9955477..ccd0b84498615 100755 --- a/docs/api/app.md +++ b/docs/api/app.md @@ -150,9 +150,20 @@ Returns: * `event` Event -Emitted when mac application become active. Difference from `activate` event is +Emitted when the application becomes active. This differs from the `activate` event in that `did-become-active` is emitted every time the app becomes active, not only -when Dock icon is clicked or application is re-launched. +when Dock icon is clicked or application is re-launched. It is also emitted when a user +switches to the app via the macOS App Switcher. + +### Event: 'did-resign-active' _macOS_ + +Returns: + +* `event` Event + +Emitted when the app is no longer active and doesn’t have focus. This can be triggered, +for example, by clicking on another application or by using the macOS App Switcher to +switch to another application. ### Event: 'continue-activity' _macOS_ diff --git a/shell/browser/api/electron_api_app.cc b/shell/browser/api/electron_api_app.cc index e2f889361abd2..f1c1396e9311d 100644 --- a/shell/browser/api/electron_api_app.cc +++ b/shell/browser/api/electron_api_app.cc @@ -787,6 +787,10 @@ void App::OnNewWindowForTab() { void App::OnDidBecomeActive() { Emit("did-become-active"); } + +void App::OnDidResignActive() { + Emit("did-resign-active"); +} #endif bool App::CanCreateWindow( diff --git a/shell/browser/api/electron_api_app.h b/shell/browser/api/electron_api_app.h index afc29e6a08a4c..8bacd432180f0 100644 --- a/shell/browser/api/electron_api_app.h +++ b/shell/browser/api/electron_api_app.h @@ -116,6 +116,7 @@ class App : public ElectronBrowserClient::Delegate, base::Value::Dict user_info) override; void OnNewWindowForTab() override; void OnDidBecomeActive() override; + void OnDidResignActive() override; #endif // content::ContentBrowserClient: diff --git a/shell/browser/browser.cc b/shell/browser/browser.cc index 29b20df384f2a..df6fff168e661 100644 --- a/shell/browser/browser.cc +++ b/shell/browser/browser.cc @@ -291,6 +291,11 @@ void Browser::DidBecomeActive() { for (BrowserObserver& observer : observers_) observer.OnDidBecomeActive(); } + +void Browser::DidResignActive() { + for (BrowserObserver& observer : observers_) + observer.OnDidResignActive(); +} #endif } // namespace electron diff --git a/shell/browser/browser.h b/shell/browser/browser.h index 25e1834c32fad..2cc0a1905b793 100644 --- a/shell/browser/browser.h +++ b/shell/browser/browser.h @@ -278,8 +278,11 @@ class Browser : public WindowListObserver { // Tell the application to create a new window for a tab. void NewWindowForTab(); - // Tell the application that application did become active + // Indicate that the app is now active. void DidBecomeActive(); + // Indicate that the app is no longer active and doesn’t have focus. + void DidResignActive(); + #endif // BUILDFLAG(IS_MAC) // Tell the application that application is activated with visible/invisible diff --git a/shell/browser/browser_observer.h b/shell/browser/browser_observer.h index aa04eade89e7d..dd3bdc665e0d3 100644 --- a/shell/browser/browser_observer.h +++ b/shell/browser/browser_observer.h @@ -78,8 +78,10 @@ class BrowserObserver : public base::CheckedObserver { // User clicked the native macOS new tab button. (macOS only) virtual void OnNewWindowForTab() {} - // Browser did become active. + // Browser became active. virtual void OnDidBecomeActive() {} + // Browser lost active status. + virtual void OnDidResignActive() {} #endif protected: diff --git a/shell/browser/mac/electron_application_delegate.mm b/shell/browser/mac/electron_application_delegate.mm index c8f66a314b46e..e20b52eb73a44 100644 --- a/shell/browser/mac/electron_application_delegate.mm +++ b/shell/browser/mac/electron_application_delegate.mm @@ -92,6 +92,10 @@ - (void)applicationDidBecomeActive:(NSNotification*)notification { electron::Browser::Get()->DidBecomeActive(); } +- (void)applicationDidResignActive:(NSNotification*)notification { + electron::Browser::Get()->DidResignActive(); +} + - (NSMenu*)applicationDockMenu:(NSApplication*)sender { if (menu_controller_) return [menu_controller_ menu];