Skip to content

JavaScript API

@lantharos/sabine is the page’s interface to Sabine: bridge calls and events, the window, guests, native video and the desktop services. It wraps the window.sabine object that Sabine installs in every page that gets the bridge, with types for all of it.

Terminal window
bun add github:Lantharos/Sabine#v0.117

Install the tag that matches the Sabine version your app depends on. Every export is a named export, so bundlers leave out the parts a page doesn’t import.

import { appWindow, events, invoke } from "@lantharos/sabine";

The functions call into window.sabine when they run, not when the module loads, so the package can be imported anywhere. Outside a Sabine window, or in a document without the bridge, they throw an Error saying that the package only works inside a Sabine window; check isAvailable() first in code that also runs in a browser.

Export Kind Section
isAvailable, sabine functions Availability
invoke, listen, bridge functions, object Bridge
fileUrl function Local files
app object App
events object Events
appWindow, region objects Window
popup object Popup
guest, Guest object, class Guests
activity object Activity
NativeVideo class Native video
clipboard object Clipboard
menu object Menus
dialog object Dialogs
downloads object Downloads
spellcheck object Spellcheck
print object Printing
shell object Shell
system object System
tray object Tray
function isAvailable(): boolean;

true when the page runs in a Sabine window and its document has the bridge. A document from an origin the app doesn’t trust has no bridge; see Bridge security.

function sabine(): SabineApi;

Returns the raw window.sabine object, and throws when it is missing. The package’s exports cover everything on it; use them instead.

Commands and Events explain the bridge.

function invoke<T = unknown>(name: string, params?: object, options?: InvokeOptions): Promise<T>;
interface CallOptions {
signal?: AbortSignal;
timeoutMs?: number;
}
interface InvokeOptions extends CallOptions {
body?: ArrayBuffer | ArrayBufferView | Blob;
}

Calls a command and resolves with its answer: the JSON value the handler returned, or a Uint8Array when it answered with bytes. params is sent as JSON and defaults to {}.

Option Default Meaning
signal Rejects the call with the signal’s reason when it aborts.
timeoutMs 60000 Rejects with a TimeoutError when no answer arrives in time. From 1 to 2147483647; other values throw a RangeError.
body Bytes sent with the call, up to 32 MiB. The handler reads them as command.body.

Typed bindings take CallOptions, since typed commands don’t take bytes.

The promise rejects with the handler’s error message, or when the command isn’t registered or not available to the page, the page already waits on 128 calls, or the page goes away. The full list is in Commands.

const controller = new AbortController();
const results = await invoke<SearchResult[]>("search.run", { query }, {
signal: controller.signal,
timeoutMs: 10_000,
});
function listen<T = unknown>(name: string, callback: (payload: T) => void): () => void;

Calls callback with each event of that name, and returns a function that stops listening. Bytes events arrive as a Uint8Array. Callbacks run in microtasks, in the order they were added.

const stop = listen<{ path: string }>("export.finished", ({ path }) => toast(path));
const bridge: {
commands(): string[];
invoke: typeof invoke;
send(name: string, params?: object): void;
listen: typeof listen;
};
Member Meaning
commands() Every command the page may call: the app’s own and Sabine’s, or only the ones its origin was allowed.
invoke, listen The functions above.
send(name, params) Calls a command without waiting for its answer, for frequent updates such as a position while dragging. It throws when the command isn’t available, holds no slot among the page’s pending calls, and gets no answer back. When the window’s queue is full, updates are dropped and the page hears sabine.bridge.dropped with { command }.
function fileUrl(path: string): string;

Turns an absolute path into a sabine://file/ URL that the app’s own pages can load, in apps with local_files = true. Backslashes become slashes, and each part of the path is percent-encoded. See Resources and files.

image.src = fileUrl("/home/ana/Pictures/beach.jpg");
takeOpenUrls(): Promise<string[]>;

Resolves with the URLs and documents the app was asked to open and the page hasn’t taken yet, including those from before the page loaded, and clears them. Call it on start and whenever events.openUrlsAvailable fires. See Links and documents.

import { app, events } from "@lantharos/sabine";
async function openPending() {
for (const url of await app.takeOpenUrls()) await openDocument(url);
}
events.openUrlsAvailable(openPending);
await openPending();

A page Sabine loads again on its own, as after a renderer crash, gets the URLs the lost page took once more.

quit(): Promise<void>;

Closes every window and ends the app, also an app that runs in the background or whose window hides when it closes, where appWindow.close() only hides the window.

setAutostart(enabled: boolean): Promise<void>;

Turns the app’s autostart entry on or off while it runs. Rejects when the app has no autostart entry; development runs resolve without changing anything. See Starting at login.

app.setBadge(badge) and app.setProgress(progress)

Section titled “app.setBadge(badge) and app.setProgress(progress)”
setBadge(badge: number | string | null): Promise<void>;
setProgress(progress: AppProgress): Promise<void>;
type AppProgress =
| number
| null
| { state: "normal" | "paused" | "error"; value: number }
| { state: "indeterminate" };

