Widget
Widget
Section titled “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:
-
measure(): Return your size requirements
-
layout(): Position yourself and children (call parent class if needed)
-
drawContent(): Render your content (not render()!)
-
onEvent(): Handle input events
-
isFocusable(): Return true if your widget can receive focus
Public Attributes
Section titled “Public Attributes”| Return | Name | Description |
|---|---|---|
bool | visible | 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. |
visible
Section titled “visible”bool visible = trueWhether this widget is visible and participates in layout. When false, the widget is not rendered, not focusable, and takes no space.
parent
Section titled “parent”std::weak_ptr< Widget > parentWeak pointer to this widget’s parent. Use weak_ptr to avoid circular references.
Public Methods
Section titled “Public Methods”| Return | Name | Description |
|---|---|---|
Size | measure virtual | Measures how much space this widget needs. |
void | layout virtual | Positions and sizes this widget and its children. |
void | render virtual | Renders this widget to the buffer. |
bool | onEvent virtual inline | Handles an input event. |
void | addEventListener inline | Registers an event listener on this widget. |
bool | isFocusable virtual const inline | Returns whether this widget can receive keyboard focus. |
void | onFocus virtual inline | Called when this widget gains keyboard focus. |
void | onBlur virtual inline | Called when this widget loses keyboard focus. |
bool | focused const | Checks if this widget currently has keyboard focus. |
void | setStyle inline | Sets the widget’s normal style. |
void | setFocusedStyle inline | Sets the widget’s style when it has focus. |
const Style & | getStyle const inline | Gets the widget’s normal style. |
const Style & | getFocusedStyle const inline | Gets the widget’s focused style. |
const Rect & | getRect const inline | Gets this widget’s bounding rectangle. |
std::vector< std::shared_ptr< Widget > > | getChildren virtual const inline | Returns this widget’s children. |
std::vector< std::shared_ptr< Widget > > | getFocusChildren virtual const inline | Returns this widget’s children for focus traversal. |
measure
Section titled “measure”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.
Parameters
Section titled “Parameters”-
availWMaximum available width (may be ignored by widget) -
availHMaximum available height (may be ignored by widget)
Returns
Section titled “Returns”The Size this widget wants, or needs at minimum
layout
Section titled “layout”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.
Parameters
Section titled “Parameters”-
xThe X coordinate for this widget -
yThe Y coordinate for this widget -
widthThe width allocated to this widget -
heightThe height allocated to this widget
render
Section titled “render”virtual
virtual void render(backend::Buffer & buf)Renders this widget to the buffer.
The default implementation handles drawing borders and background.
Parameters
Section titled “Parameters”bufThe buffer to draw to
onEvent
Section titled “onEvent”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.
Parameters
Section titled “Parameters”eventThe input event
Returns
Section titled “Returns”True if this widget (or one of its listeners) handled the event
addEventListener
Section titled “addEventListener”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;});Parameters
Section titled “Parameters”handlerThe function to call when this widget receives an event
isFocusable
Section titled “isFocusable”virtual const inline
virtual inline bool isFocusable() constReturns whether this widget can receive keyboard focus.
Widgets like Button and Input return true; containers return false.
Returns
Section titled “Returns”True if this widget can be focused, false otherwise
onFocus
Section titled “onFocus”virtual inline
virtual inline void onFocus()Called when this widget gains keyboard focus.
onBlur
Section titled “onBlur”virtual inline
virtual inline void onBlur()Called when this widget loses keyboard focus.
focused
Section titled “focused”const
bool focused() constChecks if this widget currently has keyboard focus.
Returns
Section titled “Returns”True if this widget is focused
setStyle
Section titled “setStyle”inline
inline void setStyle(const Style & s)Sets the widget’s normal style.
Parameters
Section titled “Parameters”sThe style to apply
setFocusedStyle
Section titled “setFocusedStyle”inline
inline void setFocusedStyle(const Style & s)Sets the widget’s style when it has focus.
Parameters
Section titled “Parameters”sThe focused style to apply
getStyle
Section titled “getStyle”const inline
inline const Style & getStyle() constGets the widget’s normal style.
Returns
Section titled “Returns”The current style
getFocusedStyle
Section titled “getFocusedStyle”const inline
inline const Style & getFocusedStyle() constGets the widget’s focused style.
Returns
Section titled “Returns”The focused style
getRect
Section titled “getRect”const inline
inline const Rect & getRect() constGets this widget’s bounding rectangle.
Returns
Section titled “Returns”The position and size of this widget
getChildren
Section titled “getChildren”virtual const inline
virtual inline std::vector< std::shared_ptr< Widget > > getChildren() constReturns this widget’s children.
Returns
Section titled “Returns”Vector of child widgets (empty if no children)
getFocusChildren
Section titled “getFocusChildren”virtual const inline
virtual inline std::vector< std::shared_ptr< Widget > > getFocusChildren() constReturns 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.
Protected Attributes
Section titled “Protected Attributes”| Return | Name | Description |
|---|---|---|
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. |
Style styleThe normal (unfocused) style.
focusedStyle
Section titled “focusedStyle”Style focusedStyleThe style applied when this widget has focus.
Rect rectThis widget’s bounding rectangle (set during layout)
listeners
Section titled “listeners”std::vector< EventHandler > listenersRegistered event listeners, called by onEvent() in order.
Protected Methods
Section titled “Protected Methods”| Return | Name | Description |
|---|---|---|
void | drawContent virtual inline | Draws this widget’s content. |
const Style & | activeStyle const | Gets the currently active style (normal or focused). |
Rect | innerRect const | Gets the inner rectangle, accounting for border and padding. |
int | justifyX const | Calculates the X position for content based on horizontal alignment. |
int | justifyY const | Calculates the Y position for content based on vertical alignment. |
Rect | alignContentRect const | Calculates the aligned content rectangle within the inner area. |
drawContent
Section titled “drawContent”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.
Parameters
Section titled “Parameters”-
bufThe buffer to draw to -
innerThe inner rectangle (excluding border/padding)
activeStyle
Section titled “activeStyle”const
const Style & activeStyle() constGets the currently active style (normal or focused).
Returns
Section titled “Returns”The normal style if not focused, focused style if focused
innerRect
Section titled “innerRect”const
Rect innerRect() constGets the inner rectangle, accounting for border and padding.
Returns
Section titled “Returns”The inner rectangle (content area)
justifyX
Section titled “justifyX”const
int justifyX(int innerX, int innerW, int contentW, Align align) constCalculates the X position for content based on horizontal alignment.
Parameters
Section titled “Parameters”-
innerXThe inner rectangle’s X coordinate -
innerWThe inner rectangle’s width -
contentWThe content width -
alignThe horizontal alignment
Returns
Section titled “Returns”The X coordinate for the content’s top-left corner
justifyY
Section titled “justifyY”const
int justifyY(int innerY, int innerH, int contentH, Align align) constCalculates the Y position for content based on vertical alignment.
Parameters
Section titled “Parameters”-
innerYThe inner rectangle’s Y coordinate -
innerHThe inner rectangle’s height -
contentHThe content height -
alignThe vertical alignment
Returns
Section titled “Returns”The Y coordinate for the content’s top-left corner
alignContentRect
Section titled “alignContentRect”const
Rect alignContentRect(Rect inner, int contentW, int contentH, Align ax, Align ay) constCalculates 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.
Parameters
Section titled “Parameters”-
innerThe inner rectangle to align within -
contentWThe content width -
contentHThe content height -
axHorizontal alignment -
ayVertical alignment
Returns
Section titled “Returns”The rectangle where content should be drawn
Public Types
Section titled “Public Types”| Name | Description |
|---|---|
EventHandler | Function signature for event listeners. |
EventHandler
Section titled “EventHandler”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.
Private Methods
Section titled “Private Methods”| Return | Name | Description |
|---|---|---|
void | drawBackground | Fills the widget’s background area with the active background color. |
void | drawBorder | Draws the widget’s border using the active border style. |
drawBackground
Section titled “drawBackground”void drawBackground(backend::Buffer & buf)Fills the widget’s background area with the active background color.
Parameters
Section titled “Parameters”bufThe buffer to draw to
drawBorder
Section titled “drawBorder”void drawBorder(backend::Buffer & buf)Draws the widget’s border using the active border style.
Parameters
Section titled “Parameters”bufThe buffer to draw to