Implementation:AGenUI AGenUI setDayNightMode
| Knowledge Sources | |
|---|---|
| Domains | Theming, UI_Framework |
| Last Updated | 2026-10-24 17:21 GMT |
Overview
Engine-level API for switching light/dark theme mode and broadcasting invalidation to all surface managers, provided by the AGenUI C++ core.
Description
The `setDayNightMode()` method on `IAGenUIEngine` switches the global theme mode and triggers re-evaluation of all rendered components across all surfaces.
The implementation flow: 1. Check engine is running (`_isRunning`). 2. Parse the mode string: `"dark"` → `ThemeMode::Dark`, `"light"` → `ThemeMode::Light`, invalid values default to `Light` (with a log warning). 3. Call `TokenParser::setThemeMode(themeMode)`. If the mode didn't actually change (returns `false`), the method is a no-op — no invalidation is broadcast. 4. If the mode changed: snapshot all `SurfaceManager` instances under `_surfaceManagersMutex`, then call `invalidateFunctionCallValues()` on each one outside the lock (to avoid re-entrant locking).
Each `invalidateFunctionCallValues()` posts to the worker thread, which re-evaluates all component attributes and styles on all surfaces, emitting field-level diffs to the native renderer for any value that actually changed. Action handlers are excluded (they evaluate lazily on event firing).
Usage
- Theme switch: call when the user toggles dark/light mode or when the system theme changes.
- No-op safety: calling with the same mode as current is a no-op (no invalidation).
- After loading tokens: call after `loadDesignTokenConfig()` if the initial mode should be dark.
Code Reference
Source Location
- Repository: AGenUI
- Interface: `core/include/agenui_engine.h:L121-125`
- Implementation: `core/src/module/agenui_engine_impl.cpp:L250-280`
- TokenParser: `core/src/surface/token_parser/agenui_token_parser.cpp:L74-80`
- Invalidate: `core/src/module/agenui_surface_manager.cpp:L233-247`
Signature
// core/include/agenui_engine.h
class IAGenUIEngine {
// Set the day/night mode
// @param mode "light" or "dark" (invalid values default to Light)
virtual void setDayNightMode(const std::string& mode) = 0;
};
Import
#include "agenui_engine.h"
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| mode | `const std::string&` | Yes | "light" or "dark" (invalid defaults to Light) |
Outputs
| Name | Type | Description |
|---|---|---|
| (void) | — | No return value; surfaces re-evaluate asynchronously on worker thread |
| TokenParser._themeMode | `ThemeMode` | Updated to the new mode |
| All surfaces | Diffs | Field-level style diffs emitted to native renderer via IAGenUIMessageListener |
Usage Examples
Switching to Dark Mode
auto* engine = agenui::getAGenUIEngine();
// Switch to dark mode
engine->setDayNightMode("dark");
// All surfaces re-evaluate styles with dark token values
// Native renderer receives component update diffs via onComponentsUpdate
// Switch back to light
engine->setDayNightMode("light");
Responding to System Theme Change
// On Android, in onConfigurationChanged:
if (nightModeActive) {
engine->setDayNightMode("dark");
} else {
engine->setDayNightMode("light");
}
// Calling with the same mode as current is a safe no-op