From bcdb019894c8275daa43025b992074a8236785f7 Mon Sep 17 00:00:00 2001 From: "Brian R. Bondy" Date: Fri, 23 Dec 2016 11:32:16 -0500 Subject: [PATCH] Make URLbar accessible on first launch Fix https://github.com/brave/browser-laptop/issues/6280 This fix turned out to be too aggressive: https://github.com/brave/electron/commit/f4e1e2ec32c08a35ac31682787f4265ba4ca7ea8 So it's a partial backout of that keeping the parts that keep https://github.com/brave/browser-laptop/issues/6122 fixed Auditors: @bsclifton --- atom/browser/api/atom_api_web_contents.cc | 5 +++++ atom/browser/api/atom_api_web_contents.h | 1 + lib/browser/api/browser-window.js | 11 +++++++++++ 3 files changed, 17 insertions(+) diff --git a/atom/browser/api/atom_api_web_contents.cc b/atom/browser/api/atom_api_web_contents.cc index 28145e261..fede04295 100644 --- a/atom/browser/api/atom_api_web_contents.cc +++ b/atom/browser/api/atom_api_web_contents.cc @@ -1535,6 +1535,10 @@ void WebContents::CopyImageAt(int x, int y) { host->CopyImageAt(x, y); } +void WebContents::Focus() { + web_contents()->Focus(); +} + #if !defined(OS_MACOSX) bool WebContents::IsFocused() const { auto view = web_contents()->GetRenderWidgetHostView(); @@ -1956,6 +1960,7 @@ void WebContents::BuildPrototype(v8::Isolate* isolate, .SetMethod("replaceMisspelling", &WebContents::ReplaceMisspelling) .SetMethod("findInPage", &WebContents::FindInPage) .SetMethod("stopFindInPage", &WebContents::StopFindInPage) + .SetMethod("focus", &WebContents::Focus) .SetMethod("isFocused", &WebContents::IsFocused) .SetMethod("_clone", &WebContents::Clone) .SetMethod("_send", &WebContents::SendIPCMessage) diff --git a/atom/browser/api/atom_api_web_contents.h b/atom/browser/api/atom_api_web_contents.h index 23bf3ed4b..265418a99 100644 --- a/atom/browser/api/atom_api_web_contents.h +++ b/atom/browser/api/atom_api_web_contents.h @@ -205,6 +205,7 @@ class WebContents : public mate::TrackableObject, void CopyImageAt(int x, int y); // Focus. + void Focus(); bool IsFocused() const; void SetActive(bool active); void SetTabIndex(int index); diff --git a/lib/browser/api/browser-window.js b/lib/browser/api/browser-window.js index ecc943c37..6f396a206 100644 --- a/lib/browser/api/browser-window.js +++ b/lib/browser/api/browser-window.js @@ -45,6 +45,17 @@ BrowserWindow.prototype._init = function () { this.webContents.once('render-view-ready', () => { this.notifyReady() }) + // 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. + this.webContents.once('load-url', function () { + this.focus() + }) // Redirect focus/blur event to app instance too. this.on('blur', (event) => {