widgetWindow
Create desktop widget windows. Each window hosts a UI script and supports drag, snap, transparency, context menus, and events.
import { widgetWindow } from "novadesk";Availability
Available in the Main script only.
Table of Contents
Constructor
Creates and shows a new desktop widget window.
If id is provided and a widget with the same id already exists, the existing widget is silently removed before the new one is created. If id matches a previously saved widget, the saved position and size are loaded as defaults (explicitly provided x, y, width, height override them).
Options:
| Option | Type | Default | Description |
|---|---|---|---|
id | string | "" | Unique identifier. Saved position/size are loaded by this ID on creation. |
width | number | — | Window width in pixels. |
height | number | — | Window height in pixels. |
x | number | — | Horizontal screen position in pixels. |
y | number | — | Vertical screen position in pixels. |
script | string | — | Path to the UI script. Must end with .ui.js (e.g. "ui/clock.ui.js"). Relative paths resolve from the entry script directory. |
backgroundColor | string | "rgba(0,0,0,0)" | Window background color or gradient. Supports rgb(), rgba(), linearGradient(), radialGradient(). |
opacity | number | string | 1 | Master window opacity. Accepts 0.0–1.0, 0–100, 0–255, or a percentage string like "75%". Scales the entire window including all drawn elements. |
draggable | boolean | true | Allow the user to drag the window. |
clickThrough | boolean | false | Mouse events pass through the window to whatever is behind it. |
keepOnScreen | boolean | false | Prevent dragging the window off-screen. |
snapEdges | boolean | true | Snap to screen edges and other widgets while dragging. |
show | boolean | true | Show the window immediately after creation. Pass false to create it hidden and call win.show() later. |
showInToolbar | boolean | false | Show in the Windows taskbar. |
toolbarIcon | string | "" | Path to the taskbar icon. |
toolbarTitle | string | "" | Title shown in the Windows taskbar. |
zPos | string | "normal" | Z-order position. See values below. |
zPos values (case-insensitive):
| Value | Behavior |
|---|---|
"ontopmost" | Always on top of everything, including other topmost windows. |
"ontop" | Above normal windows. |
"normal" | Normal stacking order (default). |
"onbottom" | Behind all app windows. Hidden when "Show Desktop" is triggered. |
"ondesktop" | Sits over the desktop. Ideal for wallpaper-style widgets. |
Script must end with .ui.js
If script does not end with .ui.js, the window is created but the script is not executed. The UI will be blank. Valid examples: "script.ui.js", "ui/clock.ui.js".
opacity vs backgroundColor alpha
opacity sets the Win32 layered window opacity — it scales the entire window including all UI elements. The alpha component of backgroundColor only affects the background fill. Setting opacity: 0 makes everything invisible. Setting backgroundColor: "rgba(0,0,0,0)" only makes the background transparent while UI elements remain visible.
PARAMETERS
import { widgetWindow } from "novadesk";
const win = new widgetWindow({
id: "my-widget",
width: 400,
height: 300,
script: "script.ui.js",
backgroundColor: "rgb(10,10,10)",
snapEdges: true,
showInToolbar: true,
toolbarTitle: "My Widget"
});Window State
Shows the widget window. Fires the show event.
widgetWindow The widget instance (chainable).win.show();Hides the widget window. Fires the hide event.
widgetWindow The widget instance (chainable).win.hide();Destroys the widget window and releases all resources. Fires the close and closed events.
win.close();Destroys the widget window immediately without firing the close event. Use close() for normal shutdown; use destroy() when you need to remove the window silently.
win.destroy();Returns whether the window is visible.
boolean true if the window is currently visible.if (!win.isVisible()) win.show();Returns whether the window has keyboard focus.
boolean true if the window currently has keyboard focus.console.log("Focused:", win.isFocused());Returns whether the window has been destroyed. Safe to call even after the window is closed. Check this before calling other methods on a window that may have been closed elsewhere.
boolean true if the window has been destroyed.if (!win.isDestroyed()) {
win.setProperties({ width: 500 });
}Minimizes the widget window. Fires the minimize event.
win.minimize();Restores a minimized widget window. Fires the unMinimize event.
win.unMinimize();Gives keyboard focus to the widget window.
win.setFocus();Removes keyboard focus from the widget window.
win.unFocus();Clears all UI elements and re-executes the widget's UI script. Stale ipcRenderer listeners from the previous run are automatically cleaned up before the new script executes.
win.refresh();Properties
Updates one or more window properties at runtime.
PARAMETERS
widgetWindow The widget instance (chainable).win.setProperties({ width: 600, height: 400 });
win.setProperties({ backgroundColor: "rgb(30,30,30)", draggable: false });
win.setProperties({ show: false }); // equivalent to win.hide()Returns the current state of all window properties. Note: zPos is returned as a number (-2 to 2), not a string.
object An object with id, x, y, width, height, draggable, clickThrough, keepOnScreen, snapEdges, showInToolbar, toolbarIcon, toolbarTitle, show (live visibility), windowOpacity (0–255), backgroundColor, zPos (as a number), and script.const props = win.getProperties();
console.log("Position:", props.x, props.y);
console.log("Size:", props.width, "x", props.height);Position and Size
Sets the position and/or size of the window in one call. Throws TypeError if the argument is not an object.
PARAMETERS
widgetWindow The widget instance (chainable).win.setBounds({ x: 100, y: 100, width: 500, height: 400 });
win.setBounds({ width: 600 }); // change only widthReturns the current position and size of the window using screen coordinates.
object An object with x, y, width, and height in screen coordinates. Returns null if the window handle is not available.const b = win.getBounds();
console.log("At:", b.x, b.y, "Size:", b.width, "x", b.height);Sets the window size without changing its position. Throws TypeError if fewer than two number arguments are provided.
PARAMETERS
widgetWindow The widget instance (chainable).win.setSize(800, 600);Returns the current window size.
object An object with width and height. Returns null if the window handle is not available.const { width, height } = win.getSize();
console.log(width, "x", height);Color and Opacity
Sets the window background color or gradient.
PARAMETERS
widgetWindow The widget instance (chainable).win.setBackgroundColor("rgba(20,20,30,0.95)");Returns the current background color.
string The current background color string.console.log("BG:", win.getBackgroundColor());Sets the master window opacity. Scales the entire window including all UI elements. Throws TypeError if the argument is not a number.
| Range | Interpretation |
|---|---|
0.0–1.0 | Fractional opacity (e.g. 0.75 = 75%) |
1.0–100.0 | Percentage (e.g. 75 = 75%) |
100.0–255.0 | Raw byte (e.g. 191 = 75%) |
PARAMETERS
widgetWindow The widget instance (chainable).win.setOpacity(0.75); // 75% opacity
win.setOpacity(128); // ~50% opacityContext Menu
Sets the right-click context menu. Replaces any previous menu and clears all previous action callbacks.
Menu item properties:
| Property | Type | Description |
|---|---|---|
text | string | Label text. |
action | function | Callback invoked when the item is clicked. |
type | string | "separator" inserts a horizontal divider. Separator items ignore all other properties. |
checked | boolean | Shows a checkmark when true. |
items | object[] | Nested sub-menu items (recursively supports the same schema). |
PARAMETERS
widgetWindow The widget instance (chainable).win.setContextMenu([
{ text: "Refresh", action: () => win.refresh() },
{
text: "Tools",
items: [
{ text: "Debug", checked: false, action: () => app.enableDebugging(true) }
]
},
{ type: "separator" },
{ text: "Close", action: () => win.close() }
]);Removes all custom context menu items and clears their registered callbacks.
widgetWindow The widget instance (chainable).win.clearContextMenu();Enables or disables the right-click context menu.
PARAMETERS
widgetWindow The widget instance (chainable).win.disableContextMenu(true); // no right-click menu
win.disableContextMenu(false); // restore menuControls whether built-in Novadesk context menu entries (e.g. Refresh, Close) are shown alongside custom items.
PARAMETERS
widgetWindow The widget instance (chainable).win.showDefaultContextMenuItems(false); // custom items onlyEvents
Registers an event listener on the widget window. Mouse events pass a Mouse Event Object to the callback. Throws TypeError if the callback is not a function or the event name is empty.
Supported events:
| Event | Trigger |
|---|---|
show | Window became visible |
hide | Window was hidden |
focus | Window gained keyboard focus |
unFocus | Window lost keyboard focus |
minimize | Window was minimized |
unMinimize | Window was restored from minimized state |
move | Window position changed |
refresh | UI script was refreshed |
close | Window is about to close (fired by close(), not by destroy()) |
closed | Window has been fully destroyed |
mouseOver | Mouse entered the window area |
mouseLeave | Mouse left the window area |
mouseMove | Mouse moved over the window |
mouseDown | Any mouse button was pressed |
mouseUp | Any mouse button was released |
click | Left click released on the window |
right-click | Right click released on the window |
double-click | Left button double-clicked |
scroll-up | Mouse wheel scrolled up |
scroll-down | Mouse wheel scrolled down |
PARAMETERS
widgetWindow The widget instance (chainable).win.on("click", (e) => {
console.log("Clicked at:", e.__clientX, e.__clientY);
});
win.on("mouseMove", (e) => {
console.log("Mouse:", e.__clientX, e.__clientY);
});
win.on("close", () => {
console.log("Window closing");
cleanup();
});Native Interop
Returns the native window handle. Useful when passing to a native addon that needs a parent window.
number The native Windows HWND as a number.const hwnd = win.getHandle();Returns the raw Widget pointer. Useful when passing the widget reference to a native addon.
number The internal native Widget pointer as a number.const ptr = win.getInternalPointer();Returns the current window title.
string The window title string.console.log("Title:", win.getTitle());Runtime Overrides
When the Ctrl key is held down, the runtime temporarily overrides some interaction settings:
- Dragging works even when
draggable: false - Click-through widgets become interactable
- Snap behavior can be bypassed for precise placement
Practical Examples
Basic widget with tray and context menu
import { widgetWindow, tray, app } from "novadesk";
const win = new widgetWindow({
id: "demo",
width: 400,
height: 300,
script: "script.ui.js",
backgroundColor: "rgb(10,10,10)",
snapEdges: true
});
const appTray = new tray(path.join(__dirname, "assets", "icon.ico"));
appTray.setToolTip("My Widget");
appTray.on("click", () => win.show());
win.setContextMenu([
{ text: "Refresh", action: () => win.refresh() },
{ type: "separator" },
{ text: "Exit", action: () => app.exit() }
]);Hidden window revealed on demand
const win = new widgetWindow({
id: "popup",
width: 300,
height: 200,
script: "popup.ui.js",
show: false // start hidden
});
ipcMain.on("show-popup", () => win.show());
ipcMain.on("hide-popup", () => win.hide());Resize window based on content
const win = new widgetWindow({
id: "dynamic",
width: 300,
height: 100,
script: "dynamic.ui.js"
});
ipcMain.on("content-changed", (event, payload) => {
const newHeight = 60 + payload.itemCount * 30;
win.setSize(300, newHeight);
});Dynamic opacity on hover
const win = new widgetWindow({
id: "fade",
width: 300,
height: 200,
script: "fade.ui.js",
opacity: 0.4
});
win.on("mouseOver", () => win.setOpacity(1.0));
win.on("mouseLeave", () => win.setOpacity(0.4));