Call role method on focused web contents

This commit is contained in:
Kevin Sawicki 2016-07-13 12:18:44 -07:00
parent 06e595e7cc
commit f9a8bd3ea5
3 changed files with 10 additions and 16 deletions

View file

@ -115,7 +115,7 @@ exports.getDefaultAccelerator = (role) => {
if (roles.hasOwnProperty(role)) return roles[role].accelerator
}
exports.execute = (role, focusedWindow) => {
exports.execute = (role, focusedWindow, focusedWebContents) => {
if (!roles.hasOwnProperty(role)) return false
if (process.platform === 'darwin') return false
@ -135,15 +135,8 @@ exports.execute = (role, focusedWindow) => {
return true
}
if (webContentsMethod && focusedWindow != null) {
const {webContents} = focusedWindow
if (webContents) {
if (webContents.isDevToolsFocused()) {
webContents.devToolsWebContents[webContentsMethod]()
} else {
webContents[webContentsMethod]()
}
}
if (webContentsMethod && focusedWebContents != null) {
focusedWebContents[webContentsMethod]()
return true
}

View file

@ -48,13 +48,13 @@ const MenuItem = function (options) {
this.overrideReadOnlyProperty('commandId', ++nextCommandId)
const click = options.click
this.click = (event, focusedWindow) => {
this.click = (event, focusedWindow, focusedWebContents) => {
// Manually flip the checked flags when clicked.
if (this.type === 'checkbox' || this.type === 'radio') {
this.checked = !this.checked
}
if (!roles.execute(this.role, focusedWindow)) {
if (!roles.execute(this.role, focusedWindow, focusedWebContents)) {
if (typeof click === 'function') {
click(this, focusedWindow, event)
} else if (typeof this.selector === 'string' && process.platform === 'darwin') {

View file

@ -1,7 +1,6 @@
'use strict'
const BrowserWindow = require('electron').BrowserWindow
const MenuItem = require('electron').MenuItem
const {BrowserWindow, MenuItem, webContents} = require('electron')
const EventEmitter = require('events').EventEmitter
const v8Util = process.atomBinding('v8_util')
const bindings = process.atomBinding('menu')
@ -117,8 +116,10 @@ Menu.prototype._init = function () {
return command != null ? command.icon : undefined
},
executeCommand: (event, commandId) => {
var command = this.commandsMap[commandId]
return command != null ? command.click(event, BrowserWindow.getFocusedWindow()) : undefined
const command = this.commandsMap[commandId]
if (command == null) return
command.click(event, BrowserWindow.getFocusedWindow(), webContents.getFocusedWebContents())
},
menuWillShow: () => {
// Make sure radio groups have at least one menu item seleted.