Skip to main content
The WidgetManager is responsible for maintaining a collection of widgets, handling their input events, rendering them in the correct order, and managing their lifecycle. It provides centralized widget management with proper Z-order handling and responsive layout support.

Key Features

  • Automatic widget lifecycle management
  • Proper Z-order input handling (top widgets receive input first)
  • Responsive layout support with window resize handling
  • Singleton pattern for global access
  • Memory management with shared_ptr
Widgets are rendered in the order they were added (first added = bottom layer). Input is handled in reverse order (last added = top layer receives input first).

Static Methods

getInstance()

static WidgetManager& getInstance()
return
WidgetManager&
Reference to the singleton instance

Widget Management

addWidget()

void addWidget(std::shared_ptr<Widget> widget)
Adds a widget to the collection. The widget will be rendered and can receive input events. Widgets are rendered in the order they are added (first added appears at the bottom).
widget
std::shared_ptr<Widget>
required
Shared pointer to the widget to add
auto button = Button(ButtonConfig(0, 0, 100, 30, "Click"));
WidgetManager::getInstance().addWidget(button);

removeWidget()

void removeWidget(std::shared_ptr<Widget> widget)
Removes the specified widget from the collection. The widget will no longer be rendered or receive input events.
widget
std::shared_ptr<Widget>
required
Shared pointer to the widget to remove
If the widget is not found, this method does nothing.

clear()

void clear()
Clears the entire widget collection. All widgets will be removed and will no longer be rendered or receive input events.
This is useful for scene transitions or cleanup. The widgets themselves are not destroyed if other references exist.

Input and Rendering

updateAll()

void updateAll(const InputState& input)
Processes input events for all widgets in reverse order (Z-order). The last added widget (topmost) receives input first. Once a widget handles the input, no widgets below it will receive the event.
input
const InputState&
required
Current input state containing mouse and keyboard info
Call this every frame before rendering to handle user interaction. Input propagation stops at the first widget that handles the event.

renderAll()

void renderAll()
Renders all widgets in the order they were added (first added = bottom layer). This creates the proper visual layering where newer widgets appear on top.
Call this every frame after updating to draw all widgets. Widgets are responsible for their own rendering logic.

Responsive Layout

onWindowResize()

void onWindowResize(int newWidth, int newHeight)
Notifies all widgets that implement ResponsiveWidget interface about window size changes. This allows widgets to adjust their layout and positioning automatically.
newWidth
int
required
New window width in pixels
newHeight
int
required
New window height in pixels
Only widgets that inherit from ResponsiveWidget will be notified.

refreshLayout()

void refreshLayout()
Triggers a layout refresh using the current canvas dimensions. This is useful when you need to force a layout update without an actual window resize event.

getCanvasWidth()

int getCanvasWidth() const
return
int
Current canvas width in pixels
Deprecated: Use Fern::getWidth() instead for accurate dimensions.

getCanvasHeight()

int getCanvasHeight() const
return
int
Current canvas height in pixels
Deprecated: Use Fern::getHeight() instead for accurate dimensions.

Helper Functions

addWidget()

void addWidget(std::shared_ptr<Widget> widget)
Global helper function that adds a widget to the default WidgetManager instance.
widget
std::shared_ptr<Widget>
required
Shared pointer to the widget to add
auto button = Button(ButtonConfig(0, 0, 100, 30, "Click"));
addWidget(button);  // Equivalent to WidgetManager::getInstance().addWidget(button)

removeWidget()

void removeWidget(std::shared_ptr<Widget> widget)
Global helper function that removes a widget from the default WidgetManager instance.
widget
std::shared_ptr<Widget>
required
Shared pointer to the widget to remove

Example Usage

auto button = Button(ButtonConfig(100, 100, 120, 40, "Click Me"));
WidgetManager::getInstance().addWidget(button);

// In your main loop:
WidgetManager::getInstance().updateAll(inputState);
WidgetManager::getInstance().renderAll();

// Clean up when transitioning scenes:
WidgetManager::getInstance().clear();

Z-Order Example

// Background widget (rendered first, receives input last)
auto background = Container(0, 0, 800, 600, Colors::Gray);
addWidget(background);

// Button widget (rendered second, receives input first)
auto button = Button(ButtonConfig(100, 100, 120, 40, "Click"));
addWidget(button);

// The button will receive input events before the background
// and will be rendered on top of the background

Build docs developers (and LLMs) love