plushie/subscription
Subscription types for ongoing event sources.
Return subscriptions from the subscribe callback. The runtime
diffs the list each cycle using key/1: subscriptions with the
same key are considered identical and kept alive; new keys trigger
a subscribe message to the renderer, and removed keys trigger an
unsubscribe.
Timer subscriptions have a tag that appears in the TimerTick event, giving each timer a stable identity.
Renderer subscriptions use a tag for internal management only; this tag never appears in delivered events. Their identity is derived from (kind, window_id).
Subscriptions can be scoped to a specific window using set_window
or for_window. Window-scoped subscriptions only receive events
from the given window; unscoped subscriptions receive events from
all windows.
Types
Event source kinds for renderer subscriptions.
pub type RendererSubKind {
KeyPress
KeyRelease
ModifiersChanged
WindowClose
WindowEvent
WindowOpen
WindowResize
WindowFocus
WindowUnfocus
WindowMove
PointerMove
PointerButton
PointerScroll
PointerTouch
Ime
ThemeChange
AnimationFrame
FileDrop
AllEvents
}
Constructors
-
KeyPress -
KeyRelease -
ModifiersChanged -
WindowClose -
WindowEvent -
WindowOpen -
WindowResize -
WindowFocus -
WindowUnfocus -
WindowMove -
PointerMove -
PointerButton -
PointerScroll -
PointerTouch -
Ime -
ThemeChange -
AnimationFrame -
FileDrop -
AllEventsCatch-all: receives all renderer events.
pub type Subscription {
Every(interval_ms: Int, tag: String)
Renderer(
kind: RendererSubKind,
max_rate: option.Option(Int),
window_id: option.Option(String),
)
}
Constructors
-
Every(interval_ms: Int, tag: String)Timer that fires every
interval_msmilliseconds. Delivers a TimerTick event with the given tag to update. -
Renderer( kind: RendererSubKind, max_rate: option.Option(Int), window_id: option.Option(String), )A renderer event subscription. The renderer filters and delivers matching events to the runtime.
Unique identity for subscription diffing.
pub type SubscriptionKey {
TimerKey(interval_ms: Int, tag: String)
RendererKey(kind: String, window_id: option.Option(String))
}
Constructors
-
TimerKey(interval_ms: Int, tag: String) -
RendererKey(kind: String, window_id: option.Option(String))Renderer sub identity is (kind, window_id). Two renderer subs of the same kind and window scope are the same subscription.
Values
pub fn every(interval_ms: Int, tag: String) -> Subscription
Timer that fires every interval_ms milliseconds. Delivers a
TimerTick event with the given tag to update.
pub fn for_window(
window_id: String,
subscriptions: List(Subscription),
) -> List(Subscription)
Scope a list of subscriptions to a specific window. Convenience
for applying set_window to each subscription.
subscription.for_window("editor", [
subscription.on_key_press(),
subscription.on_pointer_move() |> subscription.set_max_rate(60),
])
pub fn get_max_rate(sub: Subscription) -> option.Option(Int)
Get the max_rate from any subscription. Returns None for timer subscriptions and renderer subs without a rate.
pub fn get_window_id(sub: Subscription) -> option.Option(String)
Get the window_id scope from a subscription. Returns None for timer subscriptions and unscoped renderer subs.
pub fn key(sub: Subscription) -> SubscriptionKey
Compute the unique key for a subscription (used for diffing).
pub fn on_animation_frame() -> Subscription
Subscribe to animation frame events (vsync ticks).
pub fn on_file_drop() -> Subscription
Subscribe to file drop events. Also delivers file hovered and hover-left events.
pub fn on_ime() -> Subscription
Subscribe to IME (Input Method Editor) events for international text input.
pub fn on_modifiers_changed() -> Subscription
Subscribe to keyboard modifier state changes (shift, ctrl, alt, etc.).
pub fn on_pointer_button() -> Subscription
Subscribe to pointer button press and release events (mouse or touch).
pub fn on_pointer_move() -> Subscription
Subscribe to pointer movement events (mouse or touch). Also delivers enter and exit events for cursor enter/leave.
pub fn on_pointer_scroll() -> Subscription
Subscribe to pointer scroll events.
pub fn on_pointer_touch() -> Subscription
Subscribe to touch events (pressed, moved, lifted, lost).
pub fn on_theme_change() -> Subscription
Subscribe to system theme changes (light/dark mode).
pub fn on_window_close() -> Subscription
Subscribe to window close request events.
pub fn on_window_event() -> Subscription
Subscribe to all window events (resize, move, focus, etc.). If both this and a specific subscription (e.g. on_window_resize) are active, matching events are delivered twice.
pub fn on_window_focus() -> Subscription
Subscribe to window focus gained events.
pub fn on_window_unfocus() -> Subscription
Subscribe to window focus lost events.
pub fn set_max_rate(sub: Subscription, rate: Int) -> Subscription
Set the max_rate on a renderer subscription. The renderer coalesces events beyond this rate (events per second). Has no effect on timer subscriptions.
A rate of zero means “track only, never emit”: the renderer tracks the event source but suppresses delivery entirely.
pub fn set_window(
sub: Subscription,
window_id: String,
) -> Subscription
Scope a subscription to a specific window. The renderer only delivers events from the given window. Without a window scope, events from all windows are delivered.
pub fn timer_tag(sub: Subscription) -> String
Get the tag from a timer subscription. Returns the tag that appears in TimerTick events.
pub fn wire_kind(sub: Subscription) -> String
Wire format kind string for a subscription.
pub fn wire_tag(sub: Subscription) -> String
Derive the wire tag sent to the renderer. The tag is the stable identity for this subscription entry in the renderer’s storage. Window-scoped subscriptions include the window_id so they don’t collide with global subscriptions of the same kind.