Refactor Menu.js.

This commit is contained in:
Zhongyi Tong
2017-08-06 00:19:35 +08:00
parent 9c2b7eeb27
commit b873b358fe
2 changed files with 238 additions and 310 deletions

View File

@@ -153,7 +153,7 @@ app.on('activate', () => {
}); });
app.on('ready', () => { app.on('ready', () => {
electron.Menu.setApplicationMenu(appMenu); electron.Menu.setApplicationMenu(appMenu.getMenu());
mainWindow = createMainWindow(); mainWindow = createMainWindow();
const page = mainWindow.webContents; const page = mainWindow.webContents;

View File

@@ -10,419 +10,347 @@ const BrowserWindow = electron.BrowserWindow;
const shell = electron.shell; const shell = electron.shell;
const appName = app.getName(); const appName = app.getName();
function sendAction(action) { class AppMenu {
const win = BrowserWindow.getAllWindows()[0]; getHistorySubmenu() {
return [{
if (process.platform === 'darwin') { label: 'Back',
win.restore(); accelerator: process.platform === 'darwin' ? 'Command+Left' : 'Alt+Left',
click(item, focusedWindow) {
if (focusedWindow) {
AppMenu.sendAction('back');
}
}
}, {
label: 'Forward',
accelerator: process.platform === 'darwin' ? 'Command+Right' : 'Alt+Right',
click(item, focusedWindow) {
if (focusedWindow) {
AppMenu.sendAction('forward');
}
}
}];
} }
win.webContents.send(action); getViewSubmenu() {
} return [{
label: 'Reload',
function clearCache() { accelerator: 'CommandOrControl+R',
const win = BrowserWindow.getAllWindows()[0]; click(item, focusedWindow) {
const ses = win.webContents.session; if (focusedWindow) {
ses.clearCache(() => { AppMenu.sendAction('reload-viewer');
dialog.showMessageBox({type: 'info', buttons: [], message: 'Cache cleared!'}); }
});
}
const historySubmenu = [
{
label: 'Back',
accelerator: process.platform === 'darwin' ? 'Command+Left' : 'Alt+Left',
click(item, focusedWindow) {
if (focusedWindow) {
sendAction('back');
} }
} }, {
}, label: 'Hard Reload',
{ accelerator: 'CommandOrControl+Shift+R',
label: 'Forward', click(item, focusedWindow) {
accelerator: process.platform === 'darwin' ? 'Command+Right' : 'Alt+Right', if (focusedWindow) {
click(item, focusedWindow) { AppMenu.sendAction('hard-reload');
if (focusedWindow) { }
sendAction('forward');
} }
} }, {
type: 'separator'
}, {
role: 'togglefullscreen'
}, {
label: 'Zoom In',
accelerator: 'CommandOrControl+=',
click(item, focusedWindow) {
if (focusedWindow) {
AppMenu.sendAction('zoomIn');
}
}
}, {
label: 'Zoom Out',
accelerator: 'CommandOrControl+-',
click(item, focusedWindow) {
if (focusedWindow) {
AppMenu.sendAction('zoomOut');
}
}
}, {
label: 'Actual Size',
accelerator: 'CommandOrControl+0',
click(item, focusedWindow) {
if (focusedWindow) {
AppMenu.sendAction('zoomActualSize');
}
}
}, {
type: 'separator'
}, {
label: 'Toggle Tray Icon',
click(item, focusedWindow) {
if (focusedWindow) {
focusedWindow.webContents.send('toggletray');
}
}
}, {
label: 'Toggle Sidebar',
accelerator: 'CommandOrControl+S',
click(item, focusedWindow) {
if (focusedWindow) {
const newValue = !ConfigUtil.getConfigItem('show-sidebar');
focusedWindow.webContents.send('toggle-sidebar', newValue);
ConfigUtil.setConfigItem('show-sidebar', newValue);
}
}
}, {
label: 'Toggle DevTools for Zulip App',
accelerator: process.platform === 'darwin' ? 'Alt+Command+I' : 'Ctrl+Shift+I',
click(item, focusedWindow) {
if (focusedWindow) {
focusedWindow.webContents.toggleDevTools();
}
}
}, {
label: 'Toggle DevTools for Active Tab',
accelerator: process.platform === 'darwin' ? 'Alt+Command+U' : 'Ctrl+Shift+U',
click(item, focusedWindow) {
if (focusedWindow) {
AppMenu.sendAction('tab-devtools');
}
}
}];
} }
];
const viewSubmenu = [ getHelpSubmenu() {
{ return [{
label: 'Reload', label: `${appName} Website`,
accelerator: 'CommandOrControl+R', click() {
click(item, focusedWindow) { shell.openExternal('https://zulipchat.com/help/');
if (focusedWindow) {
sendAction('reload-viewer');
} }
} }, {
}, label: `${appName + 'Desktop'} - ${app.getVersion()}`,
{ enabled: false
label: 'Hard Reload', }, {
accelerator: 'CommandOrControl+Shift+R', label: 'Report an Issue...',
click(item, focusedWindow) { click() {
if (focusedWindow) { const body = `
sendAction('hard-reload'); <!-- Please succinctly describe your issue and steps to reproduce it. -->
-
${app.getName()} ${app.getVersion()}
Electron ${process.versions.electron}
${process.platform} ${process.arch} ${os.release()}`;
shell.openExternal(`https://github.com/zulip/zulip-electron/issues/new?body=${encodeURIComponent(body)}`);
} }
} }];
},
{
type: 'separator'
},
{
role: 'togglefullscreen'
},
{
label: 'Zoom In',
accelerator: 'CommandOrControl+=',
click(item, focusedWindow) {
if (focusedWindow) {
sendAction('zoomIn');
}
}
},
{
label: 'Zoom Out',
accelerator: 'CommandOrControl+-',
click(item, focusedWindow) {
if (focusedWindow) {
sendAction('zoomOut');
}
}
},
{
label: 'Actual Size',
accelerator: 'CommandOrControl+0',
click(item, focusedWindow) {
if (focusedWindow) {
sendAction('zoomActualSize');
}
}
},
{
type: 'separator'
},
{
label: 'Toggle Tray Icon',
click(item, focusedWindow) {
if (focusedWindow) {
focusedWindow.webContents.send('toggletray');
}
}
},
{
label: 'Toggle Sidebar',
accelerator: 'CommandOrControl+S',
click(item, focusedWindow) {
if (focusedWindow) {
const newValue = !ConfigUtil.getConfigItem('show-sidebar');
focusedWindow.webContents.send('toggle-sidebar', newValue);
ConfigUtil.setConfigItem('show-sidebar', newValue);
}
}
},
{
label: 'Toggle DevTools for Zulip App',
accelerator: process.platform === 'darwin' ? 'Alt+Command+I' : 'Ctrl+Shift+I',
click(item, focusedWindow) {
if (focusedWindow) {
focusedWindow.webContents.toggleDevTools();
}
}
},
{
label: 'Toggle DevTools for Active Tab',
accelerator: process.platform === 'darwin' ? 'Alt+Command+U' : 'Ctrl+Shift+U',
click(item, focusedWindow) {
if (focusedWindow) {
sendAction('tab-devtools');
}
}
} }
];
const helpSubmenu = [ getDarwinTpl() {
{ return [{
label: `${appName} Website`, label: `${app.getName()}`,
click() { submenu: [{
shell.openExternal('https://zulipchat.com/help/');
}
},
{
label: `${appName + 'Desktop'} - ${app.getVersion()}`,
enabled: false
},
{
label: 'Report an Issue...',
click() {
const body = `
<!-- Please succinctly describe your issue and steps to reproduce it. -->
-
${app.getName()} ${app.getVersion()}
Electron ${process.versions.electron}
${process.platform} ${process.arch} ${os.release()}`;
shell.openExternal(`https://github.com/zulip/zulip-electron/issues/new?body=${encodeURIComponent(body)}`);
}
}
];
const darwinTpl = [
{
label: `${app.getName()}`,
submenu: [
{
label: 'Zulip Desktop', label: 'Zulip Desktop',
click(item, focusedWindow) { click(item, focusedWindow) {
console.log(this);
if (focusedWindow) { if (focusedWindow) {
sendAction('open-about'); AppMenu.sendAction('open-about');
} }
} }
}, }, {
{
type: 'separator' type: 'separator'
}, }, {
{
label: 'Settings', label: 'Settings',
accelerator: 'Cmd+,', accelerator: 'Cmd+,',
click(item, focusedWindow) { click(item, focusedWindow) {
if (focusedWindow) { if (focusedWindow) {
sendAction('open-settings'); AppMenu.sendAction('open-settings');
} }
} }
}, }, {
{
label: 'Keyboard Shortcuts', label: 'Keyboard Shortcuts',
accelerator: 'Cmd+K', accelerator: 'Cmd+K',
click(item, focusedWindow) { click(item, focusedWindow) {
if (focusedWindow) { if (focusedWindow) {
sendAction('shortcut'); AppMenu.sendAction('shortcut');
} }
} }
}, }, {
{
type: 'separator' type: 'separator'
}, }, {
{
label: 'Clear Cache', label: 'Clear Cache',
click() { click() {
clearCache(); AppMenu.clearCache();
} }
}, }, {
{
label: 'Log Out', label: 'Log Out',
accelerator: 'Cmd+L', accelerator: 'Cmd+L',
click(item, focusedWindow) { click(item, focusedWindow) {
if (focusedWindow) { if (focusedWindow) {
sendAction('log-out'); AppMenu.sendAction('log-out');
} }
} }
}, }, {
{
type: 'separator' type: 'separator'
}, }, {
{
role: 'services', role: 'services',
submenu: [] submenu: []
}, }, {
{
type: 'separator' type: 'separator'
}, }, {
{
role: 'hide' role: 'hide'
}, }, {
{
role: 'hideothers' role: 'hideothers'
}, }, {
{
role: 'unhide' role: 'unhide'
}, }, {
{
type: 'separator' type: 'separator'
}, }, {
{
role: 'quit' role: 'quit'
} }]
] }, {
}, label: 'Edit',
{ submenu: [{
label: 'Edit',
submenu: [
{
role: 'undo' role: 'undo'
}, }, {
{
role: 'redo' role: 'redo'
}, }, {
{
type: 'separator' type: 'separator'
}, }, {
{
role: 'cut' role: 'cut'
}, }, {
{
role: 'copy' role: 'copy'
}, }, {
{
role: 'paste' role: 'paste'
}, }, {
{
role: 'pasteandmatchstyle' role: 'pasteandmatchstyle'
}, }, {
{
role: 'delete' role: 'delete'
}, }, {
{
role: 'selectall' role: 'selectall'
} }]
] }, {
}, label: 'View',
{ submenu: this.getViewSubmenu()
label: 'View', }, {
submenu: viewSubmenu label: 'History',
}, submenu: this.getHistorySubmenu()
{ }, {
label: 'History', role: 'window',
submenu: historySubmenu submenu: [{
},
{
role: 'window',
submenu: [
{
role: 'minimize' role: 'minimize'
}, }, {
{
role: 'close' role: 'close'
}, }, {
{
type: 'separator' type: 'separator'
}, }, {
{
role: 'front' role: 'front'
} }]
] }, {
}, role: 'help',
{ submenu: this.getHelpSubmenu()
role: 'help', }];
submenu: helpSubmenu
} }
];
const otherTpl = [ getOtherTpl() {
{ return [{
label: 'File', label: 'File',
submenu: [ submenu: [{
{
label: 'Zulip Desktop', label: 'Zulip Desktop',
click(item, focusedWindow) { click(item, focusedWindow) {
if (focusedWindow) { if (focusedWindow) {
sendAction('open-about'); AppMenu.sendAction('open-about');
} }
} }
}, }, {
{
type: 'separator' type: 'separator'
}, }, {
{
label: 'Settings', label: 'Settings',
accelerator: 'Ctrl+,', accelerator: 'Ctrl+,',
click(item, focusedWindow) { click(item, focusedWindow) {
if (focusedWindow) { if (focusedWindow) {
sendAction('open-settings'); AppMenu.sendAction('open-settings');
} }
} }
}, }, {
{
type: 'separator' type: 'separator'
}, }, {
{
label: 'Keyboard Shortcuts', label: 'Keyboard Shortcuts',
accelerator: 'Ctrl+K', accelerator: 'Ctrl+K',
click(item, focusedWindow) { click(item, focusedWindow) {
if (focusedWindow) { if (focusedWindow) {
sendAction('shortcut'); AppMenu.sendAction('shortcut');
} }
} }
}, }, {
{
type: 'separator' type: 'separator'
}, }, {
{
label: 'Clear Cache', label: 'Clear Cache',
click() { click() {
clearCache(); AppMenu.clearCache();
} }
}, }, {
{
label: 'Log Out', label: 'Log Out',
accelerator: 'Ctrl+L', accelerator: 'Ctrl+L',
click(item, focusedWindow) { click(item, focusedWindow) {
if (focusedWindow) { if (focusedWindow) {
sendAction('log-out'); AppMenu.sendAction('log-out');
} }
} }
}, }, {
{
type: 'separator' type: 'separator'
}, }, {
{
role: 'quit', role: 'quit',
accelerator: 'Ctrl+Q' accelerator: 'Ctrl+Q'
} }]
] }, {
}, label: 'Edit',
{ submenu: [{
label: 'Edit',
submenu: [
{
role: 'undo' role: 'undo'
}, }, {
{
role: 'redo' role: 'redo'
}, }, {
{
type: 'separator' type: 'separator'
}, }, {
{
role: 'cut' role: 'cut'
}, }, {
{
role: 'copy' role: 'copy'
}, }, {
{
role: 'paste' role: 'paste'
}, }, {
{
role: 'pasteandmatchstyle' role: 'pasteandmatchstyle'
}, }, {
{
role: 'delete' role: 'delete'
}, }, {
{
type: 'separator' type: 'separator'
}, }, {
{
role: 'selectall' role: 'selectall'
} }]
] }, {
}, label: 'View',
{ submenu: this.getViewSubmenu()
label: 'View', }, {
submenu: viewSubmenu label: 'History',
}, submenu: this.getHistorySubmenu()
{ }, {
label: 'History', role: 'help',
submenu: historySubmenu submenu: this.getHelpSubmenu()
}, }];
{
role: 'help',
submenu: helpSubmenu
} }
];
const tpl = process.platform === 'darwin' ? darwinTpl : otherTpl; static sendAction(action, ...params) {
const win = BrowserWindow.getAllWindows()[0];
module.exports = electron.Menu.buildFromTemplate(tpl); if (process.platform === 'darwin') {
win.restore();
}
win.webContents.send(action, ...params);
}
static clearCache() {
const win = BrowserWindow.getAllWindows()[0];
const ses = win.webContents.session;
ses.clearCache(() => {
dialog.showMessageBox({type: 'info', buttons: [], message: 'Cache cleared!'});
});
}
getMenu() {
const tpl = process.platform === 'darwin' ? this.getDarwinTpl() : this.getOtherTpl();
return electron.Menu.buildFromTemplate(tpl);
}
}
module.exports = new AppMenu();