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: add did-resign-active event on app #38018

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
15 changes: 13 additions & 2 deletions docs/api/app.md
Original file line number Diff line number Diff line change
Expand Up @@ -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_

Expand Down
4 changes: 4 additions & 0 deletions shell/browser/api/electron_api_app.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
1 change: 1 addition & 0 deletions shell/browser/api/electron_api_app.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
5 changes: 5 additions & 0 deletions shell/browser/browser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 4 additions & 1 deletion shell/browser/browser.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion shell/browser/browser_observer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 4 additions & 0 deletions shell/browser/mac/electron_application_delegate.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down