Nativefier/app/src/components/menu/menu.js

244 lines
7.1 KiB
JavaScript
Raw Normal View History

2016-02-25 03:37:06 +01:00
import {Menu, shell, clipboard} from 'electron';
/**
2016-02-25 03:37:06 +01:00
* @param nativefierVersion
* @param appQuit
* @param zoomIn
* @param zoomOut
* @param goBack
* @param goForward
* @param getCurrentUrl
* @param clearAppData
*/
2016-02-25 03:37:06 +01:00
function createMenu({nativefierVersion, appQuit, zoomIn, zoomOut, goBack, goForward, getCurrentUrl, clearAppData}) {
2016-01-23 19:02:23 +01:00
if (Menu.getApplicationMenu()) {
return;
2016-01-23 19:02:23 +01:00
}
2016-01-29 15:04:41 +01:00
const template = [
{
label: 'Edit',
submenu: [
{
label: 'Undo',
accelerator: 'CmdOrCtrl+Z',
role: 'undo'
},
{
label: 'Redo',
accelerator: 'Shift+CmdOrCtrl+Z',
role: 'redo'
},
{
type: 'separator'
},
{
label: 'Cut',
accelerator: 'CmdOrCtrl+X',
role: 'cut'
},
{
label: 'Copy',
accelerator: 'CmdOrCtrl+C',
role: 'copy'
},
{
label: 'Copy Current URL',
accelerator: 'CmdOrCtrl+L',
click: () => {
2016-02-25 03:37:06 +01:00
const currentURL = getCurrentUrl();
clipboard.writeText(currentURL);
}
},
{
label: 'Paste',
accelerator: 'CmdOrCtrl+V',
role: 'paste'
},
{
label: 'Select All',
accelerator: 'CmdOrCtrl+A',
role: 'selectall'
2016-02-25 03:37:06 +01:00
},
{
label: 'Clear App Data',
click: () => {
clearAppData();
}
2016-01-23 19:02:23 +01:00
}
]
},
{
label: 'View',
submenu: [
{
label: 'Back',
accelerator: 'CmdOrCtrl+[',
2016-01-29 15:04:41 +01:00
click: () => {
2016-02-25 03:37:06 +01:00
goBack();
}
},
{
label: 'Forward',
accelerator: 'CmdOrCtrl+]',
2016-01-29 15:04:41 +01:00
click: () => {
2016-02-25 03:37:06 +01:00
goForward();
}
},
{
label: 'Reload',
accelerator: 'CmdOrCtrl+R',
2016-01-29 15:04:41 +01:00
click: (item, focusedWindow) => {
2016-01-23 19:02:23 +01:00
if (focusedWindow) {
focusedWindow.reload();
2016-01-23 19:02:23 +01:00
}
}
},
{
type: 'separator'
},
{
label: 'Toggle Full Screen',
2016-01-29 15:04:41 +01:00
accelerator: (() => {
2016-01-23 19:02:23 +01:00
if (process.platform === 'darwin') {
return 'Ctrl+Command+F';
2016-01-23 19:02:23 +01:00
}
return 'F11';
})(),
2016-01-29 15:04:41 +01:00
click: (item, focusedWindow) => {
2016-01-23 19:02:23 +01:00
if (focusedWindow) {
focusedWindow.setFullScreen(!focusedWindow.isFullScreen());
2016-01-23 19:02:23 +01:00
}
}
},
{
label: 'Zoom In',
2016-01-29 15:04:41 +01:00
accelerator: (() => {
2016-01-23 19:02:23 +01:00
if (process.platform === 'darwin') {
return 'Command+=';
2016-01-23 19:02:23 +01:00
}
return 'Ctrl+=';
})(),
2016-01-29 15:04:41 +01:00
click: () => {
2016-02-25 03:37:06 +01:00
zoomIn();
}
},
{
label: 'Zoom Out',
2016-01-29 15:04:41 +01:00
accelerator: (() => {
2016-01-23 19:02:23 +01:00
if (process.platform === 'darwin') {
return 'Command+-';
2016-01-23 19:02:23 +01:00
}
return 'Ctrl+-';
})(),
2016-01-29 15:04:41 +01:00
click: () => {
2016-02-25 03:37:06 +01:00
zoomOut();
}
},
{
2016-02-25 03:37:06 +01:00
label: 'Toggle Developer Tools',
2016-01-29 15:04:41 +01:00
accelerator: (() => {
2016-01-23 19:02:23 +01:00
if (process.platform === 'darwin') {
return 'Alt+Command+I';
2016-01-23 19:02:23 +01:00
}
return 'Ctrl+Shift+I';
})(),
2016-01-29 15:04:41 +01:00
click: (item, focusedWindow) => {
2016-01-23 19:02:23 +01:00
if (focusedWindow) {
focusedWindow.toggleDevTools();
2016-01-23 19:02:23 +01:00
}
}
}
]
},
{
label: 'Window',
role: 'window',
submenu: [
{
label: 'Minimize',
accelerator: 'CmdOrCtrl+M',
role: 'minimize'
},
{
label: 'Close',
accelerator: 'CmdOrCtrl+W',
role: 'close'
2016-01-23 19:02:23 +01:00
}
]
},
{
label: 'Help',
role: 'help',
submenu: [
{
label: `Built with Nativefier v${nativefierVersion}`,
2016-01-29 15:04:41 +01:00
click: () => {
2016-01-23 19:02:23 +01:00
shell.openExternal('https://github.com/jiahaog/nativefier');
}
},
{
label: 'Report an Issue',
2016-01-29 15:04:41 +01:00
click: () => {
2016-01-23 19:02:23 +01:00
shell.openExternal('https://github.com/jiahaog/nativefier/issues');
}
}
]
}
];
2016-01-23 19:02:23 +01:00
if (process.platform === 'darwin') {
template.unshift({
label: 'Electron',
submenu: [
{
label: 'Services',
role: 'services',
submenu: []
},
{
type: 'separator'
},
{
label: 'Hide App',
accelerator: 'Command+H',
role: 'hide'
},
{
label: 'Hide Others',
accelerator: 'Command+Shift+H',
role: 'hideothers'
},
{
label: 'Show All',
role: 'unhide'
},
{
type: 'separator'
},
{
label: 'Quit',
accelerator: 'Command+Q',
2016-01-29 15:04:41 +01:00
click: () => {
2016-02-25 03:37:06 +01:00
appQuit();
}
2016-01-23 19:02:23 +01:00
}
]
});
template[3].submenu.push(
{
type: 'separator'
},
{
label: 'Bring All to Front',
role: 'front'
}
);
}
2016-01-29 15:04:41 +01:00
const menu = Menu.buildFromTemplate(template);
Menu.setApplicationMenu(menu);
}
2016-01-29 15:04:41 +01:00
export default createMenu;