多个窗口的电子最佳方式

dit*_*kts 2 electron

打开多个BrowserWindows的正确方法是什么?我这样做,到目前为止效果很好。取得一系列胜利更好吗?

let win;

function createWindow () {
  for (i=0; i<loadNotes.notes.length; i++){
  win = new BrowserWindow({
    'x': loadNotes.notes[i].pos.x,
    'y': loadNotes.notes[i].pos.y,
    'width': loadNotes.notes[i].pos.width,
    'height': loadNotes.notes[i].pos.height,
    'frame': false});

  win.setMenu(null);
  win.loadURL(`file://${__dirname}/index.html?${loadNotes.notes[i].name}`);
  //win.webContents.openDevTools()
  }
  win.on('close', () => {

  })

  win.on('closed', () => {
    win = null
  });
}
Run Code Online (Sandbox Code Playgroud)

小智 6

在电子中创建多窗口应用程序很容易。这里有一个 nodejs 模块,可帮助您轻松创建多窗口应用程序。

您只需执行以下命令即可安装此模块

npm install --save electron-window-manager

然后,在应用程序 main.js(或您为应用程序选择的任何内容)中, require 模块,如下所示:

var windowManager = require('electron-window-manager');

在 Main 进程中,您可以像这样使用它:

const electron = require('electron');
const app = electron.app;
const windowManager = require('electron-window-manager');

// When the application is ready
app.on('ready', function(){
    windowManager.init( ... );
    // Open a window
    windowManager.open('home', 'Welcome ...', '/home.html');
});
Run Code Online (Sandbox Code Playgroud)

在 Renderer 进程中(在创建的窗口内),您可以像这样使用它:

    var remote = require('remote');
    var windowManager = remote.require('electron-window-manager');

    // Create a new window
    var win2 = windowManager.createNew('win2', 'Windows #2');
    win2.loadURL('/win2.html');
    win2.onReady( ... );
    win2.open();
Run Code Online (Sandbox Code Playgroud)

请继续查看模块代码,亲眼看看它是如何工作的,不涉及魔法,但它是一致且易于阅读的。只要看看代码,你就可以开始了。

这是有关此模块的完整指南,希望对您有所帮助

https://github.com/TamkeenLMS/electron-window-manager


小智 5

这取决于您是否要使用的实例方法win。如果没有,您可以保留您的代码。仅是一项建议,建议为获得最佳用户体验,请优雅地显示窗口

win = new BrowserWindow({
    ...., 
    show: false})
...
win.loadURL(`file://${__dirname}/index.html?${loadNotes.notes[i].name}`)
win.once('ready-to-show', () => {
  win.show()
})
Run Code Online (Sandbox Code Playgroud)