Skip to content

Commit

Permalink
Add support for position:sticky
Browse files Browse the repository at this point in the history
This leverages the position:sticky support in WebRender to bring basic
support for position:sticky in Servo. There are still some issues with
nested sticky flows as well as a few other corner cases. Tests are
imported from WPT and can be removed once we update to the latest
version.
  • Loading branch information
mrobinson committed Sep 5, 2017
1 parent 4725a05 commit bc455c8
Show file tree
Hide file tree
Showing 84 changed files with 5,729 additions and 50 deletions.
4 changes: 3 additions & 1 deletion components/gfx/display_list/mod.rs
Expand Up @@ -34,7 +34,8 @@ use style_traits::cursor::Cursor;
use text::TextRun;
use text::glyph::ByteIndex;
use webrender_api::{self, ClipAndScrollInfo, ClipId, ColorF, GradientStop, LocalClip};
use webrender_api::{MixBlendMode, ScrollPolicy, ScrollSensitivity, TransformStyle};
use webrender_api::{MixBlendMode, ScrollPolicy, ScrollSensitivity, StickyFrameInfo};
use webrender_api::TransformStyle;

pub use style::dom::OpaqueNode;

Expand Down Expand Up @@ -559,6 +560,7 @@ impl fmt::Debug for StackingContext {
#[derive(Clone, Debug, Deserialize, HeapSizeOf, Serialize)]
pub enum ScrollRootType {
ScrollFrame(ScrollSensitivity),
StickyFrame(StickyFrameInfo),
Clip,
}

Expand Down
26 changes: 20 additions & 6 deletions components/layout/block.rs
Expand Up @@ -29,9 +29,9 @@

use app_units::{Au, MAX_AU};
use context::LayoutContext;
use display_list_builder::{BorderPaintingMode, DisplayListBuildState};
use display_list_builder::BlockFlowDisplayListBuilding;
use euclid::{Point2D, Size2D, Rect};
use display_list_builder::{BlockFlowDisplayListBuilding, BorderPaintingMode};
use display_list_builder::{DisplayListBuildState, EstablishContainingBlock};
use euclid::{Point2D, Rect, SideOffsets2D, Size2D};
use floats::{ClearType, FloatKind, Floats, PlacementInfo};
use flow::{self, BaseFlow, EarlyAbsolutePositionInfo, Flow, FlowClass, ForceNonfloatedFlag};
use flow::{BLOCK_POSITION_IS_STATIC, CLEARS_LEFT, CLEARS_RIGHT};
Expand All @@ -54,7 +54,7 @@ use std::sync::Arc;
use style::computed_values::{border_collapse, box_sizing, display, float, overflow_x};
use style::computed_values::{position, text_align};
use style::context::SharedStyleContext;
use style::logical_geometry::{LogicalPoint, LogicalRect, LogicalSize, WritingMode};
use style::logical_geometry::{LogicalMargin, LogicalPoint, LogicalRect, LogicalSize, WritingMode};
use style::properties::ComputedValues;
use style::servo::restyle_damage::{BUBBLE_ISIZES, REFLOW, REFLOW_OUT_OF_FLOW};
use style::values::computed::{LengthOrPercentageOrNone, LengthOrPercentage};
Expand Down Expand Up @@ -643,7 +643,7 @@ impl BlockFlow {
&mut self.fragment
}

pub fn stacking_relative_position(&self, coor: CoordinateSystem) -> Rect<Au> {
pub fn stacking_relative_border_box(&self, coor: CoordinateSystem) -> Rect<Au> {
return self.fragment.stacking_relative_border_box(
&self.base.stacking_relative_position,
&self.base.early_absolute_position_info.relative_containing_block_size,
Expand Down Expand Up @@ -1787,6 +1787,20 @@ impl BlockFlow {
self.flags.contains(HAS_SCROLLING_OVERFLOW)
}

// Return offset from original position because of `position: sticky`.
pub fn sticky_position(&self) -> SideOffsets2D<MaybeAuto> {
let containing_block_size = &self.base.early_absolute_position_info
.relative_containing_block_size;
let writing_mode = self.base.early_absolute_position_info.relative_containing_block_mode;
let offsets = self.fragment.style().logical_position();
let as_margins = LogicalMargin::new(writing_mode,
MaybeAuto::from_style(offsets.block_start, containing_block_size.inline),
MaybeAuto::from_style(offsets.inline_end, containing_block_size.inline),
MaybeAuto::from_style(offsets.block_end, containing_block_size.inline),
MaybeAuto::from_style(offsets.inline_start, containing_block_size.inline));
as_margins.to_physical(writing_mode)
}

}

impl Flow for BlockFlow {
Expand Down Expand Up @@ -2134,7 +2148,7 @@ impl Flow for BlockFlow {
}

fn collect_stacking_contexts(&mut self, state: &mut DisplayListBuildState) {
self.collect_stacking_contexts_for_block(state);
self.collect_stacking_contexts_for_block(state, EstablishContainingBlock::Yes);
}

fn build_display_list(&mut self, state: &mut DisplayListBuildState) {
Expand Down
151 changes: 127 additions & 24 deletions components/layout/display_list_builder.rs
Expand Up @@ -14,7 +14,8 @@ use app_units::{AU_PER_PX, Au};
use block::{BlockFlow, BlockStackingContextType};
use canvas_traits::canvas::{CanvasMsg, FromLayoutMsg};
use context::LayoutContext;
use euclid::{Transform3D, Point2D, Vector2D, Rect, SideOffsets2D, Size2D, TypedSize2D};
use euclid::{Point2D, Rect, SideOffsets2D, Size2D, Transform3D, TypedSize2D};
use euclid::Vector2D;
use flex::FlexFlow;
use flow::{BaseFlow, Flow, IS_ABSOLUTELY_POSITIONED};
use flow_ref::FlowRef;
Expand Down Expand Up @@ -72,7 +73,8 @@ use style_traits::ToCss;
use style_traits::cursor::Cursor;
use table_cell::CollapsedBordersForCell;
use webrender_api::{ClipAndScrollInfo, ClipId, ColorF, ComplexClipRegion, GradientStop, LineStyle};
use webrender_api::{LocalClip, RepeatMode, ScrollPolicy, ScrollSensitivity};
use webrender_api::{LocalClip, RepeatMode, ScrollPolicy, ScrollSensitivity, StickyFrameInfo};
use webrender_api::StickySideConstraint;
use webrender_helpers::{ToBorderRadius, ToMixBlendMode, ToRectF, ToTransformStyle};

trait ResolvePercentage {
Expand Down Expand Up @@ -101,11 +103,11 @@ fn convert_repeat_mode(from: RepeatKeyword) -> RepeatMode {
}
}

fn establishes_containing_block_for_absolute(positioning: position::T) -> bool {
match positioning {
position::T::absolute | position::T::relative | position::T::fixed => true,
_ => false,
}
fn establishes_containing_block_for_absolute(can_establish_containing_block: EstablishContainingBlock,
positioning: position::T)
-> bool {
can_establish_containing_block == EstablishContainingBlock::Yes &&
position::T::static_ != positioning
}

trait RgbColor {
Expand Down Expand Up @@ -192,6 +194,9 @@ pub struct DisplayListBuildState<'a> {
/// A stack of clips used to cull display list entries that are outside the
/// rendered region, but only collected at containing block boundaries.
pub containing_block_clip_stack: Vec<Rect<Au>>,

/// The flow parent's content box, used to calculate sticky constraints.
parent_stacking_relative_content_box: Rect<Au>,
}

impl<'a> DisplayListBuildState<'a> {
Expand All @@ -211,6 +216,7 @@ impl<'a> DisplayListBuildState<'a> {
iframe_sizes: Vec::new(),
clip_stack: Vec::new(),
containing_block_clip_stack: Vec::new(),
parent_stacking_relative_content_box: Rect::zero(),
}
}

Expand Down Expand Up @@ -2254,17 +2260,29 @@ impl FragmentDisplayListBuilding for Fragment {
}
}

#[derive(Clone, Copy, PartialEq)]
pub enum EstablishContainingBlock {
Yes,
No,
}

pub trait BlockFlowDisplayListBuilding {
fn collect_stacking_contexts_for_block(&mut self, state: &mut DisplayListBuildState);
fn collect_stacking_contexts_for_block(&mut self,
state: &mut DisplayListBuildState,
can_establish_containing_block: EstablishContainingBlock);

fn transform_clip_to_coordinate_space(&mut self,
state: &mut DisplayListBuildState,
preserved_state: &mut PreservedDisplayListState);
fn setup_clipping_for_block(&mut self,
state: &mut DisplayListBuildState,
preserved_state: &mut PreservedDisplayListState,
stacking_context_type: BlockStackingContextType)
stacking_context_type: BlockStackingContextType,
can_establish_containing_block: EstablishContainingBlock)
-> ClipAndScrollInfo;
fn setup_scroll_root_for_position(&mut self,
state: &mut DisplayListBuildState,
border_box: &Rect<Au>);
fn setup_scroll_root_for_overflow(&mut self,
state: &mut DisplayListBuildState,
border_box: &Rect<Au>);
Expand Down Expand Up @@ -2297,6 +2315,7 @@ pub struct PreservedDisplayListState {
containing_block_clip_and_scroll_info: ClipAndScrollInfo,
clips_pushed: usize,
containing_block_clips_pushed: usize,
stacking_relative_content_box: Rect<Au>,
}

impl PreservedDisplayListState {
Expand All @@ -2308,6 +2327,7 @@ impl PreservedDisplayListState {
containing_block_clip_and_scroll_info: state.containing_block_clip_and_scroll_info,
clips_pushed: 0,
containing_block_clips_pushed: 0,
stacking_relative_content_box: state.parent_stacking_relative_content_box,
}
}

Expand All @@ -2322,6 +2342,7 @@ impl PreservedDisplayListState {
state.current_real_stacking_context_id = self.real_stacking_context_id;
state.current_clip_and_scroll_info = self.clip_and_scroll_info;
state.containing_block_clip_and_scroll_info = self.containing_block_clip_and_scroll_info;
state.parent_stacking_relative_content_box = self.stacking_relative_content_box;

let truncate_length = state.clip_stack.len() - self.clips_pushed;
state.clip_stack.truncate(truncate_length);
Expand Down Expand Up @@ -2359,7 +2380,7 @@ impl BlockFlowDisplayListBuilding for BlockFlow {
if state.clip_stack.is_empty() {
return;
}
let border_box = self.stacking_relative_position(CoordinateSystem::Parent);
let border_box = self.stacking_relative_border_box(CoordinateSystem::Parent);
let transform = match self.fragment.transform_matrix(&border_box) {
Some(transform) => transform,
None => return,
Expand Down Expand Up @@ -2412,7 +2433,9 @@ impl BlockFlowDisplayListBuilding for BlockFlow {
}
}

fn collect_stacking_contexts_for_block(&mut self, state: &mut DisplayListBuildState) {
fn collect_stacking_contexts_for_block(&mut self,
state: &mut DisplayListBuildState,
can_establish_containing_block: EstablishContainingBlock) {
let mut preserved_state = PreservedDisplayListState::new(state);

let block_stacking_context_type = self.block_stacking_context_type();
Expand All @@ -2432,8 +2455,13 @@ impl BlockFlowDisplayListBuilding for BlockFlow {
// stored in state.current_clip_and_scroll_info. If we create a stacking context,
// we don't want it to be contained by its own scroll root.
let containing_clip_and_scroll_info =
self.setup_clipping_for_block(state, &mut preserved_state, block_stacking_context_type);
if establishes_containing_block_for_absolute(self.positioning()) {
self.setup_clipping_for_block(state,
&mut preserved_state,
block_stacking_context_type,
can_establish_containing_block);

if establishes_containing_block_for_absolute(can_establish_containing_block,
self.positioning()) {
state.containing_block_clip_and_scroll_info = state.current_clip_and_scroll_info;
}

Expand All @@ -2459,7 +2487,8 @@ impl BlockFlowDisplayListBuilding for BlockFlow {
fn setup_clipping_for_block(&mut self,
state: &mut DisplayListBuildState,
preserved_state: &mut PreservedDisplayListState,
stacking_context_type: BlockStackingContextType)
stacking_context_type: BlockStackingContextType,
can_establish_containing_block: EstablishContainingBlock)
-> ClipAndScrollInfo {
// If this block is absolutely positioned, we should be clipped and positioned by
// the scroll root of our nearest ancestor that establishes a containing block.
Expand All @@ -2477,26 +2506,33 @@ impl BlockFlowDisplayListBuilding for BlockFlow {
};
self.base.clip_and_scroll_info = Some(containing_clip_and_scroll_info);

let coordinate_system = if self.fragment.establishes_stacking_context() {
CoordinateSystem::Own
let stacking_relative_border_box = if self.fragment.establishes_stacking_context() {
self.stacking_relative_border_box(CoordinateSystem::Own)
} else {
CoordinateSystem::Parent
self.stacking_relative_border_box(CoordinateSystem::Parent)
};

let stacking_relative_border_box = self.fragment.stacking_relative_border_box(
&self.base.stacking_relative_position,
&self.base.early_absolute_position_info.relative_containing_block_size,
self.base.early_absolute_position_info.relative_containing_block_mode,
coordinate_system);

if stacking_context_type == BlockStackingContextType::StackingContext {
self.transform_clip_to_coordinate_space(state, preserved_state);
}

self.setup_scroll_root_for_position(state, &stacking_relative_border_box);
self.setup_scroll_root_for_overflow(state, &stacking_relative_border_box);
self.setup_scroll_root_for_css_clip(state, preserved_state, &stacking_relative_border_box);
self.base.clip = state.clip_stack.last().cloned().unwrap_or_else(max_rect);

// We keep track of our position so that any stickily positioned elements can
// properly determine the extent of their movement relative to scrolling containers.
if can_establish_containing_block == EstablishContainingBlock::Yes {
let border_box = if self.fragment.establishes_stacking_context() {
stacking_relative_border_box
} else {
self.stacking_relative_border_box(CoordinateSystem::Own)
};
state.parent_stacking_relative_content_box =
self.fragment.stacking_relative_content_box(&border_box)
}

match self.positioning() {
position::T::absolute | position::T::relative | position::T::fixed =>
state.containing_block_clip_and_scroll_info = state.current_clip_and_scroll_info,
Expand All @@ -2506,6 +2542,72 @@ impl BlockFlowDisplayListBuilding for BlockFlow {
containing_clip_and_scroll_info
}

fn setup_scroll_root_for_position(&mut self,
state: &mut DisplayListBuildState,
border_box: &Rect<Au>) {
if self.positioning() != position::T::sticky {
return;
}

let sticky_position = self.sticky_position();
if sticky_position.left == MaybeAuto::Auto && sticky_position.right == MaybeAuto::Auto &&
sticky_position.top == MaybeAuto::Auto && sticky_position.bottom == MaybeAuto::Auto {
return;
}

// Since position: sticky elements always establish a stacking context, we will
// have previously calculated our border box in our own coordinate system. In
// order to properly calculate max offsets we need to compare our size and
// position in our parent's coordinate system.
let border_box_in_parent = self.stacking_relative_border_box(CoordinateSystem::Parent);
let margins = self.fragment.margin.to_physical(
self.base.early_absolute_position_info.relative_containing_block_mode);

// Position:sticky elements are always restricted based on the size and position of
// their containing block, which for sticky items is like relative and statically
// positioned items: just the parent block.
let constraint_rect = state.parent_stacking_relative_content_box;

let to_max_offset = |constraint_edge: Au, moving_edge: Au| -> f32 {
(constraint_edge - moving_edge).to_f32_px()
};

let to_sticky_info = |margin: MaybeAuto, max_offset: f32| -> Option<StickySideConstraint> {
match margin {
MaybeAuto::Auto => None,
MaybeAuto::Specified(value) =>
Some(StickySideConstraint { margin: value.to_f32_px(), max_offset }),
}
};

let sticky_frame_info = StickyFrameInfo::new(
to_sticky_info(sticky_position.top,
to_max_offset(constraint_rect.max_y(), border_box_in_parent.max_y())),
to_sticky_info(sticky_position.right,
to_max_offset(constraint_rect.min_x(), border_box_in_parent.min_x() - margins.left)),
to_sticky_info(sticky_position.bottom,
to_max_offset(constraint_rect.min_y(), border_box_in_parent.min_y() - margins.top)),
to_sticky_info(sticky_position.left,
to_max_offset(constraint_rect.max_x(), border_box_in_parent.max_x())));

let new_scroll_root_id = ClipId::new(self.fragment.unique_id(IdType::OverflowClip),
state.layout_context.id.to_webrender());
let parent_id = self.clip_and_scroll_info(state.layout_context.id).scroll_node_id;
state.add_scroll_root(
ScrollRoot {
id: new_scroll_root_id,
parent_id: parent_id,
clip: ClippingRegion::from_rect(border_box),
content_rect: Rect::zero(),
root_type: ScrollRootType::StickyFrame(sticky_frame_info),
},
);

let new_clip_and_scroll_info = ClipAndScrollInfo::simple(new_scroll_root_id);
self.base.clip_and_scroll_info = Some(new_clip_and_scroll_info);
state.current_clip_and_scroll_info = new_clip_and_scroll_info;
}

fn setup_scroll_root_for_overflow(&mut self,
state: &mut DisplayListBuildState,
border_box: &Rect<Au>) {
Expand Down Expand Up @@ -2737,7 +2839,8 @@ impl InlineFlowDisplayListBuilding for InlineFlow {

for fragment in self.fragments.fragments.iter_mut() {
let previous_cb_clip_scroll_info = state.containing_block_clip_and_scroll_info;
if establishes_containing_block_for_absolute(fragment.style.get_box().position) {
if establishes_containing_block_for_absolute(EstablishContainingBlock::Yes,
fragment.style.get_box().position) {
state.containing_block_clip_and_scroll_info = state.current_clip_and_scroll_info;
}

Expand Down
5 changes: 3 additions & 2 deletions components/layout/fragment.rs
Expand Up @@ -2521,8 +2521,9 @@ impl Fragment {
return true
}

// Fixed position blocks always create stacking contexts.
if self.style.get_box().position == position::T::fixed {
// Fixed position and sticky position always create stacking contexts.
if self.style().get_box().position == position::T::fixed ||
self.style().get_box().position == position::T::sticky {
return true
}

Expand Down
3 changes: 2 additions & 1 deletion components/layout/query.rs
Expand Up @@ -598,6 +598,7 @@ impl FragmentBorderBoxIterator for ParentOffsetBorderBoxIterator {
(true, _, _) |
(false, computed_values::position::T::static_, &SpecificFragmentInfo::Table) |
(false, computed_values::position::T::static_, &SpecificFragmentInfo::TableCell) |
(false, computed_values::position::T::sticky, _) |
(false, computed_values::position::T::absolute, _) |
(false, computed_values::position::T::relative, _) |
(false, computed_values::position::T::fixed, _) => true,
Expand Down Expand Up @@ -766,7 +767,7 @@ where

let positioned = match style.get_box().position {
position::computed_value::T::relative |
/*position::computed_value::T::sticky |*/
position::computed_value::T::sticky |
position::computed_value::T::fixed |
position::computed_value::T::absolute => true,
_ => false
Expand Down

0 comments on commit bc455c8

Please sign in to comment.