Navigator
Navigator
Section titled “Navigator”#include <Navigator.hpp>Inherits:
Widget
A screen navigation widget that manages a stack of screens.
Navigator implements a stack-based navigation pattern, allowing you to push, pop, and replace screens. It maintains a stack of widgets and always displays the topmost one. This is useful for implementing multi-screen applications with back button support.
Example Usage:
auto nav = std::make_shared<Navigator>();auto home = std::make_shared<HomeScreen>();nav->push(home);
// When user clicks a button to go to another screen:auto details = std::make_shared<DetailsScreen>();nav->push(details);
// When user clicks back:nav->pop();Public Attributes
Section titled “Public Attributes”| Return | Name | Description |
|---|---|---|
std::function< void(std::shared_ptr< Widget >)> | onPush | Callback fired when a screen is pushed onto the stack. |
std::function< void(std::shared_ptr< Widget >)> | onPop | Callback fired when a screen is popped from the stack. |
std::function< void(std::shared_ptr< Widget >)> | onReplace | Callback fired when a screen is replaced. |
onPush
Section titled “onPush”std::function< void(std::shared_ptr< Widget >)> onPushCallback fired when a screen is pushed onto the stack.
Parameters
Section titled “Parameters”screenThe widget that was pushed.
std::function< void(std::shared_ptr< Widget >)> onPopCallback fired when a screen is popped from the stack.
Parameters
Section titled “Parameters”screenThe widget that was popped.
onReplace
Section titled “onReplace”std::function< void(std::shared_ptr< Widget >)> onReplaceCallback fired when a screen is replaced.
Parameters
Section titled “Parameters”screenThe widget that replaced the previous one.
Public Methods
Section titled “Public Methods”| Return | Name | Description |
|---|---|---|
void | push | Pushes a widget onto the navigation stack. |
void | pop | Pops the current widget from the stack. |
void | replace | Replaces the current widget with a new one. |
std::shared_ptr< Widget > | current const | Returns the currently active widget. |
bool | canPop const inline | Checks if the stack can be popped. |
int | depth const inline | Returns the current depth of the navigation stack. |
core::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 | Handles an input event. |
std::vector< std::shared_ptr< Widget > > | getChildren virtual const inline | Returns this widget’s children. |
void push(std::shared_ptr< Widget > screen)Pushes a widget onto the navigation stack.
Makes the given widget the current active screen. The previous screen remains on the stack and can be returned to via pop(). Triggers the onPush callback.
Parameters
Section titled “Parameters”screenThe widget to push onto the stack. Must not be null.
void pop()Pops the current widget from the stack.
Removes the topmost widget from the stack and displays the one beneath it. Does nothing if the stack has only one widget (cannot pop the root). Triggers the onPop callback.
replace
Section titled “replace”void replace(std::shared_ptr< Widget > screen)Replaces the current widget with a new one.
Removes the topmost widget and pushes a new one in its place. This is useful for transitions that don’t preserve history. Triggers the onReplace callback.
Parameters
Section titled “Parameters”screenThe widget to replace the current one with. Must not be null.
current
Section titled “current”const
std::shared_ptr< Widget > current() constReturns the currently active widget.
Returns
Section titled “Returns”A shared pointer to the topmost widget on the stack, or nullptr if the stack is empty.
canPop
Section titled “canPop”const inline
inline bool canPop() constChecks if the stack can be popped.
Returns true if there is more than one widget on the stack. Always returns false if there is only one (root) widget.
Returns
Section titled “Returns”True if pop() would succeed, false otherwise.
const inline
inline int depth() constReturns the current depth of the navigation stack.
Returns
Section titled “Returns”The number of widgets currently on the stack.
measure
Section titled “measure”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.
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
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.
Parameters
Section titled “Parameters”eventThe input event
Returns
Section titled “Returns”True if this widget (or one of its listeners) handled the event
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)
Private Attributes
Section titled “Private Attributes”| Return | Name | Description |
|---|---|---|
std::vector< std::shared_ptr< Widget > > | stack |
std::vector< std::shared_ptr< Widget > > stack