mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-06-11 18:20:43 +09:00
LibWeb: Add a DOM Event class (instead of events being simple strings)
This patch adds the Event base class, along with a MouseEvent subclass. We now dispatch MouseEvent objects for mousedown, mouseup and mousemove and these objects have the .offsetX and .offsetY properties. Both of those properties are hard-coded at the moment. This will be fixed in the next patch. :^)
This commit is contained in:
parent
b196665131
commit
4dde36844b
Notes:
sideshowbarker
2024-07-19 08:11:59 +09:00
Author: https://github.com/awesomekling
Commit: 4dde36844b
15 changed files with 311 additions and 10 deletions
0
Libraries/LibWeb/DOM/Event.cpp
Normal file
0
Libraries/LibWeb/DOM/Event.cpp
Normal file
36
Libraries/LibWeb/DOM/Event.h
Normal file
36
Libraries/LibWeb/DOM/Event.h
Normal file
|
@ -0,0 +1,36 @@
|
|||
#pragma once
|
||||
|
||||
#include <AK/RefCounted.h>
|
||||
#include <AK/String.h>
|
||||
#include <LibWeb/Bindings/Wrappable.h>
|
||||
|
||||
namespace Web {
|
||||
|
||||
class Event
|
||||
: public RefCounted<Event>
|
||||
, public Bindings::Wrappable {
|
||||
public:
|
||||
using WrapperType = Bindings::EventWrapper;
|
||||
|
||||
static NonnullRefPtr<Event> create(String event_name)
|
||||
{
|
||||
return adopt(*new Event(move(event_name)));
|
||||
}
|
||||
|
||||
virtual ~Event() {}
|
||||
|
||||
const String& name() const { return m_event_name; }
|
||||
|
||||
virtual bool is_mouse_event() const { return false; }
|
||||
|
||||
protected:
|
||||
Event(String event_name)
|
||||
: m_event_name(move(event_name))
|
||||
{
|
||||
}
|
||||
|
||||
private:
|
||||
String m_event_name;
|
||||
};
|
||||
|
||||
}
|
|
@ -45,7 +45,7 @@ public:
|
|||
|
||||
void add_event_listener(String event_name, NonnullRefPtr<EventListener>);
|
||||
|
||||
virtual void dispatch_event(String event_name) = 0;
|
||||
virtual void dispatch_event(NonnullRefPtr<Event>) = 0;
|
||||
|
||||
struct EventListenerRegistration {
|
||||
String event_name;
|
||||
|
|
0
Libraries/LibWeb/DOM/MouseEvent.cpp
Normal file
0
Libraries/LibWeb/DOM/MouseEvent.cpp
Normal file
36
Libraries/LibWeb/DOM/MouseEvent.h
Normal file
36
Libraries/LibWeb/DOM/MouseEvent.h
Normal file
|
@ -0,0 +1,36 @@
|
|||
#pragma once
|
||||
|
||||
#include <LibWeb/DOM/Event.h>
|
||||
|
||||
namespace Web {
|
||||
|
||||
class MouseEvent final : public Event {
|
||||
public:
|
||||
using WrapperType = Bindings::MouseEventWrapper;
|
||||
|
||||
static NonnullRefPtr<MouseEvent> create(String event_name, i32 offset_x, i32 offset_y)
|
||||
{
|
||||
return adopt(*new MouseEvent(move(event_name), offset_x, offset_y));
|
||||
}
|
||||
|
||||
virtual ~MouseEvent() override {}
|
||||
|
||||
i32 offset_x() const { return m_offset_x; }
|
||||
i32 offset_y() const { return m_offset_y; }
|
||||
|
||||
protected:
|
||||
MouseEvent(String event_name, i32 offset_x, i32 offset_y)
|
||||
: Event(move(event_name))
|
||||
, m_offset_x(offset_x)
|
||||
, m_offset_y(offset_y)
|
||||
{
|
||||
}
|
||||
|
||||
private:
|
||||
virtual bool is_mouse_event() const override { return true; }
|
||||
|
||||
i32 m_offset_x { 0 };
|
||||
i32 m_offset_y { 0 };
|
||||
};
|
||||
|
||||
}
|
|
@ -29,9 +29,11 @@
|
|||
#include <LibJS/Interpreter.h>
|
||||
#include <LibJS/Runtime/Function.h>
|
||||
#include <LibJS/Runtime/ScriptFunction.h>
|
||||
#include <LibWeb/Bindings/EventWrapper.h>
|
||||
#include <LibWeb/Bindings/NodeWrapper.h>
|
||||
#include <LibWeb/CSS/StyleResolver.h>
|
||||
#include <LibWeb/DOM/Element.h>
|
||||
#include <LibWeb/DOM/Event.h>
|
||||
#include <LibWeb/DOM/EventListener.h>
|
||||
#include <LibWeb/DOM/HTMLAnchorElement.h>
|
||||
#include <LibWeb/DOM/Node.h>
|
||||
|
@ -123,21 +125,22 @@ bool Node::is_link() const
|
|||
return enclosing_link->has_attribute("href");
|
||||
}
|
||||
|
||||
void Node::dispatch_event(String event_name)
|
||||
void Node::dispatch_event(NonnullRefPtr<Event> event)
|
||||
{
|
||||
for (auto& listener : listeners()) {
|
||||
if (listener.event_name == event_name) {
|
||||
if (listener.event_name == event->name()) {
|
||||
auto* function = const_cast<EventListener&>(*listener.listener).function();
|
||||
static_cast<const JS::ScriptFunction*>(function)->body().dump(0);
|
||||
auto* this_value = wrap(function->heap(), *this);
|
||||
dbg() << "calling event listener with this=" << this_value;
|
||||
document().interpreter().call(function, this_value, {});
|
||||
auto* event_wrapper = wrap(function->heap(), *event);
|
||||
document().interpreter().call(function, this_value, { event_wrapper });
|
||||
}
|
||||
}
|
||||
|
||||
// FIXME: This is a hack. We should follow the real rules of event bubbling.
|
||||
if (parent())
|
||||
parent()->dispatch_event(move(event_name));
|
||||
parent()->dispatch_event(move(event));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -68,7 +68,7 @@ public:
|
|||
// ^EventTarget
|
||||
virtual void ref_event_target() final { ref(); }
|
||||
virtual void unref_event_target() final { unref(); }
|
||||
virtual void dispatch_event(String event_name) final;
|
||||
virtual void dispatch_event(NonnullRefPtr<Event>) final;
|
||||
|
||||
virtual ~Node();
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue