From e68def97b3337442b45f2db4bce908f4a5a90868 Mon Sep 17 00:00:00 2001 From: Rohit <71192000+rohitsux@users.noreply.github.com> Date: Sat, 6 Jun 2026 19:03:45 +0530 Subject: [PATCH] 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. --- src/browser/Frame.zig | 19 ++++++++++++------- src/cdp/domains/input.zig | 21 ++++++++++++++------- 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/src/browser/Frame.zig b/src/browser/Frame.zig index 42d127de..2258fad3 100644 --- a/src/browser/Frame.zig +++ b/src/browser/Frame.zig @@ -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 => {}, } } diff --git a/src/cdp/domains/input.zig b/src/cdp/domains/input.zig index 64a52b83..4d0afa01 100644 --- a/src/cdp/domains/input.zig +++ b/src/cdp/domains/input.zig @@ -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) {