Skip to main content

LayoutEngine

Trait LayoutEngine 

Source
pub trait LayoutEngine {
    // Required methods
    fn create(&mut self, id: u32) -> Result<(), String>;
    fn apply_style(&mut self, id: u32, style: &Style) -> Result<(), String>;
    fn set_measure(&mut self, id: u32, f: Box<MeasureFn>);
    fn insert_child(
        &mut self,
        parent: u32,
        child: u32,
        index: usize,
    ) -> Result<(), String>;
    fn remove_child(&mut self, parent: u32, child: u32) -> Result<(), String>;
    fn destroy(&mut self, id: u32);
    fn mark_dirty(&mut self, id: u32) -> Result<(), String>;
    fn calculate(
        &mut self,
        root_id: u32,
        viewport_width: f32,
        viewport_height: Option<f32>,
    ) -> Result<(), String>;
    fn computed(&self, id: u32) -> Option<Rect>;
}
Expand description

Stable interface over a layout backend.

The only required implementation is [TaffyEngine]. ADR-1 requires this trait so a yoga-ffi backend can be swapped in if taffy diverges >10% on the conformance corpus.

§Error handling

Methods that can encounter an unknown id return Result<_, String>. The string carries a human-readable diagnostic; the caller (RT layer) will convert it to a JS exception in M3. computed returns Option because rendering an unknown id is not fatal — the renderer skips absent nodes.

Required Methods§

Source

fn create(&mut self, id: u32) -> Result<(), String>

Register a new node with the engine.

id must match the dom arena id so the caller can correlate results. Calling create with a duplicate id is an error (matches dom Create no-op logic — the caller should not do it, but the engine must not panic).

Source

fn apply_style(&mut self, id: u32, style: &Style) -> Result<(), String>

Update the layout style for id.

Style is the dom placeholder for now; M1-3 will add fields. The engine must mark the node dirty automatically (taffy does so inside set_style).

Source

fn set_measure(&mut self, id: u32, f: Box<MeasureFn>)

Attach a measure callback to leaf node id.

The engine owns the callback; calculate will call it for nodes that have one registered.

INVALIDATION: the callback snapshots text + wrap mode at build time (see text_measure::build_measure_fn). The caller MUST call set_measure again to rebuild it after Op::SetText / Op::SetStyle on the node.

Source

fn insert_child( &mut self, parent: u32, child: u32, index: usize, ) -> Result<(), String>

Insert child under parent at index.

index past the end appends (matches taffy insert_child_at_index behavior — out-of-bounds is an error there; we clamp to append to avoid panics when the reconciler sends a slightly stale index).

Source

fn remove_child(&mut self, parent: u32, child: u32) -> Result<(), String>

Remove child from parent.

The child node stays in the engine; it is simply detached.

Source

fn destroy(&mut self, id: u32)

Remove a node from the engine entirely, freeing its taffy node.

Called when the dom arena frees the corresponding slot (Op::Free). The measure function registered for id is also dropped.

Taffy 0.10 remove(node) detaches the node from its parent and any children it may have (children become orphan taffy leaves — their dom Free ops arrive separately per the Free-no-cascade contract in dom/mod.rs). After destroy computed(id) returns None.

Calling destroy on an unknown id is a silent no-op (matches ink’s guard-style error philosophy).

Source

fn mark_dirty(&mut self, id: u32) -> Result<(), String>

Mark node id dirty so it is recomputed on the next calculate.

Normally not needed (style/child changes mark dirty automatically), but exposed so the RT layer can force recalculation after a terminal resize.

Source

fn calculate( &mut self, root_id: u32, viewport_width: f32, viewport_height: Option<f32>, ) -> Result<(), String>

Compute layout for the whole tree rooted at root_id.

viewport_width maps to AvailableSpace::Definite(viewport_width).

viewport_height:

  • Some(h)AvailableSpace::Definite(h) (bounded terminal frame, M2).
  • NoneAvailableSpace::MaxContent (unconstrained height, used by render_to_string — mirrors ink’s calculateLayout(undefined, undefined, LTR) in render-to-string.ts:62-68).
Source

fn computed(&self, id: u32) -> Option<Rect>

Return the computed rect for id after the last calculate.

Returns None for unknown ids — rendering an absent node is not fatal (the renderer skips it, matching ink’s guard-style error philosophy).

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§