From cc7fe82b7414018ec31095c15cf48042153a290c Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Mon, 15 Aug 2016 14:03:31 -0700 Subject: [PATCH 1/2] Add failing webContents.isFocused spec --- spec/api-web-contents-spec.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/spec/api-web-contents-spec.js b/spec/api-web-contents-spec.js index 66b20ca2a8..e8bf146d46 100644 --- a/spec/api-web-contents-spec.js +++ b/spec/api-web-contents-spec.js @@ -71,4 +71,12 @@ describe('webContents module', function () { specWebContents.openDevTools() }) }) + + describe('isFocused() API', function () { + it('returns false when the window is hidden', function () { + BrowserWindow.getAllWindows().forEach(function (window) { + assert.equal(!window.isVisible() && window.webContents.isFocused(), false) + }) + }) + }) }) From 0755349e70afa7d3c864c647246a3258fb097752 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Mon, 15 Aug 2016 14:13:24 -0700 Subject: [PATCH 2/2] Ensure hidden windows don't have focused webContents --- atom/browser/api/atom_api_web_contents.cc | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/atom/browser/api/atom_api_web_contents.cc b/atom/browser/api/atom_api_web_contents.cc index 9baaaad292..e77338253f 100644 --- a/atom/browser/api/atom_api_web_contents.cc +++ b/atom/browser/api/atom_api_web_contents.cc @@ -37,6 +37,7 @@ #include "atom/common/native_mate_converters/image_converter.h" #include "atom/common/native_mate_converters/string16_converter.h" #include "atom/common/native_mate_converters/value_converter.h" +#include "atom/common/node_includes.h" #include "atom/common/options_switches.h" #include "base/strings/string_util.h" #include "base/strings/utf_string_conversions.h" @@ -72,7 +73,9 @@ #include "third_party/WebKit/public/web/WebFindOptions.h" #include "ui/display/screen.h" -#include "atom/common/node_includes.h" +#if !defined(OS_MACOSX) +#include "ui/aura/window.h" +#endif namespace { @@ -1169,7 +1172,15 @@ void WebContents::Focus() { #if !defined(OS_MACOSX) bool WebContents::IsFocused() const { auto view = web_contents()->GetRenderWidgetHostView(); - return view && view->HasFocus(); + if (!view) return false; + + if (GetType() != BACKGROUND_PAGE) { + auto window = web_contents()->GetTopLevelNativeWindow(); + if (window && !window->IsVisible()) + return false; + } + + return view->HasFocus(); } #endif