参考 https://www.cnblogs.com/luzhanshi/p/11338288.html https://segmentfault.com/a/1190000020521879
当在MacOS、Windows、Linux中使用menu设置程序菜单时,会设置在各个程序窗体的顶层。 Note: 如果没有在app中设置一个菜单,系统会自动生成一个默认菜单, 默认生成的菜单中包含了一些初始选项,例如 文件,编辑, 视图,窗口,帮助。
app.on('ready', function () {
createWindow ();createMenu();
});
function createMenu(){
const {app, Menu} = require('electron');
const template = [
{
label: 'Edit',
submenu: [
{role: 'undo'},
{role: 'redo'},
{type: 'separator'},
{role: 'cut'},
{role: 'copy'},
{role: 'paste'},
{role: 'pasteandmatchstyle'},
{role: 'delete'},
{role: 'selectall'}
]
},
{
label: 'View',
submenu: [
{role: 'reload'},
{role: 'forcereload'},
{role: 'toggledevtools'},
{type: 'separator'},
{role: 'resetzoom'},
{role: 'zoomin'},
{role: 'zoomout'},
{type: 'separator'},
{role: 'togglefullscreen'}
]
},
{
role: 'window',
submenu: [
{role: 'minimize'},
{role: 'close'}
]
},
{
role: 'help',
submenu: [
{
label: 'Learn More',
click () { require('electron').shell.openExternal('https://electron.atom.io'); }
}
]
}
];
if (process.platform === 'darwin') {
template.unshift({
label: app.getName(),
submenu: [
{role: 'about'},
{type: 'separator'},
{role: 'services', submenu: []},
{type: 'separator'},
{role: 'hide'},
{role: 'hideothers'},
{role: 'unhide'},
{type: 'separator'},
{role: 'quit'}
]
});
template[1].submenu.push(
{type: 'separator'},
{
label: 'Speech',
submenu: [
{role: 'startspeaking'},
{role: 'stopspeaking'}
]
}
);
template[3].submenu = [
{role: 'close'},
{role: 'minimize'},
{role: 'zoom'},
{type: 'separator'},
{role: 'front'}
];
}
const menu = Menu.buildFromTemplate(template);
Menu.setApplicationMenu(menu);
}
Electron 的所有内置的 role 如下:
undo: 撤销
redo:重做
cut:剪切
copy:复制
paste:粘贴
pasteAndMatchStyle
selectAll:全选
delete:删除
minimize:当前窗口最小化
close:关闭当前窗口
quit:退出应用程序
reload:刷新当前窗口
forceReload:强制刷新当前窗口,忽略缓存
toggleDevTools:打开或者关闭 devtool
togglefullscreen:进行全屏切换
resetZoom:重置窗口大小
zoomIn:放大窗口的10%.
zoomOut:缩小窗口的10%.
完整的 Role 可以查看:https:
|