# brookesia_jsonui_aot
Runtime support for JSON UI AOT generated pages. This component contains **no**
JSON parser, `Document` or `IBackend`; it only provides the small C helpers that
generated code and hand-written LVGL pages share.
## Contents
| Header | Purpose |
|---|---|
| `jsonui_aot.h` | font/image registries, data subjects and bindings, view registry (incl. page-scoped allocations), actions/effects, animations, segmented arc gradient, screen registration, perf marks |
| `jsonui_style.h` | size/grid/offset conversion, placement/transform helpers, text-input inner layout, image sizing, direct style setters |
| `screen_manager.hpp` | minimal B2 control plane (`ScreenDescriptor` + `ScreenManager`) |
## Generated-code contract
```c
lv_obj_t *<screen>_page_create(lv_obj_t *parent);
void <doc>_init(const char *asset_path);
const jsonui_perf_t *<doc>_perf(void);
```
`<doc>_init` registers fonts, images, data subjects and screen descriptors in the
fixed order: fonts → images → subjects → screens → flow actions.
## Performance marks
`<doc>_perf()` returns the generated page create cost. The manager exposes
`ScreenManager::perf()` and the C API `jsonui_screen_perf()` with
`create_cost_ms`, `show_cost_ms`, `hide_cost_ms` and `flow_cost_ms`, measured
with `lv_tick_get()`/`lv_tick_elaps()`:
```c
const jsonui_perf_t *perf = jsonui_screen_perf();
BROOKESIA_LOGI("create=%ums show=%ums hide=%ums flow=%ums", perf->create_cost_ms,
perf->show_cost_ms, perf->hide_cost_ms, perf->flow_cost_ms);
```
Log the build id with the marks (examples use `PROJECT_VER` +
`esp_app_get_description()->version`).
## ScreenManager (B2 minimal)
```cpp
struct ScreenDescriptor {
std::string id;
std::string path;
lv_obj_t *(*create)(lv_obj_t *parent); // native pages
std::function<void()> show;
std::function<void()> hide;
std::function<void()> destroy;
bool dynamic = false;
bool prewarm = false;
};
class ScreenManager {
void register_screen(ScreenDescriptor);
void bind_action(std::string action, std::string screen_id);
bool navigate(std::string_view action);
lv_obj_t *ensure_created(std::string_view id, lv_obj_t *parent);
bool show(std::string_view id); // radio semantics
bool hide(std::string_view id);
bool destroy(std::string_view id);
void prewarm_all(lv_obj_t *parent);
bool prewarm(std::string_view id, lv_obj_t *parent = nullptr);
void set_prewarm(std::string_view id, bool prewarm);
int32_t visible_count(lv_obj_t *container) const;
const jsonui_perf_t &perf() const;
};
```
`prewarm(id)` creates the page hidden, runs `lv_obj_update_layout()` and leaves
the radio state untouched. `set_prewarm` lets an app mark a JSON page that was
registered without the flag. The C API mirrors them:
`jsonui_screen_prewarm()`, `jsonui_screen_visible_count()`, and
`jsonui_screen_perf()`. The example schedules prewarm on a >15 ms interval and
guards it with a 2 s touch-idle window.
JSON screens can join the same control plane by leaving `create` empty and
providing `show`/`hide`/`destroy` callbacks that delegate to `gui::Runtime`
(`mount_screen` / `unmount_screen`). The manager itself stays independent of
`brookesia_gui_interface`, so B-only firmware pays no runtime cost.
## De-runtime build
`gui/brookesia_gui_interface/Kconfig` exposes:
- `BROOKESIA_GUI_INTERFACE_RUNTIME_FULL` (default)
- `BROOKESIA_GUI_INTERFACE_RUNTIME_STUB` (API present, operations return errors)
- `BROOKESIA_GUI_INTERFACE_RUNTIME_NONE` (B-only; parser/runtime sources excluded)
On PC pass `-DBROOKESIA_GUI_INTERFACE_RUNTIME_MODE=NONE`. Generated objects are
verified with `nm` to contain no `esp_brookesia::gui::Runtime|parse_document|
Document|validate_document` references.
## Usage
```cpp
#include "jsonui_hello.h"
#include "brookesia/jsonui_aot/screen_manager.hpp"
hello_init("/littlefs");
auto &manager = esp_brookesia::jsonui_aot::screen_manager();
manager.set_prewarm("hello", true);
manager.prewarm("hello"); // create hidden + layout
manager.show("hello"); // radio: hide others, show this one
int32_t pages = manager.visible_count(lv_screen_active()); // expect 1
manager.prewarm_all(lv_screen_active()); // optional: all descriptor.prewarm pages
```
## i18n text table
`aot.i18n` generates a compile-time text table and a runtime language switch:
```json
{
"i18n": {
"default": "en",
"languages": ["en", "zh_CN"],
"texts": {"Hello AOT": {"en": "Hello AOT", "zh_CN": "你好 AOT"}}
}
}
```
Text keys are the literal default texts. The generated init installs the
resolver and labels/checkbox/textarea text/placeholder use
`jsonui_i18n_bind()`; `<doc>_set_language(language)` switches the table and
calls `jsonui_i18n_refresh()` to update every bound object. Missing languages
fail at generation time (E105).
## Preload tiers
`aot.preload` maps resource ids to `eager` / `lazy` / `ondemand` (default
`lazy`):
```json
{"preload": {"images": {"logo": "eager"}, "fonts": {"main": "ondemand"}}}
```
- `eager` resources are registered by `<doc>_init()`.
- `lazy` resources register on first lookup through
`jsonui_image_set_lazy_loader()` / `jsonui_font_set_lazy_loader()`.
- `ondemand` resources register only when the app calls
`<doc>_resource_load(id)`.
The AOT component exposes the lazy-loader hooks in `jsonui_aot.h`; the
generated init installs them and emits `<doc>_resource_load()`.
## Staged page create
Every generated screen exposes a resumable create path:
```c
void *<screen>_page_create_begin(lv_obj_t *parent, bool hidden);
bool <screen>_page_create_step(void *state, int32_t budget_us);
lv_obj_t *<screen>_page_create_finish(void *state);
```
`<screen>_page_create()` is a synchronous wrapper over begin/step/finish. The
`stage` AOT annotation sets the descriptor `staged` flag, budget and interval;
`ScreenManager::prewarm()` starts a timer for staged pages and completes one
root-child slice per tick while keeping the page hidden. `show()` (and
`ensure_created()`) finishes any staged state synchronously. The current split
is per direct child of the screen root; `budget_us` is a forward-compatible
hint for finer slicing.
## FrameView frame sources
FrameView pages create an `lv_image` and register the output through a
platform-provided sink. The AOT component stays free of the Display service:
```c
typedef struct jsonui_frame_sink {
void *ctx;
bool (*register_output)(void *ctx, const char *name, int32_t w, int32_t h,
jsonui_frame_format_t format);
void (*unregister_output)(void *ctx, const char *name);
bool (*subscribe)(void *ctx, const char *name, jsonui_frame_push_cb_t cb,
void *cb_ctx, void **handle);
void (*unsubscribe)(void *ctx, void *handle);
} jsonui_frame_sink_t;
jsonui_frame_view_set_sink(&sink); /* before *_init() */
/* generated code calls jsonui_frame_view_apply("/screen/frame", true, format) */
```
The sink adapter receives frames from the Display service (or any source),
acquires the GUI lock and invokes `jsonui_frame_view_push()` (or the subscribed
callback). The runtime copies the frame into its shadow buffer, updates the
LVGL image descriptor and invalidates the view; delete unsubscribes and
unregisters. Frames may carry a dirty rect; a full frame is always accepted.
## Tests
`tools/golden/host/screen_manager_test.cpp` covers radio semantics, navigation,
hide, prewarm and destroy on the host. See `tools/golden/README.md`.
708bf2e3e2b8793f7bb9bc3bcb57f8ce9253700b
idf.py add-dependency "espressif/brookesia_jsonui_aot^0.8.0"