@gd-kirie/platform
v0.6.3
Published
Downloads
1,847
Readme
@gd-kirie/platform
@gd-kirie/platform exposes browser-side desktop capabilities supplied by the
Godot host. Create the client from an existing @gd-kirie/ipc-eventa context.
import { createContext } from "@gd-kirie/ipc-eventa";
import { createPlatformClient } from "@gd-kirie/platform";
const eventa = createContext();
const platform = createPlatformClient(eventa.context);
await platform.hostWindow.setAlwaysOnTop(true);
const windowBounds = await platform.hostWindow.getBounds();
const displayBounds = await platform.hostWindow.getCurrentDisplayBounds();
const pointer = await platform.hostWindow.getPointerPosition();
const windowState = await platform.hostWindow.getState();
console.log(windowBounds, displayBounds, pointer, windowState);
eventa.dispose();Request/response capabilities are methods on the returned client. Host-initiated
events are exported @moeru/eventa contracts; subscribe to them on the same
context and the returned function unsubscribes.
External URLs
Use openExternalUrl() to open an absolute HTTP or HTTPS URL with the system
browser:
await platform.openExternalUrl("https://example.com");The Godot host rejects relative URLs and other URI schemes. It uses
OS.shell_open()
to select the system browser.
Use openApplicationDataDirectory() to open the current Godot application's
data directory and receive its absolute path:
const path = await platform.openApplicationDataDirectory();This capability does not accept an arbitrary path.
Host window
- window position and size in screen coordinates
- current display position and size in screen coordinates
- pointer position relative to the host window, including while it is click-through
- pointer passthrough
- native move and resize gestures
- always-on-top
- centering on the current display
- visibility, focus, and minimized-state snapshots and change events
Pointer coordinates use host-window pixels and are not normalized to the
browser viewport. getPointerPosition() returns a single snapshot.
getState() returns one lifecycle snapshot and starts native state observation.
Call it once during setup. Later changes arrive as hostWindowStateChanged
events; subscribe with context.on(hostWindowStateChanged, ({ body }) => ...)
and call the returned function to unsubscribe.
Global shortcuts
The macOS and Windows hosts can register a shortcut that remains active while the Godot window is hidden or unfocused:
const shortcut = {
keycode: 0x4b, // Godot Key.K
shiftPressed: false,
altPressed: false,
ctrlPressed: false,
metaPressed: false,
commandOrControlAutoremap: true,
};
await platform.globalShortcuts.register(shortcut, ({ state }) => {
console.log(state); // "pressed" or "released"
});
await platform.globalShortcuts.unregister(shortcut);keycode uses Godot's logical Key values. Every modifier field is required;
commandOrControlAutoremap selects Command on macOS and Control on Windows.
One onKeyEvent handler receives both states; keyboard auto-repeat does not
produce extra calls.
Desktop notifications
Desktop notifications are available on macOS 11 or later and on Windows 10 version 1607 or later:
import { notificationActivated } from "@gd-kirie/platform";
const stop = context.on(notificationActivated, ({ body }) => {
if (body)
console.log(`The user clicked ${body.id}`);
});
await platform.notifications.show({
id: "assistant-answer-42",
title: "AIRI",
body: "The answer is ready.",
});
stop();The first macOS show() requests permission. Windows does not show a
permission prompt; show() fails when notifications are turned off. IDs and
titles must not be empty. With multiple Platform hosts, a click reaches only
the Eventa context that published the notification. Click events end when the
Platform host is disposed and do not survive a cold launch. The Windows
identity follows the running executable. Linux backends are not implemented.
System back
Android delivers the system Back button as a Platform event. The application decides what Back means:
import { backRequested } from "@gd-kirie/platform";
const stop = context.on(backRequested, () => {
console.log("The system Back button was pressed.");
});
stop();To handle Back instead of letting Android close the application, set
SceneTree.quit_on_go_back
to false. The event is not emitted on platforms without a system Back button.
