Event.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. * Copyright (c) 2020, 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/WindowObject.h>
  9. #include <LibWeb/Bindings/Wrappable.h>
  10. #include <LibWeb/DOM/EventTarget.h>
  11. namespace Web::DOM {
  12. struct EventInit {
  13. bool bubbles { false };
  14. bool cancelable { false };
  15. bool composed { false };
  16. };
  17. class Event
  18. : public RefCounted<Event>
  19. , public Bindings::Wrappable {
  20. public:
  21. using WrapperType = Bindings::EventWrapper;
  22. enum Phase : u16 {
  23. None = 0,
  24. CapturingPhase = 1,
  25. AtTarget = 2,
  26. BubblingPhase = 3,
  27. };
  28. using TouchTargetList = Vector<RefPtr<EventTarget>>;
  29. struct PathEntry {
  30. RefPtr<EventTarget> invocation_target;
  31. bool invocation_target_in_shadow_tree { false };
  32. RefPtr<EventTarget> shadow_adjusted_target;
  33. RefPtr<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. static NonnullRefPtr<Event> create(const FlyString& event_name, EventInit const& event_init = {})
  41. {
  42. return adopt_ref(*new Event(event_name, event_init));
  43. }
  44. static NonnullRefPtr<Event> create_with_global_object(Bindings::WindowObject&, const FlyString& event_name, EventInit const& event_init)
  45. {
  46. return Event::create(event_name, event_init);
  47. }
  48. virtual ~Event() = default;
  49. double time_stamp() const;
  50. const FlyString& type() const { return m_type; }
  51. void set_type(StringView type) { m_type = type; }
  52. RefPtr<EventTarget> target() const { return m_target; }
  53. void set_target(EventTarget* target) { m_target = target; }
  54. // NOTE: This is intended for the JS bindings.
  55. RefPtr<EventTarget> src_target() const { return target(); }
  56. RefPtr<EventTarget> related_target() const { return m_related_target; }
  57. void set_related_target(EventTarget* related_target) { m_related_target = related_target; }
  58. bool should_stop_propagation() const { return m_stop_propagation; }
  59. void set_stop_propagation(bool stop_propagation) { m_stop_propagation = stop_propagation; }
  60. bool should_stop_immediate_propagation() const { return m_stop_immediate_propagation; }
  61. void set_stop_immediate_propagation(bool stop_immediate_propagation) { m_stop_immediate_propagation = stop_immediate_propagation; }
  62. bool cancelled() const { return m_cancelled; }
  63. void set_cancelled(bool cancelled) { m_cancelled = cancelled; }
  64. bool in_passive_listener() const { return m_in_passive_listener; }
  65. void set_in_passive_listener(bool in_passive_listener) { m_in_passive_listener = in_passive_listener; }
  66. bool composed() const { return m_composed; }
  67. void set_composed(bool composed) { m_composed = composed; }
  68. bool initialized() const { return m_initialized; }
  69. void set_initialized(bool initialized) { m_initialized = initialized; }
  70. bool dispatched() const { return m_dispatch; }
  71. void set_dispatched(bool dispatched) { m_dispatch = dispatched; }
  72. void prevent_default() { set_cancelled_flag(); }
  73. bool default_prevented() const { return cancelled(); }
  74. u16 event_phase() const { return m_phase; }
  75. void set_phase(Phase phase) { m_phase = phase; }
  76. RefPtr<EventTarget> current_target() const { return m_current_target; }
  77. void set_current_target(EventTarget* current_target) { m_current_target = current_target; }
  78. bool return_value() const { return !m_cancelled; }
  79. void set_return_value(bool return_value)
  80. {
  81. if (!return_value)
  82. set_cancelled_flag();
  83. }
  84. void append_to_path(EventTarget&, RefPtr<EventTarget>, RefPtr<EventTarget>, TouchTargetList&, bool);
  85. Path& path() { return m_path; }
  86. const Path& path() const { return m_path; }
  87. void clear_path() { m_path.clear(); }
  88. void set_touch_target_list(TouchTargetList& touch_target_list) { m_touch_target_list = touch_target_list; }
  89. TouchTargetList& touch_target_list() { return m_touch_target_list; };
  90. void clear_touch_target_list() { m_touch_target_list.clear(); }
  91. bool bubbles() const { return m_bubbles; }
  92. void set_bubbles(bool bubbles) { m_bubbles = bubbles; }
  93. bool cancelable() const { return m_cancelable; }
  94. void set_cancelable(bool cancelable) { m_cancelable = cancelable; }
  95. bool is_trusted() const { return m_is_trusted; }
  96. void set_is_trusted(bool is_trusted) { m_is_trusted = is_trusted; }
  97. void stop_propagation() { m_stop_propagation = true; }
  98. bool cancel_bubble() const { return m_stop_propagation; }
  99. void set_cancel_bubble(bool cancel_bubble)
  100. {
  101. if (cancel_bubble)
  102. m_stop_propagation = true;
  103. }
  104. void stop_immediate_propagation()
  105. {
  106. m_stop_propagation = true;
  107. m_stop_immediate_propagation = true;
  108. }
  109. void init_event(const String&, bool, bool);
  110. void set_time_stamp(double time_stamp) { m_time_stamp = time_stamp; }
  111. NonnullRefPtrVector<EventTarget> composed_path() const;
  112. protected:
  113. explicit Event(FlyString const& type)
  114. : m_type(type)
  115. , m_initialized(true)
  116. {
  117. }
  118. Event(FlyString const& type, EventInit const& event_init)
  119. : m_type(type)
  120. , m_bubbles(event_init.bubbles)
  121. , m_cancelable(event_init.cancelable)
  122. , m_composed(event_init.composed)
  123. , m_initialized(true)
  124. {
  125. }
  126. void initialize(const String&, bool, bool);
  127. private:
  128. FlyString m_type;
  129. RefPtr<EventTarget> m_target;
  130. RefPtr<EventTarget> m_related_target;
  131. RefPtr<EventTarget> m_current_target;
  132. Phase m_phase { None };
  133. bool m_bubbles { false };
  134. bool m_cancelable { false };
  135. bool m_stop_propagation { false };
  136. bool m_stop_immediate_propagation { false };
  137. bool m_cancelled { false };
  138. bool m_in_passive_listener { false };
  139. bool m_composed { false };
  140. bool m_initialized { false };
  141. bool m_dispatch { false };
  142. bool m_is_trusted { true };
  143. Path m_path;
  144. TouchTargetList m_touch_target_list;
  145. double m_time_stamp { 0 };
  146. void set_cancelled_flag();
  147. };
  148. }