Skip to content

Commit 62dc548

Browse files
committed
add MouseEvent
1 parent 6506fa7 commit 62dc548

File tree

3 files changed

+109
-0
lines changed

3 files changed

+109
-0
lines changed

src/browser/events/event.zig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ const EventTargetUnion = @import("../dom/event_target.zig").Union;
2929

3030
const CustomEvent = @import("custom_event.zig").CustomEvent;
3131
const ProgressEvent = @import("../xhr/progress_event.zig").ProgressEvent;
32+
const MouseEvent = @import("mouse_event.zig").MouseEvent;
3233

3334
const log = std.log.scoped(.events);
3435

@@ -37,6 +38,7 @@ pub const Interfaces = .{
3738
Event,
3839
CustomEvent,
3940
ProgressEvent,
41+
MouseEvent,
4042
};
4143

4244
pub const Union = generate.Union(Interfaces);
@@ -60,6 +62,7 @@ pub const Event = struct {
6062
.event => .{ .Event = evt },
6163
.custom_event => .{ .CustomEvent = @as(*CustomEvent, @ptrCast(evt)).* },
6264
.progress_event => .{ .ProgressEvent = @as(*ProgressEvent, @ptrCast(evt)).* },
65+
.mouse_event => .{ .MouseEvent = @as(**parser.MouseEvent, @ptrCast(evt)).* },
6366
};
6467
}
6568

src/browser/events/mouse_event.zig

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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+
}

src/browser/netsurf.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,7 @@ pub const EventType = enum(u8) {
521521
event = 0,
522522
progress_event = 1,
523523
custom_event = 2,
524+
mouse_event = 3,
524525
};
525526

526527
pub const MutationEvent = c.dom_mutation_event;

0 commit comments

Comments
 (0)