Skip to content

Commit

Permalink
Refactor menu, add help menu and links to GitHub and README, (fixes #80)
Browse files Browse the repository at this point in the history
  • Loading branch information
maxkueng committed Nov 26, 2017
1 parent f167729 commit e881b78
Showing 1 changed file with 207 additions and 136 deletions.
343 changes: 207 additions & 136 deletions main/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const {
app,
dialog,
protocol,
shell,
Menu,
} = require('electron');
const getStdin = require('get-stdin');
Expand Down Expand Up @@ -140,6 +141,11 @@ function openFileDialog(win, openInNewWindow) {
}
}

function openAboutDialog(win) {
const readmePath = path.resolve(__dirname, '..', 'README.md');
openFileInReader(win, readmePath, true);
}

function registerEmojiProtocol() {
const emojiPath = path.resolve(path.dirname(require.resolve('emojify.js')), '..', 'images', 'basic');

Expand All @@ -161,164 +167,229 @@ function registerEmojiProtocol() {
}

function addApplicationMenu() {
// menu
let vmdSubmenu = [
{
label: 'Close window',
accelerator: 'CmdOrCtrl+W',
role: 'close',
},
{
label: 'Quit',
accelerator: 'CmdOrCtrl+Q',
click() {
app.quit();
},
},
];

if (process.platform === 'darwin') {
vmdSubmenu = [
{
label: 'About vmd',
selector: 'orderFrontStandardAboutPanel:',
},
{
type: 'separator',
},
].concat(vmdSubmenu);
function menuLabel(label) {
if (process.platform === 'darwin') {
return label.replace('&', '');
}
return label;
}
const template = [];

// Doc: https://github.com/atom/electron/blob/master/docs/api/menu-item.md
const template = [
{
if (process.platform === 'darwin') {
template.push({
label: 'vmd',
submenu: vmdSubmenu,
},
{
label: 'File',
submenu: [
{
label: 'Open',
accelerator: 'CmdOrCtrl+O',
click(model, item, win) {
openFileDialog(win, false);
label: 'About vmd',
click() {
openAboutDialog();
},
},
{ type: 'separator' },
{ role: 'hide' },
{ role: 'hideothers' },
{ role: 'unhide' },
{ type: 'separator' },
{
label: 'Open in new window',
accelerator: 'CmdOrCtrl+Shift+O',
click(model, item, win) {
openFileDialog(win, true);
},
label: 'Close window',
accelerator: 'CmdOrCtrl+W',
role: 'close',
},
{
label: 'Print',
accelerator: 'CmdOrCtrl+P',
click(model, item, win) {
if (win) {
win.webContents.send('print');
}
label: 'Quit',
accelerator: 'CmdOrCtrl+Q',
click() {
app.quit();
},
},
],
},
{
label: 'Edit',
submenu: [
{
label: 'Find',
accelerator: 'CmdOrCtrl+F',
click(model, item, win) {
win.webContents.send('find');
},
});
}

const fileMenu = {
label: menuLabel('&File'),
submenu: [
{
label: menuLabel('&Open'),
accelerator: 'CmdOrCtrl+O',
click(model, item, win) {
openFileDialog(win, false);
},
{
label: 'Copy',
accelerator: 'CmdOrCtrl+C',
role: 'copy',
enabled(model) {
return model && !!model.selection;
},
},
{
label: menuLabel('Open in &new window'),
accelerator: 'CmdOrCtrl+Shift+O',
click(model, item, win) {
openFileDialog(win, true);
},
{
label: 'Select All',
accelerator: 'CmdOrCtrl+A',
role: 'selectall',
},
{
label: menuLabel('&Print'),
accelerator: 'CmdOrCtrl+P',
click(model, item, win) {
if (win) {
win.webContents.send('print');
}
},
],
},
{
label: 'History',
submenu: [
{
label: 'Back',
accelerator: 'Alt+Left',
click(model, item, win) {
if (win) {
win.webContents.send('history-back');
}
},
enabled(model) {
return model.history && model.history.canGoBack;
},
},
],
};

if (process.platform !== 'darwin') {
fileMenu.submenu = [].concat(fileMenu.submenu, [
{ type: 'separator' },
{
label: menuLabel('C&lose window'),
accelerator: 'CmdOrCtrl+W',
role: 'close',
},
{
label: menuLabel('&Quit'),
accelerator: 'CmdOrCtrl+Q',
click() {
app.quit();
},
{
label: 'Forward',
accelerator: 'Alt+Right',
click(model, item, win) {
if (win) {
win.webContents.send('history-forward');
}
},
enabled(model) {
return model.history && model.history.canGoForward;
},
},
]);
}

template.push(fileMenu);

template.push({
label: menuLabel('&Edit'),
submenu: [
{
label: menuLabel('&Find'),
accelerator: 'CmdOrCtrl+F',
click(model, item, win) {
win.webContents.send('find');
},
],
},
{
label: 'View',
submenu: [
{
label: 'Zoom In',
accelerator: 'CmdOrCtrl+Plus',
click(model, item, win) {
if (win) {
win.webContents.send('zoom-in');
}
},
},
{
label: menuLabel('&Copy'),
accelerator: 'CmdOrCtrl+C',
role: 'copy',
enabled(model) {
return model && !!model.selection;
},
{
label: 'Zoom Out',
accelerator: 'CmdOrCtrl+-',
click(model, item, win) {
if (win) {
win.webContents.send('zoom-out');
}
},
},
{
label: menuLabel('Select &All'),
accelerator: 'CmdOrCtrl+A',
role: 'selectall',
},
],
});

template.push({
label: menuLabel('&History'),
submenu: [
{
label: menuLabel('&Back'),
accelerator: 'Alt+Left',
click(model, item, win) {
if (win) {
win.webContents.send('history-back');
}
},
{
label: 'Reset Zoom',
accelerator: 'CmdOrCtrl+0',
click(model, item, win) {
if (win) {
win.webContents.send('zoom-reset');
}
},
enabled(model) {
return model.history && model.history.canGoBack;
},
{
label: 'Toggle Developer Tools',
accelerator: process.platform === 'darwin' ? 'Alt+Command+I' : 'Ctrl+Shift+I',
click(model, item, win) {
if (win) {
win.toggleDevTools();
}
},
},
{
label: menuLabel('&Forward'),
accelerator: 'Alt+Right',
click(model, item, win) {
if (win) {
win.webContents.send('history-forward');
}
},
],
},
];
enabled(model) {
return model.history && model.history.canGoForward;
},
},
],
});

template.push({
label: menuLabel('&View'),
submenu: [
{
label: menuLabel('Zoom &In'),
accelerator: 'CmdOrCtrl+Plus',
click(model, item, win) {
if (win) {
win.webContents.send('zoom-in');
}
},
},
{
label: menuLabel('Zoom &Out'),
accelerator: 'CmdOrCtrl+-',
click(model, item, win) {
if (win) {
win.webContents.send('zoom-out');
}
},
},
{
label: menuLabel('&Reset Zoom'),
accelerator: 'CmdOrCtrl+0',
click(model, item, win) {
if (win) {
win.webContents.send('zoom-reset');
}
},
},
{
label: menuLabel('Toggle &Developer Tools'),
accelerator: process.platform === 'darwin' ? 'Alt+Command+I' : 'Ctrl+Shift+I',
click(model, item, win) {
if (win) {
win.toggleDevTools();
}
},
},
],
});

const helpMenu = {
label: menuLabel('He&lp'),
submenu: [
{
label: menuLabel('S&ource Code'),
click() {
shell.openExternal('https://github.com/yoshuawuyts/vmd');
},
},
{
label: menuLabel('Report an &Issue'),
click() {
shell.openExternal('https://github.com/yoshuawuyts/vmd/issues');
},
},
{
label: menuLabel('&Releases'),
click() {
shell.openExternal('https://github.com/yoshuawuyts/vmd/releases');
},
},
],
};

if (process.platform !== 'darwin') {
helpMenu.submenu = [].concat(helpMenu.submenu, [
{ type: 'separator' },
{
label: menuLabel('&About vmd'),
click() {
openAboutDialog();
},
},
]);
}

template.push(helpMenu);

const menu = createMenu(template, {});

Expand Down

0 comments on commit e881b78

Please sign in to comment.