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

Commit dd7f6f9

Browse files
authored
Composes the menus a little better. Not much better. (#20)
1 parent 0769bd7 commit dd7f6f9

File tree

5 files changed

+143
-49
lines changed

5 files changed

+143
-49
lines changed

src/main/menu/linux-menu.ts

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,30 @@
1-
import { visit } from './shared-menu'
1+
import { createSharedMenuItems } from './shared-menu'
2+
import * as isDev from 'electron-is-dev'
23

3-
const fileMenu: Electron.MenuItemConstructorOptions = {
4-
label: '&File',
5-
submenu: []
6-
}
4+
export function createLinuxMenu(
5+
window: Electron.BrowserWindow
6+
): Electron.MenuItemConstructorOptions[] {
7+
const shared = createSharedMenuItems(window)
78

8-
const helpMenu: Electron.MenuItemConstructorOptions = {
9-
label: 'Help',
10-
submenu: [visit]
11-
}
9+
const fileMenu: Electron.MenuItemConstructorOptions = {
10+
label: '&File',
11+
submenu: [{ ...shared.quit, accelerator: 'Alt+F4' }]
12+
}
13+
14+
const viewMenu: Electron.MenuItemConstructorOptions = {
15+
label: 'View',
16+
submenu: isDev
17+
? [
18+
{ ...shared.reload, accelerator: 'Ctrl+R' },
19+
{ ...shared.toggleDevTools, accelerator: 'Ctrl+Alt+I' }
20+
]
21+
: [{ ...shared.fullScreen, accelerator: 'Ctrl+Alt+F' }]
22+
}
1223

13-
export const linuxMenu: Electron.MenuItemConstructorOptions[] = [fileMenu, helpMenu]
24+
const helpMenu: Electron.MenuItemConstructorOptions = {
25+
label: 'Help',
26+
submenu: [shared.visit]
27+
}
28+
29+
return [fileMenu, viewMenu, helpMenu]
30+
}

src/main/menu/macos-menu.ts

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,40 @@
11
import { app } from 'electron'
2-
import { visit } from './shared-menu'
2+
import { createSharedMenuItems } from './shared-menu'
3+
import * as isDev from 'electron-is-dev'
34

4-
const name: string = app.getName()
5+
export function createMacMenu(
6+
window: Electron.BrowserWindow
7+
): Electron.MenuItemConstructorOptions[] {
8+
const shared = createSharedMenuItems(window)
9+
const name: string = app.getName()
510

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-
}
11+
const appMenu: Electron.MenuItemConstructorOptions = {
12+
label: name,
13+
submenu: [
14+
{ label: `About ${name}`, role: 'orderFrontStandardAboutPanel' },
15+
{ type: 'separator' },
16+
{ label: `Hide ${name}`, accelerator: 'Command+H', role: 'hide' },
17+
{ label: 'Hide Others', accelerator: 'Command+Option+H', role: 'hideOtherApplications' },
18+
{ label: 'Show All', role: 'unhideAllApplications' },
19+
{ type: 'separator' },
20+
{ ...shared.quit, accelerator: 'Command+Q' }
21+
]
22+
}
1823

19-
const helpMenu: Electron.MenuItemConstructorOptions = {
20-
label: 'Help',
21-
submenu: [visit]
22-
}
24+
const viewMenu: Electron.MenuItemConstructorOptions = {
25+
label: 'View',
26+
submenu: isDev
27+
? [
28+
{ ...shared.reload, accelerator: 'Command+R' },
29+
{ ...shared.toggleDevTools, accelerator: 'Alt+Command+I' }
30+
]
31+
: [{ ...shared.fullScreen, accelerator: 'Ctrl+Command+F' }]
32+
}
2333

24-
export const macMenu: Electron.MenuItemConstructorOptions[] = [appMenu, helpMenu]
34+
const helpMenu: Electron.MenuItemConstructorOptions = {
35+
label: 'Help',
36+
submenu: [shared.visit]
37+
}
38+
39+
return [appMenu, viewMenu, helpMenu]
40+
}

src/main/menu/menu.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,29 @@
11
import { Menu } from 'electron'
2-
import { macMenu } from './macos-menu'
3-
import { linuxMenu } from './linux-menu'
4-
import { windowsMenu } from './windows-menu'
2+
import { createMacMenu } from './macos-menu'
3+
import { createLinuxMenu } from './linux-menu'
4+
import { createWindowsMenu } from './windows-menu'
55
import { isMac, isLinux, isWindows } from '../../shared'
66

