Skip to content

Commit

Permalink
fix: only focus a webContents if the window was not initially hidden (#…
Browse files Browse the repository at this point in the history
…25323) (#25330)

Co-authored-by: Samuel Attard <samuel.r.attard@gmail.com>

Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
  • Loading branch information
MarshallOfSound and trop[bot] committed Sep 8, 2020
1 parent fdbd243 commit fa3652a
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 7 deletions.
8 changes: 5 additions & 3 deletions lib/browser/api/browser-window.js
Expand Up @@ -30,9 +30,11 @@ BrowserWindow.prototype._init = function () {
// 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.
this.webContents.once('load-url', function () {
this.focus();
});
if (this.webContents._initiallyShown) {
this.webContents.once('load-url', function () {
this.focus();
});
}

// Redirect focus/blur event to app instance too.
this.on('blur', (event) => {
Expand Down
13 changes: 9 additions & 4 deletions shell/browser/api/electron_api_web_contents.cc
Expand Up @@ -453,8 +453,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
bool initially_shown = type_ != Type::BROWSER_VIEW;
options.Get(options::kShow, &initially_shown);
initially_shown_ = type_ != Type::BROWSER_VIEW;
options.Get(options::kShow, &initially_shown_);

// Obtain the session.
std::string partition;
Expand Down Expand Up @@ -510,7 +510,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 @@ -2678,6 +2678,10 @@ v8::Local<v8::Value> WebContents::Debugger(v8::Isolate* isolate) {
return v8::Local<v8::Value>::New(isolate, debugger_);
}

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

void WebContents::GrantOriginAccess(const GURL& url) {
content::ChildProcessSecurityPolicy::GetInstance()->GrantCommitOrigin(
web_contents()->GetMainFrame()->GetProcess()->GetID(),
Expand Down Expand Up @@ -2852,7 +2856,8 @@ void WebContents::BuildPrototype(v8::Isolate* isolate,
.SetProperty("session", &WebContents::Session)
.SetProperty("hostWebContents", &WebContents::HostWebContents)
.SetProperty("devToolsWebContents", &WebContents::DevToolsWebContents)
.SetProperty("debugger", &WebContents::Debugger);
.SetProperty("debugger", &WebContents::Debugger)
.SetProperty("_initiallyShown", &WebContents::WasInitiallyShown);
}

ElectronBrowserContext* WebContents::GetBrowserContext() const {
Expand Down
3 changes: 3 additions & 0 deletions shell/browser/api/electron_api_web_contents.h
Expand Up @@ -376,6 +376,7 @@ class WebContents : public gin_helper::TrackableObject<WebContents>,
content::WebContents* HostWebContents() const;
v8::Local<v8::Value> DevToolsWebContents(v8::Isolate* isolate);
v8::Local<v8::Value> Debugger(v8::Isolate* isolate);
bool WasInitiallyShown();

WebContentsZoomController* GetZoomController() { return zoom_controller_; }

Expand Down Expand Up @@ -650,6 +651,8 @@ class WebContents : public gin_helper::TrackableObject<WebContents>,
// Observers of this WebContents.
base::ObserverList<ExtendedWebContentsObserver> observers_;

bool initially_shown_ = true;

// The ID of the process of the currently committed RenderViewHost.
// -1 means no speculative RVH has been committed yet.
int currently_committed_process_id_ = -1;
Expand Down
1 change: 1 addition & 0 deletions typings/internal-electron.d.ts
Expand Up @@ -32,6 +32,7 @@ declare namespace Electron {
interface WebContents {
_getURL(): string;
getOwnerBrowserWindow(): Electron.BrowserWindow;
_initiallyShown: boolean;
}

interface SerializedError {
Expand Down

0 comments on commit fa3652a

Please sign in to comment.