|
| 1 | +const std = @import("std"); |
| 2 | +const log = @import("../../log.zig"); |
| 3 | + |
| 4 | +const netsurf = @import("../netsurf.zig"); |
| 5 | +const Event = @import("event.zig").Event; |
| 6 | +const JsObject = @import("../env.zig").JsObject; |
| 7 | + |
| 8 | +const c = @cImport({ |
| 9 | + @cInclude("dom/dom.h"); |
| 10 | + @cInclude("core/pi.h"); |
| 11 | + @cInclude("dom/bindings/hubbub/parser.h"); |
| 12 | + @cInclude("events/event_target.h"); |
| 13 | + @cInclude("events/event.h"); |
| 14 | + @cInclude("events/mouse_event.h"); |
| 15 | + @cInclude("events/keyboard_event.h"); |
| 16 | + @cInclude("utils/validate.h"); |
| 17 | + @cInclude("html/html_element.h"); |
| 18 | + @cInclude("html/html_document.h"); |
| 19 | +}); |
| 20 | + |
| 21 | +// TODO: We currently don't have a UIEvent interface so we skip it in the prototype chain. |
| 22 | +// https://developer.mozilla.org/en-US/docs/Web/API/UIEvent |
| 23 | +const UIEvent = Event; |
| 24 | + |
| 25 | +// https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent |
| 26 | +pub const KeyboardEvent = struct { |
| 27 | + pub const Self = netsurf.KeyboardEvent; |
| 28 | + pub const prototype = *UIEvent; |
| 29 | + |
| 30 | + pub const KeyLocationCode = enum(u16) { |
| 31 | + standard = 0x00, |
| 32 | + left = 0x01, |
| 33 | + right = 0x02, |
| 34 | + numpad = 0x03, |
| 35 | + mobile = 0x04, // Non-standard, deprecated. |
| 36 | + joystick = 0x05, // Non-standard, deprecated. |
| 37 | + }; |
| 38 | + |
| 39 | + pub const ConstructorOptions = struct { |
| 40 | + key: []const u8 = "", |
| 41 | + code: []const u8 = "", |
| 42 | + location: KeyLocationCode = .standard, |
| 43 | + char_code: u32 = 0, |
| 44 | + key_code: u32 = 0, |
| 45 | + which: u32 = 0, |
| 46 | + repeat: bool = false, |
| 47 | + ctrl_key: bool = false, |
| 48 | + shift_key: bool = false, |
| 49 | + alt_key: bool = false, |
| 50 | + meta_key: bool = false, |
| 51 | + is_composing: bool = false, |
| 52 | + }; |
| 53 | + |
| 54 | + pub fn constructor(event_type: []const u8, maybe_options: ?ConstructorOptions) !*netsurf.KeyboardEvent { |
| 55 | + const options = maybe_options orelse ConstructorOptions{}; |
| 56 | + |
| 57 | + const event = try netsurf.keyboardEventCreate(); |
| 58 | + try netsurf.keyboardEventInit( |
| 59 | + event, |
| 60 | + event_type, |
| 61 | + .{ |
| 62 | + .bubbles = false, |
| 63 | + .cancelable = false, |
| 64 | + .key = options.key, |
| 65 | + .code = options.code, |
| 66 | + .alt = options.alt_key, |
| 67 | + .ctrl = options.ctrl_key, |
| 68 | + .meta = options.meta_key, |
| 69 | + .shift = options.shift_key, |
| 70 | + }, |
| 71 | + ); |
| 72 | + |
| 73 | + return event; |
| 74 | + } |
| 75 | +}; |
| 76 | + |
| 77 | +const testing = @import("../../testing.zig"); |
| 78 | +test "Browser: Events.Keyboard" { |
| 79 | + try testing.htmlRunner("events/keyboard.html"); |
| 80 | +} |
0 commit comments