【问题标题】:Native Menus not showing OS X Electron本机菜单不显示 OS X Electron
【发布时间】:2021-08-04 09:18:24
【问题描述】:

我使用 electron-quick-start 创建了一个 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 中这样做,我看到了建议。无济于事。它会显示默认的电子快速启动菜单,或者只是一个以应用程序命名的简单菜单,其中包含一个标有“退出”的项目。

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


编辑:我居然附错了文件,第一次尝试使用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));

【问题讨论】:

    标签: javascript electron


    【解决方案1】:

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

    【讨论】:

    • @thephpdev 您必须在应用程序准备就绪后调用Menu.setApplicationMenu(),因此将该调用移至createWindow()
    【解决方案2】:

    请注意,在 OSX 上,菜单不在窗口本身,而是在 dosktop 的顶部。

    我浪费了大量时间试图解决它为什么没有出现。

    【讨论】:

      【解决方案3】:

      正如@Vadim Macagon 在评论中所说,确保对Menu.setApplicationMenu() 的调用在createWindow() 中。对我来说,它解决了问题。

      【讨论】:

        【解决方案4】:

        也许你将 LSUIElement 设置为 1,这意味着一个代理应用程序,即不应出现在 Dock 中的应用程序。将 LSUIElement 更改为 0,将显示构建应用程序的菜单。

        电子构建配置

        mac: {
                    icon: 'build/icons/icon.icons',
                    extendInfo: {
                      LSUIElement: 0
                    }
                  }
        

        LSUIElement 的详细信息在这里 https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/LaunchServicesKeys.html#//apple_ref/doc/uid/20001431-108256

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-12-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多