|
| 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 std = @import("std"); |
| 20 | +const log = @import("../../log.zig"); |
| 21 | +const builtin = @import("builtin"); |
| 22 | + |
| 23 | +const netsurf = @import("../netsurf.zig"); |
| 24 | +const Event = @import("event.zig").Event; |
| 25 | +const JsObject = @import("../env.zig").JsObject; |
| 26 | + |
| 27 | +// TODO: We currently don't have a UIEvent interface so we skip it in the prototype chain. |
| 28 | +// https://developer.mozilla.org/en-US/docs/Web/API/UIEvent |
| 29 | +const UIEvent = Event; |
| 30 | + |
| 31 | +// https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent |
| 32 | +pub const KeyboardEvent = struct { |
| 33 | + pub const Self = netsurf.KeyboardEvent; |
| 34 | + pub const prototype = *UIEvent; |
| 35 | + |
| 36 | + pub const ConstructorOptions = struct { |
| 37 | + key: []const u8 = "", |
| 38 | + code: []const u8 = "", |
| 39 | + location: netsurf.KeyboardEventOpts.LocationCode = .standard, |
| 40 | + repeat: bool = false, |
| 41 | + isComposing: bool = false, |
| 42 | + // Currently not supported but we take as argument. |
| 43 | + charCode: u32 = 0, |
| 44 | + // Currently not supported but we take as argument. |
| 45 | + keyCode: u32 = 0, |
| 46 | + // Currently not supported but we take as argument. |
| 47 | + which: u32 = 0, |
| 48 | + ctrlKey: bool = false, |
| 49 | + shiftKey: bool = false, |
| 50 | + altKey: bool = false, |
| 51 | + metaKey: bool = false, |
| 52 | + }; |
| 53 | + |
| 54 | + pub fn constructor(event_type: []const u8, maybe_options: ?ConstructorOptions) !*netsurf.KeyboardEvent { |
| 55 | + const options: ConstructorOptions = maybe_options orelse .{}; |
| 56 | + |
| 57 | + var event = try netsurf.keyboardEventCreate(); |
| 58 | + try netsurf.eventSetInternalType(@ptrCast(&event), .keyboard_event); |
| 59 | + |
| 60 | + try netsurf.keyboardEventInit( |
| 61 | + event, |
| 62 | + event_type, |
| 63 | + .{ |
| 64 | + .key = options.key, |
| 65 | + .code = options.code, |
| 66 | + .location = options.location, |
| 67 | + .repeat = options.repeat, |
| 68 | + .is_composing = options.isComposing, |
| 69 | + .ctrl_key = options.ctrlKey, |
| 70 | + .shift_key = options.shiftKey, |
| 71 | + .alt_key = options.altKey, |
| 72 | + .meta_key = options.metaKey, |
| 73 | + }, |
| 74 | + ); |
| 75 | + |
| 76 | + return event; |
| 77 | + } |
| 78 | + |
| 79 | + // Returns the modifier state for given modifier key. |
| 80 | + pub fn _getModifierState(self: *Self, key: []const u8) bool { |
| 81 | + // Chrome and Firefox do case-sensitive match, here we prefer the same. |
| 82 | + if (std.mem.eql(u8, key, "Alt")) { |
| 83 | + return get_altKey(self); |
| 84 | + } |
| 85 | + |
| 86 | + if (std.mem.eql(u8, key, "AltGraph")) { |
| 87 | + return (get_altKey(self) and get_ctrlKey(self)); |
| 88 | + } |
| 89 | + |
| 90 | + if (std.mem.eql(u8, key, "Control")) { |
| 91 | + return get_ctrlKey(self); |
| 92 | + } |
| 93 | + |
| 94 | + if (std.mem.eql(u8, key, "Shift")) { |
| 95 | + return get_shiftKey(self); |
| 96 | + } |
| 97 | + |
| 98 | + if (std.mem.eql(u8, key, "Meta") or std.mem.eql(u8, key, "OS")) { |
| 99 | + return get_metaKey(self); |
| 100 | + } |
| 101 | + |
| 102 | + // Special case for IE. |
| 103 | + if (comptime builtin.os.tag == .windows) { |
| 104 | + if (std.mem.eql(u8, key, "Win")) { |
| 105 | + return get_metaKey(self); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + // getModifierState() also accepts a deprecated virtual modifier named "Accel". |
| 110 | + // event.getModifierState("Accel") returns true when at least one of |
| 111 | + // KeyboardEvent.ctrlKey or KeyboardEvent.metaKey is true. |
| 112 | + // |
| 113 | + // https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/getModifierState#accel_virtual_modifier |
| 114 | + if (std.mem.eql(u8, key, "Accel")) { |
| 115 | + return (get_ctrlKey(self) or get_metaKey(self)); |
| 116 | + } |
| 117 | + |
| 118 | + // TODO: Add support for "CapsLock", "ScrollLock". |
| 119 | + return false; |
| 120 | + } |
| 121 | + |
| 122 | + // Getters. |
| 123 | + |
| 124 | + pub fn get_altKey(self: *Self) bool { |
| 125 | + return netsurf.keyboardEventKeyIsSet(self, .alt); |
| 126 | + } |
| 127 | + |
| 128 | + pub fn get_ctrlKey(self: *Self) bool { |
| 129 | + return netsurf.keyboardEventKeyIsSet(self, .ctrl); |
| 130 | + } |
| 131 | + |
| 132 | + pub fn get_metaKey(self: *Self) bool { |
| 133 | + return netsurf.keyboardEventKeyIsSet(self, .meta); |
| 134 | + } |
| 135 | + |
| 136 | + pub fn get_shiftKey(self: *Self) bool { |
| 137 | + return netsurf.keyboardEventKeyIsSet(self, .shift); |
| 138 | + } |
| 139 | + |
| 140 | + pub fn get_isComposing(self: *Self) bool { |
| 141 | + return self.is_composing; |
| 142 | + } |
| 143 | + |
| 144 | + pub fn get_location(self: *Self) u32 { |
| 145 | + return self.location; |
| 146 | + } |
| 147 | + |
| 148 | + pub fn get_key(self: *Self) ![]const u8 { |
| 149 | + return netsurf.keyboardEventGetKey(self); |
| 150 | + } |
| 151 | + |
| 152 | + pub fn get_repeat(self: *Self) bool { |
| 153 | + return self.repeat; |
| 154 | + } |
| 155 | +}; |
| 156 | + |
| 157 | +const testing = @import("../../testing.zig"); |
| 158 | +test "Browser: Events.Keyboard" { |
| 159 | + try testing.htmlRunner("events/keyboard.html"); |
| 160 | +} |
0 commit comments