Skip to content

Lightpanda: the headless browser designed for AI and automation

input: report a click count of 0 on the release that ends a chord

Ramiro_quai0dda2648e9c3
diff --git a/src/browser/frame/user_input.zig b/src/browser/frame/user_input.zig
index 571303e1a..763e934e9 100644
--- a/src/browser/frame/user_input.zig
+++ b/src/browser/frame/user_input.zig
@@ -262,6 +262,9 @@ pub const PointerButtons = struct {
/// Where the gesture's pointerdown landed, until its first release fires
/// the gesture's one click.
down_target: ?*Element = null,
+ /// Whether another button was held when a button pressed, at any point
+ /// in the gesture.
+ chorded: bool = false,
/// `g.buttons_down` is ignored: the held mask supplies it.
pub fn press(self: *PointerButtons, frame: *Frame, target: *Element, g: Gesture) !void {
@@ -269,6 +272,9 @@ pub const PointerButtons = struct {
const starts_gesture = self.held & ~bit == 0;
if (starts_gesture) {
self.down_target = target;
+ self.chorded = false;
+ } else {
+ self.chorded = true;
}
self.held |= bit;
@@ -286,10 +292,14 @@ pub const PointerButtons = struct {
const click_target = if (self.down_target) |down| commonClickTarget(down, target) else null;
const was_suppressed = self.mousedown_suppressed;
self.down_target = null;
+ const was_chorded = self.chorded;
self.releaseButton(g.button);
var rg = g;
rg.buttons_down = self.held;
+ // Chrome reports the release that ends a chorded gesture with a click
+ // count of 0, whatever the client sent.
+ if (self.held == 0 and was_chorded) rg.click_count = 0;
try releaseSequence(frame, target, rg, was_suppressed, click_target);
return click_target;
}
@@ -300,6 +310,7 @@ pub const PointerButtons = struct {
if (self.held == 0) {
self.mousedown_suppressed = false;
self.down_target = null;
+ self.chorded = false;
}
}
diff --git a/src/server/cdp/domains/input.zig b/src/server/cdp/domains/input.zig
index 96ae3cab2..f9b6999b2 100644
--- a/src/server/cdp/domains/input.zig
+++ b/src/server/cdp/domains/input.zig
@@ -1981,3 +1981,105 @@ test "cdp.input: re-navigating an iframe drops the pointer state on its elements
try testing.expect(page.input_pointer.down_target == null);
try testing.expectEqual(0, page.input_pointer.held);
}
+
+test "cdp.input: an explicit clickCount is still invalidated on the button that ends a chorded gesture" {
+ 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 rect_x = try (try ls.local.compileAndRun("document.getElementById('btn').getBoundingClientRect().x", null)).toF64();
+ const rect_y = try (try ls.local.compileAndRun("document.getElementById('btn').getBoundingClientRect().y", null)).toF64();
+
+ // press-left, press-right (chord), release-right (mid-chord), release-left
+ // (ends the gesture) — clickCount 2 throughout.
+ try ctx.processMessage(.{
+ .id = 1,
+ .method = "Input.dispatchMouseEvent",
+ .params = .{ .type = "mousePressed", .x = rect_x, .y = rect_y, .button = "left", .clickCount = 2 },
+ });
+ try ctx.processMessage(.{
+ .id = 2,
+ .method = "Input.dispatchMouseEvent",
+ .params = .{ .type = "mousePressed", .x = rect_x, .y = rect_y, .button = "right", .clickCount = 2 },
+ });
+ try ctx.processMessage(.{
+ .id = 3,
+ .method = "Input.dispatchMouseEvent",
+ .params = .{ .type = "mouseReleased", .x = rect_x, .y = rect_y, .button = "right", .clickCount = 2 },
+ });
+ try ctx.processMessage(.{
+ .id = 4,
+ .method = "Input.dispatchMouseEvent",
+ .params = .{ .type = "mouseReleased", .x = rect_x, .y = rect_y, .button = "left", .clickCount = 2 },
+ });
+
+ const result = try ls.local.compileAndRun(
+ \\JSON.stringify(window.seq) === JSON.stringify([
+ \\ 'pointerdown:0:1:0:mouse:true', 'mousedown:0:1:2::true',
+ \\ 'mousedown:2:3:2::true', 'mouseup:2:1:2::true',
+ \\ 'pointerup:0:0:0:mouse:true', 'mouseup:0:0:0::true'
+ \\])
+ , null);
+ try testing.expect(result.isTrue());
+}
+
+test "cdp.input: an explicit clickCount is still invalidated on the secondary button that ends a chorded gesture" {
+ 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 rect_x = try (try ls.local.compileAndRun("document.getElementById('btn').getBoundingClientRect().x", null)).toF64();
+ const rect_y = try (try ls.local.compileAndRun("document.getElementById('btn').getBoundingClientRect().y", null)).toF64();
+
+ // press-right, press-left (chord), release-left (mid-chord), release-right
+ // (ends the gesture) - clickCount 2 throughout.
+ const steps = [_]struct { type: []const u8, button: []const u8 }{
+ .{ .type = "mousePressed", .button = "right" },
+ .{ .type = "mousePressed", .button = "left" },
+ .{ .type = "mouseReleased", .button = "left" },
+ .{ .type = "mouseReleased", .button = "right" },
+ };
+ for (steps, 1..) |step, id| {
+ try ctx.processMessage(.{
+ .id = @as(i32, @intCast(id)),
+ .method = "Input.dispatchMouseEvent",
+ .params = .{ .type = step.type, .x = rect_x, .y = rect_y, .button = step.button, .clickCount = 2 },
+ });
+ }
+
+ const result = try ls.local.compileAndRun(
+ \\const tail = window.seq.slice(-2);
+ \\tail[0] === 'pointerup:2:0:0:mouse:true' && tail[1] === 'mouseup:2:0:0::true'
+ , null);
+ try testing.expect(result.isTrue());
+}