xnano.context
xnano.context
¶
xnano.context
Access the current event, application state, runtime, device, cursor, and layout from an event hook.
Classes:
-
Context–Values and controls available inside an event hook.
Context
dataclass
¶
Context(
event: "Event",
terminal: "Runtime[StateT]",
state: StateT,
request: "Request | None" = None,
)
Bases: Generic[StateT]
Values and controls available inside an event hook.
Use the event-specific shortcuts such as keyboard_event and
mouse_event, read or update application state, move focus, or access
the current cursor, device, actions, and stage.
Attributes:
-
event('Event') –Event that triggered the hook.
-
terminal('Runtime[StateT]') –Terminal or runtime handling the event.
-
state(StateT) –Application state shared with the runtime.
-
host('Runtime[StateT]') –Session handling the event.
-
runtime('Runtime[StateT]') –Runtime handling the event.
-
surface(str) –Active presentation surface.
-
request('Request | None') –HTTP request that triggered the hook, if any.
-
tick_event('TickEventData | None') –Tick payload that triggered the hook, if any.
-
keyboard_event('KeyboardEventData | None') –Keyboard payload that triggered the hook, if any.
-
mouse_event('MouseEventData | None') –Mouse payload that triggered the hook, if any.
-
cursor('Cursor') –Cursor controls for the active runtime.
-
device('Device') –Device controls for the active runtime.
-
actions('Actions') –Synthetic action performer.
-
stage('Stage') –Current layout stage.
-
focused_group(str | None) –Name of the focused field group.
Example
def handle_key(ctx: Context[dict[str, int]]) -> None: ... if ctx.keyboard_event is not None: ... ctx.state["keys"] += 1
Methods:
-
get_state–Return the shared application state.
-
focus–Focus the field labeled
groupon any attached grid. -
blur–Clear field focus on the active runtime.
-
is_focused–Return whether the field labeled
groupcurrently holds focus. -
call_soon–Schedule
callbackto run on the UI thread before the next pump. -
field_area–Return the last painted area for field
name, if known. -
scroll–Return a scroll handle for
Field(scroll=...)labeledgroup. -
get_scroll–Return scroll state for
group, orNoneif it is unavailable. -
with_event–Return a copy carrying a different event.
-
with_scope–Return a shallow copy with the given fields replaced.
-
has_clipboard_event–Return whether this context contains a clipboard event.
-
has_focus_event–Return whether this is a focus event.
-
has_keyboard_event–Return whether this is a keyboard event.
-
has_mouse_event–Return whether this is a mouse event.
-
has_resize_event–Return whether this is a resize event.
terminal
instance-attribute
¶
Terminal or offscreen session handling the event.
request
class-attribute
instance-attribute
¶
HTTP request that triggered the hook, if any.
tick_event
property
¶
Tick payload when this context was triggered by a tick.
keyboard_event
property
¶
Keyboard sub-event when triggered by a keyboard event.
mouse_event
property
¶
Mouse sub-event when triggered by a mouse event.
render_size
property
¶
Current viewport breakpoint tier.
The same value grid_render_<size> / compose_<size> dispatch
on — one of "extra_small", "small", "medium",
"large", "extra_large" — derived from the live window
width. Read it from any hook to branch on size without declaring a
per-tier render method.
get_state
¶
Return the shared application state.
Raises:
-
RuntimeError–If no state was attached to this context.
Source code in xnano/context.py
focus
¶
Focus the field labeled group on any attached grid.
Source code in xnano/context.py
blur
¶
Clear field focus on the active runtime.
Source code in xnano/context.py
is_focused
¶
call_soon
¶
call_soon(
callback: "Callable[..., Any]", *args: Any
) -> None
Schedule callback to run on the UI thread before the next pump.
The thread-safe bridge for updating grid state from a worker thread: the callback runs on the runtime's own thread, so it can freely mutate components/fields without racing the renderer.
Source code in xnano/context.py
field_area
¶
field_area(name: str) -> 'Area | None'
Return the last painted area for field name, if known.
Reads the always-on layout map so viewports and chrome math can use measured slot sizes instead of guessing.
Source code in xnano/context.py
scroll
¶
scroll(group: str) -> 'ScrollHandle | None'
Return a scroll handle for Field(scroll=...) labeled group.