Implementation:AGenUI AGenUI IMeasurement
| Knowledge Sources | |
|---|---|
| Domains | UI_Layout, Cross_Platform |
| Last Updated | 2026-10-24 17:21 GMT |
Overview
Abstract measurement interface that each component type implements to provide intrinsic sizing to the Yoga layout engine, supporting both synchronous and asynchronous measurement patterns.
Description
The `IMeasurement` interface defines the contract for component intrinsic sizing. When Yoga needs a node's intrinsic size, the `MeasurementManagerImpl` looks up the registered `IMeasurement` by type string and calls `measure(paramJson, modes)`.
The interface has two methods:
- `measure(paramJson, modes)`: pure virtual. Receives the component's attribute JSON (constructed by `VirtualDOMNode`) and Yoga's width/height constraints (`MeasureModes`). Returns a `MeasureResult` with `calcType` (Sync/Async), `width`, `height`, and `countOfLines`.
- `allowsMeasureWithChildren()`: virtual, defaults to `false`. Override to return `true` for container components (e.g., Tabs) that need self-measurement even when Yoga child nodes exist. Yoga normally enforces that nodes with `measureFunc` cannot have children.
Sync components (Text, style-sized) return real dimensions immediately in the measure callback.
Async components (Image, Lottie, Chart, Markdown) return `{0, 0}` first, then trigger recalculation via `markDirty` after loading completes. Yoga re-measures the node, calling `measure()` again, which now returns the real dimensions.
`MeasureModes` contains width and height `MeasureModeInfo` structs, each with `maxValue` (available space from Yoga) and `mode` (YGMeasureMode: 0=Undefined, 1=Exactly, 2=AtMost).
Usage
- Sync component: implement `measure()` to return real dimensions based on attributes and Yoga constraints.
- Async component: return `{Async, 0, 0, 0}` first; after loading completes, trigger `markDirty` on the Yoga node.
- Container with children: override `allowsMeasureWithChildren()` to return `true` only if the component needs self-measurement despite having Yoga children.
Code Reference
Source Location
- Repository: AGenUI
- Interface: `core/include/agenui_measurement.h:L46-68`
- JNI bridge example: `core/src/jni/jni_agenui_measurement.cpp:L282-312`
Signature
// core/include/agenui_measurement.h
namespace agenui {
enum class CalcType {
Sync, // Returns real dimensions immediately
Async // Returns {0,0} first, markDirty after loading
};
struct MeasureModeInfo {
float maxValue = 0.0f; // Max available value from Yoga
int mode = 0; // YGMeasureMode: 0=Undefined 1=Exactly 2=AtMost
};
struct MeasureModes {
MeasureModeInfo width;
MeasureModeInfo height;
};
struct MeasureResult {
CalcType calcType = CalcType::Sync;
float width = 0.0f;
float height = 0.0f;
int countOfLines = 0; // Text line count (0 for non-text)
};
class IMeasurement {
public:
virtual ~IMeasurement() = default;
// Calculate intrinsic size from attributes and Yoga constraints
virtual MeasureResult measure(const std::string& paramJson,
const MeasureModes& modes) = 0;
// Whether to allow measureFunc with Yoga children (default false)
// Override to true for container components like Tabs
virtual bool allowsMeasureWithChildren() const { return false; }
};
} // namespace agenui
Import
#include "agenui_measurement.h"
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| paramJson | `const std::string&` | Yes | Component attribute JSON (built by VirtualDOMNode) |
| modes | `const MeasureModes&` | Yes | Width/height constraints from Yoga |
Outputs
| Name | Type | Description |
|---|---|---|
| measure returns | {calcType, width, height, countOfLines} | |
| allowsMeasureWithChildren returns | Whether measureFunc can coexist with Yoga children |
Usage Examples
Synchronous Text Measurement
class TextMeasurement : public agenui::IMeasurement {
public:
agenui::MeasureResult measure(const std::string& paramJson,
const agenui::MeasureModes& modes) override {
auto attrs = nlohmann::json::parse(paramJson);
std::string text = attrs.value("text", "");
float fontSize = attrs.value("fontSize", 14.0f);
// Calculate text dimensions using platform font metrics
float maxWidth = modes.width.maxValue > 0 ? modes.width.maxValue : 9999.0f;
auto [w, h, lines] = calculateTextSize(text, fontSize, maxWidth);
return {agenui::CalcType::Sync, w, h, lines};
}
};
Container with Children (Tabs)
class TabsMeasurement : public agenui::IMeasurement {
public:
agenui::MeasureResult measure(const std::string& paramJson,
const agenui::MeasureModes& modes) override {
auto attrs = nlohmann::json::parse(paramJson);
float tabBarHeight = attrs.value("tabBarHeight", 48.0f);
float contentHeight = attrs.value("contentHeight", 200.0f);
float width = modes.width.mode == 1 ? modes.width.maxValue : 300.0f;
float height = tabBarHeight + contentHeight;
return {agenui::CalcType::Sync, width, height, 0};
}
// Tabs have Yoga child nodes (each tab's VirtualDOMNode) but still
// need self-measurement for the total height (tabBar + content)
bool allowsMeasureWithChildren() const override { return true; }
};