Skip to content

Lightpanda: the headless browser designed for AI and automation

user_input: latch a wheel to one scroll container

Adrià Arrufat689045d72aa0
diff --git a/src/browser/frame/user_input.zig b/src/browser/frame/user_input.zig
index 961a47ff0..5e6b621ac 100644
--- a/src/browser/frame/user_input.zig
+++ b/src/browser/frame/user_input.zig
@@ -487,16 +487,48 @@ pub fn wheel(frame: *Frame, target: *Element, x: f64, y: f64, delta_x: f64, delt
}
// Deltas come from the wire, so guard NaN and saturate the addition.
- try wheelScroll(target, deltaToScroll(delta_x), deltaToScroll(delta_y), owner);
+ try scrollAxis(target, .width, deltaToScroll(delta_x), owner);
+ try scrollAxis(target, .height, deltaToScroll(delta_y), owner);
}
-/// Each axis scrolls the nearest ancestor-or-self scroll container along it,
-/// else the viewport. Relative deltas may land on different scrollers per
+/// One axis' delta goes to the nearest ancestor-or-self scroll container that
+/// can still move along it, and to that one alone: a wheel latches to a single
+/// scroller and a delta is never split across two, matching Chrome's
+/// FindNodeToLatch (cc/input/input_handler.cc). A container whose
+/// overscroll-behavior doesn't propagate takes the latch even when it can't
+/// move, which ends the walk. The viewport terminates it otherwise.
+///
+/// Each axis walks on its own, so a wheel may latch to a different scroller per
/// axis, unlike an absolute position.
-fn wheelScroll(target: *Element, delta_x: i32, delta_y: i32, frame: *Frame) !void {
- // A zero delta resolves to .viewport and scrolls it by nothing.
- try target.scrollContainer(.{ .x = delta_x != 0 }, frame).scrollBy(delta_x, 0, frame);
- try target.scrollContainer(.{ .y = delta_y != 0 }, frame).scrollBy(0, delta_y, frame);
+fn scrollAxis(target: *Element, comptime axis: Element.Axis, delta: i32, frame: *Frame) !void {
+ if (delta == 0) {
+ return;
+ }
+ const axes: Element.ScrollAxes = switch (axis) {
+ .width => .{ .x = true },
+ .height => .{ .y = true },
+ };
+
+ var current: ?*Element = target;
+ while (current) |el| {
+ const container = switch (el.scrollContainer(axes, frame)) {
+ .container => |c| c,
+ .viewport => break,
+ };
+ if (container.canScrollAxis(axis, delta, frame)) {
+ return container.scrollByAxis(axis, delta, frame);
+ }
+ if (container.containsOverscroll(axes, frame)) {
+ return;
+ }
+ current = container.parentElement();
+ }
+
+ const opts: Element.ScrollToOpts = switch (axis) {
+ .width => .{ .opts = .{ .left = delta } },
+ .height => .{ .opts = .{ .top = delta } },
+ };
+ return frame.window.scrollBy(opts, null, frame);
}
fn deltaToScroll(d: f64) i32 {
diff --git a/src/server/cdp/domains/input.zig b/src/server/cdp/domains/input.zig
index 492d604de..5dcdfb190 100644
--- a/src/server/cdp/domains/input.zig
+++ b/src/server/cdp/domains/input.zig
@@ -514,6 +514,72 @@ test "cdp.input: dispatchMouseEvent mouseWheel scrolls a scroll container, not t
try runner.waitForScript(frame._frame_id, "window.sheetScrolled === true", 1000);
}
+test "cdp.input: dispatchMouseEvent mouseWheel chains once the container is saturated" {
+ var ctx = try testing.context();
+ defer ctx.deinit();
+
+ const bc = try ctx.loadBrowserContext(.{});
+ const page = try bc.session.createPage();
+ const frame = page.frame().?;
+
+ const url = "http://localhost:9582/src/browser/tests/mcp_actions.html";
+ try frame.navigate(url, .{ .reason = .address_bar, .kind = .{ .push = null } });
+ try testing.waitForPage(bc);
+
+ var ls: lp.js.Local.Scope = undefined;
+ frame.js.localScope(&ls);
+ defer ls.deinit();
+
+ var try_catch: lp.js.TryCatch = undefined;
+ try_catch.init(&ls.local);
+ defer try_catch.deinit();
+
+ const leaf_x = try (try ls.local.compileAndRun("document.getElementById('innerleaf').getBoundingClientRect().x", null)).toF64();
+ const leaf_y = try (try ls.local.compileAndRun("document.getElementById('innerleaf').getBoundingClientRect().y", null)).toF64();
+
+ // #outerscroll is a 100px box over 500px of content. A wheel latches to one
+ // scroller: the container takes the whole delta and keeps what doesn't fit,
+ // rather than passing the rest on.
+ try ctx.processMessage(.{
+ .id = 1,
+ .method = "Input.dispatchMouseEvent",
+ .params = .{ .type = "mouseWheel", .x = leaf_x, .y = leaf_y, .deltaY = 1000 },
+ });
+ const latched = try ls.local.compileAndRun("document.getElementById('outerscroll').scrollTop === 400 && window.scrollY === 0", null);
+ try testing.expect(latched.isTrue());
+
+ // Saturated now, so the next wheel latches to the viewport instead.
+ try ctx.processMessage(.{
+ .id = 2,
+ .method = "Input.dispatchMouseEvent",
+ .params = .{ .type = "mouseWheel", .x = leaf_x, .y = leaf_y, .deltaY = 100 },
+ });
+ const chained = try ls.local.compileAndRun("document.getElementById('outerscroll').scrollTop === 400 && window.scrollY === 100", null);
+ try testing.expect(chained.isTrue());
+
+ // overscroll-behavior keeps the latch on a container that can't move, so
+ // nothing scrolls at all.
+ _ = try ls.local.compileAndRun("document.getElementById('outerscroll').style.overscrollBehavior = 'contain'", null);
+ try ctx.processMessage(.{
+ .id = 3,
+ .method = "Input.dispatchMouseEvent",
+ .params = .{ .type = "mouseWheel", .x = leaf_x, .y = leaf_y, .deltaY = 100 },
+ });
+ const contained = try ls.local.compileAndRun("document.getElementById('outerscroll').scrollTop === 400 && window.scrollY === 100", null);
+ try testing.expect(contained.isTrue());
+
+ // Reversing direction latches back to the container, which can move again.
+ // The 100 it can't give back stays unscrolled: no split here either.
+ _ = try ls.local.compileAndRun("document.getElementById('outerscroll').style.overscrollBehavior = 'auto'", null);
+ try ctx.processMessage(.{
+ .id = 4,
+ .method = "Input.dispatchMouseEvent",
+ .params = .{ .type = "mouseWheel", .x = leaf_x, .y = leaf_y, .deltaY = -500 },
+ });
+ const upward = try ls.local.compileAndRun("document.getElementById('outerscroll').scrollTop === 0 && window.scrollY === 100", null);
+ try testing.expect(upward.isTrue());
+}
+
test "cdp.input: dispatchMouseEvent mouseWheel on page content scrolls the viewport" {
var ctx = try testing.context();
defer ctx.deinit();