use log::debug;
use script_layout_interface::wrapper_traits::{LayoutNode, ThreadSafeLayoutNode};
use servo_config::opts;
use style::context::{SharedStyleContext, StyleContext};
use style::data::ElementData;
use style::dom::{NodeInfo, TElement, TNode};
use style::selector_parser::RestyleDamage;
use style::servo::restyle_damage::ServoRestyleDamage;
use style::traversal::{recalc_style_at, DomTraversal, PerLevelTraversalData};
use crate::construct::FlowConstructor;
use crate::context::LayoutContext;
use crate::display_list::DisplayListBuildState;
use crate::flow::{Flow, FlowFlags, GetBaseFlow, ImmutableFlowUtils};
use crate::wrapper::ThreadSafeLayoutNodeHelpers;
use crate::LayoutData;
pub struct RecalcStyleAndConstructFlows<'a> {
context: LayoutContext<'a>,
}
impl<'a> RecalcStyleAndConstructFlows<'a> {
pub fn new(context: LayoutContext<'a>) -> Self {
RecalcStyleAndConstructFlows { context }
}
pub fn context(&self) -> &LayoutContext<'a> {
&self.context
}
pub fn destroy(self) -> LayoutContext<'a> {
self.context
}
}
#[allow(unsafe_code)]
impl<'a, 'dom, E> DomTraversal<E> for RecalcStyleAndConstructFlows<'a>
where
E: TElement,
E::ConcreteNode: LayoutNode<'dom>,
{
fn process_preorder<F>(
&self,
traversal_data: &PerLevelTraversalData,
context: &mut StyleContext<E>,
node: E::ConcreteNode,
note_child: F,
) where
F: FnMut(E::ConcreteNode),
{
unsafe { node.initialize_style_and_layout_data::<LayoutData>() };
if !node.is_text_node() {
let el = node.as_element().unwrap();
let mut data = el.mutate_data().unwrap();
recalc_style_at(self, traversal_data, context, el, &mut data, note_child);
}
}
fn process_postorder(&self, _style_context: &mut StyleContext<E>, node: E::ConcreteNode) {
construct_flows_at(&self.context, node);
}
fn text_node_needs_traversal(node: E::ConcreteNode, parent_data: &ElementData) -> bool {
node.layout_data().is_none() || !parent_data.damage.is_empty()
}
fn shared_context(&self) -> &SharedStyleContext {
&self.context.style_context
}
}
pub trait PreorderFlowTraversal {
fn process(&self, flow: &mut dyn Flow);
fn should_process_subtree(&self, _flow: &mut dyn Flow) -> bool {
true
}
fn should_process(&self, _flow: &mut dyn Flow) -> bool {
true
}
fn traverse(&self, flow: &mut dyn Flow) {
if !self.should_process_subtree(flow) {
return;
}
if self.should_process(flow) {
self.process(flow);
}
for kid in flow.mut_base().child_iter_mut() {
self.traverse(kid);
}
}
fn traverse_absolute_flows(&self, flow: &mut dyn Flow) {
if self.should_process(flow) {
self.process(flow);
}
for descendant_link in flow.mut_base().abs_descendants.iter() {
self.traverse_absolute_flows(descendant_link)
}
}
}
pub trait PostorderFlowTraversal {
fn process(&self, flow: &mut dyn Flow);
fn should_process(&self, _flow: &mut dyn Flow) -> bool {
true
}
fn traverse(&self, flow: &mut dyn Flow) {
for kid in flow.mut_base().child_iter_mut() {
self.traverse(kid);
}
if self.should_process(flow) {
self.process(flow);
}
}
}
pub trait InorderFlowTraversal {
fn process(&mut self, flow: &mut dyn Flow, level: u32);
fn should_process_subtree(&mut self, _flow: &mut dyn Flow) -> bool {
true
}
fn traverse(&mut self, flow: &mut dyn Flow, level: u32) {
if !self.should_process_subtree(flow) {
return;
}
self.process(flow, level);
for kid in flow.mut_base().child_iter_mut() {
self.traverse(kid, level + 1);
}
}
}
pub trait PostorderNodeMutTraversal<'dom, ConcreteThreadSafeLayoutNode>
where
ConcreteThreadSafeLayoutNode: ThreadSafeLayoutNode<'dom>,
{
fn process(&mut self, node: &ConcreteThreadSafeLayoutNode);
}
#[inline]
#[allow(unsafe_code)]
pub unsafe fn construct_flows_at_ancestors<'dom>(
context: &LayoutContext,
mut node: impl LayoutNode<'dom>,
) {
while let Some(element) = node.traversal_parent() {
element.set_dirty_descendants();
node = element.as_node();
construct_flows_at(context, node);
}
}
#[inline]
#[allow(unsafe_code)]
fn construct_flows_at<'dom>(context: &LayoutContext, node: impl LayoutNode<'dom>) {
debug!("construct_flows_at: {:?}", node);
{
let tnode = node.to_threadsafe();
let nonincremental_layout = opts::get().nonincremental_layout;
if nonincremental_layout ||
tnode.restyle_damage() != RestyleDamage::empty() ||
node.as_element()
.is_some_and(|el| el.has_dirty_descendants())
{
let mut flow_constructor = FlowConstructor::new(context);
if nonincremental_layout || !flow_constructor.repair_if_possible(&tnode) {
flow_constructor.process(&tnode);
debug!(
"Constructed flow for {:?}: {:x}",
tnode,
tnode.flow_debug_id()
);
}
}
tnode
.mutate_layout_data()
.unwrap()
.flags
.insert(crate::data::LayoutDataFlags::HAS_BEEN_TRAVERSED);
}
if let Some(el) = node.as_element() {
unsafe {
el.unset_dirty_descendants();
}
}
}
pub struct BubbleISizes<'a> {
pub layout_context: &'a LayoutContext<'a>,
}
impl<'a> PostorderFlowTraversal for BubbleISizes<'a> {
#[inline]
fn process(&self, flow: &mut dyn Flow) {
flow.bubble_inline_sizes();
flow.mut_base()
.restyle_damage
.remove(ServoRestyleDamage::BUBBLE_ISIZES);
}
#[inline]
fn should_process(&self, flow: &mut dyn Flow) -> bool {
flow.base()
.restyle_damage
.contains(ServoRestyleDamage::BUBBLE_ISIZES)
}
}
#[derive(Clone, Copy)]
pub struct AssignISizes<'a> {
pub layout_context: &'a LayoutContext<'a>,
}
impl<'a> PreorderFlowTraversal for AssignISizes<'a> {
#[inline]
fn process(&self, flow: &mut dyn Flow) {
flow.assign_inline_sizes(self.layout_context);
}
#[inline]
fn should_process(&self, flow: &mut dyn Flow) -> bool {
flow.base()
.restyle_damage
.intersects(ServoRestyleDamage::REFLOW_OUT_OF_FLOW | ServoRestyleDamage::REFLOW)
}
}
#[derive(Clone, Copy)]
pub struct AssignBSizes<'a> {
pub layout_context: &'a LayoutContext<'a>,
}
impl<'a> PostorderFlowTraversal for AssignBSizes<'a> {
#[inline]
fn process(&self, flow: &mut dyn Flow) {
if flow.floats_might_flow_through() {
return;
}
flow.assign_block_size(self.layout_context);
}
#[inline]
fn should_process(&self, flow: &mut dyn Flow) -> bool {
let base = flow.base();
base.restyle_damage.intersects(ServoRestyleDamage::REFLOW_OUT_OF_FLOW | ServoRestyleDamage::REFLOW) &&
!base.flags.contains(FlowFlags::CAN_BE_FRAGMENTED)
}
}
pub struct ComputeStackingRelativePositions<'a> {
pub layout_context: &'a LayoutContext<'a>,
}
impl<'a> PreorderFlowTraversal for ComputeStackingRelativePositions<'a> {
#[inline]
fn should_process_subtree(&self, flow: &mut dyn Flow) -> bool {
flow.base()
.restyle_damage
.contains(ServoRestyleDamage::REPOSITION)
}
#[inline]
fn process(&self, flow: &mut dyn Flow) {
flow.compute_stacking_relative_position(self.layout_context);
flow.mut_base()
.restyle_damage
.remove(ServoRestyleDamage::REPOSITION)
}
}
pub struct BuildDisplayList<'a> {
pub state: DisplayListBuildState<'a>,
}
impl<'a> BuildDisplayList<'a> {
#[inline]
pub fn traverse(&mut self, flow: &mut dyn Flow) {
if flow.has_non_invertible_transform_or_zero_scale() {
return;
}
let parent_stacking_context_id = self.state.current_stacking_context_id;
self.state.current_stacking_context_id = flow.base().stacking_context_id;
let parent_clipping_and_scrolling = self.state.current_clipping_and_scrolling;
self.state.current_clipping_and_scrolling = flow.clipping_and_scrolling();
flow.build_display_list(&mut self.state);
flow.mut_base()
.restyle_damage
.remove(ServoRestyleDamage::REPAINT);
for kid in flow.mut_base().child_iter_mut() {
self.traverse(kid);
}
self.state.current_stacking_context_id = parent_stacking_context_id;
self.state.current_clipping_and_scrolling = parent_clipping_and_scrolling;
}
}