Skip to content
Permalink
Browse files

Introduce BuilderForBoxFragment

  • Loading branch information
SimonSapin committed Jan 9, 2020
1 parent c495b5a commit 75e1f116e1843af7365371cb7ac2bbb4ef5e9f75
Showing with 62 additions and 58 deletions.
  1. +62 −58 components/layout_2020/display_list.rs
@@ -72,7 +72,9 @@ impl Fragment {
containing_block: &Rect<Length>,
) {
match self {
Fragment::Box(b) => b.build_display_list(builder, containing_block),
Fragment::Box(b) => {
BuilderForBoxFragment::new(b, containing_block).build(builder, containing_block)
},
Fragment::Anonymous(a) => {
let rect = a
.rect
@@ -126,90 +128,92 @@ impl Fragment {
}
}

impl BoxFragment {
fn build_display_list(
&self,
builder: &mut DisplayListBuilder,
containing_block: &Rect<Length>,
) {
let border_rect = self
struct BuilderForBoxFragment<'a> {
fragment: &'a BoxFragment,
border_rect: units::LayoutRect,
border_radius: wr::BorderRadius,
}

impl<'a> BuilderForBoxFragment<'a> {
fn new(fragment: &'a BoxFragment, containing_block: &Rect<Length>) -> Self {
let border_rect: units::LayoutRect = fragment
.border_rect()
.to_physical(self.style.writing_mode, containing_block)
.to_physical(fragment.style.writing_mode, containing_block)
.translate(&containing_block.top_left)
.into();
let border_radius = self.border_radius(&border_rect);
let hit_info = hit_info(&self.style, self.tag, Cursor::Default);

let border_radius = {
let resolve = |radius: &LengthPercentage, box_size: f32| {
radius.percentage_relative_to(Length::new(box_size)).px()
};
let corner = |corner: &style::values::computed::BorderCornerRadius| {
Size2D::new(
resolve(&corner.0.width.0, border_rect.size.width),
resolve(&corner.0.height.0, border_rect.size.height),
)
};
let b = fragment.style.get_border();
wr::BorderRadius {
top_left: corner(&b.border_top_left_radius),
top_right: corner(&b.border_top_right_radius),
bottom_right: corner(&b.border_bottom_right_radius),
bottom_left: corner(&b.border_bottom_left_radius),
}
};

Self {
fragment,
border_rect,
border_radius,
}
}

fn build(&mut self, builder: &mut DisplayListBuilder, containing_block: &Rect<Length>) {
let hit_info = hit_info(&self.fragment.style, self.fragment.tag, Cursor::Default);
if hit_info.is_some() {
let common = builder.common_properties_with_hit_info(border_rect, hit_info);
let common = builder.common_properties_with_hit_info(self.border_rect, hit_info);
builder.wr.push_hit_test(&common)
}

self.background_display_items(builder, border_rect, &border_radius);
self.border_display_items(builder, border_rect, border_radius);
self.background_display_items(builder);
self.border_display_items(builder);
let content_rect = self
.fragment
.content_rect
.to_physical(self.style.writing_mode, containing_block)
.to_physical(self.fragment.style.writing_mode, containing_block)
.translate(&containing_block.top_left);
for child in &self.children {
for child in &self.fragment.children {
child.build_display_list(builder, &content_rect)
}
}

fn border_radius(&self, border_rect: &units::LayoutRect) -> wr::BorderRadius {
let resolve = |radius: &LengthPercentage, box_size: f32| {
radius.percentage_relative_to(Length::new(box_size)).px()
};
let corner = |corner: &style::values::computed::BorderCornerRadius| {
Size2D::new(
resolve(&corner.0.width.0, border_rect.size.width),
resolve(&corner.0.height.0, border_rect.size.height),
)
};
let b = self.style.get_border();
wr::BorderRadius {
top_left: corner(&b.border_top_left_radius),
top_right: corner(&b.border_top_right_radius),
bottom_right: corner(&b.border_bottom_right_radius),
bottom_left: corner(&b.border_bottom_left_radius),
}
}

fn background_display_items(
&self,
builder: &mut DisplayListBuilder,
border_rect: units::LayoutRect,
border_radius: &wr::BorderRadius,
) {
fn background_display_items(&mut self, builder: &mut DisplayListBuilder) {
let background_color = self
.fragment
.style
.resolve_color(self.style.clone_background_color());
.resolve_color(self.fragment.style.clone_background_color());
if background_color.alpha > 0 {
builder.clipping_and_scrolling_scope(|builder| {
if !border_radius.is_zero() {
if !self.border_radius.is_zero() {
builder.current_space_and_clip.clip_id = builder.wr.define_clip(
&builder.current_space_and_clip,
border_rect,
self.border_rect,
Some(wr::ComplexClipRegion {
rect: border_rect,
radii: *border_radius,
rect: self.border_rect,
radii: self.border_radius,
mode: wr::ClipMode::Clip,
}),
None,
);
}
let common = builder.common_properties(border_rect);
let common = builder.common_properties(self.border_rect);
builder.wr.push_rect(&common, rgba(background_color))
});
}
}

fn border_display_items(
&self,
builder: &mut DisplayListBuilder,
border_rect: units::LayoutRect,
radius: wr::BorderRadius,
) {
let b = self.style.get_border();
fn border_display_items(&mut self, builder: &mut DisplayListBuilder) {
let b = self.fragment.style.get_border();
let widths = SideOffsets2D::new(
b.border_top_width.px(),
b.border_right_width.px(),
@@ -220,7 +224,7 @@ impl BoxFragment {
return;
}
let side = |style, color| wr::BorderSide {
color: rgba(self.style.resolve_color(color)),
color: rgba(self.fragment.style.resolve_color(color)),
style: match style {
BorderStyle::None => wr::BorderStyle::None,
BorderStyle::Solid => wr::BorderStyle::Solid,
@@ -234,18 +238,18 @@ impl BoxFragment {
BorderStyle::Outset => wr::BorderStyle::Outset,
},
};
let common = builder.common_properties(border_rect);
let common = builder.common_properties(self.border_rect);
let details = wr::BorderDetails::Normal(wr::NormalBorder {
top: side(b.border_top_style, b.border_top_color),
right: side(b.border_right_style, b.border_right_color),
bottom: side(b.border_bottom_style, b.border_bottom_color),
left: side(b.border_left_style, b.border_left_color),
radius,
radius: self.border_radius,
do_aa: true,
});
builder
.wr
.push_border(&common, border_rect, widths, details)
.push_border(&common, self.border_rect, widths, details)
}
}

0 comments on commit 75e1f11

Please sign in to comment.
You can’t perform that action at this time.