Skip to content

Widget

#include <Widget.hpp>

Inherits: enable_shared_from_this< Widget > Subclassed by: Button, Container, Dropdown, DropdownPopup, Input, Label, Modal, Navigator, ScrollView, TextArea, TextView

The base class for all UI components.

Widget is the foundation of the UI system. All visual components (buttons, labels, containers, etc.) inherit from this class. Widgets form a tree hierarchy where each widget can have a parent and children.

Key responsibilities:

  • Measure: Calculate how much space is needed

  • Layout: Position and size the widget and its children

  • Render: Draw to the screen buffer

  • Events: Handle user input

  • Focus: Manage keyboard focus

Subclassing: To create a custom widget, override these virtual methods:

ReturnNameDescription
boolvisibleWhether this widget is visible and participates in layout. When false, the widget is not rendered, not focusable, and takes no space.
std::weak_ptr< Widget >parentWeak pointer to this widget’s parent. Use weak_ptr to avoid circular references.

bool visible = true

Whether this widget is visible and participates in layout. When false, the widget is not rendered, not focusable, and takes no space.


std::weak_ptr< Widget > parent

Weak pointer to this widget’s parent. Use weak_ptr to avoid circular references.

ReturnNameDescription
Sizemeasure virtualMeasures how much space this widget needs.
voidlayout virtualPositions and sizes this widget and its children.
voidrender virtualRenders this widget to the buffer.
boolonEvent virtual inlineHandles an input event.
voidaddEventListener inlineRegisters an event listener on this widget.
boolisFocusable virtual const inlineReturns whether this widget can receive keyboard focus.
voidonFocus virtual inlineCalled when this widget gains keyboard focus.
voidonBlur virtual inlineCalled when this widget loses keyboard focus.
boolfocused constChecks if this widget currently has keyboard focus.
voidsetStyle inlineSets the widget’s normal style.
voidsetFocusedStyle inlineSets the widget’s style when it has focus.
const Style &getStyle const inlineGets the widget’s normal style.
const Style &getFocusedStyle const inlineGets the widget’s focused style.
const Rect &getRect const inlineGets this widget’s bounding rectangle.
std::vector< std::shared_ptr< Widget > >getChildren virtual const inlineReturns this widget’s children.
std::vector< std::shared_ptr< Widget > >getFocusChildren virtual const inlineReturns this widget’s children for focus traversal.

virtual

virtual Size measure(int availW, int availH)

Measures how much space this widget needs.

Called during the measurement pass to determine widget size requirements. Should return the preferred size based on available space.

  • availW Maximum available width (may be ignored by widget)

  • availH Maximum available height (may be ignored by widget)

The Size this widget wants, or needs at minimum


virtual

virtual void layout(int x, int y, int width, int height)

Positions and sizes this widget and its children.

Called after measurement to actually place the widget at a specific location. If your widget has children, you must call their layout() methods here.

  • x The X coordinate for this widget

  • y The Y coordinate for this widget

  • width The width allocated to this widget

  • height The height allocated to this widget


virtual

virtual void render(backend::Buffer & buf)

Renders this widget to the buffer.

The default implementation handles drawing borders and background.

  • buf The buffer to draw to

virtual inline

virtual inline bool onEvent(const events::Event & event)

Handles an input event.

Called by the framework to deliver an event to this widget. The default implementation runs any listeners registered via addEventListener(), then returns false. Subclasses (Button, Input, etc.) override this to add built-in behavior, and should call Widget::onEvent(event) first to give user listeners a chance to handle the event before the widget’s own logic.

Return true if the event was handled, false to let it bubble to the parent widget.

  • event The input event

True if this widget (or one of its listeners) handled the event


inline

inline void addEventListener(EventHandler handler)

Registers an event listener on this widget.

Listeners are called by onEvent() before the widget’s built-in behavior. If a listener returns true, the event is considered handled and will not propagate further. Multiple listeners can be registered and are called in registration order.

Example:

root->addEventListener([&](/api/const events::event& e) -> bool {
if (auto* k = std::get_if<events::KeyEvent>(&e)) {
if (k->modifiers.ctrl && k->key == 's') {
save();
return true;
}
}
return false;
});
  • handler The function to call when this widget receives an event

virtual const inline

virtual inline bool isFocusable() const

Returns whether this widget can receive keyboard focus.

Widgets like Button and Input return true; containers return false.

True if this widget can be focused, false otherwise


virtual inline

