Electron: Can we access BrowserWindow using its unique id?

Viewed 5164

Assuming the below function is called multiple times to create BrowserWindow, say 5 times.

let mainWindow;

function createWindow() {
    "use strict";

    mainWindow = new BrowserWindow({ 
        height: height,
        width: width,
        minHeight: height,
        minWidth: width,
        icon: __dirname + iconPath,
        frame: false,
        backgroundColor: '#FFF',
        show: false
    });

    mainWindow.loadURL(url.format({ 
        pathname: path.join(__dirname, address),
        protocol: 'file',
        slashes: true
    }));

    mainWindow.once('ready-to-show', () => {
        mainWindow.show();
        mainWindow.focus();
    });

    mainWindow.on('closed', () => {
        mainWindow = null;
    });
}

this will generate 5 different BrowserWindow.

Does BrowserWindow has it's own unique id to identify? Or can we assign a unique id on it so that we can access them using its own id?

1 Answers

Yes it does. You can get it like this: mainWindow.id And it'll be something like 1 or 2 depending on how many windows were already open when this window opened.

Also you can get a BrowserWindow from it's id like this:

var myWindow = BrowserWindow.fromId(id);

BrowserWindow.fromId Docs.

window.id Docs.

Related