Skip to content
This repository was archived by the owner on Jun 5, 2019. It is now read-only.

Commit b3bfedd

Browse files
authored
Adds an app menu. (#18)
1 parent e9dc305 commit b3bfedd

File tree

10 files changed

+94
-3
lines changed

10 files changed

+94
-3
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,5 +96,5 @@
9696
"postinstall": "Used by electron-builder to build native dependencies.",
9797
"start": "Starts the app in dev mode."
9898
},
99-
"version": "0.8.0"
99+
"version": "0.9.0"
100100
}

src/main/main.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { createWindow } from './create-window'
33
import * as log from 'electron-log'
44
import * as isDev from 'electron-is-dev'
55
import { setupAutoUpdates } from './auto-updater'
6+
import { setupMenu } from './menu/menu'
67

78
// set proper logging level
89
log.transports.file.level = isDev ? false : 'info'
@@ -14,11 +15,12 @@ const appPath = app.getAppPath()
1415

1516
// fires when Electron is ready to start
1617
app.on('ready', () => {
17-
createWindow(appPath)
18+
const window = createWindow(appPath)
19+
setupMenu(window)
1820
})
1921

2022
// fires when all windows are closed
2123
app.on('window-all-closed', app.quit)
2224

2325
// setup the auto-updater
24-
setupAutoUpdates(app)
26+
setupAutoUpdates(app)

src/main/menu/linux-menu.ts

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { visit } from './shared-menu'
2+
3+
const fileMenu: Electron.MenuItemConstructorOptions = {
4+
label: '&File',
5+
submenu: []
6+
}
7+
8+
const helpMenu: Electron.MenuItemConstructorOptions = {
9+
label: 'Help',
10+
submenu: [visit]
11+
}
12+
13+
export const linuxMenu: Electron.MenuItemConstructorOptions[] = [fileMenu, helpMenu]

src/main/menu/macos-menu.ts

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { app } from 'electron'
2+
import { visit } from './shared-menu'
3+
4+
const name: string = app.getName()
5+
6+
const appMenu: Electron.MenuItemConstructorOptions = {
7+
label: name,
8+
submenu: [
9+
{ label: `About ${name}`, role: 'orderFrontStandardAboutPanel' },
10+
{ type: 'separator' },
11+
{ label: `Hide ${name}`, accelerator: 'Command+H', role: 'hide' },
12+
{ label: 'Hide Others', accelerator: 'Command+Option+H', role: 'hideOtherApplications' },
13+
{ label: 'Show All', role: 'unhideAllApplications' },
14+
{ type: 'separator' },
15+
{ label: 'Quit', accelerator: 'Command+Q', role: 'quit' }
16+
]
17+
}
18+
19+
const helpMenu: Electron.MenuItemConstructorOptions = {
20+
label: 'Help',
21+
submenu: [visit]
22+
}
23+
24+
export const macMenu: Electron.MenuItemConstructorOptions[] = [appMenu, helpMenu]

src/main/menu/menu.ts

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { Menu } from 'electron'
2+
import { macMenu } from './macos-menu'
3+
import { linuxMenu } from './linux-menu'
4+
import { windowsMenu } from './windows-menu'
5+
import { isMac, isLinux, isWindows } from '../../shared'
6+
7+
export function setupMenu(window: Electron.BrowserWindow) {
8+
if (isMac()) {
9+
const menu = Menu.buildFromTemplate(macMenu)
10+
Menu.setApplicationMenu(menu)
11+
} else if (isLinux()) {
12+
const menu = Menu.buildFromTemplate(linuxMenu)
13+
window.setMenu(menu)
14+
} else if (isWindows()) {
15+
const menu = Menu.buildFromTemplate(windowsMenu)
16+
window.setMenu(menu)
17+
}
18+
}

src/main/menu/shared-menu.ts

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { shell } from 'electron'
2+
3+
export const visit: Electron.MenuItemConstructorOptions = {
4+
label: 'Visit on Github',
5+
click() {
6+
shell.openExternal('https://github.com/skellock/electron-starter')
7+
}
8+
}

src/main/menu/windows-menu.ts

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { visit } from './shared-menu'
2+
3+
const fileMenu: Electron.MenuItemConstructorOptions = {
4+
label: '&File',
5+
submenu: []
6+
}
7+
8+
const helpMenu: Electron.MenuItemConstructorOptions = {
9+
label: 'Help',
10+
submenu: [visit]
11+
}
12+
13+
export const windowsMenu: Electron.MenuItemConstructorOptions[] = [fileMenu, helpMenu]

src/shared/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './utils'

src/shared/utils/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './platform/platform'

src/shared/utils/platform/platform.ts

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export function isLinux() {
2+
return process.platform.includes('linux')
3+
}
4+
5+
export function isWindows() {
6+
return process.platform.includes('win')
7+
}
8+
9+
export function isMac() {
10+
return process.platform === 'darwin'
11+
}

0 commit comments

Comments
 (0)