Skip to content

Commit

Permalink
feat: add fileMenu / viewMenu / appMenu roles
Browse files Browse the repository at this point in the history
  • Loading branch information
miniak committed Jan 9, 2019
1 parent 000be5d commit 59be8f5
Show file tree
Hide file tree
Showing 6 changed files with 271 additions and 263 deletions.
187 changes: 37 additions & 150 deletions default_app/menu.js
@@ -1,164 +1,51 @@
const { shell, Menu } = require('electron')

const isMac = process.platform === 'darwin'

const setDefaultApplicationMenu = () => {
if (Menu.getApplicationMenu()) return

const template = [
{
label: 'Edit',
submenu: [
{
role: 'undo'
},
{
role: 'redo'
},
{
type: 'separator'
},
{
role: 'cut'
},
{
role: 'copy'
},
{
role: 'paste'
},
{
role: 'pasteandmatchstyle'
},
{
role: 'delete'
},
{
role: 'selectall'
const helpMenu = {
role: 'help',
submenu: [
{
label: 'Learn More',
click () {
shell.openExternal('https://electronjs.org')
}
]
},
{
label: 'View',
submenu: [
{
role: 'reload'
},
{
role: 'forcereload'
},
{
role: 'toggledevtools'
},
{
type: 'separator'
},
{
role: 'resetzoom'
},
{
role: 'zoomin'
},
{
role: 'zoomout'
},
{
type: 'separator'
},
{
role: 'togglefullscreen'
},
{
label: 'Documentation',
click () {
shell.openExternal(
`https://github.com/electron/electron/tree/v${process.versions.electron}/docs#readme`
)
}
]
},
{
role: 'windowMenu'
},
{
role: 'help',
submenu: [
{
label: 'Learn More',
click () {
shell.openExternal('https://electronjs.org')
}
},
{
label: 'Documentation',
click () {
shell.openExternal(
`https://github.com/electron/electron/tree/v${process.versions.electron}/docs#readme`
)
}
},
{
label: 'Community Discussions',
click () {
shell.openExternal('https://discuss.atom.io/c/electron')
}
},
{
label: 'Search Issues',
click () {
shell.openExternal('https://github.com/electron/electron/issues')
}
}
]
}
]

if (process.platform === 'darwin') {
template.unshift({
label: 'Electron',
submenu: [
{
role: 'about'
},
{
type: 'separator'
},
{
role: 'services'
},
{
type: 'separator'
},
{
role: 'hide'
},
{
role: 'hideothers'
},
{
role: 'unhide'
},
{
type: 'separator'
},
{
role: 'quit'
},
{
label: 'Community Discussions',
click () {
shell.openExternal('https://discuss.atom.io/c/electron')
}
]
})
template[1].submenu.push({
type: 'separator'
}, {
label: 'Speech',
submenu: [
{
role: 'startspeaking'
},
{
role: 'stopspeaking'
},
{
label: 'Search Issues',
click () {
shell.openExternal('https://github.com/electron/electron/issues')
}
]
})
} else {
template.unshift({
label: 'File',
submenu: [{
role: 'quit'
}]
})
}
]
}

const template = [
...(isMac ? [{ role: 'appMenu' }] : []),
{ role: 'fileMenu' },
{ role: 'editMenu' },
{ role: 'viewMenu' },
{ role: 'windowMenu' },
helpMenu
]

const menu = Menu.buildFromTemplate(template)
Menu.setApplicationMenu(menu)
}
Expand Down
3 changes: 3 additions & 0 deletions docs/api/menu-item.md
Expand Up @@ -82,11 +82,14 @@ The `role` property can have following values:
* `resetZoom` - Reset the focused page's zoom level to the original size.
* `zoomIn` - Zoom in the focused page by 10%.
* `zoomOut` - Zoom out the focused page by 10%.
* `fileMenu` - Whole default "File" menu (Close / Quit)
* `editMenu` - Whole default "Edit" menu (Undo, Copy, etc.).
* `viewMenu` - Whole default "View" menu (Reload, Toggle Developer Tools, etc.)
* `windowMenu` - Whole default "Window" menu (Minimize, Close, etc.).

The following additional roles are available on _macOS_:

* `appMenu` - Whole default "App" menu (About, Services, etc.)
* `about` - Map to the `orderFrontStandardAboutPanel` action.
* `hide` - Map to the `hide` action.
* `hideOthers` - Map to the `hideOtherApplications` action.
Expand Down
95 changes: 52 additions & 43 deletions docs/api/menu.md
Expand Up @@ -158,6 +158,29 @@ simple template API:
const { app, Menu } = require('electron')

const template = [
// { role: 'appMenu' }
...(process.platform === 'darwin' ? [{
label: app.getName(),
submenu: [
{ role: 'about' },
{ type: 'separator' },
{ role: 'services' },
{ type: 'separator' },
{ role: 'hide' },
{ role: 'hideothers' },
{ role: 'unhide' },
{ type: 'separator' },
{ role: 'quit' }
]
}] : []),
// { role: 'fileMenu' }
{
label: 'File',
submenu: [
isMac ? { role: 'close' } : { role: 'quit' }
]
},
// { role: 'editMenu' }
{
label: 'Edit',
submenu: [
Expand All @@ -167,11 +190,26 @@ const template = [
{ role: 'cut' },
{ role: 'copy' },
{ role: 'paste' },
{ role: 'pasteandmatchstyle' },
{ role: 'delete' },
{ role: 'selectall' }
...(isMac ? [
{ role: 'pasteAndMatchStyle' },
{ role: 'delete' },
{ role: 'selectAll' },
{ type: 'separator' },
{
label: 'Speech',
submenu: [
{ role: 'startspeaking' },
{ role: 'stopspeaking' }
]
}
] : [
{ role: 'delete' },
{ type: 'separator' },
{ role: 'selectAll' }
])
]
},
// { role: 'viewMenu' }
{
label: 'View',
submenu: [
Expand All @@ -186,11 +224,20 @@ const template = [
{ role: 'togglefullscreen' }
]
},
// { role: 'windowMenu' }
{
role: 'window',
label: 'Window',
submenu: [
{ role: 'minimize' },
{ role: 'close' }
{ role: 'zoom' },
...(isMac ? [
{ type: 'separator' },
{ role: 'front' },
{ type: 'separator' },
{ role: 'window' }
] : [
{ role: 'close' }
])
]
},
{
Expand All @@ -204,44 +251,6 @@ const template = [
}
]

if (process.platform === 'darwin') {
template.unshift({
label: app.getName(),
submenu: [
{ role: 'about' },
{ type: 'separator' },
{ role: 'services' },
{ type: 'separator' },
{ role: 'hide' },
{ role: 'hideothers' },
{ role: 'unhide' },
{ type: 'separator' },
{ role: 'quit' }
]
})

// Edit menu
template[1].submenu.push(
{ type: 'separator' },
{
label: 'Speech',
submenu: [
{ role: 'startspeaking' },
{ role: 'stopspeaking' }
]
}
)

// Window menu
template[3].submenu = [
{ role: 'close' },
{ role: 'minimize' },
{ role: 'zoom' },
{ type: 'separator' },
{ role: 'front' }
]
}

const menu = Menu.buildFromTemplate(template)
Menu.setApplicationMenu(menu)
```
Expand Down

0 comments on commit 59be8f5

Please sign in to comment.