Skip to content

Commit

Permalink
fix: adjust initial webContents focus calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
rzhao271 authored and deepak1556 committed May 18, 2021
1 parent 33035f9 commit 418f1e5
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 26 deletions.
14 changes: 0 additions & 14 deletions lib/browser/api/browser-window.ts
Expand Up @@ -20,20 +20,6 @@ BrowserWindow.prototype._init = function (this: BWT) {
nativeSetBounds.call(this, bounds, ...opts);
};

// Sometimes the webContents doesn't get focus when window is shown, so we
// have to force focusing on webContents in this case. The safest way is to
// focus it when we first start to load URL, if we do it earlier it won't
// have effect, if we do it later we might move focus in the page.
//
// Though this hack is only needed on macOS when the app is launched from
// Finder, we still do it on all platforms in case of other bugs we don't
// know.
if (this.webContents._initiallyShown) {
this.webContents.once('load-url' as any, function (this: WebContents) {
this.focus();
});
}

// Redirect focus/blur event to app instance too.
this.on('blur', (event: Event) => {
app.emit('browser-window-blur', event, this);
Expand Down
31 changes: 23 additions & 8 deletions shell/browser/api/electron_api_web_contents.cc
Expand Up @@ -702,8 +702,8 @@ WebContents::WebContents(v8::Isolate* isolate,
// BrowserViews are not attached to a window initially so they should start
// off as hidden. This is also important for compositor recycling. See:
// https://github.com/electron/electron/pull/21372
initially_shown_ = type_ != Type::kBrowserView;
options.Get(options::kShow, &initially_shown_);
bool initially_shown = type_ != Type::kBrowserView;
options.Get(options::kShow, &initially_shown);

// Obtain the session.
std::string partition;
Expand Down Expand Up @@ -759,7 +759,7 @@ WebContents::WebContents(v8::Isolate* isolate,
#endif
} else {
content::WebContents::CreateParams params(session->browser_context());
params.initially_hidden = !initially_shown_;
params.initially_hidden = !initially_shown;
web_contents = content::WebContents::Create(params);
}

Expand Down Expand Up @@ -1653,6 +1653,26 @@ void WebContents::DidRedirectNavigation(
EmitNavigationEvent("did-redirect-navigation", navigation_handle);
}

void WebContents::ReadyToCommitNavigation(
content::NavigationHandle* navigation_handle) {
// Don't focus content in an inactive window.
if (!owner_window())
return;
if (!owner_window()->widget()->IsActive())
return;
// 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();
}

void WebContents::DidFinishNavigation(
content::NavigationHandle* navigation_handle) {
if (!navigation_handle->HasCommitted())
Expand Down Expand Up @@ -3113,10 +3133,6 @@ v8::Local<v8::Value> WebContents::Debugger(v8::Isolate* isolate) {
return v8::Local<v8::Value>::New(isolate, debugger_);
}

bool WebContents::WasInitiallyShown() {
return initially_shown_;
}

content::RenderFrameHost* WebContents::MainFrame() {
return web_contents()->GetMainFrame();
}
Expand Down Expand Up @@ -3688,7 +3704,6 @@ v8::Local<v8::ObjectTemplate> WebContents::FillObjectTemplate(
.SetProperty("hostWebContents", &WebContents::HostWebContents)
.SetProperty("devToolsWebContents", &WebContents::DevToolsWebContents)
.SetProperty("debugger", &WebContents::Debugger)
.SetProperty("_initiallyShown", &WebContents::WasInitiallyShown)
.SetProperty("mainFrame", &WebContents::MainFrame)
.Build();
}
Expand Down
5 changes: 2 additions & 3 deletions shell/browser/api/electron_api_web_contents.h
Expand Up @@ -324,7 +324,6 @@ class WebContents : public gin::Wrappable<WebContents>,
content::WebContents* HostWebContents() const;
v8::Local<v8::Value> DevToolsWebContents(v8::Isolate* isolate);
v8::Local<v8::Value> Debugger(v8::Isolate* isolate);
bool WasInitiallyShown();
content::RenderFrameHost* MainFrame();

WebContentsZoomController* GetZoomController() { return zoom_controller_; }
Expand Down Expand Up @@ -559,6 +558,8 @@ class WebContents : public gin::Wrappable<WebContents>,
content::NavigationHandle* navigation_handle) override;
void DidRedirectNavigation(
content::NavigationHandle* navigation_handle) override;
void ReadyToCommitNavigation(
content::NavigationHandle* navigation_handle) override;
void DidFinishNavigation(
content::NavigationHandle* navigation_handle) override;
bool OnMessageReceived(const IPC::Message& message) override;
Expand Down Expand Up @@ -722,8 +723,6 @@ class WebContents : public gin::Wrappable<WebContents>,

v8::Global<v8::Value> pending_child_web_preferences_;

bool initially_shown_ = true;

// The window that this WebContents belongs to.
base::WeakPtr<NativeWindow> owner_window_;

Expand Down
1 change: 0 additions & 1 deletion typings/internal-electron.d.ts
Expand Up @@ -67,7 +67,6 @@ declare namespace Electron {
getLastWebPreferences(): Electron.WebPreferences;
_getPreloadPaths(): string[];
equal(other: WebContents): boolean;
_initiallyShown: boolean;
browserWindowOptions: BrowserWindowConstructorOptions;
_windowOpenHandler: ((details: Electron.HandlerDetails) => any) | null;
_callWindowOpenHandler(event: any, details: Electron.HandlerDetails): Electron.BrowserWindowConstructorOptions | null;
Expand Down

0 comments on commit 418f1e5

Please sign in to comment.