From f756d077db9a2bb2009a4595e94169501bb2263f Mon Sep 17 00:00:00 2001 From: Lukas Weber Date: Fri, 22 May 2020 13:27:51 +0200 Subject: [PATCH] fix: trigger activate event when app is activated via app switcher When application is activated thru macOS app switcher (cmd+tab) the App's activate event is note emitted. The reason is that `applicationShouldHandleReopen:hasVisibleWindows:` is sent only when app is activated via Dock. Using `applicationDidBecomeActive:` is handling all cases properly. --- .../mac/electron_application_delegate.mm | 39 ++++++++++++++++--- 1 file changed, 34 insertions(+), 5 deletions(-) diff --git a/shell/browser/mac/electron_application_delegate.mm b/shell/browser/mac/electron_application_delegate.mm index f5da4f4febfba..635480bc20c21 100644 --- a/shell/browser/mac/electron_application_delegate.mm +++ b/shell/browser/mac/electron_application_delegate.mm @@ -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 @@ -78,6 +81,8 @@ - (void)applicationDidFinishLaunching:(NSNotification*)notify { @selector(_coreAttributesFromRange:whichAttributes:completionHandler:)); } #endif + + isFirstActivation_ = true; } - (NSMenu*)applicationDockMenu:(NSApplication*)sender { @@ -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(flag)); - return flag; + browser->Activate(hasVisibleWindows); } - (BOOL)application:(NSApplication*)sender