Show a badge or a progress bar on the app’s Dock or taskbar icon. A count of zero and null show no badge, a number is normal progress from 0 to 1, and null hides the bar. Text badges and indeterminate progress reject on Linux. See Dock and taskbar.

app.addRecentDocument(path) and app.clearRecentDocuments()

Section titled “app.addRecentDocument(path) and app.clearRecentDocuments()”
addRecentDocument(path: string): Promise<void>;
clearRecentDocuments(): Promise<void>;

Add a file to the desktop’s list of recent files, or take off every file the app added. Adding rejects when the path is not a file.

Typed listeners for the events Sabine sends. Each takes a callback and returns a function that stops listening.

events.appearanceChanged(({ colorScheme }) => applyTheme(colorScheme));
Listener Payload Fires when
appearanceChanged SystemAppearance The desktop switches between light and dark, changes its accent color, or the window’s glass changes material.
displaysChanged Display[] A display is connected or disconnected, or changes its bounds, work area, scale or refresh rate.
trayActivate TrayActivateEvent The tray icon or one of its menu items is activated.
globalShortcut GlobalShortcutEvent A registered global shortcut is pressed.
globalShortcutFailed GlobalShortcutFailedEvent The desktop refused a global shortcut.
singleInstance SingleInstanceEvent The app is launched again while it runs.
openUrlsAvailable null URLs or documents arrived; take them with app.takeOpenUrls().
rendererCrashed RendererCrashedEvent The page’s or a guest’s renderer stopped.
storeChanged StoreChangedEvent A key of the store changed, from any window or the app’s Rust side.
pen PenEvent A pen or its eraser comes near the screen or leaves it, touches or lifts, a barrel button goes down or up, or the pen tilts.
touchpadRotate TouchpadRotateEvent Two fingers turn on a touchpad, on Linux and macOS.
notificationClick NotificationClickEvent A desktop notification the window showed, or one of its buttons, was clicked.
notificationClose NotificationEvent One of its notifications was dismissed or closed.
notificationError NotificationErrorEvent One of its notifications could not be shown.
systemSuspend null The system is about to sleep.
systemResume null The system woke from sleep.
screenLocked null The screen locked.
screenUnlocked null The screen unlocked.
powerSourceChanged PowerSource The machine switched between battery and mains power, or its battery’s charge changed.
download DownloadEvent A download of the app’s page was requested, progressed or ended.
guestCreated GuestInfo A guest finished opening.
guestDestroyed GuestIdEvent A guest closed.
guestLoading GuestLoadingEvent A guest started or finished loading.
guestTitle GuestTitleEvent A guest’s title changed.
guestNavigated GuestNavigatedEvent A guest navigated.
guestNewWindow GuestNewWindowEvent A guest with the deny popup policy tried to open a window.
guestDownload GuestDownloadEvent A guest’s download was requested, progressed or ended.
guestShortcut GuestShortcutEvent A focused guest received an intercepted shortcut.
guestWheel GuestWheelEvent Horizontal scrolling over a guest was intercepted.
guestFavicon GuestFaviconEvent A guest’s favicon URLs changed.
interface TrayActivateEvent {
trayId: string;
itemId: string | null; // the menu item, or null for the icon itself
action: string | null;
checked: boolean | null; // a checkbox item's new state
}
interface GlobalShortcutEvent {
id: string;
action: string;
activationToken: string | null; // pass to appWindow.focus
}
interface GlobalShortcutFailedEvent {
id: string;
action: string;
message: string;
}
interface SingleInstanceEvent {
policy: "allowMultiple" | "reuseExisting" | "focusExisting";
arguments: string[]; // the second launch's command line, starting with the program
workingDirectory: string | null;
activationToken: string | null; // pass to appWindow.focus
}
interface RendererCrashedEvent {
guestId: string; // empty for the app's own page
code: number;
recovering: boolean; // false after three crashes within a minute
}
interface NotificationEvent {
id: string; // the id the app gave it, or tag:<tag> for one the page showed with a tag
}
interface NotificationClickEvent extends NotificationEvent {
action: string | null; // the button's action, or null for the notification itself
reply: string | null; // what people typed into a reply button's field
}
interface NotificationErrorEvent extends NotificationEvent {
message: string;
}
interface PowerSource {
onBattery: boolean;
batteryLevel: number | null; // percent, on machines with a battery
}
interface PenEvent {
tool: "pen" | "eraser";
near: boolean; // close enough to the screen for the window to see it
contact: boolean; // the tip or eraser touches the screen
buttons: number; // 1 is the barrel button, 2 the second one
tiltX: number | null; // degrees toward the right, -90 to 90; null when the pen doesn't tell
tiltY: number | null; // degrees toward the user, -90 to 90; null when the pen doesn't tell
}
interface TouchpadRotateEvent {
degrees: number; // turned since the last event, clockwise
x: number; // the pointer, in CSS pixels from the page's top left
y: number;
}

