|
| 1 | +// Copyright (C) 2023-2024 Lightpanda (Selecy SAS) |
| 2 | +// |
| 3 | +// Francis Bouvier <[email protected]> |
| 4 | +// Pierre Tachoire <[email protected]> |
| 5 | +// |
| 6 | +// This program is free software: you can redistribute it and/or modify |
| 7 | +// it under the terms of the GNU Affero General Public License as |
| 8 | +// published by the Free Software Foundation, either version 3 of the |
| 9 | +// License, or (at your option) any later version. |
| 10 | +// |
| 11 | +// This program is distributed in the hope that it will be useful, |
| 12 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | +// GNU Affero General Public License for more details. |
| 15 | +// |
| 16 | +// You should have received a copy of the GNU Affero General Public License |
| 17 | +// along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 18 | + |
| 19 | +const parser = @import("../netsurf.zig"); |
| 20 | +const Event = @import("event.zig").Event; |
| 21 | +const JsObject = @import("../env.zig").JsObject; |
| 22 | + |
| 23 | +// https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent |
| 24 | +pub const MouseEvent = struct { |
| 25 | + pub const Self = parser.MouseEvent; |
| 26 | + pub const prototype = *Event; |
| 27 | + |
| 28 | + const MouseEventInit = struct { |
| 29 | + screenX: i32 = 0, |
| 30 | + screenY: i32 = 0, |
| 31 | + clientX: i32 = 0, |
| 32 | + clientY: i32 = 0, |
| 33 | + ctrlKey: bool = false, |
| 34 | + shiftKey: bool = false, |
| 35 | + altKey: bool = false, |
| 36 | + metaKey: bool = false, |
| 37 | + button: u16 = 0, |
| 38 | + }; |
| 39 | + |
| 40 | + pub fn constructor(event_type: []const u8, opts_: ?MouseEventInit) !*Self { |
| 41 | + const opts = opts_ orelse MouseEventInit{}; |
| 42 | + |
| 43 | + const mouse_event = try parser.mouseEventCreate(); |
| 44 | + |
| 45 | + try parser.mouseEventInit(mouse_event, event_type, .{ |
| 46 | + .x = opts.clientX, |
| 47 | + .y = opts.clientY, |
| 48 | + .ctrl = opts.ctrlKey, |
| 49 | + .shift = opts.shiftKey, |
| 50 | + .alt = opts.altKey, |
| 51 | + .meta = opts.metaKey, |
| 52 | + .button = opts.button, |
| 53 | + }); |
| 54 | + |
| 55 | + return mouse_event; |
| 56 | + } |
| 57 | + |
| 58 | + // These is just an alias for clientX. |
| 59 | + pub fn get_x(self: *Self) !i32 { |
| 60 | + return self.cx; |
| 61 | + } |
| 62 | + |
| 63 | + // These is just an alias for clientY. |
| 64 | + pub fn get_y(self: *Self) !i32 { |
| 65 | + return self.cy; |
| 66 | + } |
| 67 | + |
| 68 | + pub fn get_clientX(self: *Self) !i32 { |
| 69 | + return self.cx; |
| 70 | + } |
| 71 | + |
| 72 | + pub fn get_clientY(self: *Self) !i32 { |
| 73 | + return self.cy; |
| 74 | + } |
| 75 | + |
| 76 | + pub fn get_screenX(self: *Self) !i32 { |
| 77 | + return self.sx; |
| 78 | + } |
| 79 | + |
| 80 | + pub fn get_screenY(self: *Self) !i32 { |
| 81 | + return self.sy; |
| 82 | + } |
| 83 | +}; |
| 84 | + |
| 85 | +const testing = @import("../../testing.zig"); |
| 86 | +test "Browser.MouseEvent" { |
| 87 | + var runner = try testing.jsRunner(testing.tracking_allocator, .{}); |
| 88 | + defer runner.deinit(); |
| 89 | + |
| 90 | + try runner.testCases(&.{ |
| 91 | + .{ "let event = new MouseEvent('click')", "undefined" }, |
| 92 | + .{ "event.type", "click" }, |
| 93 | + .{ "event instanceof MouseEvent", "true" }, |
| 94 | + .{ "event instanceof Event", "true" }, |
| 95 | + .{ "event.clientX", "0" }, |
| 96 | + .{ "event.clientY", "0" }, |
| 97 | + .{ "event.screenX", "0" }, |
| 98 | + .{ "event.screenY", "0" }, |
| 99 | + .{ "let new_event = new MouseEvent('click2', { 'clientX': 10, 'clientY': 20 })", "undefined" }, |
| 100 | + .{ "new_event.x", "10" }, |
| 101 | + .{ "new_event.y", "20" }, |
| 102 | + .{ "new_event.screenX", "10" }, |
| 103 | + .{ "new_event.screenY", "20" }, |
| 104 | + }, .{}); |
| 105 | +} |
0 commit comments