Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Electron - Change Menu Bar in Different Windows

Tags:

electron

menu

In my electron app, a user can open different windows.

I have the menu initialized when the main window is created like so:

const menuContents = Menu.buildFromTemplate(menuTemplate(mainWindow))
Menu.setApplicationMenu(menuContents)

However, when a user clicks a link and opens a new window, this same menu bar still appears in that window. I would like to change it and/or completely remove it.

How would I do that?

like image 506
akcorp2003 Avatar asked Dec 11 '25 04:12

akcorp2003


1 Answers

It's possible:

Example for Menus

In Renderer or Main Proccess make two functions with menu templates like this:

main menu

function createMenu(){
    var menu = Menu.buildFromTemplate([
    {
        //your menu items
    }
    ])
    Menu.setApplicationMenu(menu);
}

second menu

function createMenuwin(){
    var menu = Menu.buildFromTemplate([
    {
        //your other window menu items
    }
    ])
    Menu.setApplicationMenu(menu);
}

when you call the second window : Example open an image

function openIMG(path){
    win = new BrowserWindow({  
        width: 800, 
        height: 550,
       frame: true, 
        vibrancy: 'medium-light',
    });
    win.loadFile(path);
    createMenuwin();
    win.on('close', () =>{
        createMenu();
    })
}

when the second window is open create an app menu with

createmenuwin()

when the second window is closed call the event:

win.on('close', () =>{})

inside the close event of the second window, call:

 createMenu();
like image 89
Alex Santos Avatar answered Dec 14 '25 17:12

Alex Santos