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 convertToMenuTemplate = function (event, items) {
const convertToMenuTemplate = function (items, handler) {
return items.map(function (item) {
const transformed = item.type === 'subMenu' ? {
type: 'submenu',
label: item.label,
enabled: item.enabled,
submenu: convertToMenuTemplate(event, item.subItems)
submenu: convertToMenuTemplate(item.subItems, handler)
} : item.type === 'separator' ? {
type: 'separator'
} : item.type === 'checkbox' ? {
@ -30,9 +30,7 @@ const convertToMenuTemplate = function (event, items) {
}
if (item.id != null) {
transformed.click = function () {
event._replyInternal('ELECTRON_INSPECTOR_CONTEXT_MENU_CLICK', item.id)
}
transformed.click = () => handler(item.id)
}
return transformed
@ -67,22 +65,22 @@ const assertChromeDevTools = function (contents, api) {
}
ipcMainUtils.handle('ELECTRON_INSPECTOR_CONTEXT_MENU', function (event, items, isEditMenu) {
assertChromeDevTools(event.sender, 'window.InspectorFrontendHost.showContextMenuAtPoint()')
return new Promise(resolve => {
assertChromeDevTools(event.sender, 'window.InspectorFrontendHost.showContextMenuAtPoint()')
const template = isEditMenu ? getEditMenuItems() : convertToMenuTemplate(event, items)
const menu = Menu.buildFromTemplate(template)
const window = event.sender.getOwnerBrowserWindow()
const template = isEditMenu ? getEditMenuItems() : convertToMenuTemplate(items, resolve)
const menu = Menu.buildFromTemplate(template)
const window = event.sender.getOwnerBrowserWindow()
menu.once('menu-will-close', () => {
// menu-will-close is emitted before the click handler, but needs to be sent after.
// otherwise, DevToolsAPI.contextMenuCleared() would be called before
// DevToolsAPI.contextMenuItemSelected(id) and the menu will not work properly.
setTimeout(() => {
event._replyInternal('ELECTRON_INSPECTOR_CONTEXT_MENU_CLOSE')
menu.once('menu-will-close', () => {
// menu-will-close is emitted before the click handler, but needs to be sent after.
// otherwise, DevToolsAPI.contextMenuCleared() would be called before
// DevToolsAPI.contextMenuItemSelected(id) and the menu will not work properly.
setTimeout(() => resolve())
})
})
menu.popup({ window })
menu.popup({ window })
})
})
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'
window.onload = function () {
@ -23,14 +22,6 @@ function completeURL (project: string, path: string) {
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[]) {
return items.length === 0 && document.elementsFromPoint(x, y).some(function (element) {
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 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) {