Skip to content

Commit

Permalink
fix: active window check on mac
Browse files Browse the repository at this point in the history
  • Loading branch information
deepak1556 committed May 18, 2021
1 parent 418f1e5 commit b4b9b7f
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 5 deletions.
1 change: 1 addition & 0 deletions shell/browser/api/electron_api_browser_window.cc
Expand Up @@ -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
}

Expand Down
10 changes: 6 additions & 4 deletions shell/browser/api/electron_api_web_contents.cc
Expand Up @@ -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();
}

Expand Down
4 changes: 4 additions & 0 deletions shell/browser/native_window.h
Expand Up @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions shell/browser/native_window_mac.h
Expand Up @@ -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();
Expand Down Expand Up @@ -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_;
Expand Down
10 changes: 10 additions & 0 deletions shell/browser/native_window_mac.mm
Expand Up @@ -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);
Expand Down
1 change: 0 additions & 1 deletion spec-main/chromium-spec.ts
Expand Up @@ -1617,7 +1617,6 @@ describe('navigator.clipboard', () => {
let w: BrowserWindow;
before(async () => {
w = new BrowserWindow({
show: false,
webPreferences: {
enableBlinkFeatures: 'Serial'
}
Expand Down

0 comments on commit b4b9b7f

Please sign in to comment.