关闭Controller中加载的BrowserWindow

时间:2016-06-23 19:27:01

标签: angularjs electron

给定一个应用程序,我有一个帮助按钮,在控制器类中打开另一个BrowserWindow,其中包含帮助文档文件。当我先关闭帮助窗口时,一切都还可以。当我关闭原始窗口时,帮助窗口保持打开状态,然后当我关闭它时,它会崩溃,因为控制器本身已被删除。我已经尝试了各种使用ipc消息的方法,但我认为我可以解决它的一种方法,但我还没想出如何向所有打开的窗口广播“关闭”消息。只需在原始窗口关闭时使用app.quit()或app.exit(status)关闭应用程序似乎也不会关闭帮助窗口。这是一个片段

home.html的

<button ng-click="_ctrl.showHelp()">Help</button>

homeController.js

const {BrowserWindow} = require('electron').remote;
const {ipcRenderer} = require('electron');

export class HomeController {
  constructor() {} //stuff

  showHelp() {
    let win = new BrowserWindow({
      title: 'Help Pages',
      nodeIntegration: false,
      show: false
    });

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

    win.load(`${__dirname}/help.html`);

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

index.js

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

let mainWindow = null;

app.on('window-all-closed', () => {
  app.quit();
}

app.on('ready', () => {
  mainWindow = new BrowserWindow({
    width: 1200,
    height: 800,
    show: false,
  });

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

    app.quit();  //neither of these work
    app.exit(0); //neither of these work
  });

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

任何方向都会受到赞赏。

1 个答案:

答案 0 :(得分:0)

要解决此问题,您只需要在index.js上放置帮助窗口的实例,就可以将帮助窗口实例设置为close事件上的主窗口,以在关闭帮助窗口时将其关闭。

示例

const mainWindow = null;
const helpWindow = null;

mainWindow = new BrowserWindow()
mainWindow.on('close', function(){
   helpWindow = null
   mainWindow = null
})

为了更好的实践,您希望将所有窗口存储在一个数组中,但是为了简单起见,我这样做了。希望对您有帮助。哦,仅在渲染器上使用remote和ipcRenderers,ipcMain仅用于index.js。