pen carries what pointer events can’t. Chromium’s pen input has no tilt, and its buttons stays 1 while the pen touches the screen whatever barrel buttons are held, so a drawing app reads both from the latest PenEvent. It reaches the page well under a millisecond after the window hears about the change, and only comes when something changed. near makes palm rejection simple: ignore touch pointers while it is true.

let pen: PenEvent | null = null;
events.pen((state) => (pen = state));
canvas.addEventListener("pointermove", (event) => {
if (event.pointerType === "touch" && pen?.near) return;
const erasing = pen?.tool === "eraser" || (pen?.buttons ?? 0) & 1;
draw(event.getCoalescedEvents(), { erasing, tilt: [pen?.tiltX ?? 0, pen?.tiltY ?? 0] });
});

A barrel button pressed while the pen hovers also clicks as a mouse button, as it does in browsers: the first one as the right button, the second as the middle one. Cancel contextmenu to use the first as a tool instead.

The guest payloads are listed under Guests. The desktop pages explain each event: Tray, Shortcuts, Single instance, Notifications, Power and idle and Appearance and shell.

Controls the window that shows the page. Window chrome and Lifecycle explain the concepts.

These take effect without an answer.

Method Effect
show() Shows the window.
hide() Hides the window.
focus(activationToken?) Brings the window forward. On Wayland, pass the token an event such as singleInstance or globalShortcut carried.
close() Closes the window, or hides it when the window hides on close. app.quit() ends the app.
minimize() Minimizes the window.
maximize() Maximizes the window.
toggleMaximize() Maximizes the window, or restores it when maximized.
setFullscreen(enabled) Enters or leaves fullscreen.
restore() Leaves fullscreen, maximized and minimized states.
startDrag() Starts moving the window with the pointer, from a pointerdown handler.

These return a promise that rejects when the desktop can’t make the change.

Method Effect
setTitle(title) Names the window, whatever the page’s document.title says. null hands the title back to the page.
setSize(width, height) Resizes the window, in CSS pixels.
setMinSize(width, height), setMaxSize(width, height) The smallest and largest the window can get. null lifts the limit.
setPosition(x, y) Moves the window’s top left corner, in the physical pixels of display bounds. Rejects on Wayland, where the desktop places windows.
setAlwaysOnTop(enabled) Keeps the window above other windows. On Wayland only with kestrel_window_v1.
setResizable(enabled) Whether people can resize the window.
requestAttention(critical?) Bounces the Dock icon or flashes the taskbar button until the window is focused. See Dock and taskbar.
unmaximize() Restores a maximized window.

The window shows the page’s document.title, and the app’s title while the page has none. A title from setTitle, or from control_window in Rust, wins over the page’s until it is set to null.

open(url: string, options?: OpenWindowOptions): Promise<number>;
close(id: number): Promise<void>;
overlaysSupported(): Promise<boolean>;
interface OpenWindowOptions {
title?: string;
width?: number;
height?: number;
minWidth?: number;
minHeight?: number;
resizable?: boolean;
alwaysOnTop?: boolean;
overlay?: boolean | OverlayPlacement;
}
interface OverlayPlacement {
anchor?: "top" | "top-left" | "top-right" | "bottom" | "bottom-left" | "bottom-right" | "left" | "right" | "center";
margin?: number;
}

open shows one of the app’s own pages in another window and resolves with the window’s id. The URL resolves against the page’s, and must stay on the app’s origin; other sites are rejected. The window opens like the app’s first window, with its chrome, glass and commands, as an ordinary window: shown, in the taskbar, and closed when it is closed. Its title shows while the page has no document.title. close(id) closes one of the app’s windows as its close button does.

overlay opens the page as an overlay, such as a hint over a game: transparent, frameless and out of the taskbar, above every other window, full-screen ones included, and leaving focus and the pointer to whatever is below it. It keeps to anchor of the screen at margin logical pixels from the edges; true means the top right corner, 24 pixels in. The window keeps the size it opens with.

const hint = await appWindow.open("hint.html", { width: 320, height: 72, overlay: { anchor: "top-right" } });
setTimeout(() => appWindow.close(hint), 4000);

On Linux an overlay is a surface on the overlay layer of wlr-layer-shell, so the compositor has to offer it: Kestrel offers it to sandboxed apps that declare windows = "capture" in [sandbox] or that the person allowed, and opening one elsewhere fails: open rejects and no window appears. overlaysSupported() says beforehand, so the app can show a notification instead. On Windows and macOS it is a topmost window that never activates and lets clicks through.

