Expand description
Handling Viewport Resizing and Layout Thrashing
The viewport size is a fundamental input to the entire layout process. A change in viewport size must trigger a relayout.
- The
layout_documentfunction takes theviewportas an argument. TheLayoutCachestores theviewportfrom the previous frame. - The
reconcile_and_invalidatefunction detects that the viewport has changed size - This single change—marking the root as a layout root—forces a full top-down pass
(
calculate_layout_for_subtreestarting from the root). This correctly recalculates all percentage-based sizes and repositions all elements according to the new viewport dimensions. - The intrinsic size calculation (bottom-up) can often be skipped, as it’s independent of the container size, which is a significant optimization.
Structs§
- Layout
Cache - The persistent cache that holds the layout state between frames.
- Layout
Cache Entry - Cache entry for full layout (
PerformLayoutmode). - Layout
Cache Map - External layout cache, parallel to
LayoutTree.nodes. - Node
Cache - Per-node cache entry with 9 measurement slots + 1 full layout slot.
- Reconciliation
Result - The result of a reconciliation pass.
- Sizing
Cache Entry - Cache entry for sizing (
ComputeSizemode) — stores NO positions. - Solver3
Cache Memory Report - Approximate heap-byte breakdown of the solver3
LayoutCache.
Enums§
- Available
Width Type - Constraint classification for deterministic cache slot selection.
- Compute
Mode - Determines whether
calculate_layout_for_subtreeshould only compute the node’s size (for parent’s sizing pass) or perform full layout including child positioning.
Functions§
- calculate_
layout_ for_ subtree - Recursive, top-down pass to calculate used sizes and positions for a given subtree. This is the single, authoritative function for in-flow layout.
- collect_
children_ dom_ ids - Collects DOM child IDs from the node hierarchy into a Vec.
- compute_
counters - Computes CSS counter values for all nodes in the layout tree.
- compute_
scrollbar_ info_ core - Core scrollbar info computation: given pre-computed content and container sizes plus a DOM node for style look-up, determines whether scrollbars are needed.
- reconcile_
and_ invalidate - Errors
- reconcile_
recursive - Recursively traverses the new DOM and old tree, building a new tree and marking dirty nodes.
- reposition_
block_ flow_ siblings - Repositions clean children within a simple block-flow layout (like a BFC or a table-row-group). It stacks children along the main axis, preserving their previously calculated cross-axis alignment.
- reposition_
clean_ subtrees - After dirty subtrees are laid out, this repositions their clean siblings without recalculating their internal layout. This is a critical optimization.