嗨,我想从电子标题菜单中删除 3 个选项,它们是
如下图
这是我尝试过的
背景.js
import { app, session, protocol, BrowserWindow , dialog, ipcMain, Menu} from 'electron'
const menu = Menu.getApplicationMenu(); // get default menu
console.log('menu %s',menu.items);
let menuIndex = menu.items.findIndex(o => {
return o.role == 'viewmenu'
})
//splice items based on names , i'm not able to do that because i cannot see the full json object and its structure in console.log
//Menu.setApplicationMenu(menu); //set menu
另外我不知道该怎么办
请帮助我提前致谢!
基于默认值创建一个new 菜单,并将默认的菜单项作为模板传递。
然后将其设置为应用程序菜单。在构建过程中,您可以剪掉不需要的元素
const menu = Menu.getApplicationMenu(); // get default menu
// build a new menu based on default one
const newmenu = Menu.buildFromTemplate(
menu.items.map(i => {
// overwrite viewmenu item
if (i.role === 'viewmenu') {
// create new submenu
const newviewsub = Menu.buildFromTemplate(
i.submenu.items.slice(4) // cut first 4 item (4th is separator)
)
// replace this item's submenu with the news
return Object.assign({}, i, { submenu: newviewsub })
}
// otherwise keep
return i
})
)
Menu.setApplicationMenu(newmenu)
你可以设置自己的菜单,这并不难:
const menu = Menu.buildFromTemplate(
[
{ role: 'appMenu' },
{ role: 'editMenu' },
{
label: 'View',
submenu: [
{ role: 'resetZoom' },
{ role: 'zoomIn' },
{ role: 'zoomOut' },
{ type: 'separator' },
{ role: 'togglefullscreen' }
]
},
{ role: 'windowMenu' },
{
role: 'help',
submenu: [
{
label: 'Contact Support',
click: async () => {
const { shell } = require('electron')
await shell.openExternal('https://www.example.com/contact/')
}
}
]
}
]);
Menu.setApplicationMenu(menu);
请参阅 Menu Electron 文档中的 示例。