2016-02-24 00:19:52 +01:00
|
|
|
import {Menu, shell, clipboard, dialog} from 'electron';
|
2016-01-19 12:53:10 +01:00
|
|
|
|
2016-01-23 03:09:47 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {string} nativefierVersion
|
2016-01-23 08:12:53 +01:00
|
|
|
* @param {function} onQuit should be from app.quit
|
2016-01-23 03:09:47 +01:00
|
|
|
* @param {function} onZoomIn
|
|
|
|
* @param {function} onZoomOut
|
2016-02-24 15:45:22 +01:00
|
|
|
* @param {{}}} mainWindow
|
|
|
|
* @param {{}}} options
|
2016-01-23 03:09:47 +01:00
|
|
|
*/
|
2016-02-24 15:45:22 +01:00
|
|
|
function createMenu(nativefierVersion, onQuit, onZoomIn, onZoomOut, mainWindow, options) {
|
2016-01-23 19:02:23 +01:00
|
|
|
if (Menu.getApplicationMenu()) {
|
2016-01-19 12:53:10 +01:00
|
|
|
return;
|
2016-01-23 19:02:23 +01:00
|
|
|
}
|
2016-01-19 12:53:10 +01:00
|
|
|
|
2016-01-29 15:04:41 +01:00
|
|
|
const template = [
|
2016-01-19 12:53:10 +01:00
|
|
|
{
|
|
|
|
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'
|
|
|
|
},
|
2016-01-25 11:34:44 +01:00
|
|
|
{
|
|
|
|
label: 'Copy Current URL',
|
|
|
|
accelerator: 'CmdOrCtrl+C',
|
|
|
|
click: () => {
|
2016-02-24 15:45:22 +01:00
|
|
|
const currentURL = mainWindow.webContents.getURL();
|
2016-01-25 11:34:44 +01:00
|
|
|
clipboard.writeText(currentURL);
|
|
|
|
}
|
|
|
|
},
|
2016-01-19 12:53:10 +01:00
|
|
|
{
|
|
|
|
label: 'Paste',
|
|
|
|
accelerator: 'CmdOrCtrl+V',
|
|
|
|
role: 'paste'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: 'Select All',
|
|
|
|
accelerator: 'CmdOrCtrl+A',
|
|
|
|
role: 'selectall'
|
2016-01-23 19:02:23 +01:00
|
|
|
}
|
2016-01-19 12:53:10 +01:00
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: 'View',
|
|
|
|
submenu: [
|
2016-01-23 07:15:25 +01:00
|
|
|
{
|
|
|
|
label: 'Back',
|
2016-01-26 07:55:02 +01:00
|
|
|
accelerator: 'CmdOrCtrl+[',
|
2016-01-29 15:04:41 +01:00
|
|
|
click: () => {
|
2016-02-24 15:45:22 +01:00
|
|
|
mainWindow.webContents.goBack();
|
2016-01-23 07:15:25 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: 'Forward',
|
2016-01-26 07:55:02 +01:00
|
|
|
accelerator: 'CmdOrCtrl+]',
|
2016-01-29 15:04:41 +01:00
|
|
|
click: () => {
|
2016-02-24 15:45:22 +01:00
|
|
|
mainWindow.webContents.goForward();
|
2016-01-23 07:15:25 +01:00
|
|
|
}
|
|
|
|
},
|
2016-01-19 12:53:10 +01:00
|
|
|
{
|
|
|
|
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) {
|
2016-01-19 12:53:10 +01:00
|
|
|
focusedWindow.reload();
|
2016-01-23 19:02:23 +01:00
|
|
|
}
|
2016-01-19 12:53:10 +01:00
|
|
|
}
|
|
|
|
},
|
2016-01-23 07:15:25 +01:00
|
|
|
{
|
|
|
|
type: 'separator'
|
|
|
|
},
|
2016-01-19 12:53:10 +01:00
|
|
|
{
|
|
|
|
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') {
|
2016-01-19 12:53:10 +01:00
|
|
|
return 'Ctrl+Command+F';
|
2016-01-23 19:02:23 +01:00
|
|
|
}
|
|
|
|
return 'F11';
|
2016-01-19 12:53:10 +01:00
|
|
|
})(),
|
2016-01-29 15:04:41 +01:00
|
|
|
click: (item, focusedWindow) => {
|
2016-01-23 19:02:23 +01:00
|
|
|
if (focusedWindow) {
|
2016-01-19 12:53:10 +01:00
|
|
|
focusedWindow.setFullScreen(!focusedWindow.isFullScreen());
|
2016-01-23 19:02:23 +01:00
|
|
|
}
|
2016-01-19 12:53:10 +01:00
|
|
|
}
|
|
|
|
},
|
2016-01-23 02:27:09 +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') {
|
2016-01-23 02:27:09 +01:00
|
|
|
return 'Command+=';
|
2016-01-23 19:02:23 +01:00
|
|
|
}
|
|
|
|
return 'Ctrl+=';
|
2016-01-23 02:27:09 +01:00
|
|
|
})(),
|
2016-01-29 15:04:41 +01:00
|
|
|
click: () => {
|
2016-01-23 02:27:09 +01:00
|
|
|
onZoomIn();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: 'Zoom Out',
|
2016-01-29 15:04:41 +01:00
|
|
|
accelerator: (() => {
|
2016-01-23 19:02:23 +01:00
|
|
|
if (process.platform === 'darwin') {
|
2016-01-23 02:27:09 +01:00
|
|
|
return 'Command+-';
|
2016-01-23 19:02:23 +01:00
|
|
|
}
|
|
|
|
return 'Ctrl+-';
|
2016-01-23 02:27:09 +01:00
|
|
|
})(),
|
2016-01-29 15:04:41 +01:00
|
|
|
click: () => {
|
2016-01-23 02:27:09 +01:00
|
|
|
onZoomOut();
|
|
|
|
}
|
2016-02-24 00:19:52 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
label: 'Clear App Data',
|
2016-02-24 15:45:22 +01:00
|
|
|
click: () => {
|
|
|
|
dialog.showMessageBox(mainWindow, {
|
|
|
|
type: 'warning',
|
|
|
|
buttons: ['Yes', 'Cancel'],
|
|
|
|
defaultId: 1,
|
|
|
|
title: 'Clear cache confirmation',
|
|
|
|
message: 'This will clear all data (cookies, local storage etc) from this app. Are you sure you wish to proceed?'
|
|
|
|
}, response => {
|
|
|
|
if (response === 0) {
|
|
|
|
mainWindow.webContents.session.clearStorageData({},
|
|
|
|
() => {
|
|
|
|
mainWindow.webContents.session.clearCache(() => {
|
|
|
|
mainWindow.loadURL(options.targetUrl);
|
2016-02-24 00:19:52 +01:00
|
|
|
});
|
2016-02-24 15:45:22 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2016-02-24 00:19:52 +01:00
|
|
|
}
|
2016-01-23 02:27:09 +01:00
|
|
|
},
|
2016-01-19 12:53:10 +01:00
|
|
|
{
|
|
|
|
label: 'Toggle Window Developer Tools',
|
2016-01-29 15:04:41 +01:00
|
|
|
accelerator: (() => {
|
2016-01-23 19:02:23 +01:00
|
|
|
if (process.platform === 'darwin') {
|
2016-01-19 12:53:10 +01:00
|
|
|
return 'Alt+Command+I';
|
2016-01-23 19:02:23 +01:00
|
|
|
}
|
|
|
|
return 'Ctrl+Shift+I';
|
2016-01-19 12:53:10 +01:00
|
|
|
})(),
|
2016-01-29 15:04:41 +01:00
|
|
|
click: (item, focusedWindow) => {
|
2016-01-23 19:02:23 +01:00
|
|
|
if (focusedWindow) {
|
2016-01-19 12:53:10 +01:00
|
|
|
focusedWindow.toggleDevTools();
|
2016-01-23 19:02:23 +01:00
|
|
|
}
|
2016-01-19 12:53:10 +01:00
|
|
|
}
|
2016-01-21 18:32:21 +01:00
|
|
|
}
|
2016-01-19 12:53:10 +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
|
|
|
}
|
2016-01-19 12:53:10 +01:00
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: 'Help',
|
|
|
|
role: 'help',
|
|
|
|
submenu: [
|
|
|
|
{
|
2016-01-21 06:42:27 +01:00
|
|
|
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');
|
2016-01-23 03:09:47 +01:00
|
|
|
}
|
2016-01-19 12:53:10 +01:00
|
|
|
},
|
|
|
|
{
|
2016-01-19 13:33:25 +01:00
|
|
|
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 03:09:47 +01:00
|
|
|
}
|
2016-01-19 12:53:10 +01:00
|
|
|
}
|
|
|
|
]
|
2016-01-19 13:33:25 +01:00
|
|
|
}
|
2016-01-19 12:53:10 +01:00
|
|
|
];
|
|
|
|
|
2016-01-23 19:02:23 +01:00
|
|
|
if (process.platform === 'darwin') {
|
2016-01-19 12:53:10 +01:00
|
|
|
template.unshift({
|
|
|
|
label: 'Electron',
|
|
|
|
submenu: [
|
|
|
|
{
|
|
|
|
label: 'Services',
|
|
|
|
role: 'services',
|
|
|
|
submenu: []
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'separator'
|
|
|
|
},
|
|
|
|
{
|
2016-01-21 06:42:27 +01:00
|
|
|
label: 'Hide App',
|
2016-01-19 12:53:10 +01:00
|
|
|
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-01-23 03:09:47 +01:00
|
|
|
onQuit();
|
|
|
|
}
|
2016-01-23 19:02:23 +01:00
|
|
|
}
|
2016-01-19 12:53:10 +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);
|
2016-01-19 12:53:10 +01:00
|
|
|
Menu.setApplicationMenu(menu);
|
2016-01-23 03:09:47 +01:00
|
|
|
}
|
|
|
|
|
2016-01-29 15:04:41 +01:00
|
|
|
export default createMenu;
|