Merge pull request #6944 from electron/check-window-from-native-view

Access native window through native view
This commit is contained in:
Cheng Zhao 2016-08-24 16:25:27 +09:00 committed by GitHub
commit 7bdce52290
3 changed files with 28 additions and 10 deletions

View file

@ -1004,7 +1004,8 @@ void WebContents::InspectElement(int x, int y) {
if (type_ == REMOTE) if (type_ == REMOTE)
return; return;
OpenDevTools(nullptr); if (!managed_web_contents()->GetDevToolsWebContents())
OpenDevTools(nullptr);
scoped_refptr<content::DevToolsAgentHost> agent( scoped_refptr<content::DevToolsAgentHost> agent(
content::DevToolsAgentHost::GetOrCreateFor(web_contents())); content::DevToolsAgentHost::GetOrCreateFor(web_contents()));
agent->InspectElement(x, y); agent->InspectElement(x, y);
@ -1175,7 +1176,7 @@ bool WebContents::IsFocused() const {
if (!view) return false; if (!view) return false;
if (GetType() != BACKGROUND_PAGE) { if (GetType() != BACKGROUND_PAGE) {
auto window = web_contents()->GetTopLevelNativeWindow(); auto window = web_contents()->GetNativeView()->GetToplevelWindow();
if (window && !window->IsVisible()) if (window && !window->IsVisible())
return false; return false;
} }

View file

@ -4,9 +4,7 @@
#include "atom/browser/api/atom_api_web_contents.h" #include "atom/browser/api/atom_api_web_contents.h"
@interface NSWindow #import <Cocoa/Cocoa.h>
- (BOOL)isKeyWindow;
@end
namespace atom { namespace atom {
@ -17,7 +15,7 @@ bool WebContents::IsFocused() const {
if (!view) return false; if (!view) return false;
if (GetType() != BACKGROUND_PAGE) { if (GetType() != BACKGROUND_PAGE) {
auto window = web_contents()->GetTopLevelNativeWindow(); auto window = [web_contents()->GetNativeView() window];
// On Mac the render widget host view does not lose focus when the window // On Mac the render widget host view does not lose focus when the window
// loses focus so check if the top level window is the key window. // loses focus so check if the top level window is the key window.
if (window && ![window isKeyWindow]) if (window && ![window isKeyWindow])

View file

@ -49,11 +49,9 @@ describe('webContents module', function () {
}) })
describe('getFocusedWebContents() API', function () { describe('getFocusedWebContents() API', function () {
if (isCi) {
return
}
it('returns the focused web contents', function (done) { it('returns the focused web contents', function (done) {
if (isCi) return done()
const specWebContents = remote.getCurrentWebContents() const specWebContents = remote.getCurrentWebContents()
assert.equal(specWebContents.getId(), webContents.getFocusedWebContents().getId()) assert.equal(specWebContents.getId(), webContents.getFocusedWebContents().getId())
@ -69,6 +67,27 @@ describe('webContents module', function () {
specWebContents.openDevTools() specWebContents.openDevTools()
}) })
it('does not crash when called on a detached dev tools window', function (done) {
const specWebContents = w.webContents
specWebContents.once('devtools-opened', function () {
assert.doesNotThrow(function () {
webContents.getFocusedWebContents()
})
specWebContents.closeDevTools()
})
specWebContents.once('devtools-closed', function () {
assert.doesNotThrow(function () {
webContents.getFocusedWebContents()
})
done()
})
specWebContents.openDevTools({mode: 'detach'})
w.inspectElement(100, 100)
})
}) })
describe('isFocused() API', function () { describe('isFocused() API', function () {