Event.h 6.0 KB

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