This repository has been archived on 2018-10-12. You can view files and clone it, but cannot push or open issues or pull requests.
image-juicer/app/menu.js

186 lines
5.2 KiB
JavaScript
Raw Normal View History

import { app, Menu, shell, BrowserWindow } from 'electron';
export default class MenuBuilder {
2017-04-06 15:23:47 -04:00
mainWindow: BrowserWindow;
2017-04-06 15:23:47 -04:00
constructor(mainWindow: BrowserWindow) {
this.mainWindow = mainWindow;
}
2017-04-06 15:23:47 -04:00
buildMenu() {
if (process.env.NODE_ENV === 'development') {
this.setupDevelopmentEnvironment();
}
2017-04-06 15:23:47 -04:00
let template;
2017-04-06 15:23:47 -04:00
if (process.platform === 'darwin') {
template = this.buildDarwinTemplate();
} else {
template = this.buildDefaultTemplate();
}
2017-04-06 15:23:47 -04:00
const menu = Menu.buildFromTemplate(template);
Menu.setApplicationMenu(menu);
2017-04-06 15:23:47 -04:00
return menu;
}
2017-04-06 15:23:47 -04:00
setupDevelopmentEnvironment() {
this.mainWindow.openDevTools();
this.mainWindow.webContents.on('context-menu', (e, props) => {
const { x, y } = props;
2017-04-06 15:23:47 -04:00
Menu
.buildFromTemplate([{
2017-04-06 15:23:47 -04:00
label: 'Inspect element',
click: () => {
this.mainWindow.inspectElement(x, y);
}
}])
.popup(this.mainWindow);
2017-04-06 15:23:47 -04:00
});
}
2017-04-06 15:23:47 -04:00
buildDarwinTemplate() {
const subMenuAbout = {
label: 'Electron',
submenu: [
{ label: 'About ElectronReact', selector: 'orderFrontStandardAboutPanel:' },
{ type: 'separator' },
{ label: 'Services', submenu: [] },
{ type: 'separator' },
{ label: 'Hide ElectronReact', accelerator: 'Command+H', selector: 'hide:' },
{ label: 'Hide Others', accelerator: 'Command+Shift+H', selector: 'hideOtherApplications:' },
{ label: 'Show All', selector: 'unhideAllApplications:' },
{ type: 'separator' },
{ label: 'Quit', accelerator: 'Command+Q', click: () => { app.quit(); } }
2017-04-06 15:23:47 -04:00
]
};
const subMenuEdit = {
label: 'Edit',
submenu: [
{ label: 'Undo', accelerator: 'Command+Z', selector: 'undo:' },
{ label: 'Redo', accelerator: 'Shift+Command+Z', selector: 'redo:' },
{ type: 'separator' },
{ label: 'Cut', accelerator: 'Command+X', selector: 'cut:' },
{ label: 'Copy', accelerator: 'Command+C', selector: 'copy:' },
{ label: 'Paste', accelerator: 'Command+V', selector: 'paste:' },
{ label: 'Select All', accelerator: 'Command+A', selector: 'selectAll:' }
2017-04-06 15:23:47 -04:00
]
};
const subMenuViewDev = {
label: 'View',
submenu: [
{ label: 'Reload', accelerator: 'Command+R', click: () => { this.mainWindow.webContents.reload(); } },
{ label: 'Toggle Full Screen', accelerator: 'Ctrl+Command+F', click: () => { this.mainWindow.setFullScreen(!this.mainWindow.isFullScreen()); } },
{ label: 'Toggle Developer Tools', accelerator: 'Alt+Command+I', click: () => { this.mainWindow.toggleDevTools(); } }
2017-04-06 15:23:47 -04:00
]
};
const subMenuViewProd = {
label: 'View',
submenu: [
{ label: 'Toggle Full Screen', accelerator: 'Ctrl+Command+F', click: () => { this.mainWindow.setFullScreen(!this.mainWindow.isFullScreen()); } }
2017-04-06 15:23:47 -04:00
]
};
const subMenuWindow = {
label: 'Window',
submenu: [
{ label: 'Minimize', accelerator: 'Command+M', selector: 'performMiniaturize:' },
{ label: 'Close', accelerator: 'Command+W', selector: 'performClose:' },
{ type: 'separator' },
{ label: 'Bring All to Front', selector: 'arrangeInFront:' }
2017-04-06 15:23:47 -04:00
]
};
const subMenuHelp = {
label: 'Help',
submenu: [
{ label: 'Learn More', click() { shell.openExternal('http://electron.atom.io'); } },
{ label: 'Documentation', click() { shell.openExternal('https://github.com/atom/electron/tree/master/docs#readme'); } },
{ label: 'Community Discussions', click() { shell.openExternal('https://discuss.atom.io/c/electron'); } },
{ label: 'Search Issues', click() { shell.openExternal('https://github.com/atom/electron/issues'); } }
2017-04-06 15:23:47 -04:00
]
};
2017-04-06 15:23:47 -04:00
const subMenuView = process.env.NODE_ENV === 'development'
? subMenuViewDev
: subMenuViewProd;
2017-04-06 15:23:47 -04:00
return [
subMenuAbout,
subMenuEdit,
subMenuView,
subMenuWindow,
subMenuHelp
];
}
2017-04-06 15:23:47 -04:00
buildDefaultTemplate() {
const templateDefault = [{
label: '&File',
submenu: [{
label: '&Open',
accelerator: 'Ctrl+O'
}, {
label: '&Close',
accelerator: 'Ctrl+W',
click: () => {
this.mainWindow.close();
}
}]
}, {
label: '&View',
submenu: (process.env.NODE_ENV === 'development') ? [{
label: '&Reload',
accelerator: 'Ctrl+R',
click: () => {
this.mainWindow.webContents.reload();
}
}, {
label: 'Toggle &Full Screen',
accelerator: 'F11',
click: () => {
this.mainWindow.setFullScreen(!this.mainWindow.isFullScreen());
}
}, {
label: 'Toggle &Developer Tools',
accelerator: 'Alt+Ctrl+I',
click: () => {
this.mainWindow.toggleDevTools();
}
}] : [{
label: 'Toggle &Full Screen',
accelerator: 'F11',
click: () => {
this.mainWindow.setFullScreen(!this.mainWindow.isFullScreen());
}
}]
}, {
label: 'Help',
submenu: [{
label: 'Learn More',
click() {
shell.openExternal('http://electron.atom.io');
}
}, {
label: 'Documentation',
click() {
shell.openExternal('https://github.com/atom/electron/tree/master/docs#readme');
}
}, {
label: 'Community Discussions',
click() {
shell.openExternal('https://discuss.atom.io/c/electron');
}
}, {
label: 'Search Issues',
click() {
shell.openExternal('https://github.com/atom/electron/issues');
}
}]
}];
2017-04-06 15:23:47 -04:00
return templateDefault;
}
}