Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle cursor and hit testing in 2020 #25280

Merged
merged 6 commits into from Dec 13, 2019
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

Some generated files are not rendered by default. Learn more.

@@ -16,6 +16,7 @@ doctest = false
app_units = "0.7"
atomic_refcell = "0.1"
cssparser = "0.27"
embedder_traits = {path = "../embedder_traits"}
euclid = "0.20"
gfx = {path = "../gfx"}
gfx_traits = {path = "../gfx_traits"}
@@ -4,9 +4,11 @@

use crate::fragments::{BoxFragment, Fragment};
use crate::geom::physical::{Rect, Vec2};
use embedder_traits::Cursor;
use euclid::{Point2D, SideOffsets2D};
use gfx::text::glyph::GlyphStore;
use std::sync::Arc;
use style::properties::ComputedValues;
use style::values::computed::{BorderStyle, Length};
use webrender_api::{self as wr, units, CommonItemProperties, PrimitiveFlags};

@@ -58,11 +60,12 @@ impl Fragment {
.translate(&containing_block.top_left);
let mut baseline_origin = rect.top_left.clone();
baseline_origin.y += t.ascent;
let cursor = cursor(&t.parent_style, Cursor::Text);
let common = CommonItemProperties {
clip_rect: rect.clone().into(),
clip_id: wr::ClipId::root(builder.pipeline_id),
spatial_id: wr::SpatialId::root_scroll_node(builder.pipeline_id),
hit_info: None,
hit_info: cursor.map(|cursor| (t.tag.0 as u64, cursor as u16)),
// TODO(gw): Make use of the WR backface visibility functionality.
flags: PrimitiveFlags::default(),
};
@@ -119,11 +122,12 @@ impl BoxFragment {
.to_physical(self.style.writing_mode, containing_block)
.translate(&containing_block.top_left)
.into();
let cursor = cursor(&self.style, Cursor::Default);
let common = CommonItemProperties {
clip_rect: border_rect,
clip_id: wr::ClipId::root(builder.pipeline_id),
spatial_id: wr::SpatialId::root_scroll_node(builder.pipeline_id),
hit_info: None,
hit_info: cursor.map(|cursor| (self.tag.0 as u64, cursor as u16)),
// TODO(gw): Make use of the WR backface visibility functionality.
flags: PrimitiveFlags::default(),
};
@@ -147,7 +151,7 @@ impl BoxFragment {
let background_color = self
.style
.resolve_color(self.style.clone_background_color());
if background_color.alpha > 0 {
if background_color.alpha > 0 || common.hit_info.is_some() {
builder.wr.push_rect(common, rgba(background_color))
}
}
@@ -228,3 +232,51 @@ fn glyphs(glyph_runs: &[Arc<GlyphStore>], mut origin: Vec2<Length>) -> Vec<wr::G
}
glyphs
}

fn cursor(values: &ComputedValues, default: Cursor) -> Option<Cursor> {
use style::computed_values::pointer_events::T as PointerEvents;
use style::values::specified::ui::CursorKind;

let inherited_ui = values.get_inherited_ui();
if inherited_ui.pointer_events == PointerEvents::None {
return None;
}
Some(match inherited_ui.cursor.keyword {
CursorKind::Auto => default,
CursorKind::None => Cursor::None,
CursorKind::Default => Cursor::Default,
CursorKind::Pointer => Cursor::Pointer,
CursorKind::ContextMenu => Cursor::ContextMenu,
CursorKind::Help => Cursor::Help,
CursorKind::Progress => Cursor::Progress,
CursorKind::Wait => Cursor::Wait,
CursorKind::Cell => Cursor::Cell,
CursorKind::Crosshair => Cursor::Crosshair,
CursorKind::Text => Cursor::Text,
CursorKind::VerticalText => Cursor::VerticalText,
CursorKind::Alias => Cursor::Alias,
CursorKind::Copy => Cursor::Copy,
CursorKind::Move => Cursor::Move,
CursorKind::NoDrop => Cursor::NoDrop,
CursorKind::NotAllowed => Cursor::NotAllowed,
CursorKind::Grab => Cursor::Grab,
CursorKind::Grabbing => Cursor::Grabbing,
CursorKind::EResize => Cursor::EResize,
CursorKind::NResize => Cursor::NResize,
CursorKind::NeResize => Cursor::NeResize,
CursorKind::NwResize => Cursor::NwResize,
CursorKind::SResize => Cursor::SResize,
CursorKind::SeResize => Cursor::SeResize,
CursorKind::SwResize => Cursor::SwResize,
CursorKind::WResize => Cursor::WResize,
CursorKind::EwResize => Cursor::EwResize,
CursorKind::NsResize => Cursor::NsResize,
CursorKind::NeswResize => Cursor::NeswResize,
CursorKind::NwseResize => Cursor::NwseResize,
CursorKind::ColResize => Cursor::ColResize,
CursorKind::RowResize => Cursor::RowResize,
CursorKind::AllScroll => Cursor::AllScroll,
CursorKind::ZoomIn => Cursor::ZoomIn,
CursorKind::ZoomOut => Cursor::ZoomOut,
})
}
@@ -14,7 +14,7 @@ use script_layout_interface::wrapper_traits::{LayoutNode, ThreadSafeLayoutNode};
use servo_arc::Arc as ServoArc;
use std::marker::PhantomData as marker;
use std::sync::Arc;
use style::dom::TNode;
use style::dom::{OpaqueNode, TNode};
use style::properties::ComputedValues;
use style::selector_parser::PseudoElement;

@@ -24,9 +24,9 @@ pub enum WhichPseudoElement {
After,
}

pub(super) enum Contents<Node> {
pub(super) enum Contents {
/// Refers to a DOM subtree, plus `::before` and `::after` pseudo-elements.
OfElement(Node),
OfElement,

/// Example: an `<img src=…>` element.
/// <https://drafts.csswg.org/css2/conform.html#replaced-element>
@@ -37,8 +37,8 @@ pub(super) enum Contents<Node> {
OfPseudoElement(Vec<PseudoElementContentItem>),
}

pub(super) enum NonReplacedContents<Node> {
OfElement(Node),
pub(super) enum NonReplacedContents {
OfElement,
OfPseudoElement(Vec<PseudoElementContentItem>),
}

@@ -51,14 +51,15 @@ pub(super) trait TraversalHandler<'dom, Node>
where
Node: 'dom,
{
fn handle_text(&mut self, text: String, parent_style: &ServoArc<ComputedValues>);
fn handle_text(&mut self, node: Node, text: String, parent_style: &ServoArc<ComputedValues>);

/// Or pseudo-element
fn handle_element(
&mut self,
node: Node,
style: &ServoArc<ComputedValues>,
display: DisplayGeneratingBox,
contents: Contents<Node>,
contents: Contents,
box_slot: BoxSlot<'dom>,
);
}
@@ -75,7 +76,7 @@ fn traverse_children_of<'dom, Node>(
let mut next = parent_element.first_child();
while let Some(child) = next {
if let Some(contents) = child.as_text() {
handler.handle_text(contents, &child.style(context));
handler.handle_text(child, contents, &child.style(context));
} else if child.is_element() {
traverse_element(child, context, handler);
}
@@ -108,9 +109,10 @@ fn traverse_element<'dom, Node>(
},
Display::GeneratingBox(display) => {
handler.handle_element(
element,
&style,
display,
replaced.map_or(Contents::OfElement(element), Contents::Replaced),
replaced.map_or(Contents::OfElement, Contents::Replaced),
element.element_box_slot(),
);
},
@@ -131,19 +133,20 @@ fn traverse_pseudo_element<'dom, Node>(
Display::Contents => {
element.unset_pseudo_element_box(which);
let items = generate_pseudo_element_content(&style, element, context);
traverse_pseudo_element_contents(&style, context, handler, items);
traverse_pseudo_element_contents(element, &style, context, handler, items);
},
Display::GeneratingBox(display) => {
let items = generate_pseudo_element_content(&style, element, context);
let contents = Contents::OfPseudoElement(items);
let box_slot = element.pseudo_element_box_slot(which);
handler.handle_element(&style, display, contents, box_slot);
handler.handle_element(element, &style, display, contents, box_slot);
},
}
}
}

fn traverse_pseudo_element_contents<'dom, Node>(
node: Node,
pseudo_element_style: &ServoArc<ComputedValues>,
context: &LayoutContext,
handler: &mut impl TraversalHandler<'dom, Node>,
@@ -154,7 +157,9 @@ fn traverse_pseudo_element_contents<'dom, Node>(
let mut anonymous_style = None;
for item in items {
match item {
PseudoElementContentItem::Text(text) => handler.handle_text(text, pseudo_element_style),
PseudoElementContentItem::Text(text) => {
handler.handle_text(node, text, pseudo_element_style)
},
PseudoElementContentItem::Replaced(contents) => {
let item_style = anonymous_style.get_or_insert_with(|| {
context
@@ -176,6 +181,7 @@ fn traverse_pseudo_element_contents<'dom, Node>(
Display::GeneratingBox(display_inline)
);
handler.handle_element(
node,
item_style,
display_inline,
Contents::Replaced(contents),
@@ -187,51 +193,51 @@ fn traverse_pseudo_element_contents<'dom, Node>(
}
}

impl<Node> Contents<Node> {
impl Contents {
/// Returns true iff the `try_from` impl below would return `Err(_)`
pub fn is_replaced(&self) -> bool {
match self {
Contents::OfElement(_) | Contents::OfPseudoElement(_) => false,
Contents::OfElement | Contents::OfPseudoElement(_) => false,
Contents::Replaced(_) => true,
}
}
}

impl<Node> std::convert::TryFrom<Contents<Node>> for NonReplacedContents<Node> {
impl std::convert::TryFrom<Contents> for NonReplacedContents {
type Error = ReplacedContent;

fn try_from(contents: Contents<Node>) -> Result<Self, Self::Error> {
fn try_from(contents: Contents) -> Result<Self, Self::Error> {
match contents {
Contents::OfElement(node) => Ok(NonReplacedContents::OfElement(node)),
Contents::OfElement => Ok(NonReplacedContents::OfElement),
Contents::OfPseudoElement(items) => Ok(NonReplacedContents::OfPseudoElement(items)),
Contents::Replaced(replaced) => Err(replaced),
}
}
}

impl<Node> std::convert::From<NonReplacedContents<Node>> for Contents<Node> {
fn from(contents: NonReplacedContents<Node>) -> Self {
impl From<NonReplacedContents> for Contents {
fn from(contents: NonReplacedContents) -> Self {
match contents {
NonReplacedContents::OfElement(node) => Contents::OfElement(node),
NonReplacedContents::OfElement => Contents::OfElement,
NonReplacedContents::OfPseudoElement(items) => Contents::OfPseudoElement(items),
}
}
}

impl<'dom, Node> NonReplacedContents<Node>
where
Node: NodeExt<'dom>,
{
pub(crate) fn traverse(
impl NonReplacedContents {
pub(crate) fn traverse<'dom, Node>(
self,
inherited_style: &ServoArc<ComputedValues>,
context: &LayoutContext,
node: Node,
inherited_style: &ServoArc<ComputedValues>,
handler: &mut impl TraversalHandler<'dom, Node>,
) {
) where
Node: NodeExt<'dom>,
{
match self {
NonReplacedContents::OfElement(node) => traverse_children_of(node, context, handler),
NonReplacedContents::OfElement => traverse_children_of(node, context, handler),
NonReplacedContents::OfPseudoElement(items) => {
traverse_pseudo_element_contents(inherited_style, context, handler, items)
traverse_pseudo_element_contents(node, inherited_style, context, handler, items)
},
}
}
@@ -307,6 +313,7 @@ pub(crate) trait NodeExt<'dom>: 'dom + Copy + LayoutNode + Send + Sync {
fn parent_node(self) -> Option<Self>;
fn style(self, context: &LayoutContext) -> ServoArc<ComputedValues>;

fn as_opaque(self) -> OpaqueNode;
fn layout_data_mut(&self) -> AtomicRefMut<LayoutDataForElement>;
fn element_box_slot(&self) -> BoxSlot<'dom>;
fn pseudo_element_box_slot(&self, which: WhichPseudoElement) -> BoxSlot<'dom>;
@@ -366,6 +373,10 @@ where
self.to_threadsafe().style(context.shared_context())
}

fn as_opaque(self) -> OpaqueNode {
self.opaque()
}

fn layout_data_mut(&self) -> AtomicRefMut<LayoutDataForElement> {
self.get_raw_data()
.map(|d| d.layout_data.borrow_mut())
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.