const settings = await appWindow.open("settings.html", { title: "Settings", width: 560, height: 420 });
await appWindow.close(settings);
Member Kind Meaning
visible boolean Whether the window is shown and the desktop hasn’t reported it out of sight, as for a minimized or fully covered window.
suspended boolean Whether Sabine runs the page at its background frame rate.
onVisibilityChanged(callback) (state: { visible, suspended }) => void Calls callback when either changes. Returns a function that stops listening.
maximized boolean Whether the window is maximized.
fullscreen boolean Whether the window covers its screen.
tiled boolean Whether the desktop tiled the window against a screen edge. Wayland only.
onStateChanged(callback) (state: { maximized, fullscreen, tiled }) => void Calls callback when any of these change. Returns a function that stops listening.
const stop = appWindow.onStateChanged(({ maximized, fullscreen, tiled }) => {
document.documentElement.classList.toggle("docked", maximized || fullscreen || tiled);
});
controlsOverlay(): Promise<ControlsOverlay>;
onControlsOverlayChanged(callback: (overlay: ControlsOverlay) => void): () => void;
interface ControlsOverlay {
left: number;
right: number;
height: number;
}

The top corners the system’s own window controls cover, in CSS pixels, which a page-drawn titlebar leaves free. With AppChrome on macOS the traffic lights cover the left; elsewhere the page draws its controls and nothing is covered. Every page also gets the values as the --sabine-controls-left, --sabine-controls-right and --sabine-controls-height CSS properties. onControlsOverlayChanged follows changes, such as entering fullscreen.

revealWhen(ready: PromiseLike<unknown>): void;

Keeps the window off screen until ready settles, so it first appears with the page’s real first view. Call it while the page starts. After five seconds the window shows the page anyway. See Frontend.

activationToken(): Promise<string | null>;

A token that lets another app’s window take focus, for handing something to that app because the person asked to. Pass it to the app as XDG_ACTIVATION_TOKEN in its environment, or as activation_token in a portal request. It works only while this window is in front, and is null where the desktop needs none.

identifier(): Promise<string | null>;

The window as a dialog of it names its parent. Pass it to SabineWindow::parent_window to open a window that stays over this one; see Dialogs for other apps’ windows. Linux exports the window for it as wayland:<handle>. It is null on other desktops and on compositors that can’t share windows.

restoredState<T = unknown>(): Promise<T | null>;
keepState(state: unknown): void;

keepState keeps what the window shows, at most 256 KiB as JSON, for a desktop that opens the window again after the session ends while it is open. Sabine writes the latest state once it stops changing, away from the page, and forgets it when the window closes or the app quits, or at once for null. restoredState gives a window the desktop opened again the state it last kept, and null to every other window. See Opening again after signing in.

inhibitShortcuts(enabled: boolean): Promise<void>;

While enabled and the window is focused, the desktop’s own keyboard shortcuts reach the page, for example to record a shortcut in a settings page. It rejects on desktops that don’t allow it, and on macOS until the app has the Accessibility permission.

setRegions(regions: WindowRegions): Promise<void>;
interface WindowRegions {
blur?: WindowRegion | null;
opaque?: WindowRegion | null;
input?: WindowRegion | null;
}

Replaces the window’s blur, opaque and input regions. A region left out returns to its default: blur behind the whole window, nothing opaque, and input everywhere. Build regions with region. See Glass and effects.

setControlRegions(regions: WindowControlRegion[]): Promise<void>;
interface WindowControlRegion {
action: "minimize" | "maximize" | "close";
x: number;
y: number;
width: number;
height: number;
}

Replaces the areas Sabine answers as the window’s minimize, maximize and close buttons, which start as the window’s control_regions. Positions are in the page’s CSS pixels, and a negative x counts from the window’s right edge. A page whose buttons move, for example when a sidebar comes and goes, sends their new places so the buttons keep working while the page is busy. See Window chrome.

Builds regions for setRegions. Sizes and positions are in the page’s CSS pixels, so regions grow and shrink with the page when it is zoomed; corner radii stay as they are. Regions other than rect follow the window’s size as it resizes, and rounded corners turn square while the window is maximized, fullscreen or tiled.

Function Region
region.empty() Nothing.
region.rect(x, y, width, height) One fixed rectangle.
region.full() The whole window.
region.roundedRect(radius) The whole window, with rounded corners.
region.roundedLeft(width, radius) A strip width wide along the left edge, rounded on the left.
region.titlebarAndSidebar(sidebarWidth, titlebarHeight, radius) A titlebar across the top and a sidebar down the left.
region.contentAfterSidebar(sidebarWidth, titlebarHeight?) Everything right of the sidebar and below the titlebar.
region.contentAfterSidebarRoundedRight(sidebarWidth, titlebarHeight, radius) The same, with its right corners rounded.
import { appWindow, region } from "@lantharos/sabine";
await appWindow.setRegions({
blur: sidebarOpen ? region.roundedLeft(240, 12) : region.empty(),
opaque: region.contentAfterSidebarRoundedRight(sidebarOpen ? 240 : 0, 0, 12),
});
const popup: {
open(options?: PopupOptions): Promise<void>;
close(): Promise<void>;
};
interface PopupOptions {
x?: number;
y?: number;
width?: number;
height?: number;
html?: string;
url?: string;
}

