Merge pull request #6480 from electron/webcontents-focus
Execute role-based menu items on focused web contents
This commit is contained in:
commit
21a2feaf23
9 changed files with 66 additions and 16 deletions
|
@ -784,6 +784,13 @@ WebContents::Type WebContents::GetType() const {
|
||||||
return type_;
|
return type_;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if !defined(OS_MACOSX)
|
||||||
|
bool WebContents::IsFocused() const {
|
||||||
|
auto view = web_contents()->GetRenderWidgetHostView();
|
||||||
|
return view && view->HasFocus();
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
bool WebContents::Equal(const WebContents* web_contents) const {
|
bool WebContents::Equal(const WebContents* web_contents) const {
|
||||||
return GetID() == web_contents->GetID();
|
return GetID() == web_contents->GetID();
|
||||||
}
|
}
|
||||||
|
@ -1413,6 +1420,7 @@ void WebContents::BuildPrototype(v8::Isolate* isolate,
|
||||||
.SetMethod("showDefinitionForSelection",
|
.SetMethod("showDefinitionForSelection",
|
||||||
&WebContents::ShowDefinitionForSelection)
|
&WebContents::ShowDefinitionForSelection)
|
||||||
.SetMethod("capturePage", &WebContents::CapturePage)
|
.SetMethod("capturePage", &WebContents::CapturePage)
|
||||||
|
.SetMethod("isFocused", &WebContents::IsFocused)
|
||||||
.SetProperty("id", &WebContents::ID)
|
.SetProperty("id", &WebContents::ID)
|
||||||
.SetProperty("session", &WebContents::Session)
|
.SetProperty("session", &WebContents::Session)
|
||||||
.SetProperty("hostWebContents", &WebContents::HostWebContents)
|
.SetProperty("hostWebContents", &WebContents::HostWebContents)
|
||||||
|
|
|
@ -67,6 +67,7 @@ class WebContents : public mate::TrackableObject<WebContents>,
|
||||||
|
|
||||||
int GetID() const;
|
int GetID() const;
|
||||||
Type GetType() const;
|
Type GetType() const;
|
||||||
|
bool IsFocused() const;
|
||||||
bool Equal(const WebContents* web_contents) const;
|
bool Equal(const WebContents* web_contents) const;
|
||||||
void LoadURL(const GURL& url, const mate::Dictionary& options);
|
void LoadURL(const GURL& url, const mate::Dictionary& options);
|
||||||
void DownloadURL(const GURL& url);
|
void DownloadURL(const GURL& url);
|
||||||
|
|
30
atom/browser/api/atom_api_web_contents_mac.mm
Normal file
30
atom/browser/api/atom_api_web_contents_mac.mm
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
// Copyright (c) 2016 GitHub, Inc.
|
||||||
|
// Use of this source code is governed by the MIT license that can be
|
||||||
|
// found in the LICENSE file.
|
||||||
|
|
||||||
|
#include "atom/browser/api/atom_api_web_contents.h"
|
||||||
|
|
||||||
|
@interface NSWindow
|
||||||
|
- (BOOL)isKeyWindow;
|
||||||
|
@end
|
||||||
|
|
||||||
|
namespace atom {
|
||||||
|
|
||||||
|
namespace api {
|
||||||
|
|
||||||
|
bool WebContents::IsFocused() const {
|
||||||
|
if (GetType() != BACKGROUND_PAGE) {
|
||||||
|
auto window = web_contents()->GetTopLevelNativeWindow();
|
||||||
|
// 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.
|
||||||
|
if (window && ![window isKeyWindow])
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto view = web_contents()->GetRenderWidgetHostView();
|
||||||
|
return view && view->HasFocus();
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace api
|
||||||
|
|
||||||
|
} // namespace atom
|
|
@ -463,6 +463,10 @@ let currentURL = win.webContents.getURL();
|
||||||
|
|
||||||
Returns the title of the current web page.
|
Returns the title of the current web page.
|
||||||
|
|
||||||
|
### `webContents.isFocused()`
|
||||||
|
|
||||||
|
Returns a Boolean, whether the web page is focused.
|
||||||
|
|
||||||
### `webContents.isLoading()`
|
### `webContents.isLoading()`
|
||||||
|
|
||||||
Returns whether web page is still loading resources.
|
Returns whether web page is still loading resources.
|
||||||
|
|
|
@ -131,6 +131,7 @@
|
||||||
'atom/browser/api/atom_api_tray.h',
|
'atom/browser/api/atom_api_tray.h',
|
||||||
'atom/browser/api/atom_api_web_contents.cc',
|
'atom/browser/api/atom_api_web_contents.cc',
|
||||||
'atom/browser/api/atom_api_web_contents.h',
|
'atom/browser/api/atom_api_web_contents.h',
|
||||||
|
'atom/browser/api/atom_api_web_contents_mac.mm',
|
||||||
'atom/browser/api/atom_api_web_request.cc',
|
'atom/browser/api/atom_api_web_request.cc',
|
||||||
'atom/browser/api/atom_api_web_request.h',
|
'atom/browser/api/atom_api_web_request.h',
|
||||||
'atom/browser/api/atom_api_web_view_manager.cc',
|
'atom/browser/api/atom_api_web_view_manager.cc',
|
||||||
|
|
|
@ -115,7 +115,7 @@ exports.getDefaultAccelerator = (role) => {
|
||||||
if (roles.hasOwnProperty(role)) return roles[role].accelerator
|
if (roles.hasOwnProperty(role)) return roles[role].accelerator
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.execute = (role, focusedWindow) => {
|
exports.execute = (role, focusedWindow, focusedWebContents) => {
|
||||||
if (!roles.hasOwnProperty(role)) return false
|
if (!roles.hasOwnProperty(role)) return false
|
||||||
if (process.platform === 'darwin') return false
|
if (process.platform === 'darwin') return false
|
||||||
|
|
||||||
|
@ -135,15 +135,8 @@ exports.execute = (role, focusedWindow) => {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
if (webContentsMethod && focusedWindow != null) {
|
if (webContentsMethod && focusedWebContents != null) {
|
||||||
const {webContents} = focusedWindow
|
focusedWebContents[webContentsMethod]()
|
||||||
if (webContents) {
|
|
||||||
if (webContents.isDevToolsFocused()) {
|
|
||||||
webContents.devToolsWebContents[webContentsMethod]()
|
|
||||||
} else {
|
|
||||||
webContents[webContentsMethod]()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -48,13 +48,13 @@ const MenuItem = function (options) {
|
||||||
this.overrideReadOnlyProperty('commandId', ++nextCommandId)
|
this.overrideReadOnlyProperty('commandId', ++nextCommandId)
|
||||||
|
|
||||||
const click = options.click
|
const click = options.click
|
||||||
this.click = (event, focusedWindow) => {
|
this.click = (event, focusedWindow, focusedWebContents) => {
|
||||||
// Manually flip the checked flags when clicked.
|
// Manually flip the checked flags when clicked.
|
||||||
if (this.type === 'checkbox' || this.type === 'radio') {
|
if (this.type === 'checkbox' || this.type === 'radio') {
|
||||||
this.checked = !this.checked
|
this.checked = !this.checked
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!roles.execute(this.role, focusedWindow)) {
|
if (!roles.execute(this.role, focusedWindow, focusedWebContents)) {
|
||||||
if (typeof click === 'function') {
|
if (typeof click === 'function') {
|
||||||
click(this, focusedWindow, event)
|
click(this, focusedWindow, event)
|
||||||
} else if (typeof this.selector === 'string' && process.platform === 'darwin') {
|
} else if (typeof this.selector === 'string' && process.platform === 'darwin') {
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
const BrowserWindow = require('electron').BrowserWindow
|
const {BrowserWindow, MenuItem, webContents} = require('electron')
|
||||||
const MenuItem = require('electron').MenuItem
|
|
||||||
const EventEmitter = require('events').EventEmitter
|
const EventEmitter = require('events').EventEmitter
|
||||||
const v8Util = process.atomBinding('v8_util')
|
const v8Util = process.atomBinding('v8_util')
|
||||||
const bindings = process.atomBinding('menu')
|
const bindings = process.atomBinding('menu')
|
||||||
|
@ -117,8 +116,9 @@ Menu.prototype._init = function () {
|
||||||
return command != null ? command.icon : undefined
|
return command != null ? command.icon : undefined
|
||||||
},
|
},
|
||||||
executeCommand: (event, commandId) => {
|
executeCommand: (event, commandId) => {
|
||||||
var command = this.commandsMap[commandId]
|
const command = this.commandsMap[commandId]
|
||||||
return command != null ? command.click(event, BrowserWindow.getFocusedWindow()) : undefined
|
if (command == null) return
|
||||||
|
command.click(event, BrowserWindow.getFocusedWindow(), webContents.getFocusedWebContents())
|
||||||
},
|
},
|
||||||
menuWillShow: () => {
|
menuWillShow: () => {
|
||||||
// Make sure radio groups have at least one menu item seleted.
|
// Make sure radio groups have at least one menu item seleted.
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
const {EventEmitter} = require('events')
|
const {EventEmitter} = require('events')
|
||||||
const {app, ipcMain, session, Menu, NavigationController} = require('electron')
|
const {app, ipcMain, session, Menu, NavigationController} = require('electron')
|
||||||
|
const {getAllWebContents} = process.atomBinding('web_contents')
|
||||||
|
|
||||||
// session is not used here, the purpose is to make sure session is initalized
|
// session is not used here, the purpose is to make sure session is initalized
|
||||||
// before the webContents module.
|
// before the webContents module.
|
||||||
|
@ -244,5 +245,17 @@ module.exports = {
|
||||||
|
|
||||||
fromId (id) {
|
fromId (id) {
|
||||||
return binding.fromId(id)
|
return binding.fromId(id)
|
||||||
|
},
|
||||||
|
|
||||||
|
getFocusedWebContents () {
|
||||||
|
let focused = null
|
||||||
|
for (let contents of getAllWebContents()) {
|
||||||
|
if (!contents.isFocused()) continue
|
||||||
|
if (focused == null) focused = contents
|
||||||
|
// Return webview web contents which may be embedded inside another
|
||||||
|
// web contents that is also reporting as focused
|
||||||
|
if (contents.getType() === 'webview') return contents
|
||||||
|
}
|
||||||
|
return focused
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue