Event.h 5.5 KB

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