The event system lets backend and frontend communicate in both directions: the
frontend can listen to any named event emitted by the backend, and can also
send targeted events to the frontend/other windows. The listener registry lives in
the backend's EventManager; the API is provided by the event module of
@zturnlibs/ztron-api.
Actual signatures (from packages/api/src/event.ts):
Typical usage:
The full WindowEventName list from packages/api/src/window.ts (verbatim):
Of these, suspended/resumed are names reserved for mobile lifecycles; the
desktop host never fires them; the four drag-* entries correspond to file
drag & drop (ztron://drag-enter/over/drop/leave). event.ts also exports
the ZtronEvent constant enum (the counterpart of upstream TauriEvent;
e.g. ZtronEvent.WINDOW_RESIZED = "ztron://resize"), and window instances offer
convenience methods like onResized/onMoved/onScaleChanged/onThemeChanged/onDragDropEvent.
A one-shot invoke can only return a single value; when the backend needs to
push continuously, pass { kind: "channel", id } in the command arguments —
the backend obtains a handle via ctx.getChannel(id) and pushes repeatedly
with handle.send(...) and finishes with handle.end(). The hello example's
m3:stream command is exactly this pattern (M1_EVENTS_CHANNEL_WINDOW_OK
verified).
On the plugin side there is also a plugin:*|__listener contract (e.g. the
log plugin pushing logs to a webview target); the P2 plugins page will expand
on it.
适用版本:ztron 0.3.1