Skip to content

Input

#include <Input.hpp>

Inherits: Widget

A text input widget for user text entry.

Input provides a text field where users can type, edit, and submit text. It supports standard keyboard editing (arrow keys, backspace, delete, etc.) and displays a placeholder when empty.

Features:

  • Text editing with cursor

  • Placeholder text when empty

  • onChange callback when text changes

  • onSubmit callback when user presses Enter

  • Horizontal scrolling for long text

Example Usage:

auto input = std::make_shared<Input>("Enter name...");
input->setOnChange([](/api/const std::string &text) {
std::cout << "User typing: " << text << "\n";
});
input->setOnSubmit([](/api/const std::string &text) {
std::cout << "User submitted: " << text << "\n";
});
ReturnNameDescription
Input inline explicitCreates an input field with optional placeholder text.
voidsetText inlineSets the input field’s text. Moves the cursor to the end of the text.
const std::string &getText const inlineGets the current text in the input field.
voidsetPlaceholder inlineSets the placeholder text shown when the input is empty.
voidsetOnChange inlineSets the callback invoked when text changes. Called after each character insertion or deletion.
voidsetOnSubmit inlineSets the callback invoked when user presses Enter. Typically used to submit or confirm the input.
boolisFocusable virtual const inlineReturns whether this widget can receive keyboard focus.
core::Sizemeasure virtualMeasures how much space this widget needs.
boolonEvent virtualHandles an input event.

inline explicit

inline explicit Input(const std::string & placeholder)

Creates an input field with optional placeholder text.

  • placeholder The text to show when input is empty (default: empty)

inline

inline void setText(const std::string & t)

Sets the input field’s text. Moves the cursor to the end of the text.

  • t The text to set

const inline

inline const std::string & getText() const

Gets the current text in the input field.

The current text


inline

inline void setPlaceholder(const std::string & p)

Sets the placeholder text shown when the input is empty.

  • p The placeholder text

inline

inline void setOnChange(std::function< void(const std::string &)> cb)

Sets the callback invoked when text changes. Called after each character insertion or deletion.

  • cb Function receiving the current text (can be empty)

inline

inline void setOnSubmit(std::function< void(const std::string &)> cb)

Sets the callback invoked when user presses Enter. Typically used to submit or confirm the input.

  • cb Function receiving the final text (can be empty)

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

virtual core::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 bool onEvent(const core::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

ReturnNameDescription
voiddrawContent virtualDraws this widget’s content.

virtual

virtual void drawContent(backend::Buffer & buf, core::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)

ReturnNameDescription
std::stringtext
std::stringplaceholder
intcursor
intscroll
std::function< void(const std::string &)>onChange
std::function< void(const std::string &)>onSubmit

std::string text

std::string placeholder

int cursor = 0

int scroll = 0

std::function< void(const std::string &)> onChange

std::function< void(const std::string &)> onSubmit
ReturnNameDescription
voidinsertChar
voiddeleteBack
voiddeleteForward
voidmoveCursor
voidupdateScroll

void insertChar(char c)

void deleteBack()

void deleteForward()

void moveCursor(int delta)

void updateScroll()