Event.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * Copyright (c) 2020-2022, Andreas Kling <andreas@ladybird.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/FlyString.h>
  8. #include <LibWeb/Bindings/PlatformObject.h>
  9. #include <LibWeb/DOM/EventTarget.h>
  10. #include <LibWeb/HighResolutionTime/DOMHighResTimeStamp.h>
  11. namespace Web::DOM {
  12. struct EventInit {
  13. bool bubbles { false };
  14. bool cancelable { false };
  15. bool composed { false };
  16. };
  17. class Event : public Bindings::PlatformObject {
  18. WEB_PLATFORM_OBJECT(Event, Bindings::PlatformObject);
  19. JS_DECLARE_ALLOCATOR(Event);
  20. public:
  21. enum Phase : u16 {
  22. None = 0,
  23. CapturingPhase = 1,
  24. AtTarget = 2,
  25. BubblingPhase = 3,
  26. };
  27. // FIXME: These need explicit marking somehow.
  28. using TouchTargetList = Vector<JS::GCPtr<EventTarget>>;
  29. struct PathEntry {
  30. JS::GCPtr<EventTarget> invocation_target;
  31. bool invocation_target_in_shadow_tree { false };
  32. JS::GCPtr<EventTarget> shadow_adjusted_target;
  33. JS::GCPtr<EventTarget> related_target;
  34. TouchTargetList touch_target_list;
  35. bool root_of_closed_tree { false };
  36. bool slot_in_closed_tree { false };
  37. size_t index;
  38. };
  39. using Path = Vector<PathEntry>;
  40. [[nodiscard]] static JS::NonnullGCPtr<Event> create(JS::Realm&, FlyString const& event_name, EventInit const& event_init = {});
  41. static WebIDL::ExceptionOr<JS::NonnullGCPtr<Event>> construct_impl(JS::Realm&, FlyString const& event_name, EventInit const& event_init);
  42. Event(JS::Realm&, FlyString const& type);
  43. Event(JS::Realm&, FlyString const& type, EventInit const& event_init);
  44. virtual ~Event() = default;
  45. // https://dom.spec.whatwg.org/#dom-event-timestamp
  46. HighResolutionTime::DOMHighResTimeStamp time_stamp() const { return m_time_stamp; }
  47. FlyString const& type() const { return m_type; }
  48. void set_type(FlyString const& type) { m_type = type; }
  49. JS::GCPtr<EventTarget> target() const { return m_target; }
  50. void set_target(EventTarget* target) { m_target = target; }
  51. // NOTE: This is intended for the JS bindings.
  52. JS::GCPtr<EventTarget> src_element() const { return target(); }
  53. JS::GCPtr<EventTarget> related_target() const { return m_related_target; }
  54. void set_related_target(EventTarget* related_target) { m_related_target = related_target; }
  55. bool should_stop_propagation() const { return m_stop_propagation; }
  56. void set_stop_propagation(bool stop_propagation) { m_stop_propagation = stop_propagation; }
  57. bool should_stop_immediate_propagation() const { return m_stop_immediate_propagation; }
  58. void set_stop_immediate_propagation(bool stop_immediate_propagation) { m_stop_immediate_propagation = stop_immediate_propagation; }
  59. bool cancelled() const { return m_cancelled; }
  60. void set_cancelled(bool cancelled) { m_cancelled = cancelled; }
  61. bool in_passive_listener() const { return m_in_passive_listener; }
  62. void set_in_passive_listener(bool in_passive_listener) { m_in_passive_listener = in_passive_listener; }
  63. bool composed() const { return m_composed; }
  64. void set_composed(bool composed) { m_composed = composed; }
  65. bool initialized() const { return m_initialized; }
  66. void set_initialized(bool initialized) { m_initialized = initialized; }
  67. bool dispatched() const { return m_dispatch; }
  68. void set_dispatched(bool dispatched) { m_dispatch = dispatched; }
  69. void prevent_default() { set_cancelled_flag(); }
  70. bool default_prevented() const { return cancelled(); }
  71. u16 event_phase() const { return m_phase; }
  72. void set_phase(Phase phase) { m_phase = phase; }
  73. JS::GCPtr<EventTarget> current_target() const { return m_current_target; }
  74. void set_current_target(EventTarget* current_target) { m_current_target = current_target; }
  75. bool return_value() const { return !m_cancelled; }
  76. void set_return_value(bool return_value)
  77. {
  78. if (!return_value)
  79. set_cancelled_flag();
  80. }
  81. void append_to_path(EventTarget&, JS::GCPtr<EventTarget>, JS::GCPtr<EventTarget>, TouchTargetList&, bool);
  82. Path& path() { return m_path; }
  83. Path const& path() const { return m_path; }
  84. void clear_path() { m_path.clear(); }
  85. void set_touch_target_list(TouchTargetList& touch_target_list) { m_touch_target_list = touch_target_list; }
  86. TouchTargetList& touch_target_list() { return m_touch_target_list; }
  87. void clear_touch_target_list() { m_touch_target_list.clear(); }
  88. bool bubbles() const { return m_bubbles; }
  89. void set_bubbles(bool bubbles) { m_bubbles = bubbles; }
  90. bool cancelable() const { return m_cancelable; }
  91. void set_cancelable(bool cancelable) { m_cancelable = cancelable; }
  92. bool is_trusted() const { return m_is_trusted; }
  93. void set_is_trusted(bool is_trusted) { m_is_trusted = is_trusted; }
  94. void stop_propagation() { m_stop_propagation = true; }
  95. bool cancel_bubble() const { return m_stop_propagation; }
  96. void set_cancel_bubble(bool cancel_bubble)
  97. {
  98. if (cancel_bubble)
  99. m_stop_propagation = true;
  100. }
  101. void stop_immediate_propagation()
  102. {
  103. m_stop_propagation = true;
  104. m_stop_immediate_propagation = true;
  105. }
  106. void init_event(String const&, bool, bool);
  107. void set_time_stamp(double time_stamp) { m_time_stamp = time_stamp; }
  108. Vector<JS::Handle<EventTarget>> composed_path() const;
  109. template<typename T>
  110. bool fast_is() const = delete;
  111. virtual bool is_mouse_event() const { return false; }
  112. virtual bool is_pointer_event() const { return false; }
  113. protected:
  114. void initialize_event(String const&, bool, bool);
  115. virtual void initialize(JS::Realm&) override;
  116. virtual void visit_edges(Visitor&) override;
  117. private:
  118. FlyString m_type;
  119. JS::GCPtr<EventTarget> m_target;
  120. JS::GCPtr<EventTarget> m_related_target;
  121. JS::GCPtr<EventTarget> m_current_target;
  122. Phase m_phase { None };
  123. bool m_bubbles { false };
  124. bool m_cancelable { false };
  125. bool m_stop_propagation { false };
  126. bool m_stop_immediate_propagation { false };
  127. bool m_cancelled { false };
  128. bool m_in_passive_listener { false };
  129. bool m_composed { false };
  130. bool m_initialized { false };
  131. bool m_dispatch { false };
  132. bool m_is_trusted { false };
  133. Path m_path;
  134. TouchTargetList m_touch_target_list;
  135. HighResolutionTime::DOMHighResTimeStamp m_time_stamp { 0 };
  136. void set_cancelled_flag();
  137. };
  138. }