Skip to content

Commit 44e597b

Browse files
committed
Merge branch 'context-menu' of https://github.com/danhp/electron-api-demos into danhp-context-menu
2 parents ac8b018 + c8d386f commit 44e597b

File tree

5 files changed

+31
-21
lines changed

5 files changed

+31
-21
lines changed

main-process/menus/context-menu.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const electron = require('electron')
2+
const {BrowserWindow, Menu, MenuItem} = electron
3+
const ipc = electron.ipcMain
4+
5+
let menu = new Menu()
6+
7+
menu.append(new MenuItem({ label: 'Hello' }))
8+
menu.append(new MenuItem({ type: 'separator' }))
9+
menu.append(new MenuItem({ label: 'Electron', type: 'checkbox', checked: true }))
10+
11+
// Show when the window is right clicked.
12+
// Adds event listener to all created windows.
13+
for (const win of BrowserWindow.getAllWindows()) {
14+
win.webContents.on('context-menu', function (e, params) {
15+
menu.popup(win, params.x, params.y)
16+
})
17+
}
18+
19+
// Show when the renderer asks for a menu.
20+
ipc.on('show-context-menu', function () {
21+
menu.popup(BrowserWindow.getFocusedWindow())
22+
})

main.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,13 @@ function initialize () {
1414
var shouldQuit = makeSingleInstance()
1515
if (shouldQuit) return app.quit()
1616

17-
loadDemos()
18-
1917
function createWindow () {
2018
var windowOptions = {
2119
width: 1080,
2220
minWidth: 680,
2321
height: 840
2422
}
23+
2524
if (process.platform === 'linux') {
2625
windowOptions.icon = path.join(__dirname, '/assets/app-icon/png/512.png')
2726
}
@@ -43,6 +42,7 @@ function initialize () {
4342
app.on('ready', function () {
4443
createWindow()
4544
autoUpdater.initialize()
45+
loadDemos()
4646
})
4747

4848
app.on('window-all-closed', function () {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
"chai-as-promised": "^5.1.0",
4141
"devtron": "^1.0.0",
4242
"electron-packager": "^7.0.1",
43-
"electron-prebuilt": "~1.0.1",
43+
"electron-prebuilt": "~1.0.2",
4444
"electron-winstaller": "^2.2.0",
4545
"mocha": "^2.3.4",
4646
"request": "^2.70.0",
Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,7 @@
1-
const remote = require('electron').remote
2-
const Menu = remote.Menu
3-
const MenuItem = remote.MenuItem
1+
const ipc = require('electron').ipcRenderer
42

5-
let menu = new Menu()
6-
7-
menu.append(new MenuItem({ label: 'Hello' }))
8-
menu.append(new MenuItem({ type: 'separator' }))
9-
menu.append(new MenuItem({ label: 'Electron', type: 'checkbox', checked: true }))
10-
11-
// Show when window is right-clicked
12-
window.addEventListener('contextmenu', function (e) {
13-
e.preventDefault()
14-
menu.popup(remote.getCurrentWindow())
15-
}, false)
16-
17-
// Show when demo button is clicked
3+
// Tell main process to show the menu when demo button is clicked
184
const contextMenuBtn = document.getElementById('context-menu')
195
contextMenuBtn.addEventListener('click', function () {
20-
menu.popup(remote.getCurrentWindow())
6+
ipc.send('show-context-menu')
217
})

sections/menus/menus.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ <h2>ProTip</h2>
5252
</div>
5353
<p>A context, or right-click, menu can be created with the <code>menu</code> and <code>menuitem</code> modules as well. You can right-click anywhere in this app or click the demo button to see an example context menu.</p>
5454

55-
<p>In this demo we use the <code>remote</code> module to access <code>menu</code> and <code>menuitem</code> from the renderer process.</p>
55+
<p>In this demo we use the <code>ipcRenderer</code> module to show the context menu when explicitly calling it from the renderer process.</p>
56+
<h5>Main Process</h5>
57+
<pre><code data-path="main-process/menus/context-menu.js"></pre></code>
5658
<h5>Renderer Process</h5>
5759
<pre><code data-path="renderer-process/menus/context-menu.js"></pre></code>
5860
</div>

0 commit comments

Comments
 (0)