Create new window nwjs
Here is an example of how to create a new window in NW.js (formerly known as Node-Webkit):
const { app, BrowserWindow } = require('nwjs');
let win;
function createWindow() {
win = new BrowserWindow({
width: 800,
height: 600,
show: false,
webPreferences: {
nodeIntegration: true
}
});
win.loadURL(`file://${__dirname}/index.html`);
win.on('closed', () => {
win = null;
});
win.show();
}
app.on('ready', createWindow);
app.on('window-all-closed', () => {
if (process.platform!== 'darwin') {
app.quit();
}
});
app.on('activate', () => {
if (win === null) {
createWindow();
}
});
This code creates a new window with a width of 800 pixels and a height of 600 pixels. The show
property is set to false
so that the window is not visible until it is loaded. The webPreferences
object is used to enable node integration, which allows you to use Node.js modules in your web page.
The createWindow
function is called when the application is ready, and it creates a new window and loads an HTML file into it. The loadURL
method is used to load the HTML file, and the __dirname
variable is used to get the directory of the current script.
The win.on('closed',...)
event listener is used to handle the closed
event, which is fired when the window is closed. In this case, we set win
to null
to indicate that the window is no longer valid.
The app.on('window-all-closed',...)
event listener is used to handle the window-all-closed
event, which is fired when all windows are closed. In this case, we quit the application if the platform is not Darwin (i.e., macOS).
The app.on('activate',...)
event listener is used to handle the activate
event, which is fired when the application is activated (i.e., when the user clicks on the application's icon in the dock). In this case, we create a new window if the current window is null.
You can then use the win
variable to interact with the window, for example to set its title or to send messages to it.
Note that this is just a basic example, and you will likely want to customize the window further to suit your needs.