One surface drawn over the window and its guests, such as a menu or a tooltip that must reach past a guest. It shows html or url at the given position in CSS pixels, has no bridge, and closes when the person clicks the page beneath it. Opening another popup replaces the first. The page receives popup.open and popup.close events through listen as it appears and goes away.

await popup.open({ x: 40, y: 80, width: 280, height: 160, html: "<p>Saved</p>" });
await popup.close();

Guests covers guest views in depth.

const guest: {
create(options: GuestCreateOptions): Promise<Guest>;
list(): Promise<GuestInfo[]>;
get(id: string): Promise<GuestInfo>;
destroy(id: string): Promise<GuestIdEvent>;
navigate(id: string, target: GuestNavigateTarget): Promise<void>;
setBounds(id: string, bounds: GuestBounds): Promise<GuestInfo>;
setVisible(id: string, visible: boolean): Promise<GuestInfo>;
setCovered(covered: boolean): Promise<void>;
capturePreview(id: string): Promise<GuestPreview>;
focus(id: string): Promise<void>;
reload(id: string, options?: { ignoreCache?: boolean }): Promise<void>;
goBack(id: string): Promise<void>;
goForward(id: string): Promise<void>;
setZoom(id: string, factor: number): Promise<GuestInfo>;
executeJavaScript(id: string, code: string): Promise<void>;
downloadAction(
downloadId: string,
action: GuestDownloadAction,
options?: GuestDownloadOptions,
): Promise<void>;
};
Method Effect
create(options) Opens a guest and resolves with its handle.
list() Every guest, bottom to top. Sabine’s own surfaces, such as the popup, aren’t listed.
get(id) One guest’s state.
destroy(id) Closes a guest.
navigate(id, target) Loads a URL, { url } or { html }.
setBounds(id, bounds) Moves and resizes a guest.
setVisible(id, visible) Shows or hides a guest.
setCovered(covered) Hides every guest while the page draws over them, and shows them again.
capturePreview(id) A PNG of the guest as a data URL. Rejects while the guest isn’t on screen.
focus(id) Gives a guest keyboard focus and raises it.
reload(id, options) Reloads, bypassing the cache with ignoreCache.
goBack(id), goForward(id) Moves through history. Rejects when there is nowhere to go.
setZoom(id, factor) Zooms, from 0.25 to 5.
executeJavaScript(id, code) Runs a script in the guest’s main frame, without its result.
downloadAction(downloadId, action, options) Accepts, cancels, pauses or resumes a download.
class Guest {
readonly id: string;
constructor(id: string);
static create(options: GuestCreateOptions): Promise<Guest>;
get(): Promise<GuestInfo>;
navigate(target: GuestNavigateTarget): Promise<void>;
setBounds(bounds: GuestBounds): Promise<GuestInfo>;
setVisible(visible: boolean): Promise<GuestInfo>;
focus(): Promise<void>;
reload(options?: { ignoreCache?: boolean }): Promise<void>;
goBack(): Promise<void>;
goForward(): Promise<void>;
setZoom(factor: number): Promise<GuestInfo>;
executeJavaScript(code: string): Promise<void>;
capturePreview(): Promise<GuestPreview>;
destroy(): Promise<GuestIdEvent>;
}

A handle for one guest, with the guest methods bound to its id. new Guest(id) makes a handle for a guest that already exists, such as one the app created from Rust.

