最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Electron - Showing Menu on only Main Window - Stack Overflow

programmeradmin1浏览0评论

I am new to writing in Electron on windows. When I create a menuTemplate this is showing on all of my windows that are opened. Is there a way to only have the menu appear on my mainWindow? (Not on the pop up windows I create)

Index.js

const electron = require('electron');

const { app, BrowserWindow, Menu } = electron;

let mainWindow;
let addWindow;

app.on('ready', () => {
  mainWindow = new BrowserWindow({});
  mainWindow.loadURL(`file://${__dirname}/main.html`);
  mainWindow.on('closed', () => app.quit());

  const mainMenu = Menu.buildFromTemplate(menuTemplate);
  Menu.setApplicationMenu(mainMenu);
});

function createAddWindow() {
  addWindow = new BrowserWindow({
    width: 300,
    height: 200,
    title: 'Add New Todo'
  });
  addWindow.loadURL(`file://${__dirname}/add.html`)
}

const menuTemplate = [
  {
    label: 'File',
    submenu: [
      {
        label: 'New Todo' ,
        click() { createAddWindow(); }
      },
      {
        label: 'Quit',
        accelerator: process.platform === 'darwin' ? 'Command+Q' : 'Ctrl+Q',
        click() {
          app.quit();
        }
      }
    ]
  }
];

if (process.platform === 'darwin') {
  menuTemplate.unshift({});
}

I am new to writing in Electron on windows. When I create a menuTemplate this is showing on all of my windows that are opened. Is there a way to only have the menu appear on my mainWindow? (Not on the pop up windows I create)

Index.js

const electron = require('electron');

const { app, BrowserWindow, Menu } = electron;

let mainWindow;
let addWindow;

app.on('ready', () => {
  mainWindow = new BrowserWindow({});
  mainWindow.loadURL(`file://${__dirname}/main.html`);
  mainWindow.on('closed', () => app.quit());

  const mainMenu = Menu.buildFromTemplate(menuTemplate);
  Menu.setApplicationMenu(mainMenu);
});

function createAddWindow() {
  addWindow = new BrowserWindow({
    width: 300,
    height: 200,
    title: 'Add New Todo'
  });
  addWindow.loadURL(`file://${__dirname}/add.html`)
}

const menuTemplate = [
  {
    label: 'File',
    submenu: [
      {
        label: 'New Todo' ,
        click() { createAddWindow(); }
      },
      {
        label: 'Quit',
        accelerator: process.platform === 'darwin' ? 'Command+Q' : 'Ctrl+Q',
        click() {
          app.quit();
        }
      }
    ]
  }
];

if (process.platform === 'darwin') {
  menuTemplate.unshift({});
}
Share Improve this question asked Jun 16, 2017 at 12:31 David BriertonDavid Brierton 7,39713 gold badges55 silver badges107 bronze badges 1
  • You can use CommandOrControl+Q or CmdOrCtrl+Q instead of process.platform === 'darwin' ? 'Command+Q' : 'Ctrl+Q'. See Accelerator – RoyalBingBong Commented Jun 17, 2017 at 17:04
Add a ment  | 

1 Answer 1

Reset to default 8

Change Menu.setApplicationMenu(mainMenu); to mainWindow.setMenu(mainMenu).

The Menu.setApplicationMenu sets the Menu for all BrowserWindows, while win.setMenu sets it only for the window it is called on.

发布评论

评论列表(0)

  1. 暂无评论