Skip to content

Commit f74b612

Browse files
IdanHoawesomekling
authored andcommitted
LibWeb: Add the missing KeyboardEvent IDL constructor
This commit also does a bit of general cleanup on the header file.
1 parent c5b924b commit f74b612

File tree

5 files changed

+99
-35
lines changed

5 files changed

+99
-35
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
3+
*
4+
* SPDX-License-Identifier: BSD-2-Clause
5+
*/
6+
7+
#pragma once
8+
9+
#include <LibWeb/UIEvents/UIEvent.h>
10+
11+
namespace Web::UIEvents {
12+
13+
// https://w3c.github.io/uievents/#event-modifier-initializers
14+
struct EventModifierInit : public UIEventInit {
15+
bool ctrl_key { false };
16+
bool shift_key { false };
17+
bool alt_key { false };
18+
bool meta_key { false };
19+
};
20+
21+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#import <UIEvents/UIEvent.idl>
2+
3+
dictionary EventModifierInit : UIEventInit {
4+
boolean ctrlKey = false;
5+
boolean shiftKey = false;
6+
boolean altKey = false;
7+
boolean metaKey = false;
8+
9+
// FIXME: modifier* fields
10+
};

Userland/Libraries/LibWeb/UIEvents/KeyboardEvent.cpp

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -65,34 +65,29 @@ static unsigned long determine_key_code(KeyCode platform_key, u32 code_point)
6565
return platform_key;
6666
}
6767

68-
NonnullRefPtr<KeyboardEvent> KeyboardEvent::create_from_platform_event(FlyString event_name, KeyCode platform_key, unsigned modifiers, u32 code_point)
68+
NonnullRefPtr<KeyboardEvent> KeyboardEvent::create_from_platform_event(FlyString const& event_name, KeyCode platform_key, unsigned modifiers, u32 code_point)
6969
{
7070
// FIXME: Figure out what these should actually contain.
7171
String event_key = key_code_to_string(platform_key);
7272
String event_code = "FIXME";
7373

7474
auto key_code = determine_key_code(platform_key, code_point);
75-
return KeyboardEvent::create(move(event_name), move(event_key), move(event_code), 0, modifiers & Mod_Ctrl, modifiers & Mod_Shift, modifiers & Mod_Alt, false, false, false, key_code, code_point);
76-
}
77-
78-
KeyboardEvent::KeyboardEvent(FlyString event_name, String key, String code, unsigned long location, bool ctrl_key, bool shift_key, bool alt_key, bool meta_key, bool repeat, bool is_composing, unsigned long key_code, unsigned long char_code)
79-
: UIEvent(move(event_name))
80-
, m_key(move(key))
81-
, m_code(move(code))
82-
, m_location(location)
83-
, m_ctrl_key(ctrl_key)
84-
, m_shift_key(shift_key)
85-
, m_alt_key(alt_key)
86-
, m_meta_key(meta_key)
87-
, m_repeat(repeat)
88-
, m_is_composing(is_composing)
89-
, m_key_code(key_code)
90-
, m_char_code(char_code)
91-
{
92-
}
93-
94-
KeyboardEvent::~KeyboardEvent()
95-
{
75+
KeyboardEventInit event_init {};
76+
event_init.key = move(event_key);
77+
event_init.code = move(event_code);
78+
event_init.location = 0;
79+
event_init.ctrl_key = modifiers & Mod_Ctrl;
80+
event_init.shift_key = modifiers & Mod_Shift;
81+
event_init.alt_key = modifiers & Mod_Alt;
82+
event_init.meta_key = false;
83+
event_init.repeat = false;
84+
event_init.is_composing = false;
85+
event_init.key_code = key_code;
86+
event_init.char_code = code_point;
87+
event_init.bubbles = true;
88+
event_init.cancelable = true;
89+
event_init.composed = true;
90+
return KeyboardEvent::create(event_name, event_init);
9691
}
9792

9893
bool KeyboardEvent::get_modifier_state(String const& key_arg)

Userland/Libraries/LibWeb/UIEvents/KeyboardEvent.h

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,45 @@
88

99
#include <AK/TypeCasts.h>
1010
#include <Kernel/API/KeyCode.h>
11+
#include <LibWeb/UIEvents/EventModifier.h>
1112
#include <LibWeb/UIEvents/UIEvent.h>
1213