const tab = await Guest.create({
url: "https://example.com",
bounds: { x: 0, y: 48, width: 1024, height: 720 },
partition: "browser",
popupPolicy: "openGuest",
});
await tab.setZoom(1.25);
interface GuestBounds {
x: number;
y: number;
width: number;
height: number;
}
type GuestPopupPolicy = "deny" | "navigateSame" | "openGuest";
type SabinePermission =
| "camera" | "microphone" | "screen" | "geolocation" | "notifications" | "midi"
| "clipboard" | "pointer-lock" | "keyboard-lock" | "local-fonts" | "window-management"
| "idle-detection" | "storage-access" | "protected-media" | "file-system" | "sensors"
| "local-network" | "downloads";
interface GuestCreateOptions {
id?: string;
url?: string;
html?: string;
bounds: GuestBounds;
partition?: string;
allowBridge?: boolean; // default false
interceptedShortcuts?: string[]; // such as "Primary+K"
interceptHorizontalWheel?: boolean; // default false
visible?: boolean; // default true
popupPolicy?: GuestPopupPolicy; // default "deny"
allowDownloads?: boolean; // default true
permissions?: SabinePermission[]; // what the guest may ask people for, default none
backgroundColor?: string; // "#RRGGBB" or "#AARRGGBB"
}
type GuestNavigateTarget = string | { url: string } | { html: string };
interface GuestInfo {
id: string;
url: string;
title: string;
bounds: GuestBounds;
visible: boolean;
loading: boolean;
canGoBack: boolean;
canGoForward: boolean;
partition: string;
allowBridge: boolean;
popupPolicy: GuestPopupPolicy;
zoomFactor: number;
}
interface GuestPreview {
dataUrl: string; // a PNG
}
interface GuestIdEvent {
id: string;
}
interface GuestLoadingEvent {
id: string;
loading: boolean;
}
interface GuestTitleEvent {
id: string;
title: string;
}
interface GuestNavigatedEvent {
id: string;
url: string;
title: string;
canGoBack: boolean;
canGoForward: boolean;
}
interface GuestNewWindowEvent {
id: string;
url: string;
disposition: string; // such as "newForegroundTab", "newBackgroundTab", "newPopup" or "newWindow"
}
interface GuestDownloadEvent {
guestId: string;
downloadId: string;
url: string;
filename: string;
mimeType: string;
totalBytes: number;
receivedBytes: number;
state: "requested" | "progress" | "completed" | "cancelled" | "interrupted";
savePath: string;
error?: string; // why an interrupted download stopped, such as "networkFailed" or "fileNoSpace"
}
type GuestDownloadAction = "accept" | "cancel" | "pause" | "resume";
interface GuestDownloadOptions {
savePath?: string;
showDialog?: boolean;
}
interface GuestShortcutEvent {
id: string;
accelerator: string; // as listed in interceptedShortcuts
key: string;
repeat: boolean;
ctrlKey: boolean;
metaKey: boolean;
altKey: boolean;
shiftKey: boolean;
}
interface GuestWheelEvent {
id: string;
deltaX: number;
deltaY: number;
ctrlKey: boolean;
metaKey: boolean;
altKey: boolean;
shiftKey: boolean;
}
interface GuestFaviconEvent {
id: string;
favicons: string[];
}
const activity: {
begin(options?: ActivityOptions): Promise<ActivityHandle>;
list(): Promise<ActivityList>;
};
interface ActivityOptions {
name?: string; // up to 128 characters, default "activity"
keepRunning?: boolean; // default true
keepAwake?: "system" | "display"; // default none
}
interface ActivityHandle {
id: string;
name: string;
keepRunning: boolean;
keepAwake: "system" | "display" | null;
end(): Promise<{ id: string; ended: boolean }>;
}
interface ActivityList {
activities: { id: string; name: string; keepRunning: boolean; keepAwake: "system" | "display" | null }[];
keepingRunning: number;
}

An activity keeps the page running while its window is out of sight, instead of letting Sabine freeze it, unless it is begun with keepRunning: false. With keepAwake, it also keeps the system, or the screen too, from sleeping, with its name as the reason. It belongs to the page that began it and ends when that page reloads, navigates away, loses its renderer or its window closes. end() resolves with ended: false when the activity had already ended. A window’s pages keep up to 32 activities at once, and begin rejects beyond that. list() returns the activities that apply to this window, the page’s own and those the app began from Rust, and how many of them keep it running. See Lifecycle.

import { activity } from "@lantharos/sabine";
const busy = await activity.begin({ name: "Indexing" });
try {
await indexLibrary();
} finally {
await busy.end();
}
class NativeVideo extends EventTarget {
static isSupported(): boolean;
static create(src: string, options?: NativeVideoOptions): Promise<NativeVideo>;
readonly src: string;
currentTime: number;
readonly duration: number;
readonly paused: boolean;
readonly ended: boolean;
readonly seeking: boolean;
readonly readyState: number;
readonly error: NativeVideoError | null;
readonly videoWidth: number;
readonly videoHeight: number;
playbackRate: number;
volume: number;
muted: boolean;
loop: boolean;
readonly audioTracks: NativeVideoAudioTrack[];
readonly subtitleTracks: NativeVideoTrack[];
readonly activeCue: NativeVideoCue | null;
fastSeek(time: number): void;
play(): Promise<void>;
pause(): void;
setAudioTracks(tracks: NativeVideoAudioMix[]): Promise<void>;
selectSubtitleTrack(id: string | null): Promise<void>;
attach(element: Element, options?: NativeVideoAttachOptions): void;
detach(): void;
updateRect(): void;
destroy(): void;
}
interface NativeVideoAttachOptions {
cutout?: HTMLElement;
}
interface NativeVideoOptions extends NativeVideoAttachOptions {
element?: Element;
autoplay?: boolean;
loop?: boolean;
volume?: number;
muted?: boolean;
playbackRate?: number;
mixAudio?: boolean;
}
interface NativeVideoTrack {
id: string;
language: string | null;
label: string | null;
codec: string | null;
selected: boolean;
}
interface NativeVideoAudioTrack extends NativeVideoTrack {
volume: number;
}
interface NativeVideoAudioMix {
id: string;
volume?: number; // from 0 to 1, defaults to 1
}
interface NativeVideoCue {
text: string;
start: number;
end: number | null; // null when the cue lasts until the next one, as on macOS
}
interface NativeVideoError {
code: number;
message: string;
}

