refactor(cdp): use named constants for mouse button values

Address review: replace the bare 0/1/2 button values in the
mousedown/release switch (Frame.zig) and the CDP button mapping
(input.zig) with named constants so the code self-documents.
This commit is contained in:
Rohit
2026-06-06 19:03:45 +05:30
parent 1776d0ea71
commit e68def97b3
2 changed files with 26 additions and 14 deletions

View File

@@ -3902,6 +3902,14 @@ fn findFrameByName(frame: *Frame, name: []const u8) ?*Frame {
return null;
}
// DOM MouseEvent.button values.
// https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/button
const mouse_button = struct {
const main: i32 = 0; // left
const auxiliary: i32 = 1; // middle
const secondary: i32 = 2; // right
};
// Dispatch a single trusted mouse event of the given type on `target`, carrying
// the pressed button and pointer position. `detail` is the click count (used for
// click/dblclick); 0 for events where it does not apply.
@@ -3988,20 +3996,17 @@ pub fn triggerMouseRelease(self: *Frame, x: f64, y: f64, button: i32, click_coun
try self.dispatchMouseEventOn(target, "mouseup", x, y, button, detail);
// After mouseup, the activation event depends on the button:
// main (0) -> click
// auxiliary (1) -> auxclick
// secondary (2) -> contextmenu
// After mouseup, the activation event depends on the button.
switch (button) {
0 => {
mouse_button.main => {
try self.dispatchMouseEventOn(target, "click", x, y, button, detail);
// A second click in quick succession also fires dblclick.
if (click_count == 2) {
try self.dispatchMouseEventOn(target, "dblclick", x, y, button, detail);
}
},
1 => try self.dispatchMouseEventOn(target, "auxclick", x, y, button, detail),
2 => try self.dispatchMouseEventOn(target, "contextmenu", x, y, button, detail),
mouse_button.auxiliary => try self.dispatchMouseEventOn(target, "auxclick", x, y, button, detail),
mouse_button.secondary => try self.dispatchMouseEventOn(target, "contextmenu", x, y, button, detail),
else => {},
}
}

View File

@@ -111,14 +111,21 @@ fn dispatchMouseEvent(cmd: *CDP.Command) !void {
const bc = cmd.browser_context orelse return;
const frame = bc.session.currentFrame() orelse return;
// Map the CDP button name to the DOM MouseEvent.button numbering
// (left/main = 0, middle/auxiliary = 1, right/secondary = 2, ...).
// Map the CDP button name to the DOM MouseEvent.button value.
// https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/button
const dom_button = struct {
const main: i32 = 0;
const auxiliary: i32 = 1;
const secondary: i32 = 2;
const fourth: i32 = 3;
const fifth: i32 = 4;
};
const button: i32 = switch (params.button) {
.none, .left => 0,
.middle => 1,
.right => 2,
.back => 3,
.forward => 4,
.none, .left => dom_button.main,
.middle => dom_button.auxiliary,
.right => dom_button.secondary,
.back => dom_button.fourth,
.forward => dom_button.fifth,
};
switch (params.type) {