xnano.hooks
xnano.hooks
¶
xnano.hooks
Handle events by decorating methods on a BaseGrid subclass::
from xnano import hooks
@hooks.on_keyboard("q")
def quit(self, ctx): ...
Functions:
-
on_keyboard–Register a keyboard event hook.
-
on_mouse–Register a mouse event hook.
-
on_click–Register a click handler for a grid layout field, or a group.
-
on_tick–Register a tick hook, optionally at a fixed interval.
-
on_event–Register an event hook that triggers on every detected event.
-
on_resize–Register a hook fired on terminal resize events.
-
on_focus–Register a focus hook for the terminal window, a field, or a group.
-
on_clipboard–Register a hook fired on clipboard paste events.
-
on_state–Fire the decorated handler based on the application state.
-
on_field–Fire the decorated handler based on this grid's own field values.
-
on_poll–Register a poll hook that fires on idle event waits or every frame.
-
on_action–Bind a prebuilt
Actionas a hook trigger.
Attributes:
-
KeyboardEventKind(TypeAlias) –The kind of keyboard event a handler filters by.
-
MouseEventKind(TypeAlias) –The kind of mouse event a handler filters by.
-
FocusHookKind(TypeAlias) –Which half of a focus transition a handler fires on.
-
PollWhen(TypeAlias) –When a poll hook fires — once per idle event wait, or every frame.
KeyboardEventKind
module-attribute
¶
The kind of keyboard event a handler filters by.
Values
"press": A key was pressed down.
"release": A key was released.
"repeat": A key was held down and is repeating.
MouseEventKind
module-attribute
¶
MouseEventKind: TypeAlias = Literal[
"press",
"release",
"drag",
"move",
"scroll_up",
"scroll_down",
"scroll_left",
"scroll_right",
]
The kind of mouse event a handler filters by.
FocusHookKind
module-attribute
¶
Which half of a focus transition a handler fires on.
PollWhen
module-attribute
¶
When a poll hook fires — once per idle event wait, or every frame.
on_keyboard
¶
on_keyboard(
key: KeyboardBinding,
/,
*keys: KeyboardBinding,
kind: KeyboardEventKind | None = None,
) -> Callable[[EventHookFunction], EventHookFunction]
on_keyboard(
*,
key: KeyboardBinding,
kind: KeyboardEventKind | None = None
) -> Callable[[EventHookFunction], EventHookFunction]
on_keyboard(
handler: EventHookFunction,
/,
*,
key: KeyboardBinding | None = None,
kind: KeyboardEventKind | None = None,
) -> EventHookFunction
on_keyboard(
handler_or_key: "EventHookFunction | KeyboardBinding | None" = None,
/,
*keys: KeyboardBinding,
key: KeyboardBinding | None = None,
kind: KeyboardEventKind | None = None,
) -> "EventHookFunction | Callable[[EventHookFunction], EventHookFunction]"
Register a keyboard event hook.
Example
@on_keyboard("q") def quit(self, ctx): ...
@on_keyboard("enter", kind="press") def submit(self): ...
Source code in xnano/hooks.py
on_mouse
¶
on_mouse(
button: MouseButton,
/,
*,
field: str | None = None,
kind: MouseEventKind | None = None,
) -> Callable[[EventHookFunction], EventHookFunction]
on_mouse(
*,
button: MouseButton | None = None,
field: str | None = None,
kind: MouseEventKind | None = None
) -> Callable[[EventHookFunction], EventHookFunction]
on_mouse(
handler: EventHookFunction,
/,
*,
button: MouseButton | None = None,
field: str | None = None,
kind: MouseEventKind | None = None,
) -> EventHookFunction
on_mouse(
handler_or_button: "EventHookFunction | MouseButton | None" = None,
/,
*buttons: MouseButton,
button: MouseButton | None = None,
field: str | None = None,
kind: MouseEventKind | None = None,
) -> "EventHookFunction | Callable[[EventHookFunction], EventHookFunction]"
Register a mouse event hook.
Defaults to left-button press when button and kind are
omitted. Pass field to bind to a grid layout field's region.
Source code in xnano/hooks.py
on_click
¶
on_click(
handler: EventHookFunction,
/,
*,
field: str,
button: MouseButton = "left",
kind: MouseEventKind = "press",
) -> EventHookFunction
on_click(
*,
group: str,
button: MouseButton = "left",
kind: MouseEventKind = "press"
) -> Callable[[EventHookFunction], EventHookFunction]
on_click(
field_or_handler: "str | EventHookFunction | None" = None,
/,
*,
field: str | None = None,
group: str | None = None,
button: MouseButton = "left",
kind: MouseEventKind = "press",
) -> "EventHookFunction | Callable[[EventHookFunction], EventHookFunction]"
Register a click handler for a grid layout field, or a group.
A field-scoped handler binds to one field on the declaring grid
class. A group-scoped handler fires whenever any field sharing
group is clicked, regardless of which grid it lives on — see
Field(group=...).
Example
@on_click("body") def highlight_body(self, ctx): ...
@on_click(group="composer") def focus_composer(self, ctx): ctx.focus("composer")
Source code in xnano/hooks.py
on_tick
¶
on_tick(
handler_or_interval: "EventHookFunction | int | None" = None,
/,
*,
interval_milliseconds: int | None = None,
) -> "EventHookFunction | Callable[[EventHookFunction], EventHookFunction]"
Register a tick hook, optionally at a fixed interval.
Source code in xnano/hooks.py
on_event
¶
Register an event hook that triggers on every detected event.
on_resize
¶
on_focus
¶
on_focus(
field: str, /, *, kind: FocusHookKind | None = None
) -> Callable[[EventHookFunction], EventHookFunction]
on_focus(
*,
field: str | None = None,
group: str | None = None,
kind: FocusHookKind | None = None
) -> Callable[[EventHookFunction], EventHookFunction]
on_focus(
handler_or_field: "EventHookFunction | str | None" = None,
/,
*,
field: str | None = None,
group: str | None = None,
kind: FocusHookKind | None = None,
) -> "EventHookFunction | Callable[[EventHookFunction], EventHookFunction]"
Register a focus hook for the terminal window, a field, or a group.
Bare @on_focus fires on OS-level terminal focus gained/lost.
Pass a field name for application field focus; pass group= to
listen across grids — see Field(group=...).
Source code in xnano/hooks.py
on_clipboard
¶
on_state
¶
Fire the decorated handler based on the application state.
Pass an expression ("count > 0") to fire each frame it is truthy
against the state's attributes. Pass a bare reference ("count",
"user.name") to fire only when that value is mutated — once per
change rather than every frame.
Example
@on_state("count > 0") def _on_positive_count(self, ctx): ...
@on_state("count") def _on_count_changed(self, ctx): ...
Source code in xnano/hooks.py
on_field
¶
Fire the decorated handler based on this grid's own field values.
Pass an expression ("count > 0") to fire each frame it is truthy
against the grid's fields. Pass a bare reference ("count",
"items[0]") to fire only when that field is mutated — once per
change rather than every frame.
Example
@on_field("count > 0") def _show_count(self): ...
@on_field("count") def _on_count_changed(self): ...
Source code in xnano/hooks.py
on_poll
¶
on_poll(
handler_or_when: "EventHookFunction | PollWhen | None" = None,
/,
*,
when: PollWhen | None = None,
) -> "EventHookFunction | Callable[[EventHookFunction], EventHookFunction]"
Register a poll hook that fires on idle event waits or every frame.
Source code in xnano/hooks.py
on_action
¶
Bind a prebuilt Action as a hook trigger.
User-facing sugar for storing an Action on a handler:
SAVE = Action.keyboard("ctrl+s")
@on_action(SAVE)
def save(self, ctx): ...