Dock (macOS)

Electron has APIs to configure the app’s icon in the macOS Dock. A macOS-only API exists to create a custom dock menu, but Electron also uses the app dock icon as the entry point for cross-platform features like recent documents and application progress.

The custom dock is commonly used to add shortcuts to tasks the user wouldn’t want to open the whole app window for.

Dock menu of Terminal.app:

Dock Menu

To set your custom dock menu, you need to use the app.dock.setMenu API, which is only available on macOS.

Example

Starting with a working application from the Quick Start Guide, update the main.js file with the following lines:

  • index.html
  • main.js
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Hello World!</title>
  6. <meta http-equiv="Content-Security-Policy" content="script-src 'self' 'unsafe-inline';" />
  7. </head>
  8. <body>
  9. <h1>Hello World!</h1>
  10. <p>Right click the dock icon to see the custom menu options.</p>
  11. </body>
  12. </html>
  1. const { app, BrowserWindow, Menu } = require('electron')
  2. function createWindow () {
  3. const win = new BrowserWindow({
  4. width: 800,
  5. height: 600,
  6. })
  7. win.loadFile('index.html')
  8. }
  9. const dockMenu = Menu.buildFromTemplate([
  10. {
  11. label: 'New Window',
  12. click () { console.log('New Window') }
  13. }, {
  14. label: 'New Window with Settings',
  15. submenu: [
  16. { label: 'Basic' },
  17. { label: 'Pro' }
  18. ]
  19. },
  20. { label: 'New Command...' }
  21. ])
  22. app.whenReady().then(() => {
  23. if (process.platform === 'darwin') {
  24. app.dock.setMenu(dockMenu)
  25. }
  26. }).then(createWindow)
  27. app.on('window-all-closed', () => {
  28. if (process.platform !== 'darwin') {
  29. app.quit()
  30. }
  31. })
  32. app.on('activate', () => {
  33. if (BrowserWindow.getAllWindows().length === 0) {
  34. createWindow()
  35. }
  36. })

Open in Fiddle

After launching the Electron application, right click the application icon. You should see the custom menu you just defined:

macOS dock menu