Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Communicating between two renderer processes in Electron

I am writing an Eletron program. In the program there is one index-window which is created by the main process (main.js). In this window there is a list of files (images). When I click on one of the files in that list I want to start a second window that displays that file. That second window is started by the renderer process of the index-window (index.js). How can I communicate between the renderer process of the index-window and the renderer process of the second window?

Code:

Creating the index-window from the main process in main.js:

let win;

function createWindow(){
  // Create the browser window.

  win = new BrowserWindow({width: 1024, height: 768, minWidth: 800, minHeight: 600, show: false, icon: 'files/images/icon.png'});

  win.loadURL(`file://${__dirname}/files/html/index.html`);
  win.once('ready-to-show', () => {
    win.show()
  })

  // Emitted when the window is closed.
  win.on('closed', () => {
    win = null;
  });
}
app.on('ready', createWindow);

In the index.html index.js (renderer process) is started:

<script src="../javascript/index.js"></script>

In index.js the function create_sprite_window() is called which creates a child window:

const fs = require('fs');
const path = require('path');
const {BrowserWindow} = require('electron').remote
let child_windows = [];


function create_child_window(URL, width, height){
  let rem_win = require('electron').remote.getCurrentWindow();
  let new_win = new BrowserWindow({width: width, height: height, minWidth: 400, minHeight: 300, show: false, parent: rem_win, minimizable: true, maximizable: true, skipTaskbar: true});
  child_windows[child_windows.length] = new_win;
  console.log(child_windows);
  new_win.loadURL(URL);
  new_win.once('ready-to-show', () => {
    new_win.show()
  })
  return new_win;
}
function create_sprite_window(){
  new_win = create_child_window(`file://${__dirname}/../html/sprite_manager.html`, 800, 400);

}

The child windows are stored in the array child_windows.

Is it possible to then send the path of the image to the second window or, alternatively, edit the <img> tag of the second window (setting the source of the <img> tag in the second window to the image with getElementById.src = path;) from the index-window?.

like image 544
niklassc Avatar asked Oct 18 '22 04:10

niklassc


1 Answers

I have found the answer by myself. To show the correct image in the second renderer window, I add a GET parameter to the URL which contains the path of the image.

like image 142
niklassc Avatar answered Oct 28 '22 23:10

niklassc