Modifiers¶
Functional-style modifiers to apply styling, layout, and event handling to widgets.
modifiers
¶
focusable
¶
focusable(enabled: bool = True, on_focus_change: Optional[FocusChangeCallback] = None, on_key: Optional[Callable[[str, int], bool]] = None, on_key_up: Optional[Callable[[str, int], bool]] = None) -> FocusableModifier
Mark the widget as focusable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
enabled
|
bool
|
Whether the widget is focusable. |
True
|
on_focus_change
|
Optional[FocusChangeCallback]
|
Callback invoked when focus state changes. |
None
|
on_key
|
Optional[Callable[[str, int], bool]]
|
Callback invoked as |
None
|
on_key_up
|
Optional[Callable[[str, int], bool]]
|
Callback invoked as |
None
|
Source code in src/nuiitivet/modifiers/focus.py
on_mount
¶
Return a modifier that runs callback when the widget is mounted.
The callback runs right after the widget's :meth:Widget.on_mount hook and
before its children are mounted. Exceptions are logged and contained.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
callback
|
VoidCallback
|
A no-argument callable. If it is a coroutine function, it is started as a task on mount and cancelled on unmount. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
An |
OnMountModifier
|
class: |
Note
Mount is not "once per logical component". A ComposableWidget
rebuild discards the built subtree and mounts freshly-created widget
instances, so the callback runs again for the new instance. Use it for
work tied to the widget instance's presence in the tree, not for
one-time initialization of a component.
Source code in src/nuiitivet/modifiers/lifecycle.py
on_unmount
¶
Return a modifier that runs callback when the widget is unmounted.
The callback runs right after the widget's :meth:Widget.on_unmount hook
and before its children are unmounted. Exceptions are logged and contained.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
callback
|
VoidCallback
|
A no-argument callable. A coroutine function is scheduled as a task, which may outlive the widget — prefer a synchronous callback for cleanup that must complete. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
An |
OnUnmountModifier
|
class: |
Source code in src/nuiitivet/modifiers/lifecycle.py
shadows
¶
Draw a stack of shadow layers behind the widget.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
layers
|
ShadowLike
|
A |
required |
Returns:
| Type | Description |
|---|---|
ShadowModifier
|
The modifier to apply. |
Source code in src/nuiitivet/modifiers/shadow.py
on_size_changed
¶
Return a modifier that calls callback with the widget's measured size.
The callback receives a :class:Size - the widget's own (width, height)
after layout, excluding its position. Use it to feed a size into imperative
state: a ViewModel, or a plain Observable a child widget binds to::
class ResponsiveScaffold(ComposableWidget):
def __init__(self) -> None:
super().__init__()
self.expanded = Observable(False)
def _on_size(self, size: Size) -> None:
self.expanded.value = size.width >= 700
def build(self) -> Widget:
rail = NavigationRail(..., expanded=self.expanded)
return Row([rail, card], ...).modifier(on_size_changed(self._on_size))
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
callback
|
SizeCallback
|
A callable taking a :class: |
required |
Returns:
| Name | Type | Description |
|---|---|---|
An |
OnSizeChangedModifier
|
class: |
Note
Fires once with the first measurement, so the callback alone is enough to seed the state it drives. After that it fires only when the measured size actually changed; a widget that is re-laid-out at the same size, or merely moved, is silent.
Note
Dispatched between frames, never during layout, so the callback may safely do anything - push a route, write an Observable, replace children - and its effect lands on the frame after the measurement.
That includes the first call, which therefore arrives after the first paint. Give an Observable the value you expect at the initial size and the first report writes the same value, which de-dupes: no visible transition. Seed it differently and the widget animates once on startup.
Warning
Avoid making the callback change the measured widget's own size: that
feeds back into the next measurement and can oscillate. The structurally
safe pattern is to measure a widget whose size the parent imposes
(Sizing.weight(...) / "wt") and let the callback change only what
is inside it.
Source code in src/nuiitivet/modifiers/size_changed.py
opacity
¶
Return a modifier that applies opacity to a widget during paint.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
OpacityLike
|
Opacity value between 0.0 (transparent) and 1.0 (opaque), or an observable providing opacity. |
required |
Note
Opacity is paint-only. Layout and hit-testing remain unchanged.
Source code in src/nuiitivet/modifiers/transform.py
rotate
¶
Return a modifier that rotates a widget during paint.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
angle
|
AngleLike
|
Rotation angle in degrees, or an observable providing degrees. |
required |
origin
|
OriginLike
|
Rotation origin. Accepts any of the nine-point alignment tokens in canonical hyphen form ("center", "top-left", "top-center", "top-right", "center-left", "center-right", "bottom-left", "bottom-center", "bottom-right"); the underscore form ("top_left") is accepted as an alias. Alternatively an (x, y) tuple in local coords. Defaults to "center". |
'center'
|
Note
Rotation is paint-only. Layout and hit-testing remain untransformed.
Source code in src/nuiitivet/modifiers/transform.py
scale
¶
Return a modifier that scales a widget during paint.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
factor
|
ScaleLike
|
Scale factor (uniform) or (sx, sy) tuple, or an observable. |
required |
origin
|
OriginLike
|
Scale origin. Accepts any of the nine-point alignment tokens in canonical hyphen form ("center", "top-left", "top-center", "top-right", "center-left", "center-right", "bottom-left", "bottom-center", "bottom-right"); the underscore form ("top_left") is accepted as an alias. Alternatively an (x, y) tuple in local coords. Defaults to "center". |
'center'
|
Note
Scale is paint-only. Layout and hit-testing remain untransformed.
Source code in src/nuiitivet/modifiers/transform.py
translate
¶
Return a modifier that translates a widget during paint.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
offset
|
TranslateLike
|
Translation offset as (dx, dy) tuple, or an observable. |
required |
Note
Translation is paint-only. Layout and hit-testing remain untransformed.