refactor: make ELECTRON_INSPECTOR_CONTEXT_MENU handler async (#17411)

This commit is contained in:
Milan Burda 2019-03-19 18:37:43 +01:00 committed by Jeremy Apthorp
parent 879462af4b
commit ddd51525f1
2 changed files with 21 additions and 27 deletions

View file

@ -9,13 +9,13 @@ const ipcMainUtils = require('@electron/internal/browser/ipc-main-internal-utils
const readFile = util.promisify(fs.readFile) const readFile = util.promisify(fs.readFile)
const convertToMenuTemplate = function (event, items) { const convertToMenuTemplate = function (items, handler) {
return items.map(function (item) { return items.map(function (item) {
const transformed = item.type === 'subMenu' ? { const transformed = item.type === 'subMenu' ? {
type: 'submenu', type: 'submenu',
label: item.label, label: item.label,
enabled: item.enabled, enabled: item.enabled,
submenu: convertToMenuTemplate(event, item.subItems) submenu: convertToMenuTemplate(item.subItems, handler)
} : item.type === 'separator' ? { } : item.type === 'separator' ? {
type: 'separator' type: 'separator'
} : item.type === 'checkbox' ? { } : item.type === 'checkbox' ? {
@ -30,9 +30,7 @@ const convertToMenuTemplate = function (event, items) {
} }
if (item.id != null) { if (item.id != null) {
transformed.click = function () { transformed.click = () => handler(item.id)
event._replyInternal('ELECTRON_INSPECTOR_CONTEXT_MENU_CLICK', item.id)
}
} }
return transformed return transformed
@ -67,9 +65,10 @@ const assertChromeDevTools = function (contents, api) {
} }
ipcMainUtils.handle('ELECTRON_INSPECTOR_CONTEXT_MENU', function (event, items, isEditMenu) { ipcMainUtils.handle('ELECTRON_INSPECTOR_CONTEXT_MENU', function (event, items, isEditMenu) {
return new Promise(resolve => {
assertChromeDevTools(event.sender, 'window.InspectorFrontendHost.showContextMenuAtPoint()') assertChromeDevTools(event.sender, 'window.InspectorFrontendHost.showContextMenuAtPoint()')
const template = isEditMenu ? getEditMenuItems() : convertToMenuTemplate(event, items) const template = isEditMenu ? getEditMenuItems() : convertToMenuTemplate(items, resolve)
const menu = Menu.buildFromTemplate(template) const menu = Menu.buildFromTemplate(template)
const window = event.sender.getOwnerBrowserWindow() const window = event.sender.getOwnerBrowserWindow()
@ -77,12 +76,11 @@ ipcMainUtils.handle('ELECTRON_INSPECTOR_CONTEXT_MENU', function (event, items, i
// menu-will-close is emitted before the click handler, but needs to be sent after. // menu-will-close is emitted before the click handler, but needs to be sent after.
// otherwise, DevToolsAPI.contextMenuCleared() would be called before // otherwise, DevToolsAPI.contextMenuCleared() would be called before
// DevToolsAPI.contextMenuItemSelected(id) and the menu will not work properly. // DevToolsAPI.contextMenuItemSelected(id) and the menu will not work properly.
setTimeout(() => { setTimeout(() => resolve())
event._replyInternal('ELECTRON_INSPECTOR_CONTEXT_MENU_CLOSE')
})
}) })
menu.popup({ window }) menu.popup({ window })
})
}) })
ipcMainUtils.handle('ELECTRON_INSPECTOR_SELECT_FILE', async function (event) { ipcMainUtils.handle('ELECTRON_INSPECTOR_SELECT_FILE', async function (event) {

View file

@ -1,4 +1,3 @@
import { ipcRendererInternal } from '@electron/internal/renderer/ipc-renderer-internal'
import { invoke, invokeSync } from '@electron/internal/renderer/ipc-renderer-internal-utils' import { invoke, invokeSync } from '@electron/internal/renderer/ipc-renderer-internal-utils'
window.onload = function () { window.onload = function () {
@ -23,14 +22,6 @@ function completeURL (project: string, path: string) {
return invokeSync('ELECTRON_INSPECTOR_CONFIRM', message, title) as boolean return invokeSync('ELECTRON_INSPECTOR_CONFIRM', message, title) as boolean
} }
ipcRendererInternal.on('ELECTRON_INSPECTOR_CONTEXT_MENU_CLICK', function (_event: Electron.Event, id: number) {
window.DevToolsAPI!.contextMenuItemSelected(id)
})
ipcRendererInternal.on('ELECTRON_INSPECTOR_CONTEXT_MENU_CLOSE', function () {
window.DevToolsAPI!.contextMenuCleared()
})
const useEditMenuItems = function (x: number, y: number, items: any[]) { const useEditMenuItems = function (x: number, y: number, items: any[]) {
return items.length === 0 && document.elementsFromPoint(x, y).some(function (element) { return items.length === 0 && document.elementsFromPoint(x, y).some(function (element) {
return element.nodeName === 'INPUT' || return element.nodeName === 'INPUT' ||
@ -41,7 +32,12 @@ const useEditMenuItems = function (x: number, y: number, items: any[]) {
const createMenu = function (x: number, y: number, items: any[]) { const createMenu = function (x: number, y: number, items: any[]) {
const isEditMenu = useEditMenuItems(x, y, items) const isEditMenu = useEditMenuItems(x, y, items)
invoke('ELECTRON_INSPECTOR_CONTEXT_MENU', items, isEditMenu) invoke<number>('ELECTRON_INSPECTOR_CONTEXT_MENU', items, isEditMenu).then(id => {
if (typeof id === 'number') {
window.DevToolsAPI!.contextMenuItemSelected(id)
}
window.DevToolsAPI!.contextMenuCleared()
})
} }
const showFileChooserDialog = function (callback: (blob: File) => void) { const showFileChooserDialog = function (callback: (blob: File) => void) {