Skip to content

Truly independent web browser

LibWeb: Map mouse event offsets through every scroll offset

Aliaksandr Kalenik4efff6eb84fe
diff --git a/Libraries/LibCompositing/DisplayList/AccumulatedVisualContext.cpp b/Libraries/LibCompositing/DisplayList/AccumulatedVisualContext.cpp
index 4e2a57afed1..aa72a20b5ae 100644
--- a/Libraries/LibCompositing/DisplayList/AccumulatedVisualContext.cpp
+++ b/Libraries/LibCompositing/DisplayList/AccumulatedVisualContext.cpp
@@ -181,13 +181,6 @@ Optional<Gfx::FloatPoint> AccumulatedVisualContextTree::transform_point_for_hit_
return local_point;
}
-Gfx::FloatPoint AccumulatedVisualContextTree::inverse_transform_point(SpatialNodeIndex index, Gfx::FloatPoint screen_point) const
-{
- Gfx::FloatPoint point;
- Compositing::RustFFI::visual_context_tree_inverse_transform_point(m_rust_tree, index, screen_point, &point);
- return point;
-}
-
Gfx::FloatRect AccumulatedVisualContextTree::transform_rect_to_viewport(SpatialNodeIndex index, Gfx::FloatRect const& source_rect, ScrollStateSnapshot const& scroll_state, IncludeVisualViewportTransform include_visual_viewport_transform) const
{
Gfx::FloatRect rect;
diff --git a/Libraries/LibCompositing/DisplayList/AccumulatedVisualContext.h b/Libraries/LibCompositing/DisplayList/AccumulatedVisualContext.h
index 7046739490a..b8d46edd940 100644
--- a/Libraries/LibCompositing/DisplayList/AccumulatedVisualContext.h
+++ b/Libraries/LibCompositing/DisplayList/AccumulatedVisualContext.h
@@ -90,7 +90,6 @@ public:
bool translations_snap_to_device_pixels(SpatialNodeIndex, ScrollStateSnapshot const&) const;
Optional<Gfx::FloatPoint> transform_point_for_hit_test(ContextRef, Gfx::FloatPoint, ScrollStateSnapshot const&, ClipBehavior = ClipBehavior::Respect) const;
- Gfx::FloatPoint inverse_transform_point(SpatialNodeIndex, Gfx::FloatPoint) const;
Gfx::FloatRect transform_rect_to_viewport(SpatialNodeIndex, Gfx::FloatRect const&, ScrollStateSnapshot const&, IncludeVisualViewportTransform = IncludeVisualViewportTransform::Yes) const;
// Sum of the snapshot entries along the scroll-parent chain from the given scroll-like node.
Gfx::FloatMatrix4x4 accumulated_matrix(SpatialNodeIndex, ScrollStateSnapshot const&, IncludeVisualViewportTransform) const;
diff --git a/Libraries/LibCompositing/Rust/src/ffi.rs b/Libraries/LibCompositing/Rust/src/ffi.rs
index ade2ac14939..a515d2bd463 100644
--- a/Libraries/LibCompositing/Rust/src/ffi.rs
+++ b/Libraries/LibCompositing/Rust/src/ffi.rs
@@ -371,19 +371,6 @@ pub unsafe extern "C" fn visual_context_tree_transform_point_for_hit_test(
}
}
-/// # Safety
-///
-/// `tree` must be a live retained tree handle.
-#[unsafe(no_mangle)]
-pub unsafe extern "C" fn visual_context_tree_inverse_transform_point(
- tree: *const c_void,
- spatial: SpatialNodeIndex,
- screen_point: libgfx_rust::FloatPoint,
- out: &mut libgfx_rust::FloatPoint,
-) {
- *out = unsafe { tree_from_handle(tree) }.inverse_transform_point(spatial, screen_point);
-}
-
fn include_visual_viewport_transform_from(include: bool) -> crate::visual_context::IncludeVisualViewportTransform {
if include {
crate::visual_context::IncludeVisualViewportTransform::Yes
diff --git a/Libraries/LibCompositing/Rust/src/visual_context/queries.rs b/Libraries/LibCompositing/Rust/src/visual_context/queries.rs
index 9f162f302c7..a1db89195d7 100644
--- a/Libraries/LibCompositing/Rust/src/visual_context/queries.rs
+++ b/Libraries/LibCompositing/Rust/src/visual_context/queries.rs
@@ -7,8 +7,8 @@
use super::world_transforms::MINIMUM_PROJECTION_W;
use super::{
ClipData, ClipMode, ClipNode, ClipNodeData, ClipNodeIndex, ContextRef, EffectNodeData, EffectNodeIndex,
- IncludeVisualViewportTransform, LocalTransform, LocalTransformKind, SpatialData, SpatialNodeIndex,
- TransformDataRole, VISUAL_VIEWPORT_NODE_INDEX, VisualContextTree, WorldTransforms, device_offset_for_index,
+ IncludeVisualViewportTransform, SpatialData, SpatialNodeIndex, TransformDataRole, VISUAL_VIEWPORT_NODE_INDEX,
+ VisualContextTree, WorldTransforms, device_offset_for_index,
};
use ak::math;
use libgfx_rust::{CompositingAndBlendingOperator, FloatMatrix4x4, FloatPoint, FloatRect, IntPoint, IntRect};
@@ -127,19 +127,6 @@ impl VisualContextTree {
});
}
- fn chain_contains_3d_transform(&self, index: SpatialNodeIndex) -> bool {
- let mut current = index;
- loop {
- if self.local_transform(current, &[]).kind == LocalTransformKind::Projective {
- return true;
- }
- if current == VISUAL_VIEWPORT_NODE_INDEX {
- return false;
- }
- current = self.spatial_nodes[current.0 as usize].parent;
- }
- }
-
fn begin_hit_test_walk(
&self,
world: &WorldTransforms,
@@ -287,41 +274,6 @@ impl VisualContextTree {
Some(context_walk.point)
}
- pub fn inverse_transform_point(&self, index: SpatialNodeIndex, screen_point: FloatPoint) -> FloatPoint {
- let chain = self.ancestor_chain(index);
-
- // This walk deliberately skips translation-only nodes. Callers resolve offsets and scroll positions
- // themselves. The per-node inverses below are only exact for chains of 2D transforms, so a chain containing a
- // 3D transform inverts the flattened accumulated matrix, mapping the screen point onto the plane the content
- // was rendered into.
- let transforms = chain
- .iter()
- .rev()
- .map(|node_index| self.local_transform(*node_index, &[]))
- .filter(LocalTransform::is_transform_or_perspective);
- if self.chain_contains_3d_transform(index) {
- let matrix = transforms.fold(FloatMatrix4x4::identity(), |matrix, local| local.applied_to(matrix));
- let Some(inverse) = matrix.flattened().inverse() else {
- return screen_point;
- };
- let mapped = inverse.map_vector4([screen_point.x, screen_point.y, 0.0, 1.0]);
- if mapped[3] < MINIMUM_PROJECTION_W {
- return screen_point;
- }
- return FloatPoint {
- x: mapped[0] / mapped[3],
- y: mapped[1] / mapped[3],
- };
- }
- transforms.fold(screen_point, |point, local| {
- local
- .matrix
- .extract_2d_affine()
- .inverse()
- .map_or(point, |inverse| inverse.map_point(point))
- })
- }
-
/// The sum of the resolved offsets of the sticky nodes on the spatial chain from `index` up to the viewport, in
/// device pixels.
pub fn accumulated_sticky_offset(&self, index: SpatialNodeIndex, resolved_offsets: &[FloatPoint]) -> FloatPoint {
@@ -977,10 +929,6 @@ mod tests {
)
.expect("the point maps");
assert_eq!(local, point(20.0, 125.0));
- assert_eq!(
- tree.inverse_transform_point(scroll_node, point(40.0, 60.0)),
- point(20.0, 25.0)
- );
}
#[test]
@@ -1009,10 +957,6 @@ mod tests {
)
.expect("the point maps");
assert_eq!(local, point(50.0, 50.0));
- assert_eq!(
- tree.inverse_transform_point(lifted, point(100.0, 100.0)),
- point(50.0, 50.0)
- );
assert_eq!(
rect_to_viewport(
&tree,
diff --git a/Libraries/LibWeb/DOM/Document.cpp b/Libraries/LibWeb/DOM/Document.cpp
index 7c543cfcf1e..8bc2d77ee77 100644
--- a/Libraries/LibWeb/DOM/Document.cpp
+++ b/Libraries/LibWeb/DOM/Document.cpp
@@ -3519,7 +3519,7 @@ static CSSPixelPoint hover_event_offset_for_target(Layout::BegunRead const& read
if (!Painting::has_committed_box(*layout_node))
return hover_event_data->viewport_position;
- return Painting::mouse_event_offset(*layout_node, hover_event_data->page_offset);
+ return Painting::mouse_event_offset(*layout_node, hover_event_data->viewport_position);
}
static void mark_mouse_transition_event_as_trusted_if_needed(Event& event, Optional<HoverEventData> const& hover_event_data)
diff --git a/Libraries/LibWeb/HTML/HTMLLabelElement.cpp b/Libraries/LibWeb/HTML/HTMLLabelElement.cpp
index b06fb6e9cbb..fa6b8beb175 100644
--- a/Libraries/LibWeb/HTML/HTMLLabelElement.cpp
+++ b/Libraries/LibWeb/HTML/HTMLLabelElement.cpp
@@ -114,15 +114,10 @@ void HTMLLabelElement::activation_behavior(DOM::Event const& event, bool is_inte
// Recompute offsetX/offsetY relative to the control element, since the original values are relative to the label.
auto const* layout_node = control_element->layout_node(read);
- if (layout_node && Painting::has_committed_box(*layout_node) && document().navigable()) {
- auto scroll_offset = document().navigable()->viewport_scroll_offset();
- auto page_position = CSSPixelPoint {
- CSSPixels::nearest_value_for(mouse_event.client_x() + scroll_offset.x().to_double()),
- CSSPixels::nearest_value_for(mouse_event.client_y() + scroll_offset.y().to_double())
- };
- auto box_position = Painting::box_type_agnostic_position(*layout_node);
- click_event->set_offset_x(AK::round((page_position.x() - box_position.x()).to_double()));
- click_event->set_offset_y(AK::round((page_position.y() - box_position.y()).to_double()));
+ if (layout_node && Painting::has_committed_box(*layout_node)) {
+ auto offset = Painting::mouse_event_offset(*layout_node, { CSSPixels::nearest_value_for(mouse_event.client_x()), CSSPixels::nearest_value_for(mouse_event.client_y()) });
+ click_event->set_offset_x(AK::round(offset.x().to_double()));
+ click_event->set_offset_y(AK::round(offset.y().to_double()));
}
click_event->set_bubbles(true);
diff --git a/Libraries/LibWeb/Page/EventHandler.cpp b/Libraries/LibWeb/Page/EventHandler.cpp
index 8aac7010761..51794c8a3be 100644
--- a/Libraries/LibWeb/Page/EventHandler.cpp
+++ b/Libraries/LibWeb/Page/EventHandler.cpp
@@ -1287,8 +1287,7 @@ EventResult EventHandler::dispatch_wheel_event(Layout::BegunRead const& read, La
auto viewport_position = document->visual_viewport()->map_to_layout_viewport(visual_viewport_position);
auto page_offset = compute_mouse_event_page_offset(viewport_position);
auto const* offset_layout_node = Painting::has_committed_box(*layout_node) ? layout_node : &hit_layout_node;
- auto scroll_offset = document->navigable()->viewport_scroll_offset();
- auto offset = Painting::mouse_event_offset(*offset_layout_node, visual_viewport_position.translated(scroll_offset));
+ auto offset = Painting::mouse_event_offset(*offset_layout_node, viewport_position);
auto cancelability = is_cancelable ? UIEvents::WheelEventIsCancelable::Yes : UIEvents::WheelEventIsCancelable::No;
auto event = UIEvents::WheelEvent::create_from_platform_event(HTML::relevant_global_object(*node), m_navigable->active_window_proxy(), UIEvents::EventNames::wheel, screen_position, page_offset, viewport_position, offset, wheel_delta_x, wheel_delta_y, wheel_tick_120ths_x, wheel_tick_120ths_y, button, buttons, modifiers, cancelability).release_value_but_fixme_should_propagate_errors();
return node->dispatch_event(event) ? EventResult::Accepted : EventResult::Cancelled;
@@ -2486,8 +2485,7 @@ EventResult EventHandler::handle_drag_and_drop_event(DragEvent::Type type, CSSPi
return *dispath_result;
auto page_offset = compute_mouse_event_page_offset(viewport_position);
- auto scroll_offset = document.navigable()->viewport_scroll_offset();
- auto offset = Painting::mouse_event_offset(*target_layout_node, visual_viewport_position.translated(scroll_offset));
+ auto offset = Painting::mouse_event_offset(*target_layout_node, viewport_position);
auto was_dragging = should_ignore_device_input_event();
ScopeGuard update_keyboard_routing = [&] {
@@ -3419,8 +3417,7 @@ bool EventHandler::fire_click_events(GC::Ref<DOM::Node> node, MouseEventCoordina
EventHandler::MouseEventCoordinates EventHandler::compute_mouse_event_coordinates(CSSPixelPoint visual_viewport_position, CSSPixelPoint viewport_position, Layout::Node const& layout_node) const
{
auto page_offset = compute_mouse_event_page_offset(viewport_position);
- auto scroll_offset = m_navigable->active_document()->navigable()->viewport_scroll_offset();
- auto offset = Painting::mouse_event_offset(layout_node, visual_viewport_position.translated(scroll_offset));
+ auto offset = Painting::mouse_event_offset(layout_node, viewport_position);
return { page_offset, visual_viewport_position, viewport_position, offset };
}
diff --git a/Libraries/LibWeb/Painting/BoxViews.cpp b/Libraries/LibWeb/Painting/BoxViews.cpp
index a977bf9920b..32bf71f1c1b 100644
--- a/Libraries/LibWeb/Painting/BoxViews.cpp
+++ b/Libraries/LibWeb/Painting/BoxViews.cpp
@@ -701,25 +701,12 @@ CSSPixelPoint point_to_local(Layout::Node const& node, CSSPixelPoint point, Layo
return local;
}
-CSSPixelPoint inverse_transform_point(Layout::Node const& node, CSSPixelPoint position)
-{
- if (!has_committed_box(node))
- return {};
- auto const& document = node.document();
- if (!document.has_committed_viewport_box())
- return position;
- auto pixel_ratio = static_cast<float>(document.page().client().device_pixels_per_css_pixel());
- auto visual_context_tree = document.visual_context_tree();
- auto result = visual_context_tree.inverse_transform_point(accumulated_visual_context(node).spatial, position.to_type<float>() * pixel_ratio);
- return (result / pixel_ratio).to_type<CSSPixels>();
-}
-
// https://drafts.csswg.org/cssom-view/#dom-mouseevent-offsetx
-CSSPixelPoint mouse_event_offset(Layout::Node const& node, CSSPixelPoint page_position)
+CSSPixelPoint mouse_event_offset(Layout::Node const& node, CSSPixelPoint viewport_position)
{
// The position where the event occurred, ignoring the transforms that apply to the element and its ancestors,
// relative to the origin of the padding edge of the target node.
- return inverse_transform_point(node, page_position) - box_type_agnostic_position(node);
+ return point_to_local(node, viewport_position, Layout::RustFFI::ViewportSpace::LayoutViewport) - box_type_agnostic_position(node);
}
CSSPixelPoint presented_scroll_offset(Layout::Node const& node, CSSPixelPoint scroll_offset)
diff --git a/Libraries/LibWeb/Painting/BoxViews.h b/Libraries/LibWeb/Painting/BoxViews.h
index e27b00c939c..8434b48c615 100644
--- a/Libraries/LibWeb/Painting/BoxViews.h
+++ b/Libraries/LibWeb/Painting/BoxViews.h
@@ -65,11 +65,10 @@ WEB_API bool is_paintable_with_lines(Layout::Node const&);
WEB_API Layout::RustFFI::FfiViewportMapping viewport_mapping(DOM::Document const&);
WEB_API Layout::RustFFI::FfiViewportMapping viewport_mapping(DOM::Document const&, Compositing::AccumulatedVisualContextTree const&);
WEB_API CSSPixelRect rect_to_viewport(Layout::Node const&, CSSPixelRect const&, Layout::RustFFI::ViewportSpace);
-// The point mapped into the space the box is painted in, or the point itself where the box's clips or transforms leave
-// it out.
+// The point mapped into the space the box is painted in, past any clips, or the point itself where the box's transforms
+// hide it.
WEB_API CSSPixelPoint point_to_local(Layout::Node const&, CSSPixelPoint, Layout::RustFFI::ViewportSpace);
-WEB_API CSSPixelPoint inverse_transform_point(Layout::Node const&, CSSPixelPoint);
-WEB_API CSSPixelPoint mouse_event_offset(Layout::Node const&, CSSPixelPoint page_position);
+WEB_API CSSPixelPoint mouse_event_offset(Layout::Node const&, CSSPixelPoint viewport_position);
// The scroll offset at which the box's content is drawn, hit tested and measured: rounded to whole device pixels where
// nothing but identity transforms lies above the box.
WEB_API CSSPixelPoint presented_scroll_offset(Layout::Node const&, CSSPixelPoint scroll_offset);
diff --git a/Libraries/LibWeb/Rust/src/painting/viewport_mapping.rs b/Libraries/LibWeb/Rust/src/painting/viewport_mapping.rs
index f5399469f2c..bf0fa614678 100644
--- a/Libraries/LibWeb/Rust/src/painting/viewport_mapping.rs
+++ b/Libraries/LibWeb/Rust/src/painting/viewport_mapping.rs
@@ -241,7 +241,8 @@ pub extern "C" fn viewport_mapping_rect_in_space_to_viewport(
.into();
}
-/// Gives false where [`ViewportMapping::point_to_local`] gives none.
+/// Gives false where [`ViewportMapping::point_to_local`] gives none. Clips are ignored: C++ maps points over a box it hit
+/// or captured, and the corners of a rect it clipped already.
#[unsafe(no_mangle)]
pub extern "C" fn viewport_mapping_point_to_local(
mapping: FfiViewportMapping<'_>,
@@ -252,7 +253,7 @@ pub extern "C" fn viewport_mapping_point_to_local(
) -> bool {
let Some(local) = mapping
.mapping()
- .point_to_local(context, point.into(), space, ClipBehavior::Respect)
+ .point_to_local(context, point.into(), space, ClipBehavior::Ignore)
else {
return false;
};
diff --git a/Tests/LibWeb/Text/expected/CSSOMView/mouse-event-offset-in-scrolled-containers.txt b/Tests/LibWeb/Text/expected/CSSOMView/mouse-event-offset-in-scrolled-containers.txt
new file mode 100644
index 00000000000..6a961be5505
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/CSSOMView/mouse-event-offset-in-scrolled-containers.txt
@@ -0,0 +1,9 @@
+t1 mouseover: client=(60,50) offset=(43,65)
+t1 click: client=(60,50) offset=(43,65)
+t2 mouseover: client=(350,50) offset=(37,80)
+t2 click: client=(350,50) offset=(37,80)
+t3 mouseover: client=(90,250) offset=(30,42)
+t3 click: client=(90,250) offset=(30,42)
+sticky mouseover: client=(350,205) offset=(50,10)
+sticky click: client=(350,205) offset=(50,10)
+control click: client=(80,450) offset=(20,35)
diff --git a/Tests/LibWeb/Text/input/CSSOMView/mouse-event-offset-in-scrolled-containers.html b/Tests/LibWeb/Text/input/CSSOMView/mouse-event-offset-in-scrolled-containers.html
new file mode 100644
index 00000000000..0a8d1b8b002
--- /dev/null
+++ b/Tests/LibWeb/Text/input/CSSOMView/mouse-event-offset-in-scrolled-containers.html
@@ -0,0 +1,52 @@
+<!DOCTYPE html>
+<style>
+body { margin: 0; height: 2000px; }
+.scroller { position: absolute; width: 200px; height: 100px; overflow: scroll; }
+.content { width: 400px; height: 400px; }
+.target { position: relative; width: 150px; height: 300px; margin: 10px 0 0 20px; }
+#s1 { left: 10px; top: 10px; }
+#s2 { left: 300px; top: 10px; }
+#inner { width: 180px; height: 300px; overflow: scroll; }
+#s3 { left: 10px; top: 200px; transform: translate(5px, 7px) scale(1.5); transform-origin: 0 0; }
+#sticky-scroller { position: absolute; left: 300px; top: 200px; width: 200px; height: 100px; overflow: scroll; }
+#sticky { position: sticky; top: 0; height: 30px; }
+#label { display: block; margin-left: 40px; width: 100px; height: 300px; }
+#control { display: block; width: 60px; height: 20px; margin: 50px 0 0 15px; border: 0; padding: 0; }
+</style>
+<script src="../include.js"></script>
+<div class="scroller" id="s1"><div class="content"><div class="target" id="t1"></div></div></div>
+<div class="scroller" id="s2"><div class="content"><div id="inner"><div class="content"><div class="target" id="t2"></div></div></div></div></div>
+<div class="scroller" id="s3"><div class="content"><div class="target" id="t3"></div></div></div>
+<div id="sticky-scroller"><div style="height: 50px"></div><div id="sticky"></div><div style="height: 400px"></div></div>
+<div class="scroller" id="s4" style="left: 10px; top: 400px"><div class="content"><label id="label"><input id="control"></label></div></div>
+<script>
+ asyncTest(async done => {
+ const lines = [];
+ for (const id of ["t1", "t2", "t3", "sticky", "control"]) {
+ for (const type of ["mouseover", "click"]) {
+ document.getElementById(id).addEventListener(type, event => {
+ if (event.target.id === id)
+ lines.push(`${id} ${type}: client=(${event.clientX},${event.clientY}) offset=(${event.offsetX},${event.offsetY})`);
+ });
+ }
+ }
+ document.getElementById("s1").scrollTo(13, 30);
+ document.getElementById("s2").scrollTo(0, 20);
+ document.getElementById("inner").scrollTo(7, 25);
+ document.getElementById("s3").scrollTo(0, 20);
+ document.getElementById("sticky-scroller").scrollTo(0, 80);
+ document.getElementById("s4").scrollTo(5, 30);
+ window.scrollTo(0, 5);
+ await new Promise(resolve => requestAnimationFrame(() => requestAnimationFrame(resolve)));
+
+ // A target in a scroller, in nested scrollers, in a transformed scroller, a stuck sticky box, and the control
+ // a click on its label in a scroller is passed on to.
+ for (const [x, y] of [[60, 50], [350, 50], [90, 250], [350, 205], [80, 450]]) {
+ internals.mouseMove(x, y);
+ internals.click(x, y);
+ }
+ for (const line of lines)
+ println(line);
+ done();
+ });
+</script>