Principle:AGenUI AGenUI Flexbox Layout Measurement
| Knowledge Sources | |
|---|---|
| Domains | UI_Layout, Cross_Platform |
| Last Updated | 2026-10-24 17:21 GMT |
Overview
A platform-abstracted flexbox layout system where an external layout engine (Yoga) drives tree-based measurement, and each component type delegates intrinsic sizing to a host-registered measurement implementation.
Description
Flexbox layout arranges UI components along main and cross axes with flexible sizing. The Yoga layout engine (Facebook) provides a cross-platform C++ implementation of CSS flexbox, handling the recursive tree traversal that computes positions and sizes for all nodes.
In AGenUI, Yoga drives the layout tree, but it does not know how to measure the intrinsic size of a Text, Image, or custom component — that requires platform-specific font metrics, image decoders, or custom logic. The measurement delegation pattern solves this: the host registers an `IMeasurement` implementation for each component type. When Yoga needs a node's intrinsic size, the `MeasurementManagerImpl` looks up the registered `IMeasurement` by type string, calls `measure(paramJson, modes)`, and returns the `MeasureResult`.
Components are classified as Sync (return real dimensions immediately: Text, style-sized) or Async (return `{0,0}` first, then trigger `markDirty` after loading completes: Image, Lottie, Chart). The `shouldUseMeasureFunc` decision policy optimizes by clearing the measure function when Yoga can size the node directly (explicit px, aspect-ratio with one solvable axis, or platform-locked size).
Usage
Use this principle when:
- A cross-platform UI framework needs flexbox layout without per-platform layout code
- Component intrinsic sizing requires platform-specific measurement (font metrics, image decoding)
- Some components need async measurement (load-then-measure pattern)
- The framework should optimize by skipping measurement when style constraints suffice
Theoretical Basis
The measurement delegation flow:
Yoga layout pass
→ for each node needing intrinsic size:
→ MeasurementManager.measure(type, paramJson, modes)
→ look up IMeasurement by type
→ impl.measure(paramJson, modes) → MeasureResult
→ return {width, height} to Yoga
→ Yoga computes final positions and sizes for all nodes
The measureFunc decision policy (evaluated in priority order):
shouldUseMeasureFunc(type, ctx):
1. No IMeasurement registered? → Clear (Yoga uses styles only)
2. Platform size locked? → Clear (trust platform value)
3. Has children & no opt-in? → Clear (Yoga hard rule)
4. Both axes explicit px? → Clear (Yoga can size directly)
5. aspect-ratio + one axis solvable? → Clear (Yoga derives other axis)
6. Otherwise → Register (Yoga needs measureFunc)
Practical Guide
- Implement `IMeasurement` for each custom component type.
- Register via `engine->getMeasurementManager()->registerMeasurement("MyType", shared_ptr)`.
- Sync components return real dimensions; Async components return `{0,0}` then call `markDirty`.
- Override `allowsMeasureWithChildren()` to return `true` only for container components (e.g., Tabs) that need self-measurement with Yoga children.