Events¶
Event callbacks live in a process-wide registry keyed by a view's
integer tag and the event name. The
Reconciler writes to it during a
commit; platform handlers read from it when a native widget fires.
Tag-based event routing between native views and Python callbacks.
Event props such as on_press and on_change stay in Python.
Native views send a tag and event name through the bridge:
- The reconciler strips callable props out of the payload sent to
native handlers and keeps them on the view's node
(
VNode.events). - When a view is committed, its node is registered here under its tag.
Handlers wire their platform listener once at view creation; the
listener calls
dispatch_eventwith the view's tag and the event name, and the registry reads the handler from the node. - Re-renders only change the node's handler table; no native call and no registry write is made when just a callback identity changes. A render pass that's rolled back restores the node's table with the rest of the node.
The set of event names present on an element is forwarded to handlers
under the EVENTS_PROP key (a
frozenset), so handlers that wire expensive listeners (scroll
delegates, gesture recognizers) can do so conditionally. Dispatching an
event nobody listens to is a cheap dict miss.
Classes:
| Name | Description |
|---|---|
EventRegistry |
Process-wide map of view tag to the node holding that view's handlers. |
Functions:
| Name | Description |
|---|---|
get_event_registry |
Return the process-wide |
dispatch_event |
Dispatch an event from a native view into Python. |
extract_events |
Split |
event_names |
Return the event-name set a handler should consult for |
Attributes:
| Name | Type | Description |
|---|---|---|
EVENTS_PROP |
Prop key carrying the |
|
GESTURES_PROP |
Prop key carrying gesture descriptors (see |
|
MOTION_PROP |
Prop key carrying a view's animated style and worklet handler bindings (RFC 0009). |
|
WorkletBinder |
Turns an event or gesture worklet into the handler binding the core runs, or |
EVENTS_PROP
module-attribute
¶
Prop key carrying the frozenset of event names wired on an element.
GESTURES_PROP
module-attribute
¶
Prop key carrying gesture descriptors (see pythonnative.gestures).
MOTION_PROP
module-attribute
¶
Prop key carrying a view's animated style and worklet handler bindings (RFC 0009).
WorkletBinder
module-attribute
¶
Turns an event or gesture worklet into the handler binding the core runs, or None without a core.
EventRegistry
¶
Process-wide map of view tag to the node holding that view's handlers.
A registered source is anything with an events attribute that
is a mapping of event name to callback, or None: the
reconciler registers each committed native node. Registration,
lookup, and dispatch happen on the application thread; bridge
backends queue native input there before dispatching it.
Methods:
| Name | Description |
|---|---|
register |
Route events for |
invoke |
Invoke a sync or async handler with component cancellation. |
clear |
Drop the registration for |
get |
Return the callback for |
has |
Return whether a callback is registered for |
dispatch |
Invoke the callback for |
reset |
Drop every registration (test helper). |
register
¶
Route events for tag to source.events, run in the owning component's scope.
invoke
¶
Invoke a sync or async handler with component cancellation.
get
¶
Return the callback for (tag, name), or None.
dispatch
¶
Invoke the callback for (tag, name) with args.
Returns:
| Type | Description |
|---|---|
bool
|
|
bool
|
it raised: exceptions never propagate into the platform's |
bool
|
UI thread; in dev mode they are routed to the RedBox via |
bool
|
|
bool
|
otherwise the traceback is printed), |
bool
|
is registered. |
dispatch_event
¶
Dispatch an event from a native view into Python.
This is the single entry point platform handlers call when a native listener fires.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tag
|
int
|
The view's reconciler-assigned tag. |
required |
name
|
str
|
Event name, the original prop name ( |
required |
*args
|
Any
|
Positional arguments forwarded to the user callback, preserving each prop's documented signature. |
()
|
Returns:
| Type | Description |
|---|---|
bool
|
Whether a callback was registered for |
extract_events
¶
extract_events(
props: Dict[str, Any],
bind_worklet: Optional[WorkletBinder] = None,
) -> Tuple[Dict[str, Any], Dict[str, Callable[..., Any]]]
Split props into native-safe props and Python event callbacks.
Rules:
- Top-level callables named
on_*become events under their prop name and are removed from the native payload. refresh_controldicts have their nestedon_refreshhoisted to the"on_refresh"event; the remaining keys (refreshing,tint_color) stay in the payload.gestureslists of gesture descriptors are serialized to plain dicts (handlers wire recognizers from them) while their callbacks are folded into per-gesture"gesture:<i>"routers.- The resulting payload carries
_pn_events(a frozenset of the event names present), so handlers can wire listeners conditionally and the prop differ can detect listener addition/removal without comparing closures. - With
bind_worklet, anon_*callback marked@workletruns in the renderer core instead (RFC 0009): it stays in_pn_eventsso the platform reports the event, and its handler binding goes in_pn_motion["events"]rather than in Python's registry. Gesture worklets are bound the same way. Animated style bindings thatresolve_stylecollected become_pn_motion["style"].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
props
|
Dict[str, Any]
|
Raw element props (already stripped of reconciler-owned keys). |
required |
bind_worklet
|
Optional[WorkletBinder]
|
Installs a worklet handler and returns its binding,
or |
None
|
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
|
Dict[str, Callable[..., Any]]
|
callables and |
Next steps¶
- The dataclasses those callbacks receive (
LayoutEvent,ScrollEvent,KeyPressEvent, ...) are documented under Typed event payloads. - See what carries the remaining, non-callable props in Mutation ops.
- See how component managers wire a platform listener once, at creation, in Native views.
- Read the other Python-side store the native layer reads on demand in Platform metrics.
- Follow a full commit end to end in Reconciliation.