1314
namespace Web::UIEvents {
1415

16+
struct KeyboardEventInit : public EventModifierInit {
17+
String key { "" };
18+
String code { "" };
19+
u32 location { 0 };
20+
bool repeat { false };
21+
bool is_composing { false };
22+
u32 key_code { 0 };
23+
u32 char_code { 0 };
24+
};
25+
1526
// https://www.w3.org/TR/uievents/#interface-keyboardevent
1627
class KeyboardEvent final : public UIEvent {
1728
public:
1829
using WrapperType = Bindings::KeyboardEventWrapper;
1930

20-
static NonnullRefPtr<KeyboardEvent> create(FlyString event_name, String key, String code, unsigned long location, bool ctrl_key, bool shift_key, bool alt_key, bool meta_key, bool repeat, bool is_composing, unsigned long key_code, unsigned long char_code)
31+
static NonnullRefPtr<KeyboardEvent> create(FlyString const& event_name, KeyboardEventInit const& event_init = {})
32+
{
33+
return adopt_ref(*new KeyboardEvent(event_name, event_init));
34+
}
35+
static NonnullRefPtr<KeyboardEvent> create_with_global_object(Bindings::WindowObject&, FlyString const& event_name, KeyboardEventInit const& event_init)
2136
{
22-
return adopt_ref(*new KeyboardEvent(move(event_name), move(key), move(code), location, ctrl_key, shift_key, alt_key, meta_key, repeat, is_composing, key_code, char_code));
37+
return KeyboardEvent::create(event_name, event_init);
2338
}
2439

25-
static NonnullRefPtr<KeyboardEvent> create_from_platform_event(FlyString event_name, KeyCode, unsigned modifiers, u32 code_point);
40+
static NonnullRefPtr<KeyboardEvent> create_from_platform_event(FlyString const& event_name, KeyCode, unsigned modifiers, u32 code_point);
2641

27-
virtual ~KeyboardEvent() override;
42+
virtual ~KeyboardEvent() override = default;
2843

29-
unsigned long key_code() const { return m_key_code; }
30-
unsigned long char_code() const { return m_char_code; }
44+
u32 key_code() const { return m_key_code; }
45+
u32 char_code() const { return m_char_code; }
3146

3247
String key() const { return m_key; }
3348
String code() const { return m_code; }
34-
unsigned long location() const { return m_location; }
49+
u32 location() const { return m_location; }
3550

3651
bool ctrl_key() const { return m_ctrl_key; }
3752
bool shift_key() const { return m_shift_key; }
@@ -44,19 +59,31 @@ class KeyboardEvent final : public UIEvent {
4459
bool get_modifier_state(String const& key_arg);
4560

4661
private:
47-
KeyboardEvent(FlyString event_name, String key, String code, unsigned long location, bool ctrl_key, bool shift_key, bool alt_key, bool meta_key, bool repeat, bool is_composing, unsigned long key_code, unsigned long char_code);
62+
KeyboardEvent(FlyString const& event_name, KeyboardEventInit const& event_init)
63+
: UIEvent(event_name, event_init)
64+
, m_key(event_init.key)
65+
, m_code(event_init.code)
66+
, m_location(event_init.location)
67+
, m_ctrl_key(event_init.ctrl_key)
68+
, m_shift_key(event_init.shift_key)
69+
, m_alt_key(event_init.alt_key)
70+
, m_meta_key(event_init.meta_key)
71+
, m_repeat(event_init.repeat)
72+
, m_is_composing(event_init.is_composing)
73+
, m_key_code(event_init.key_code)
74+
, m_char_code(event_init.char_code) {};
4875

4976
String m_key;
5077
String m_code;
51-
unsigned long m_location { 0 };
78+
u32 m_location { 0 };
5279
bool m_ctrl_key { false };
5380
bool m_shift_key { false };
5481
bool m_alt_key { false };
5582
bool m_meta_key { false };
5683
bool m_repeat { false };
5784
bool m_is_composing { false };
58-
unsigned long m_key_code { 0 };
59-
unsigned long m_char_code { 0 };
85+
u32 m_key_code { 0 };
86+
u32 m_char_code { 0 };
6087
};
6188

6289
}

Userland/Libraries/LibWeb/UIEvents/KeyboardEvent.idl

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
#import <UIEvents/EventModifier.idl>
2+
13
[Exposed=Window]
24
interface KeyboardEvent : UIEvent {
35

4-
// FIXME: Implement this.
5-
// constructor(DOMString type, optional KeyboardEventInit eventInitDict = {});
6+
constructor(DOMString type, optional KeyboardEventInit eventInitDict = {});
67

78
// KeyLocationCode
89
const unsigned long DOM_KEY_LOCATION_STANDARD = 0x00;
@@ -28,3 +29,13 @@ interface KeyboardEvent : UIEvent {
2829
boolean getModifierState(DOMString keyArg);
2930

3031
};
32+
33+
dictionary KeyboardEventInit : EventModifierInit {
34+
DOMString key = "";
35+
DOMString code = "";
36+
unsigned long location = 0;
37+
boolean repeat = false;
38+
boolean isComposing = false;
39+
unsigned long charCode = 0;
40+
unsigned long keyCode = 0;
41+
};

0 commit comments

Comments
 (0)