Problem with multi windows - if you click child window than electron throws error message


#1

Hello Ladies and gentlemen,

I am new here now. Thanks for welcome!
I am developing electron application as awesome applications.

I have tried for example ipcMain and ipcRenderer function

Example for that:
const {app, BrowserWindow} = require(‘electron’)
const path = require(‘path’)
const url = require(‘url’)
const {ipcMain} = require(‘electron’);

let win
let prefWin

function createWindow () {
  win = new BrowserWindow({width: 800, height: 600})
  win.loadURL(url.format({
    pathname: path.join(__dirname, 'index.html'),
    protocol: 'file:',
    slashes: true
  }))

  prefWin = new BrowserWindow({width: 400, height: 400, show: false, parent: win, modal: true})
  prefWin.loadURL(url.format({
    pathname: path.join(__dirname, 'pref.html'),
    protocol: 'file:',
    slashes: true
  }))
  
  win.on('closed', () => {
    win = null
  })

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

app.on('ready', createWindow)
app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit()
  }
})

app.on('activate', () => {
  if (win === null) {
    createWindow()
  }

  if (prefWin === null) {
    createWindow()
  }
})

ipcMain.on('togglePrefWindow', () => {
  if(prefWin.isVisible())
  {
    prefWin.hide()
  }else {
    prefWin.show()
  }
})

index.html:




Hello World!


Hello World!


We are using node ,
Chrome ,
and Electron .

const {ipcRenderer} = require(‘electron’);
      var button = document.createElement('button')
      button.textContent = 'Show'
      button.addEventListener('click', function () {
        ipcRenderer.send('togglePrefWindow')
      })
      document.body.appendChild(button)
    </script>
  </body>
</html>

pref.html:




Preference



const {ipcRenderer} = require(‘electron’)

      var button = document.createElement('button')
      button.textContent = 'Hide'
      button.addEventListener('click', function () {
        ipcRenderer.send('togglePrefWindow')
      })
      document.body.appendChild(button)
    </script>
  </body>
</html>

Output if you click button from htmls = no problem.

But it has problem why does it happen if you click native window like "X"
than electron throws error message.

How do I fix? If you say different ipcMain or ipcRenderer are not same or what do I use?

Thanks for help!

Best regards!