mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 04:57:45 +00:00
LibWeb: Add the missing KeyboardEvent IDL constructor
This commit also does a bit of general cleanup on the header file.
This commit is contained in:
parent
c5b924b1e6
commit
f74b612aa4
5 changed files with 99 additions and 35 deletions
21
Userland/Libraries/LibWeb/UIEvents/EventModifier.h
Normal file
21
Userland/Libraries/LibWeb/UIEvents/EventModifier.h
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <LibWeb/UIEvents/UIEvent.h>
|
||||||
|
|
||||||
|
namespace Web::UIEvents {
|
||||||
|
|
||||||
|
// https://w3c.github.io/uievents/#event-modifier-initializers
|
||||||
|
struct EventModifierInit : public UIEventInit {
|
||||||
|
bool ctrl_key { false };
|
||||||
|
bool shift_key { false };
|
||||||
|
bool alt_key { false };
|
||||||
|
bool meta_key { false };
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
10
Userland/Libraries/LibWeb/UIEvents/EventModifier.idl
Normal file
10
Userland/Libraries/LibWeb/UIEvents/EventModifier.idl
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
#import <UIEvents/UIEvent.idl>
|
||||||
|
|
||||||
|
dictionary EventModifierInit : UIEventInit {
|
||||||
|
boolean ctrlKey = false;
|
||||||
|
boolean shiftKey = false;
|
||||||
|
boolean altKey = false;
|
||||||
|
boolean metaKey = false;
|
||||||
|
|
||||||
|
// FIXME: modifier* fields
|
||||||
|
};
|
|
@ -65,34 +65,29 @@ static unsigned long determine_key_code(KeyCode platform_key, u32 code_point)
|
||||||
return platform_key;
|
return platform_key;
|
||||||
}
|
}
|
||||||
|
|
||||||
NonnullRefPtr<KeyboardEvent> KeyboardEvent::create_from_platform_event(FlyString event_name, KeyCode platform_key, unsigned modifiers, u32 code_point)
|
NonnullRefPtr<KeyboardEvent> KeyboardEvent::create_from_platform_event(FlyString const& event_name, KeyCode platform_key, unsigned modifiers, u32 code_point)
|
||||||
{
|
{
|
||||||
// FIXME: Figure out what these should actually contain.
|
// FIXME: Figure out what these should actually contain.
|
||||||
String event_key = key_code_to_string(platform_key);
|
String event_key = key_code_to_string(platform_key);
|
||||||
String event_code = "FIXME";
|
String event_code = "FIXME";
|
||||||
|
|
||||||
auto key_code = determine_key_code(platform_key, code_point);
|
auto key_code = determine_key_code(platform_key, code_point);
|
||||||
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);
|
KeyboardEventInit event_init {};
|
||||||
}
|
event_init.key = move(event_key);
|
||||||
|
event_init.code = move(event_code);
|
||||||
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)
|
event_init.location = 0;
|
||||||
: UIEvent(move(event_name))
|
event_init.ctrl_key = modifiers & Mod_Ctrl;
|
||||||
, m_key(move(key))
|
event_init.shift_key = modifiers & Mod_Shift;
|
||||||
, m_code(move(code))
|
event_init.alt_key = modifiers & Mod_Alt;
|
||||||
, m_location(location)
|
event_init.meta_key = false;
|
||||||
, m_ctrl_key(ctrl_key)
|
event_init.repeat = false;
|
||||||
, m_shift_key(shift_key)
|
event_init.is_composing = false;
|
||||||
, m_alt_key(alt_key)
|
event_init.key_code = key_code;
|
||||||
, m_meta_key(meta_key)
|
event_init.char_code = code_point;
|
||||||
, m_repeat(repeat)
|
event_init.bubbles = true;
|
||||||
, m_is_composing(is_composing)
|
event_init.cancelable = true;
|
||||||
, m_key_code(key_code)
|
event_init.composed = true;
|
||||||
, m_char_code(char_code)
|
return KeyboardEvent::create(event_name, event_init);
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
KeyboardEvent::~KeyboardEvent()
|
|
||||||
{
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool KeyboardEvent::get_modifier_state(String const& key_arg)
|
bool KeyboardEvent::get_modifier_state(String const& key_arg)
|
||||||
|
|
|
@ -8,30 +8,45 @@
|
||||||
|
|
||||||
#include <AK/TypeCasts.h>
|
#include <AK/TypeCasts.h>
|
||||||
#include <Kernel/API/KeyCode.h>
|
#include <Kernel/API/KeyCode.h>
|
||||||
|
#include <LibWeb/UIEvents/EventModifier.h>
|
||||||
#include <LibWeb/UIEvents/UIEvent.h>
|
#include <LibWeb/UIEvents/UIEvent.h>
|
||||||
|
|
||||||
namespace Web::UIEvents {
|
namespace Web::UIEvents {
|
||||||
|
|
||||||
|
struct KeyboardEventInit : public EventModifierInit {
|
||||||
|
String key { "" };
|
||||||
|
String code { "" };
|
||||||
|
u32 location { 0 };
|
||||||
|
bool repeat { false };
|
||||||
|
bool is_composing { false };
|
||||||
|
u32 key_code { 0 };
|
||||||
|
u32 char_code { 0 };
|
||||||
|
};
|
||||||
|
|
||||||
// https://www.w3.org/TR/uievents/#interface-keyboardevent
|
// https://www.w3.org/TR/uievents/#interface-keyboardevent
|
||||||
class KeyboardEvent final : public UIEvent {
|
class KeyboardEvent final : public UIEvent {
|
||||||
public:
|
public:
|
||||||
using WrapperType = Bindings::KeyboardEventWrapper;
|
using WrapperType = Bindings::KeyboardEventWrapper;
|
||||||
|
|
||||||
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)
|
static NonnullRefPtr<KeyboardEvent> create(FlyString const& event_name, KeyboardEventInit const& event_init = {})
|
||||||
{
|
{
|
||||||
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));
|
return adopt_ref(*new KeyboardEvent(event_name, event_init));
|
||||||
|
}
|
||||||
|
static NonnullRefPtr<KeyboardEvent> create_with_global_object(Bindings::WindowObject&, FlyString const& event_name, KeyboardEventInit const& event_init)
|
||||||
|
{
|
||||||
|
return KeyboardEvent::create(event_name, event_init);
|
||||||
}
|
}
|
||||||
|
|
||||||
static NonnullRefPtr<KeyboardEvent> create_from_platform_event(FlyString event_name, KeyCode, unsigned modifiers, u32 code_point);
|
static NonnullRefPtr<KeyboardEvent> create_from_platform_event(FlyString const& event_name, KeyCode, unsigned modifiers, u32 code_point);
|
||||||
|
|
||||||
virtual ~KeyboardEvent() override;
|
virtual ~KeyboardEvent() override = default;
|
||||||
|
|
||||||
unsigned long key_code() const { return m_key_code; }
|
u32 key_code() const { return m_key_code; }
|
||||||
unsigned long char_code() const { return m_char_code; }
|
u32 char_code() const { return m_char_code; }
|
||||||
|
|
||||||
String key() const { return m_key; }
|
String key() const { return m_key; }
|
||||||
String code() const { return m_code; }
|
String code() const { return m_code; }
|
||||||
unsigned long location() const { return m_location; }
|
u32 location() const { return m_location; }
|
||||||
|
|
||||||
bool ctrl_key() const { return m_ctrl_key; }
|
bool ctrl_key() const { return m_ctrl_key; }
|
||||||
bool shift_key() const { return m_shift_key; }
|
bool shift_key() const { return m_shift_key; }
|
||||||
|
@ -44,19 +59,31 @@ public:
|
||||||
bool get_modifier_state(String const& key_arg);
|
bool get_modifier_state(String const& key_arg);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
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);
|
KeyboardEvent(FlyString const& event_name, KeyboardEventInit const& event_init)
|
||||||
|
: UIEvent(event_name, event_init)
|
||||||
|
, m_key(event_init.key)
|
||||||
|
, m_code(event_init.code)
|
||||||
|
, m_location(event_init.location)
|
||||||
|
, m_ctrl_key(event_init.ctrl_key)
|
||||||
|
, m_shift_key(event_init.shift_key)
|
||||||
|
, m_alt_key(event_init.alt_key)
|
||||||
|
, m_meta_key(event_init.meta_key)
|
||||||
|
, m_repeat(event_init.repeat)
|
||||||
|
, m_is_composing(event_init.is_composing)
|
||||||
|
, m_key_code(event_init.key_code)
|
||||||
|
, m_char_code(event_init.char_code) {};
|
||||||
|
|
||||||
String m_key;
|
String m_key;
|
||||||
String m_code;
|
String m_code;
|
||||||
unsigned long m_location { 0 };
|
u32 m_location { 0 };
|
||||||
bool m_ctrl_key { false };
|
bool m_ctrl_key { false };
|
||||||
bool m_shift_key { false };
|
bool m_shift_key { false };
|
||||||
bool m_alt_key { false };
|
bool m_alt_key { false };
|
||||||
bool m_meta_key { false };
|
bool m_meta_key { false };
|
||||||
bool m_repeat { false };
|
bool m_repeat { false };
|
||||||
bool m_is_composing { false };
|
bool m_is_composing { false };
|
||||||
unsigned long m_key_code { 0 };
|
u32 m_key_code { 0 };
|
||||||
unsigned long m_char_code { 0 };
|
u32 m_char_code { 0 };
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
|
#import <UIEvents/EventModifier.idl>
|
||||||
|
|
||||||
[Exposed=Window]
|
[Exposed=Window]
|
||||||
interface KeyboardEvent : UIEvent {
|
interface KeyboardEvent : UIEvent {
|
||||||
|
|
||||||
// FIXME: Implement this.
|
constructor(DOMString type, optional KeyboardEventInit eventInitDict = {});
|
||||||
// constructor(DOMString type, optional KeyboardEventInit eventInitDict = {});
|
|
||||||
|
|
||||||
// KeyLocationCode
|
// KeyLocationCode
|
||||||
const unsigned long DOM_KEY_LOCATION_STANDARD = 0x00;
|
const unsigned long DOM_KEY_LOCATION_STANDARD = 0x00;
|
||||||
|
@ -28,3 +29,13 @@ interface KeyboardEvent : UIEvent {
|
||||||
boolean getModifierState(DOMString keyArg);
|
boolean getModifierState(DOMString keyArg);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
dictionary KeyboardEventInit : EventModifierInit {
|
||||||
|
DOMString key = "";
|
||||||
|
DOMString code = "";
|
||||||
|
unsigned long location = 0;
|
||||||
|
boolean repeat = false;
|
||||||
|
boolean isComposing = false;
|
||||||
|
unsigned long charCode = 0;
|
||||||
|
unsigned long keyCode = 0;
|
||||||
|
};
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue