Skip to content

Commit 297ca84

Browse files
committed
Use browser-window-created event to register context menu
1 parent 4ee0754 commit 297ca84

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

main-process/menus/context-menu.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
const electron = require('electron')
2-
const {BrowserWindow, Menu, MenuItem} = electron
2+
const BrowserWindow = electron.BrowserWindow
3+
const Menu = electron.Menu
4+
const MenuItem = electron.MenuItem
35
const ipc = electron.ipcMain
6+
const app = electron.app
47

5-
let menu = new Menu()
6-
8+
const menu = new Menu()
79
menu.append(new MenuItem({ label: 'Hello' }))
810
menu.append(new MenuItem({ type: 'separator' }))
911
menu.append(new MenuItem({ label: 'Electron', type: 'checkbox', checked: true }))
1012

11-
// Show when the window is right clicked.
12-
// Adds event listener to all created windows.
13-
for (const win of BrowserWindow.getAllWindows()) {
13+
app.on('browser-window-created', function (event, win) {
1414
win.webContents.on('context-menu', function (e, params) {
1515
menu.popup(win, params.x, params.y)
1616
})
17-
}
17+
})
1818

19-
// Show when the renderer asks for a menu.
20-
ipc.on('show-context-menu', function () {
21-
menu.popup(BrowserWindow.getFocusedWindow())
19+
ipc.on('show-context-menu', function (event) {
20+
const win = BrowserWindow.fromWebContents(event.sender)
21+
menu.popup(win)
2222
})

0 commit comments

Comments
 (0)