arcovoltaico arcovoltaico - 1 month ago 6
AngularJS Question

Accesing one of the opened windows from a controller

I have created 2 windows in main.js

app.on('ready', () => {
mainWindow = new BrowserWindow({
width: mainWindowState.width,
height: mainWindowState.height,
minWidth: 800,
minHeight: 600,
x: mainWindowState.x,
y: mainWindowState.y,
});

mainWindow.loadURL(`file://${__dirname}/index.html`);

playerWindow = new BrowserWindow({
width: playerWindowState.width,
height: playerWindowState.height,
minWidth: 800,
minHeight: 600,
x: playerWindowState.x,
y: playerWindowState.y,
});


Now I'd tried to access the playerWindow from a controller.
I used remote, global, different approached but I did not able to access the global window array defined on main.js from the controller, so I still don't know how to access the playerWindow to be able to do this:

playerWindow.location.href = '#/player';


Any idea? Thanks!

Answer Source

This task can be achieved using IPC.

On the controller just add:

const {ipcRenderer} = require('electron');
$scope.openPlayer = function () {
    $scope.playerOpen = true;
    ipcRenderer.send('open-player-window', 'an-argument');
};

$scope.closePlayer = function () {
    $scope.playerOpen = false;
    ipcRenderer.send('close-player-window', 'an-argument');

};

And on the main.js

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


app.on('ready', () => {

mainWindow = new BrowserWindow(etc... //

ipcMain.on('close-player-window', (event, arg) => {
    playerWindow.setClosable(true);
    playerWindow.hide();
});

ipcMain.on('open-player-window', (event, arg) => {
    playerWindow = new BrowserWindow({etc...});
    playerWindow.loadURL(`file://${__dirname}/index.html#/player`);
    playerWindow.setClosable(false);
    playerWindow.setMinimizable(false);


});