7+
/**
8+
* Attaches the menu to the appropriate place.
9+
*
10+
* @param window The main window.
11+
*/
712
export function setupMenu(window: Electron.BrowserWindow) {
813
if (isMac()) {
9-
const menu = Menu.buildFromTemplate(macMenu)
14+
// on mac, the menu goes on the app
15+
const template = createMacMenu(window)
16+
const menu = Menu.buildFromTemplate(template)
1017
Menu.setApplicationMenu(menu)
1118
} else if (isLinux()) {
12-
const menu = Menu.buildFromTemplate(linuxMenu)
19+
// on linux, the menu goes on the window
20+
const template = createLinuxMenu(window)
21+
const menu = Menu.buildFromTemplate(template)
1322
window.setMenu(menu)
1423
} else if (isWindows()) {
15-
const menu = Menu.buildFromTemplate(windowsMenu)
24+
// on windows, the menu goes on the window
25+
const template = createWindowsMenu(window)
26+
const menu = Menu.buildFromTemplate(template)
1627
window.setMenu(menu)
1728
}
1829
}

src/main/menu/shared-menu.ts

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,41 @@
11
import { shell } from 'electron'
22

3-
export const visit: Electron.MenuItemConstructorOptions = {
4-
label: 'Visit on Github',
5-
click() {
6-
shell.openExternal('https://github.com/skellock/electron-starter')
3+
export function createSharedMenuItems(window: Electron.BrowserWindow) {
4+
const visit: Electron.MenuItemConstructorOptions = {
5+
label: 'Visit on Github',
6+
click() {
7+
shell.openExternal('https://github.com/skellock/electron-starter')
8+
}
9+
}
10+
11+
const reload: Electron.MenuItemConstructorOptions = {
12+
label: 'Reload',
13+
click() {
14+
window.webContents.reload()
15+
}
16+
}
17+
18+
const quit: Electron.MenuItemConstructorOptions = { label: 'Quit', role: 'quit' }
19+
20+
const toggleDevTools: Electron.MenuItemConstructorOptions = {
21+
label: 'Toggle Developer Tools',
22+
click() {
23+
window.webContents.toggleDevTools()
24+
}
25+
}
26+
27+
const fullScreen: Electron.MenuItemConstructorOptions = {
28+
label: 'Toggle Full Screen',
29+
click() {
30+
window.setFullScreen(!window.isFullScreen())
31+
}
32+
}
33+
34+
return {
35+
visit,
36+
reload,
37+
quit,
38+
toggleDevTools,
39+
fullScreen
740
}
841
}

src/main/menu/windows-menu.ts

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,30 @@
1-
import { visit } from './shared-menu'
1+
import { createSharedMenuItems } from './shared-menu'
2+
import * as isDev from 'electron-is-dev'
23

3-
const fileMenu: Electron.MenuItemConstructorOptions = {
4-
label: '&File',
5-
submenu: []
6-
}
4+
export function createWindowsMenu(
5+
window: Electron.BrowserWindow
6+
): Electron.MenuItemConstructorOptions[] {
7+
const shared = createSharedMenuItems(window)
78

8-
const helpMenu: Electron.MenuItemConstructorOptions = {
9-
label: 'Help',
10-
submenu: [visit]
11-
}
9+
const fileMenu: Electron.MenuItemConstructorOptions = {
10+
label: '&File',
11+
submenu: [{ ...shared.quit, accelerator: 'Alt+F4' }]
12+
}
13+
14+
const viewMenu: Electron.MenuItemConstructorOptions = {
15+
label: 'View',
16+
submenu: isDev
17+
? [
18+
{ ...shared.reload, accelerator: 'Ctrl+R' },
19+
{ ...shared.toggleDevTools, accelerator: 'Ctrl+Alt+I' }
20+
]
21+
: [{ ...shared.fullScreen, accelerator: 'Ctrl+Alt+F' }]
22+
}
1223

13-
export const windowsMenu: Electron.MenuItemConstructorOptions[] = [fileMenu, helpMenu]
24+
const helpMenu: Electron.MenuItemConstructorOptions = {
25+
label: 'Help',
26+
submenu: [shared.visit]
27+
}
28+
29+
return [fileMenu, viewMenu, helpMenu]
30+
}

0 commit comments

Comments
 (0)