virtual inline void onFocus()

Called when this widget gains keyboard focus.


virtual inline

virtual inline void onBlur()

Called when this widget loses keyboard focus.


const

bool focused() const

Checks if this widget currently has keyboard focus.

True if this widget is focused


inline

inline void setStyle(const Style & s)

Sets the widget’s normal style.

  • s The style to apply

inline

inline void setFocusedStyle(const Style & s)

Sets the widget’s style when it has focus.

  • s The focused style to apply

const inline

inline const Style & getStyle() const

Gets the widget’s normal style.

The current style


const inline

inline const Style & getFocusedStyle() const

Gets the widget’s focused style.

The focused style


const inline

inline const Rect & getRect() const

Gets this widget’s bounding rectangle.

The position and size of this widget


virtual const inline

virtual inline std::vector< std::shared_ptr< Widget > > getChildren() const

Returns this widget’s children.

Vector of child widgets (empty if no children)


virtual const inline

virtual inline std::vector< std::shared_ptr< Widget > > getFocusChildren() const

Returns this widget’s children for focus traversal.

By default this matches getChildren(). Widgets with custom rendering behavior (like clipping/compositing) can override this without affecting paint traversal.

ReturnNameDescription
StylestyleThe normal (unfocused) style.
StylefocusedStyleThe style applied when this widget has focus.
RectrectThis widget’s bounding rectangle (set during layout)
std::vector< EventHandler >listenersRegistered event listeners, called by onEvent() in order.

Style style

The normal (unfocused) style.


Style focusedStyle

The style applied when this widget has focus.


Rect rect

This widget’s bounding rectangle (set during layout)


std::vector< EventHandler > listeners

Registered event listeners, called by onEvent() in order.

ReturnNameDescription
voiddrawContent virtual inlineDraws this widget’s content.
const Style &activeStyle constGets the currently active style (normal or focused).
RectinnerRect constGets the inner rectangle, accounting for border and padding.
intjustifyX constCalculates the X position for content based on horizontal alignment.
intjustifyY constCalculates the Y position for content based on vertical alignment.
RectalignContentRect constCalculates the aligned content rectangle within the inner area.

virtual inline

virtual inline void drawContent(backend::Buffer & buf, Rect inner)

Draws this widget’s content.

Called by render() after background and borders are drawn. This is where you should draw your widget’s actual content. The inner rectangle accounts for borders and padding.

  • buf The buffer to draw to

  • inner The inner rectangle (excluding border/padding)


const

const Style & activeStyle() const

Gets the currently active style (normal or focused).

The normal style if not focused, focused style if focused


const

Rect innerRect() const

Gets the inner rectangle, accounting for border and padding.

The inner rectangle (content area)


const

int justifyX(int innerX, int innerW, int contentW, Align align) const

Calculates the X position for content based on horizontal alignment.

  • innerX The inner rectangle’s X coordinate

  • innerW The inner rectangle’s width

  • contentW The content width

  • align The horizontal alignment

The X coordinate for the content’s top-left corner


const

int justifyY(int innerY, int innerH, int contentH, Align align) const

Calculates the Y position for content based on vertical alignment.

  • innerY The inner rectangle’s Y coordinate

  • innerH The inner rectangle’s height

  • contentH The content height

  • align The vertical alignment

The Y coordinate for the content’s top-left corner


const

Rect alignContentRect(Rect inner, int contentW, int contentH, Align ax, Align ay) const

Calculates the aligned content rectangle within the inner area.

Combines justifyX and justifyY to produce a full Rect for placing content inside the inner rectangle with the given alignments.

  • inner The inner rectangle to align within

  • contentW The content width

  • contentH The content height

  • ax Horizontal alignment

  • ay Vertical alignment

The rectangle where content should be drawn

NameDescription
EventHandlerFunction signature for event listeners.

std::function< bool(const events::Event &)> EventHandler()

Function signature for event listeners.

Takes the event and returns true if the listener handled it (stopping propagation) or false to let the event continue to the widget’s own onEvent() and then bubble to the parent.

ReturnNameDescription
voiddrawBackgroundFills the widget’s background area with the active background color.
voiddrawBorderDraws the widget’s border using the active border style.

void drawBackground(backend::Buffer & buf)

Fills the widget’s background area with the active background color.

  • buf The buffer to draw to

void drawBorder(backend::Buffer & buf)

Draws the widget’s border using the active border style.

  • buf The buffer to draw to