Skip to content
Permalink
Browse files

fix: ensure app is focused after menu interaction

  • Loading branch information...
korhaliv committed Sep 3, 2019
1 parent 4fea544 commit d40be7070b307c7fb936b4658ced325d8b211882
Showing with 29 additions and 2 deletions.
  1. +29 −2 electron/menuBuilder.js
@@ -31,6 +31,30 @@ const buildAboutMenu = () => {
}
}

/**
* interceptMethod - Alters the `template` to inject `handler` call into `method`.
*
* @param {Array} template menu template
* @param {string} method method to intercept
* @param {Function} handler extract method to be called alongside with the original `method`
* @returns {object} updated menu template
*/
function interceptMethod(template, method, handler) {
return template.map(({ submenu, ...rest }) => {
return {
...rest,
submenu: submenu.map(item => {
const { [method]: wrappedMethod } = item
item[method] = () => {
wrappedMethod && wrappedMethod()
handler()
}
return item
}),
}
})
}

export default class ZapMenuBuilder {
constructor(mainWindow) {
this.mainWindow = mainWindow
@@ -46,11 +70,14 @@ export default class ZapMenuBuilder {
if (os.platform() === 'darwin') {
template = this.buildDarwinTemplate()
} else {
template = this.buildDefaultTemplate()
// add global `click` interceptor that re-focuses
// Web app after menu is closed
template = interceptMethod(this.buildDefaultTemplate(), 'click', () =>
this.mainWindow.webContents.focus()
)
}

this.setupInputTemplate()

const menu = Menu.buildFromTemplate(template)
Menu.setApplicationMenu(menu)

0 comments on commit d40be70

Please sign in to comment.
You can’t perform that action at this time.