gpt4 book ai didi

javascript - native 菜单不显示 OS X Electron

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:31:34 24 4
gpt4 key购买 nike

我使用 electron-quick-start 创建了一个 Electron 应用程序,我希望唯一显示的 native 菜单是“编辑”菜单,里面有常见的嫌疑人。

但是,在搜索并用尽“Electron 菜单不起作用”的所有相关 Google 结果后,我不知所措。

我当前的 main.js 文件:

const {app, Menu, BrowserWindow} = require('electron')

// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow;

app.setName('mathulator');

function createWindow () {
// Create the browser window.
mainWindow = new BrowserWindow({width: 900, height: 550})

// and load the index.html of the app.
mainWindow.loadURL(`file://${__dirname}/index.html`)

// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.

const template = [
{
label: 'Mathulator',
submenu: [
{
role: 'quit'
}
]
},
{
label: 'Edit',
submenu: [
{
role: 'undo'
},
{
role: 'redo'
},
{
type: 'separator'
},
{
role: 'cut'
},
{
role: 'copy'
},
{
role: 'paste'
},
{
role: 'pasteandmatchstyle'
},
{
role: 'delete'
},
{
role: 'selectall'
}
]
}
]

mainWindow.setMenu(Menu.buildFromTemplate(template))

// Emitted when the window is closed.
mainWindow.on('closed', function () {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
mainWindow = null
})
}

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', createWindow)

// Quit when all windows are closed.
app.on('window-all-closed', function () {
// On OS X it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
app.quit()
}
})

app.on('activate', function () {
// On OS X it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (mainWindow === null) {
createWindow()
}
})

我也用electron-packager打包过,没用。

我在 main.js 文件中运行它,我可以从网络上大量模糊或复杂的信息中收集到它,这是主要过程,因此我应该在其中创建菜单。

我还尝试在 render.js 中执行此操作,我看到了建议。无济于事。它会显示默认的 Electron 快速启动菜单,或者只是一个以应用程序命名的简单菜单,其中包含一个标记为“退出”的项目。

我可能做错了什么,我可能从现有信息中误解了什么?


编辑: 我实际上附加了错误的文件,第一次尝试使用 Menu.setApplicationMenu(),如下所示:

const {app, Menu, BrowserWindow} = require('electron')

// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow;

app.setName('mathulator');

function createWindow () {
// Create the browser window.
mainWindow = new BrowserWindow({width: 900, height: 550});

// and load the index.html of the app.
mainWindow.loadURL(`file://${__dirname}/index.html`);

// Emitted when the window is closed.
mainWindow.on('closed', function () {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
mainWindow = null;
});
}

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', createWindow);

// Quit when all windows are closed.
app.on('window-all-closed', function () {
// On OS X it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
app.quit();
}
})

app.on('activate', function () {
// On OS X it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (mainWindow === null) {
createWindow();
}
})

const template = [
{
label: 'Mathulator',
submenu: [
{
role: 'quit'
}
]
},
{
label: 'Edit',
submenu: [
{
role: 'undo'
},
{
role: 'redo'
},
{
type: 'separator'
},
{
role: 'cut'
},
{
role: 'copy'
},
{
role: 'paste'
},
{
role: 'pasteandmatchstyle'
},
{
role: 'delete'
},
{
role: 'selectall'
}
]
}
];

Menu.setApplicationMenu(Menu.buildFromTemplate(template));

最佳答案

这里的问题是 BrowserWindow.setMenu() 仅在 Windows 和 Linux 上可用。在 macOS 上你应该使用 Menu.setApplicationMenu() .

关于javascript - native 菜单不显示 OS X Electron,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40440666/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com