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

fix: trigger activate event when app is activated via app switcher #23772

Merged
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
39 changes: 34 additions & 5 deletions shell/browser/mac/electron_application_delegate.mm
Expand Up @@ -41,7 +41,10 @@ - (void)_coreAttributesFromRange:(NSRange)range
@end
#endif // BUILDFLAG(USE_ALLOCATOR_SHIM)

@implementation ElectronApplicationDelegate
@implementation ElectronApplicationDelegate {
@private
bool isFirstActivation_;
}

- (void)setApplicationDockMenu:(electron::ElectronMenuModel*)model {
menu_controller_.reset([[ElectronMenuController alloc] initWithModel:model
Expand Down Expand Up @@ -78,6 +81,8 @@ - (void)applicationDidFinishLaunching:(NSNotification*)notify {
@selector(_coreAttributesFromRange:whichAttributes:completionHandler:));
}
#endif

isFirstActivation_ = true;
}

- (NSMenu*)applicationDockMenu:(NSApplication*)sender {
Expand All @@ -92,11 +97,35 @@ - (BOOL)application:(NSApplication*)sender openFile:(NSString*)filename {
return electron::Browser::Get()->OpenFile(filename_str) ? YES : NO;
}

- (BOOL)applicationShouldHandleReopen:(NSApplication*)theApplication
hasVisibleWindows:(BOOL)flag {
- (void)applicationDidBecomeActive:(NSNotification*)notify {
// Originally `applicationShouldHandleReopen:hasVisibleWindows:` was used to
// emit Activate event. But the message is sent only when application is
// activated via Dock or Finder ignoring the App Switcher (cmd+tab).
//
// Using `applicationDidBecomeActive' is more reliable but to maintain
// compatibility with previous implementation we ignore activation
// immediately after the application launch, and compute the
// hasVisibleWindows on our own.
//
// Details in https://github.com/electron/electron/pull/23727.

if (isFirstActivation_) {
isFirstActivation_ = false;
return;
}

NSApplication* app = notify.object;
bool hasVisibleWindows = false;

for (NSWindow* win in app.windows) {
if (win.isVisible || win.miniaturized) {
hasVisibleWindows = true;
break;
}
}

electron::Browser* browser = electron::Browser::Get();
browser->Activate(static_cast<bool>(flag));
return flag;
browser->Activate(hasVisibleWindows);
}

- (BOOL)application:(NSApplication*)sender
Expand Down