2017-04-20 22:42:04 +00:00
|
|
|
var electron = require('electron');
|
|
|
|
var app = electron.app;
|
|
|
|
|
|
|
|
var win, menu;
|
|
|
|
|
|
|
|
function getMenuItem (label) {
|
|
|
|
var menuItem;
|
|
|
|
menu.items.some(function (topLvl) {
|
|
|
|
menuItem = topLvl.submenu.items.find(function (item) {
|
|
|
|
return item.label === label;
|
|
|
|
});
|
|
|
|
return menuItem;
|
|
|
|
});
|
|
|
|
return menuItem || {};
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateFullScreenCheckbox() {
|
|
|
|
getMenuItem('Full Screen').checked = win.isFullScreen();
|
|
|
|
win.setMenuBarVisibility(!win.isFullScreen());
|
|
|
|
}
|
|
|
|
function toggleFullScreen(flag) {
|
|
|
|
if (!win || !win.isVisible()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (typeof flag !== 'boolean') {
|
|
|
|
flag = !win.isFullScreen();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flag) {
|
|
|
|
// Fullscreen and aspect ratio do not play well together. (Mac)
|
|
|
|
win.setAspectRatio(0);
|
|
|
|
}
|
|
|
|
win.setFullScreen(flag);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sets whether the window should always show on top of other windows
|
|
|
|
function toggleAlwaysOnTop(flag) {
|
|
|
|
if (!win) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (typeof flag !== 'boolean') {
|
|
|
|
flag = !win.isAlwaysOnTop();
|
|
|
|
}
|
|
|
|
|
|
|
|
win.setAlwaysOnTop(flag);
|
|
|
|
getMenuItem('Float on Top').checked = flag;
|
|
|
|
}
|
|
|
|
|
|
|
|
function toggleDevTools() {
|
|
|
|
if (!win) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (win.webContents.isDevToolsOpened()) {
|
|
|
|
win.webContents.closeDevTools();
|
|
|
|
} else {
|
|
|
|
win.webContents.openDevTools();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function init(window) {
|
|
|
|
if (win) {
|
|
|
|
console.error("can't initialize the tray mulitple times");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
win = window;
|
|
|
|
|
|
|
|
var template = [
|
|
|
|
{
|
|
|
|
label: 'Edit',
|
|
|
|
submenu: [
|
2017-04-24 22:31:17 +00:00
|
|
|
{ role: 'undo' },
|
|
|
|
{ role: 'redo' },
|
|
|
|
{ type: 'separator' },
|
|
|
|
|
|
|
|
{ role: 'cut' },
|
|
|
|
{ role: 'copy' },
|
|
|
|
{ role: 'paste' },
|
|
|
|
{ role: 'pasteandmatchstyle' },
|
|
|
|
{ role: 'delete' },
|
|
|
|
{ role: 'selectall' }
|
2017-04-20 22:42:04 +00:00
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: 'View',
|
|
|
|
submenu: [
|
2017-04-24 22:31:17 +00:00
|
|
|
{ role: 'reload' },
|
|
|
|
{ role: 'forcereload' },
|
|
|
|
{ type: 'separator' },
|
|
|
|
|
|
|
|
{ role: 'resetzoom' },
|
|
|
|
{ role: 'zoomin' },
|
|
|
|
{ role: 'zoomout' },
|
|
|
|
{ type: 'separator' },
|
|
|
|
|
|
|
|
{ role: 'togglefullscreen' },
|
2017-04-20 22:42:04 +00:00
|
|
|
{
|
|
|
|
label: 'Float on Top',
|
|
|
|
type: 'checkbox',
|
|
|
|
click: toggleAlwaysOnTop,
|
|
|
|
},
|
2017-04-24 22:31:17 +00:00
|
|
|
{ type: 'separator' },
|
|
|
|
|
2017-04-20 22:42:04 +00:00
|
|
|
{
|
|
|
|
label: 'Developer',
|
|
|
|
submenu: [
|
|
|
|
{
|
|
|
|
label: 'Developer Tools',
|
|
|
|
accelerator: process.platform === 'darwin' ? 'Alt+Command+I' : 'Ctrl+Shift+I',
|
|
|
|
click: toggleDevTools,
|
|
|
|
},
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
if (process.platform === 'darwin') {
|
|
|
|
// WebTorrent menu (Mac)
|
|
|
|
template.unshift({
|
|
|
|
label: 'Electron Demo',
|
|
|
|
submenu: [
|
2017-04-24 22:31:17 +00:00
|
|
|
{ role: 'about' },
|
|
|
|
{ type: 'separator' },
|
|
|
|
|
2017-04-20 22:42:04 +00:00
|
|
|
{
|
|
|
|
label: 'Preferences',
|
|
|
|
accelerator: 'Cmd+,',
|
|
|
|
click: function () { console.log('TODO: implement preferences'); }
|
|
|
|
},
|
2017-04-24 22:31:17 +00:00
|
|
|
{ type: 'separator' },
|
|
|
|
|
|
|
|
{ role: 'services', submenu: [] },
|
|
|
|
{ type: 'separator' },
|
|
|
|
{ role: 'hide' },
|
|
|
|
{ role: 'hideothers' },
|
|
|
|
{ role: 'unhide' },
|
|
|
|
{ type: 'separator' },
|
|
|
|
|
|
|
|
{ role: 'quit' }
|
2017-04-20 22:42:04 +00:00
|
|
|
]
|
|
|
|
});
|
|
|
|
|
|
|
|
// Edit menu (Mac)
|
|
|
|
template[2].submenu.push(
|
2017-04-24 22:31:17 +00:00
|
|
|
{ type: 'separator' },
|
2017-04-20 22:42:04 +00:00
|
|
|
{
|
|
|
|
label: 'Speech',
|
|
|
|
submenu: [
|
2017-04-24 22:31:17 +00:00
|
|
|
{ role: 'startspeaking' },
|
|
|
|
{ role: 'stopspeaking' }
|
2017-04-20 22:42:04 +00:00
|
|
|
]
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
// Window menu (Mac)
|
|
|
|
template.push({
|
2017-04-24 22:31:17 +00:00
|
|
|
role: 'Window',
|
2017-04-20 22:42:04 +00:00
|
|
|
submenu: [
|
2017-04-24 22:31:17 +00:00
|
|
|
{ role: 'close' },
|
|
|
|
{ role: 'minimize' },
|
|
|
|
{ role: 'zoom' },
|
|
|
|
{ type: 'separator' },
|
|
|
|
|
|
|
|
{ role: 'front' }
|
2017-04-20 22:42:04 +00:00
|
|
|
]
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// On Windows and Linux, open dialogs do not support selecting both files and
|
|
|
|
// folders and files, so add an extra menu item so there is one for each type.
|
|
|
|
if (process.platform === 'linux' || process.platform === 'win32') {
|
2017-04-24 22:31:17 +00:00
|
|
|
template.unshift({
|
|
|
|
label: 'File',
|
|
|
|
submenu: [
|
|
|
|
{ role: 'quit' },
|
|
|
|
]
|
|
|
|
});
|
|
|
|
|
2017-04-20 22:42:04 +00:00
|
|
|
// Edit menu (Windows, Linux)
|
|
|
|
template[1].submenu.push(
|
2017-04-24 22:31:17 +00:00
|
|
|
{ type: 'separator' },
|
2017-04-20 22:42:04 +00:00
|
|
|
{
|
|
|
|
label: 'Preferences',
|
|
|
|
accelerator: 'CmdOrCtrl+,',
|
|
|
|
click: function () { console.log('TODO: implement preferences'); }
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2017-04-24 22:31:17 +00:00
|
|
|
// Window menu (Windows, Linux)
|
|
|
|
template.push({
|
|
|
|
role: 'Qindow',
|
|
|
|
submenu: [
|
|
|
|
{ role: 'minimize' },
|
|
|
|
{ role: 'close' },
|
|
|
|
]
|
|
|
|
});
|
|
|
|
|
2017-04-20 22:42:04 +00:00
|
|
|
// Add an help.about option
|
|
|
|
template.push({
|
|
|
|
label: 'Help',
|
|
|
|
submenu: [
|
|
|
|
{
|
|
|
|
label: 'About',
|
|
|
|
click: function () { console.log('TODO: implement about'); },
|
|
|
|
}
|
|
|
|
],
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
menu = electron.Menu.buildFromTemplate(template);
|
|
|
|
electron.Menu.setApplicationMenu(menu);
|
|
|
|
|
|
|
|
win.webContents.on('dom-ready', updateFullScreenCheckbox);
|
|
|
|
win.on('enter-full-screen', updateFullScreenCheckbox);
|
|
|
|
win.on('leave-full-screen', updateFullScreenCheckbox);
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports.init = init;
|