Skip to content

Window API

The Window class is the primary way to create and manage application windows. It wraps the native webview process and provides methods for navigation, IPC, window control, and access to sub-APIs (dialogs, menus, notifications, automation).

typescript
import { Window } from "tronbun";

Constructor

typescript
const win = new Window(options?: WindowOptions);

WindowOptions

OptionTypeDefaultDescription
titlestringWindow title bar text.
widthnumber800Window width in pixels.
heightnumber600Window height in pixels.
htmlstringInitial HTML content.
urlstringInitial URL to load.
initScriptstringJavaScript to inject before page loads.
alwaysOnTopbooleanfalseKeep window above other windows.
transparentbooleanfalseEnable window transparency.
opaquebooleantrueOpaque window background.
blurbooleanfalseEnable background blur effect.
decorationsbooleantrueShow window decorations (title bar, borders).
resizablebooleantrueAllow window resizing.
position{ x: number; y: number }Initial window position.
centerbooleanCenter window on screen.
hiddenbooleanfalseStart window hidden.
debugbooleanfalseEnable webview debug tools.

Properties

PropertyTypeDescription
idstringUnique window identifier.
automationWindowAutomationDOM automation API.
protocolProtocolCDP protocol API.
dialogWindowDialogNative dialog API.
menuWindowMenuMenu bar API.
contextWindowContextContext menu API.
notificationWindowNotificationNotification API.

Navigate the webview to a URL or local file path. In dev mode, uses file:// URLs for hot reload. In compiled mode, uses embedded HTML content.

typescript
await win.navigate("public/index.html");
await win.navigate("https://example.com");

setHtml(html: string)

Set the webview content directly from an HTML string.

typescript
await win.setHtml("<h1>Hello World</h1>");

setTitle(title: string)

Update the window title bar text.

typescript
await win.setTitle("My App - Untitled");

executeScript(script: string)

Execute JavaScript in the webview and return the result.

typescript
const title = await win.executeScript("document.title");
const count = await win.executeScript("document.querySelectorAll('li').length");

IPC Handlers

registerIPCHandler(name: string, handler: IPCHandler)

Register a handler for frontend-to-backend IPC calls.

typescript
win.registerIPCHandler("getData", async (params) => {
  const data = await fetchFromDatabase(params.id);
  return data;
});

handle(channel: string, handler: IPCHandler)

Alias for registerIPCHandler.

typescript
win.handle("save", async (data) => {
  await saveToFile(data);
  return { success: true };
});

unregisterIPCHandler(name: string)

Remove a previously registered IPC handler.

typescript
win.unregisterIPCHandler("getData");

IPCHandler Type

typescript
type IPCHandler = (data: any) => any | Promise<any>;

Window Control

Size & Position

typescript
await win.setSize(1024, 768);
await win.setPosition(100, 200);
await win.centerWindow();

const size = await win.getWindowSize();
// { width: 1024, height: 768 }

Window State

typescript
await win.minimizeWindow();
await win.maximizeWindow();
await win.restoreWindow();
await win.hideWindow();
await win.showWindow();

Appearance

typescript
await win.setOpacity(0.9);          // 0.0 to 1.0
await win.setResizable(false);
await win.setAlwaysOnTop(true);
await win.setTransparent();
await win.setOpaque();
await win.enableBlur();
await win.removeDecorations();
await win.addDecorations();

Close

typescript
await win.close();

Child Views

Create embedded webviews within the window. See Child Views for full details.

typescript
const child = await win.createChildView({
  bounds: { x: 0, y: 0, width: 300, height: 400 },
  url: "https://example.com",
});

await win.destroyChildView(child);

const view = win.getChildView("view-id");
const allViews = win.getChildViews();

Init Scripts

Register JavaScript that runs before every page load:

typescript
// Protected method — use in subclass
class MyWindow extends Window {
  constructor() {
    super({ title: "My App" });
    this.init(`
      window.appVersion = "1.0.0";
      console.log("Init script loaded");
    `);
  }
}

Events

Window events are dispatched through the internal onEvent callback:

EventDataDescription
window_resize{ width, height }Window was resized.
menu_click{ menuId }Menu item was clicked.
context_menu_click{ menuId }Context menu item was clicked.
notification_click{ id }Notification was clicked.
notification_close{ id }Notification was dismissed.
notification_action{ id, actionIndex }Notification action button was clicked.

Example

typescript
import { Window } from "tronbun";

const win = new Window({
  title: "My Application",
  width: 1024,
  height: 768,
  center: true,
  debug: true,
});

// Navigate to frontend
win.navigate("public/index.html");

// Register IPC handlers
win.handle("loadUser", async ({ userId }) => {
  return await db.getUser(userId);
});

win.handle("savePreferences", async (prefs) => {
  await fs.writeFile("prefs.json", JSON.stringify(prefs));
  return { saved: true };
});

See Also

Released under the GPLv3 License.