Widget

class tanmatsu.widgets.Widget(w=<tanmatsu.size.Auto object>, h=<tanmatsu.size.Auto object>, theme: ~tanmatsu.theme.Theme = <tanmatsu.theme.DefaultTheme object>)

Bases: ABC

Abstract base class. Parent class of all widgets.

Parameters
  • w (tanmatsu.size.SizeResolver) – used for calculating the width of this widget.

  • h (tanmatsu.size.SizeResolver) – used for calculating the height of this widget.

  • theme (tanmatsu.theme.Theme) – used for calculating the theme controlling the styles used to draw this widget.

property size: tanmatsu.geometry.Dimensions | None

Returns the widget’s size, if it has one. Widgets that have not had their layout() method called have not been given a size.

layout(position: Point, size: Dimensions)

Calculates the widget layout. Will be called before every draw().

Parameters
  • position (Point) – The location of this widget in space.

  • size (Dimensions) – The size of this widget.

abstract draw(s: Screenbuffer, clip: tanmatsu.geometry.Rectangle | None = None)

Draws the widget to the given screenbuffer.

Parameters
  • screenbuffer (Screenbuffer) – The Screenbuffer to draw to.

  • clip (Rectangle) – An area, outside of which, this widget should not be drawn.

Note

If a widget has child widgets, it must make sure to pass the correct clip to them when drawing.

The widget must pass self._Widget__available_space to the geometry.Rectangle.__and__() method of the clip it receives, and then pass the resulting clip to the child widget when calling draw() on it, so that the clips correctly combine and propogate down the chain.

mouse_event(button: Mouse_button, modifier: Mouse_modifier, state: Mouse_state, position: Point) bool

Process a mouse event.

Returns

True: Treat the mouse event as consumed. Do not pass the mouse event to other widgets after this one.

False: Treat the mouse event as ignored. Pass the mouse event to other widgets after this one.

keyboard_event(key: tanmatsu.input.Keyboard_key | str, modifier: Keyboard_modifier) bool

Process a keyboard event.

Parameters
Returns

True: Treat the keyboard event as consumed. Do not pass the keyboard event to other widgets after this one.

False: Treat the keyboard event as ignored. Pass the keyboard event to other widgets after this one.