Plays media Chromium can’t decode on a native surface beneath the page. isSupported() is false in opaque windows. It dispatches loadedmetadata, loadeddata, canplay, canplaythrough, durationchange, timeupdate, play, playing, pause, waiting, seeking, seeked, ended, error, ratechange, volumechange, resize, trackschange and cuechange. Native video explains layout, sources and each platform.

const native = await NativeVideo.create(fileUrl(path), { element: stage, autoplay: true });
native.addEventListener("timeupdate", () => render(native.currentTime, native.duration));
const clipboard: {
read(options?: ClipboardReadOptions): Promise<ClipboardData>;
write(
data: Record<string, string | Uint8Array | Blob>,
options?: ClipboardWriteOptions,
): Promise<void>;
};
type ClipboardSelection = "clipboard" | "primary";
type ClipboardData = Record<string, string | Uint8Array>;
interface ClipboardReadOptions {
selection?: ClipboardSelection; // default "clipboard"
types?: string[];
}
interface ClipboardWriteOptions {
selection?: ClipboardSelection; // default "clipboard"
}

Reads and writes the desktop clipboard in any MIME type, for formats navigator.clipboard doesn’t carry, such as a file manager’s copied files. read resolves with the types that were there, text types as strings and others as Uint8Arrays; without types, it reads plain text, HTML, file lists and one image. write replaces the clipboard with every type you pass. The primary selection, which middle-click pastes, is Linux only. Available to the app’s own pages; elsewhere the methods throw. See Clipboard and drag and drop.

await clipboard.write({ "text/uri-list": "file:///home/ana/notes.txt\r\n" });
const { "text/plain": text } = await clipboard.read({ types: ["text/plain"] });
const menu: {
onContext(callback: (context: MenuContext) => void): () => void;
run(role: MenuRole): Promise<void>;
popup(items: MenuItem[], at: { x: number; y: number }): Promise<string | null>;
setAppMenu(items: MenuItem[]): Promise<void>;
appMenu(): Promise<MenuItem[]>;
activate(id: string): Promise<void>;
onSelect(callback: (selection: { id: string }) => void): () => void;
onAppMenuChanged(callback: (items: MenuItem[]) => void): () => void;
};
interface MenuContext {
x: number; // CSS pixels of the app's page
y: number;
guest: string | null;
editable: boolean;
selection: string;
link: string | null;
misspelled: string | null;
suggestions: string[];
canUndo: boolean;
canRedo: boolean;
canCut: boolean;
canCopy: boolean;
canPaste: boolean;
canSelectAll: boolean;
}
interface MenuItem {
id?: string;
label?: string;
role?: MenuRole;
accelerator?: string; // such as "Primary+Shift+S"
enabled?: boolean; // default true
checked?: boolean;
separator?: boolean;
items?: MenuItem[];
}

onContext hears each time someone asks for a context menu in the page or one of its guests, for the page to draw; Chromium opens none of its own. run does what a role does, such as pasting into the focused field. popup shows a menu at a point of the page in CSS pixels and resolves with the id chosen, or null. setAppMenu replaces the menu bar of every window; appMenu reads it with labels, accelerators and ids filled in, for pages that draw it, and activate chooses one of its items. onSelect hears the items chosen from the menu bar. Available to the app’s own pages; elsewhere the methods throw. See Menus.

const dialog: {
message(options: {
title?: string;
message?: string;
buttons?: string[]; // default ["OK"]
defaultButton?: number; // default the last
cancelButton?: number; // default the first
}): Promise<number>;
confirm(options: { title?: string; message?: string; okLabel?: string; cancelLabel?: string }): Promise<boolean>;
prompt(options: {
title?: string;
message?: string;
value?: string;
placeholder?: string;
okLabel?: string;
cancelLabel?: string;
}): Promise<string | null>;
};

Shows a dialog over the page and resolves with the button picked, whether OK was picked, or the text entered, null when cancelled. Available to the app’s own pages; elsewhere the methods throw. See Dialogs.

const downloads: {
pause(downloadId: string): Promise<void>;
resume(downloadId: string): Promise<void>;
cancel(downloadId: string): Promise<void>;
};
interface DownloadEvent {
downloadId: string;
url: string;
filename: string;
mimeType: string;
totalBytes: number; // 0 or less while the size is unknown
receivedBytes: number;
state: "requested" | "progress" | "completed" | "cancelled" | "interrupted";
savePath: string;
error?: string; // why an interrupted download stopped
}

Controls the downloads the app’s page started, which events.download reports. Where they go is up to the app’s Rust code or the person, see Downloads.

const spellcheck: {
get(): Promise<SpellcheckSettings>;
set(settings: Partial<SpellcheckSettings>): Promise<SpellcheckSettings>;
addWord(word: string): Promise<void>;
replace(word: string): Promise<void>;
};
interface SpellcheckSettings {
enabled: boolean;
languages: string[]; // such as "en-US", at most 16
}

