xnano.components.button
xnano.components.button
¶
xnano.components.button
Display a focusable button and handle activation with normal keyboard or click hooks.
Classes:
-
Button–Focusable styled text button.
Button
dataclass
¶
Button(
label: str | Any = "",
disabled: bool = False,
focusable: bool = True,
foreground: ColorLike | None = None,
background: ColorLike | None = None,
focused_color: ColorLike | None = "black",
focused_background: ColorLike | None = "white",
disabled_color: ColorLike | None = "gray",
disabled_background: ColorLike | None = None,
left: str = "[ ",
right: str = " ]",
activation_keys: Sequence[str] = ("enter", "space"),
*,
visible: bool = True,
z: int = 0,
fit_content: bool = True
)
Bases: Component
Focusable styled text button.
Buttons use the same hooks as every other field. Assign a group and handle clicks or activation keys on the grid:
class Form(BaseGrid):
submit: Button = Field(
default=Button("Submit"),
group="submit",
)
@on_click(group="submit")
@on_keyboard("enter", group="submit")
def submit_form(self, ctx: Context) -> None: ...
Example
Button(label="Save", focused_background="blue")
Attributes:
-
label(str | Any) –Button caption (plain string or styled
Text). -
disabled(bool) –When
True, paints disabled colors and ignores activation. -
focusable(bool) –Whether this button participates in field focus.
-
foreground(ColorLike | None) –Idle foreground color (deprecated alias:
color). -
background(ColorLike | None) –Idle background color.
-
focused_color(ColorLike | None) –Foreground while focused.
-
focused_background(ColorLike | None) –Background while focused.
-
disabled_color(ColorLike | None) –Foreground while disabled.
-
disabled_background(ColorLike | None) –Background while disabled.
-
left(str) –Prefix chrome around the label.
-
right(str) –Suffix chrome around the label.
-
activation_keys(Sequence[str]) –Bindings that should bubble to hooks (never consumed by the button itself).
Methods:
-
get_label_text–Return the plain-text form of
label. -
handle_keyboard–Leave keyboard activation to the grid's event hooks.
-
compose–Compose a styled label, wrapped in a panel when focused.
-
component_post_init–Initialize subclass state after dataclass fields are assigned.
-
get_frame–Optional frame/panel chrome around composed content.
-
get_size–Return the preferred cell size of this component.
-
before_render–Called before rendering; returns the effective render area.
-
after_render–Called after rendering for optional post-paint work.
-
compose_extra_small–Compose content when the viewport is extra small (< 40 cols).
-
compose_small–Compose content when the viewport is small (40–79 cols).
-
compose_medium–Compose content when the viewport is medium (80–119 cols).
-
compose_large–Compose content when the viewport is large (120–159 cols).
-
compose_extra_large–Compose content when the viewport is extra large (>= 160 cols).
-
handle_paste–Optional paste handler while focused.
label
class-attribute
instance-attribute
¶
Button caption (plain string or styled Text).
disabled
class-attribute
instance-attribute
¶
disabled: bool = False
When True, paints disabled colors and ignores activation.
focusable
class-attribute
instance-attribute
¶
focusable: bool = True
Whether this button participates in field focus (tab order).
foreground
class-attribute
instance-attribute
¶
foreground: ColorLike | None = None
Idle foreground color (deprecated alias: color).
background
class-attribute
instance-attribute
¶
background: ColorLike | None = None
Idle background color.
focused_color
class-attribute
instance-attribute
¶
focused_color: ColorLike | None = 'black'
Foreground while focused.
focused_background
class-attribute
instance-attribute
¶
focused_background: ColorLike | None = 'white'
Background while focused.
disabled_color
class-attribute
instance-attribute
¶
disabled_color: ColorLike | None = 'gray'
Foreground while disabled.
disabled_background
class-attribute
instance-attribute
¶
disabled_background: ColorLike | None = None
Background while disabled.
activation_keys
class-attribute
instance-attribute
¶
Bindings that bubble to hooks (never consumed here).
visible
class-attribute
instance-attribute
¶
visible: bool = dataclasses.field(
default=True, kw_only=True
)
Whether this component paints at all.
z
class-attribute
instance-attribute
¶
z: int = dataclasses.field(default=0, kw_only=True)
Stacking order among sibling content.
fit_content
class-attribute
instance-attribute
¶
fit_content: bool = dataclasses.field(
default=True, kw_only=True
)
When True, paint at natural size inside a larger slot.
get_label_text
¶
get_label_text() -> str
Return the plain-text form of label.
Source code in xnano/components/button.py
handle_keyboard
¶
handle_keyboard(keyboard: 'KeyboardEventData') -> bool
Leave keyboard activation to the grid's event hooks.
Parameters:
-
keyboard('KeyboardEventData') –The keyboard event payload.
Returns:
-
bool–Falseso the event continues to hooks.
Source code in xnano/components/button.py
compose
¶
compose(ctx: 'ComponentRenderContext') -> Any
Compose a styled label, wrapped in a panel when focused.
Parameters:
-
ctx('ComponentRenderContext') –Render-time scope for this paint.
Returns:
Source code in xnano/components/button.py
component_post_init
¶
Initialize subclass state after dataclass fields are assigned.
Override this method instead of __post_init__.
get_size
¶
get_size(ctx: ComponentRenderContext[StateT]) -> Size
before_render
¶
before_render(
ctx: ComponentRenderContext[StateT], area: "Area"
) -> "Area"
after_render
¶
after_render(
ctx: ComponentRenderContext[StateT], area: "Area"
) -> None
compose_extra_small
¶
compose_extra_small(
ctx: ComponentRenderContext[StateT],
) -> Content | None
Compose content when the viewport is extra small (< 40 cols).
Optional responsive counterpart to :meth:compose. When
overridden, it is used instead of compose while the window
is in this size tier. Overriding any compose_* variant opts the
component into breakpoint dispatch; a component that overrides none
pays no per-frame cost.
Source code in xnano/components/component.py
compose_small
¶
compose_small(
ctx: ComponentRenderContext[StateT],
) -> Content | None
Compose content when the viewport is small (40–79 cols).
See :meth:compose_extra_small.
compose_medium
¶
compose_medium(
ctx: ComponentRenderContext[StateT],
) -> Content | None
Compose content when the viewport is medium (80–119 cols).
See :meth:compose_extra_small.
compose_large
¶
compose_large(
ctx: ComponentRenderContext[StateT],
) -> Content | None
Compose content when the viewport is large (120–159 cols).
See :meth:compose_extra_small.
compose_extra_large
¶
compose_extra_large(
ctx: ComponentRenderContext[StateT],
) -> Content | None
Compose content when the viewport is extra large (>= 160 cols).
See :meth:compose_extra_small.