Merge pull request #6851 from electron/focused-web-contents-from-hidden-windows

Prevent web contents in hidden windows from reporting as focused
This commit is contained in:
Cheng Zhao 2016-08-17 20:15:37 +09:00 коммит произвёл GitHub
Родитель 925fb27b1e 0755349e70
Коммит 4e355355a9
2 изменённых файлов: 21 добавлений и 2 удалений

Просмотреть файл

@ -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

Просмотреть файл

@ -70,4 +70,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)
})
})
})
})