0

Here is my code Github repo url

const {app, Menu, ipcMain} = require('electron');
const mainWindow = require("./windows/mainWindow");

// Catching events from clients
ipcMain.on("login", ()=>{
  dashboardWindow();
  mainWindow.close();
})

./windows/mainWindow file:

// Initialize main window
const mainWindow = () => {
  let mainWindow = new BrowserWindow({
      autoHideMenuBar: true,
      webPreferences: {
                  nodeIntegration: true
                      }
    });
  // Load html in window
  mainWindow.loadFile("./templates/index.html")
  mainWindow.webContents.openDevTools();
}

module.exports = mainWindow;

How could I close a window?

MauriceNino
  • 6,214
  • 1
  • 23
  • 60
Manas Paul
  • 86
  • 1
  • 9

1 Answers1

3

mainWindow is a function, so you need to call it and inside the function also return the window:

const {app, Menu, ipcMain} = require('electron');
const mainWindowFunc = require("./windows/mainWindow");

const mainWindow = mainWindowFunc(); // <-- Call function

ipcMain.on("login", ()=>{
  dashboardWindow();
  mainWindow.close();  // <-- Now close should be available, because you call it on the instance of BrowserWindow
})

./windows/mainWindow file:

const mainWindow = () => {
  let mainWindow = new BrowserWindow({
      autoHideMenuBar: true,
      webPreferences: {
                  nodeIntegration: true
                      }
    });

  mainWindow.loadFile("./templates/index.html")
  mainWindow.webContents.openDevTools();
  return mainWindow; // <-- return window
}

module.exports = mainWindow;
MauriceNino
  • 6,214
  • 1
  • 23
  • 60
  • It's not giving me the error, but the main window is not closing, it behaves like it's closed but it's still there underneath the dashboard window – Manas Paul Oct 21 '20 at 07:51
  • Maybe you can find your answer there: https://stackoverflow.com/questions/31171597/atom-electron-close-the-window-with-javascript. If not please open a new question with your new problem. @ManasPaul – MauriceNino Oct 21 '20 at 08:21
  • Also, maybe look here: https://github.com/electron/electron/issues/11790 Don't know if that's the issue though. – MauriceNino Oct 21 '20 at 08:23
  • 1
    You could also try to call `.destroy()` on it: https://stackoverflow.com/questions/41937111/electron-close-button-not-working @ManasPaul – MauriceNino Oct 21 '20 at 08:24
  • No problem - feel free to accept my answer then @ManasPaul – MauriceNino Oct 21 '20 at 13:15