Window manager for NW.js.
A simple way to manage windows, including the following features:
- Manage the parent/child relations of windows
- The child window is always on top of the parent window
- If the window is closed, all child windows also be closed
- If the window is modal or any child is modal, it will not be closed
- Append a translucent mask on the body to simulate a modal window
- Merge EventEmitter to make communication between windows easier
npm install nwjs-window-manager
const { wm } = require("nwjs-window-manager");
// Promise style
async function main() {
// Open a window
const win1 = await wm.open("1.html");
// Open a child window of win1
const win2 = await win1.open("2.html");
// Or
const win3 = await wm.open("3.html", { parent: win1 });
// More options
const win4 = await win1.open("4.html", {
// Set a name to open a singleton window
name: "singleton",
// win1 will be modal like
modal: true,
// Center relative to win1
position: "parentCenter",
// Other nw.Window.open() options
width: 300,
height: 200,
// ...
});
// Manage an existing nw window
const curWin = wm(nw.Window.get());
const win5 = curWin.open("5.html");
nw.Window.open("6.html", (win6) => {
nw.Window.open("7.html", (win7) => {
// Make win7 a modal child window of win6
wm(win7, { parent: win6, modal: true });
});
});
}
main();
1.html
// Manage the current nw window as `win1`
const win1 = wm(nw.Window.get(), { name: "win1" });
win1.open("2.html", { name: "win2" }).then((win2) => {
// Listen message from win2
win2.$on("from_win2", (msg) => {
console.log("[from_win2]", msg);
});
// After win2 is loaded, you can send message to win2
win2.on("loaded", () => {
win2.$emit("emit_win2", "Hello win2, I am win1");
});
});
win1.$on("win1_on", (msg) => {
console.log("[win1_on]", msg);
});
// wm global message
wm.$on("global_msg", (msg) => {
console.log("[global_msg]", msg);
});
2.html
// Get win2 by the current nw window
const win2 = wm(nw.Window.get());
// Or get win2 by name
// const win2 = wm("win2");
// Listen message
win2.$on("emit_win2", (msg) => {
console.log("[emit_win2]", msg);
});
// Send message
win2.$emit("from_win2", "Hello, I am win2");
// You can also send messages through win1
wm("win1").$emit("win1_on", "Hello win1, I am win2");
// Or
win2.parent.$emit("win1_on", "Hello win1, I am win2");
// wm global message
wm.$emit("global_msg", "Global message from win2");
Manage a nw.win as WM_Window, or get a WM_Window by name or nw.win.
maybeWin
WM_Window | nw.win | string - win or win's nameoptions
WM_Options (optional)name
string (optional) - Window's name.parent
WM_Window | nw.win | string (optional) - Parent window.modal
boolean (optional) - Whether to make the parent window modal.always_on_top
boolean (optional) - Whether the window is always on top of other windows.position
string (optional) - Controls where window will be put. Can benull
orcenter
ormouse
orparentCenter
x
number (optional) - Left offset from window frame to screen.y
number (optional) - Top offset from window frame to screen.width
number (optional) - window's width, including the window's frame.height
number (optional) - window's height, including the window's frame.
Returns WM_Window | undefined
- Returns wm window if maybeWin
is valid, otherwise returns undefined.
Open a new WM_Window.
url
string - URL to be loaded in the opened windowoptions
WM_OpenOptions (optional) - Open optionsname
string (optional) - Window's name.parent
WM_Window | nw.win | string (optional) - Parent window.modal
boolean (optional) - Whether to make the parent window modal.always_on_top
boolean (optional) - Whether the window is always on top of other windows.position
string (optional) - Controls where window will be put. Can benull
orcenter
ormouse
orparentCenter
- Other
nw.Window.open()
's options
callback
function (optional) - Callback when with the opened WM_Window instancewin
WM_Window - The opened WM_Window instance
Returns Promise<WM_Window>
- Resolve with the opened WM_Window instance.
Get a WM_Window by name or nw.win.
win
WM_Window | nw.win | string - win or win's name
Returns WM_Window | undefined
- Returns wm window if win
is valid, otherwise returns undefined.
Get all WM_Window instances.
callback
function (optional) - Callback with array of all WM_Window instances. Generally not needed because the return is fully synchronous.
Returns an array of all WM_Window instances.
Add a global listener.
Add a one-time global listener.
Remove a global listener.
Removes all global listeners, or those of the specified event name.
Synchronously calls each of the global listeners.
options
- Config optionsmodalStyle
string (optional) - Modal styleonAutoFocusTailWindow
function (optional) - Called when the tail window is auto focus
wm.config({
modalStyle: `.nwjs-window-manager-modal {
background: rgba(0, 0, 0, 0.3);
}`,
onAutoFocusTailWindow(tailWin) {
tailWin.requestAttention(2);
},
});
Extends from nw.win
, so you can use all properties and methods of nw.win
.
The following properties and methods are extended:
Get the managed nw window.
Get window's name.
Parent window.
Get child windows.
Whether to make the parent window modal.
Whether the window is modal.
Whether the window is always on top of other windows.
Get whether the window is minimize.
Get whether the window is maximize.
Open a child window. See wm.open()
.
Center relative to parent.
Center relative to screen.
Add a listener.
Add a one-time listener.
Remove a listener.
Removes all listeners, or those of the specified event name.
Synchronously calls each of the listeners.
WM_Options
: Addx/y/width/height
options, you can set the window's position and size when manage/get the nw.win bywm()
now.WM_Window
: AddwmWin.centerToParent()
andwmWin.centerToScreen()
methods.
- Fix: use
nw.require(...)
instead ofimport ... from ...
to importnode:events
.