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(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.
- Reconciliation
Result - The result of a reconciliation pass.
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.
- is_
simple_ flex_ stack - Checks if a flex container is simple enough to be treated like a block-stack for repositioning.
- reconcile_
and_ invalidate - Compares the new DOM against the cached tree, creating a new tree and identifying which parts need to be re-laid out.
- 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.
- shift_
subtree_ position - Helper to recursively shift the absolute position of a node and all its descendants.
- style_
text_ align_ to_ fc - Convert StyleTextAlign to fc::TextAlign
- to_
overflow_ behavior - Convert LayoutOverflow to OverflowBehavior