Reads and changes spellchecking in the app’s profile, adds words to its custom dictionary, and replaces the misspelled word the last context menu was asked for over. See Spellcheck.

const print: {
page(): Promise<void>;
element(element: Element): Promise<void>;
files(paths: Iterable<string>, options?: { title?: string }): Promise<boolean>;
};
Method Effect
page() Prints the page, like window.print().
element(element) Prints one element on its own, with images, canvases and videos scaled to fit the paper.
files(paths, { title }) Prints PDF, image and text files as one document. title names the print job and defaults to the first file’s name. Resolves false when the person cancels on Linux, and rejects naming any file it can’t print.

See Printing.

const shell: {
openLink(url: string): Promise<void>;
showFolder(path: string): Promise<void>;
};

openLink opens an http:, https: or mailto: link in the person’s default browser or mail app, and showFolder opens a folder in their file manager. Other schemes and files are refused. See Appearance and shell.

await shell.openLink("https://example.com/help");
const store: {
get<T = unknown>(key: string): Promise<T | undefined>;
set(key: string, value: unknown): Promise<void>;
delete(key: string): Promise<boolean>;
entries(): Promise<Record<string, unknown>>;
clear(): Promise<void>;
};
interface StoreChangedEvent {
key: string;
value: unknown; // null once deleted
}

The app’s JSON values, shared with every window and its Rust side, and kept between runs. delete resolves to whether the store held the key. See Store and secrets.

const secrets: {
get(key: string): Promise<string | null>;
set(key: string, value: string): Promise<void>;
delete(key: string): Promise<boolean>;
};

Text in the system’s credential store, under keys of 1 to 256 bytes. Calls can wait while the system asks the person to unlock it, and reject where no credential store is available.

const system: {
appearance(): Promise<SystemAppearance>;
displays(): Promise<Display[]>;
power(): Promise<PowerState>;
watchIdle(seconds: number, callback: (state: "idle" | "active") => void): Promise<() => void>;
};
interface SystemAppearance {
colorScheme: "light" | "dark";
accentColor: string | null; // "#rrggbb" where the desktop has one
backdrop: "luca" | "maris" | "niko" | "rina";
}
interface PowerState {
source: PowerSource | null; // null until the desktop has reported it
locked: boolean;
}

The desktop’s light or dark preference and accent color, and the material of the window’s glass, see Glass materials on Kestrel. Pages also get the accent color as the --sabine-accent-color CSS property, and hear about changes through events.appearanceChanged.

interface Display {
id: number;
name: string | null;
bounds: DisplayRect;
workArea: DisplayRect | null; // without the taskbar, dock or menu bar; null on Wayland
scaleFactor: number;
refreshRate: number | null; // hertz
primary: boolean; // never on Wayland
}
interface DisplayRect {
x: number;
y: number;
width: number;
height: number;
}

displays() lists the displays connected to the computer, in the physical pixels windows are positioned in. events.displaysChanged follows changes.

const [display] = (await system.displays()).filter((display) => display.primary);
const width = 800 * display.scaleFactor;
await appWindow.setPosition(display.bounds.x + (display.bounds.width - width) / 2, display.bounds.y);

power() returns the power source and screen lock as the desktop last reported them. watchIdle calls back with "idle" once nobody used the machine for seconds, and with "active" when someone does again. It resolves with a function that stops the watch, which also stops when the page goes away. See Power and idle.

const tray: {
update(changes: TrayUpdate): Promise<void>;
};
interface TrayUpdate {
icon?: string | ArrayBuffer | ArrayBufferView | Blob | null; // a resource name, the image, or null for a plain dot
template?: boolean; // a macOS template image, tinted to match the menu bar
tooltip?: string | null;
menu?: TrayMenuItem[];
}
interface TrayMenuItem {
id?: string; // reported as itemId when chosen
label?: string;
action?: string;
enabled?: boolean;
type?: "normal" | "separator" | "checkbox" | "submenu";
checked?: boolean; // a checkbox item's state
items?: TrayMenuItem[]; // a submenu's items
}

Changes the app’s tray icon; fields left out stay as they are. It rejects when the app has no tray icon. See Tray.

await tray.update({
tooltip: "Syncing",
menu: [
{ id: "open", label: "Open" },
{ id: "pause", label: "Pause sync", type: "checkbox", checked: false },
{ type: "separator" },
{ id: "quit", label: "Quit" },
],
});

The package’s types also declare the global, so window.sabine is typed as SabineApi | undefined in pages that import the package. It has bridge, window, guest, activity, popup, system, print, downloads, spellcheck and, in the app’s own pages, clipboard, menu and dialog: the objects the exports above stand for. appWindow, popup, guest, activity, system, print, downloads, spellcheck, clipboard, menu and dialog look up their part of window.sabine on each use, so they have the same members; guest.create alone differs, resolving a Guest instead of its GuestInfo.