From b4b9b7f52f510801fe830adf379dc0dba4abdc8c Mon Sep 17 00:00:00 2001 From: deepak1556 Date: Tue, 18 May 2021 07:47:28 -0700 Subject: [PATCH] fix: active window check on mac --- shell/browser/api/electron_api_browser_window.cc | 1 + shell/browser/api/electron_api_web_contents.cc | 10 ++++++---- shell/browser/native_window.h | 4 ++++ shell/browser/native_window_mac.h | 3 +++ shell/browser/native_window_mac.mm | 10 ++++++++++ spec-main/chromium-spec.ts | 1 - 6 files changed, 24 insertions(+), 5 deletions(-) diff --git a/shell/browser/api/electron_api_browser_window.cc b/shell/browser/api/electron_api_browser_window.cc index 11438528160fc..c19544ec44b92 100644 --- a/shell/browser/api/electron_api_browser_window.cc +++ b/shell/browser/api/electron_api_browser_window.cc @@ -289,6 +289,7 @@ void BrowserWindow::OnWindowIsKeyChanged(bool is_key) { auto* rwhv = web_contents()->GetRenderWidgetHostView(); if (rwhv) rwhv->SetActive(is_key); + window()->SetActive(is_key); #endif } diff --git a/shell/browser/api/electron_api_web_contents.cc b/shell/browser/api/electron_api_web_contents.cc index d0b9c59f00e5d..9ecb52e9ac12b 100644 --- a/shell/browser/api/electron_api_web_contents.cc +++ b/shell/browser/api/electron_api_web_contents.cc @@ -1655,21 +1655,23 @@ void WebContents::DidRedirectNavigation( void WebContents::ReadyToCommitNavigation( content::NavigationHandle* navigation_handle) { + LOG(WARNING) << "called"; // Don't focus content in an inactive window. if (!owner_window()) return; +#if defined(OS_MAC) + if (!owner_window()->IsActive()) + return; +#else if (!owner_window()->widget()->IsActive()) return; +#endif // Don't focus content after subframe navigations. if (!navigation_handle->IsInMainFrame()) return; // Only focus for top-level contents. if (type_ != Type::kBrowserWindow) return; - // Only set the initial focus when navigating away from the - // blank page. - if (web_contents()->GetLastCommittedURL() != "about:blank") - return; web_contents()->SetInitialFocus(); } diff --git a/shell/browser/native_window.h b/shell/browser/native_window.h index fd0c0675db906..c1bb7ba28a3fc 100644 --- a/shell/browser/native_window.h +++ b/shell/browser/native_window.h @@ -135,6 +135,10 @@ class NativeWindow : public base::SupportsUserData, virtual void Invalidate() = 0; virtual void SetTitle(const std::string& title) = 0; virtual std::string GetTitle() = 0; +#if defined(OS_MAC) + virtual void SetActive(bool is_key) = 0; + virtual bool IsActive() const = 0; +#endif // Ability to augment the window title for the screen readers. void SetAccessibleTitle(const std::string& title); diff --git a/shell/browser/native_window_mac.h b/shell/browser/native_window_mac.h index 65fe746a58e16..c02d923084baa 100644 --- a/shell/browser/native_window_mac.h +++ b/shell/browser/native_window_mac.h @@ -149,6 +149,8 @@ class NativeWindowMac : public NativeWindow, gfx::Rect WindowBoundsToContentBounds(const gfx::Rect& bounds) const override; void NotifyWindowEnterFullScreen() override; void NotifyWindowLeaveFullScreen() override; + void SetActive(bool is_key) override; + bool IsActive() const override; void NotifyWindowWillEnterFullScreen(); void NotifyWindowWillLeaveFullScreen(); @@ -267,6 +269,7 @@ class NativeWindowMac : public NativeWindow, bool is_simple_fullscreen_ = false; bool was_maximizable_ = false; bool was_movable_ = false; + bool is_active_ = false; NSRect original_frame_; NSInteger original_level_; NSUInteger simple_fullscreen_mask_; diff --git a/shell/browser/native_window_mac.mm b/shell/browser/native_window_mac.mm index f6867beb87976..247ae20ab100e 100644 --- a/shell/browser/native_window_mac.mm +++ b/shell/browser/native_window_mac.mm @@ -1640,6 +1640,16 @@ void ViewDidMoveToSuperview(NSView* self, SEL _cmd) { UpdateVibrancyRadii(false); } +void NativeWindowMac::SetActive(bool is_key) { + if (is_key) + widget()->Activate(); + is_active_ = is_key; +} + +bool NativeWindowMac::IsActive() const { + return is_active_; +} + void NativeWindowMac::Cleanup() { DCHECK(!IsClosed()); ui::NativeTheme::GetInstanceForNativeUi()->RemoveObserver(this); diff --git a/spec-main/chromium-spec.ts b/spec-main/chromium-spec.ts index 40d9de0739fc0..dbb72ef59a217 100644 --- a/spec-main/chromium-spec.ts +++ b/spec-main/chromium-spec.ts @@ -1617,7 +1617,6 @@ describe('navigator.clipboard', () => { let w: BrowserWindow; before(async () => { w = new BrowserWindow({ - show: false, webPreferences: { enableBlinkFeatures: 'Serial' }