EventHandler.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <LibGUI/Event.h>
  27. #include <LibGUI/Window.h>
  28. #include <LibJS/Runtime/Value.h>
  29. #include <LibWeb/DOM/Document.h>
  30. #include <LibWeb/DOM/HTMLAnchorElement.h>
  31. #include <LibWeb/DOM/HTMLIFrameElement.h>
  32. #include <LibWeb/DOM/MouseEvent.h>
  33. #include <LibWeb/Frame/EventHandler.h>
  34. #include <LibWeb/Frame/Frame.h>
  35. #include <LibWeb/Layout/LayoutDocument.h>
  36. #include <LibWeb/PageView.h>
  37. namespace Web {
  38. static Gfx::IntPoint compute_mouse_event_offset(const Gfx::IntPoint& position, const LayoutNode& layout_node)
  39. {
  40. auto top_left_of_layout_node = layout_node.box_type_agnostic_position();
  41. return {
  42. position.x() - static_cast<int>(top_left_of_layout_node.x()),
  43. position.y() - static_cast<int>(top_left_of_layout_node.y())
  44. };
  45. }
  46. EventHandler::EventHandler(Badge<Frame>, Frame& frame)
  47. : m_frame(frame)
  48. {
  49. }
  50. EventHandler::~EventHandler()
  51. {
  52. }
  53. const LayoutDocument* EventHandler::layout_root() const
  54. {
  55. if (!m_frame.document())
  56. return nullptr;
  57. return static_cast<LayoutDocument*>(m_frame.document()->layout_node());
  58. }
  59. LayoutDocument* EventHandler::layout_root()
  60. {
  61. if (!m_frame.document())
  62. return nullptr;
  63. return const_cast<LayoutDocument*>(m_frame.document()->layout_node());
  64. }
  65. bool EventHandler::handle_mouseup(const Gfx::IntPoint& position, unsigned button, unsigned modifiers)
  66. {
  67. auto* layout_root_ptr = this->layout_root();
  68. if (!layout_root_ptr)
  69. return false;
  70. auto& layout_root = *layout_root_ptr;
  71. bool handled_event = false;
  72. auto result = layout_root.hit_test(position);
  73. if (result.layout_node && result.layout_node->node()) {
  74. RefPtr<Node> node = result.layout_node->node();
  75. if (is<HTMLIFrameElement>(*node)) {
  76. if (auto* subframe = to<HTMLIFrameElement>(*node).hosted_frame())
  77. return subframe->event_handler().handle_mouseup(position.translated(compute_mouse_event_offset({}, *result.layout_node)), button, modifiers);
  78. return false;
  79. }
  80. auto offset = compute_mouse_event_offset(position, *result.layout_node);
  81. node->dispatch_event(MouseEvent::create("mouseup", offset.x(), offset.y()));
  82. handled_event = true;
  83. }
  84. if (button == GUI::MouseButton::Left) {
  85. dump_selection("MouseUp");
  86. m_in_mouse_selection = false;
  87. }
  88. return handled_event;
  89. }
  90. bool EventHandler::handle_mousedown(const Gfx::IntPoint& position, unsigned button, unsigned modifiers)
  91. {
  92. auto* layout_root_ptr = this->layout_root();
  93. if (!layout_root_ptr)
  94. return false;
  95. auto& layout_root = *layout_root_ptr;
  96. auto& document = *m_frame.document();
  97. auto& page_client = m_frame.page().client();
  98. auto result = layout_root.hit_test(position);
  99. if (!result.layout_node)
  100. return false;
  101. RefPtr<Node> node = result.layout_node->node();
  102. document.set_hovered_node(node);
  103. if (!node)
  104. return false;
  105. if (is<HTMLIFrameElement>(*node)) {
  106. if (auto* subframe = to<HTMLIFrameElement>(*node).hosted_frame())
  107. return subframe->event_handler().handle_mousedown(position.translated(compute_mouse_event_offset({}, *result.layout_node)), button, modifiers);
  108. return false;
  109. }
  110. auto offset = compute_mouse_event_offset(position, *result.layout_node);
  111. node->dispatch_event(MouseEvent::create("mousedown", offset.x(), offset.y()));
  112. if (RefPtr<HTMLAnchorElement> link = node->enclosing_link_element()) {
  113. dbg() << "Web::EventHandler: Clicking on a link to " << link->href();
  114. if (button == GUI::MouseButton::Left) {
  115. auto href = link->href();
  116. if (href.starts_with("javascript:")) {
  117. document.run_javascript(href.substring_view(11, href.length() - 11));
  118. } else {
  119. if (m_frame.is_main_frame()) {
  120. page_client.page_did_click_link(link->href(), link->target(), modifiers);
  121. } else {
  122. // FIXME: Handle different targets!
  123. m_frame.loader().load(document.complete_url(link->href()));
  124. }
  125. }
  126. } else if (button == GUI::MouseButton::Right) {
  127. page_client.page_did_request_link_context_menu(m_frame.to_main_frame_position(position), link->href(), link->target(), modifiers);
  128. } else if (button == GUI::MouseButton::Middle) {
  129. page_client.page_did_middle_click_link(link->href(), link->target(), modifiers);
  130. }
  131. } else {
  132. if (button == GUI::MouseButton::Left) {
  133. layout_root.selection().set({ result.layout_node, result.index_in_node }, {});
  134. dump_selection("MouseDown");
  135. m_in_mouse_selection = true;
  136. }
  137. }
  138. return true;
  139. }
  140. bool EventHandler::handle_mousemove(const Gfx::IntPoint& position, unsigned buttons, unsigned modifiers)
  141. {
  142. auto* layout_root_ptr = this->layout_root();
  143. if (!layout_root_ptr)
  144. return false;
  145. auto& layout_root = *layout_root_ptr;
  146. auto& document = *m_frame.document();
  147. auto& page_client = m_frame.page().client();
  148. bool hovered_node_changed = false;
  149. bool is_hovering_link = false;
  150. bool was_hovering_link = document.hovered_node() && document.hovered_node()->is_link();
  151. auto result = layout_root.hit_test(position);
  152. const HTMLAnchorElement* hovered_link_element = nullptr;
  153. if (result.layout_node) {
  154. RefPtr<Node> node = result.layout_node->node();
  155. if (node && is<HTMLIFrameElement>(*node)) {
  156. if (auto* subframe = to<HTMLIFrameElement>(*node).hosted_frame())
  157. return subframe->event_handler().handle_mousemove(position.translated(compute_mouse_event_offset({}, *result.layout_node)), buttons, modifiers);
  158. return false;
  159. }
  160. hovered_node_changed = node != document.hovered_node();
  161. document.set_hovered_node(node);
  162. if (node) {
  163. hovered_link_element = node->enclosing_link_element();
  164. if (hovered_link_element) {
  165. #ifdef HTML_DEBUG
  166. dbg() << "PageView: hovering over a link to " << hovered_link_element->href();
  167. #endif
  168. is_hovering_link = true;
  169. }
  170. auto offset = compute_mouse_event_offset(position, *result.layout_node);
  171. node->dispatch_event(MouseEvent::create("mousemove", offset.x(), offset.y()));
  172. }
  173. if (m_in_mouse_selection) {
  174. layout_root.selection().set_end({ result.layout_node, result.index_in_node });
  175. dump_selection("MouseMove");
  176. page_client.page_did_change_selection();
  177. }
  178. }
  179. page_client.page_did_request_cursor_change(is_hovering_link ? GUI::StandardCursor::Hand : GUI::StandardCursor::None);
  180. if (hovered_node_changed) {
  181. RefPtr<HTMLElement> hovered_html_element = document.hovered_node() ? document.hovered_node()->enclosing_html_element() : nullptr;
  182. if (hovered_html_element && !hovered_html_element->title().is_null()) {
  183. page_client.page_did_enter_tooltip_area(m_frame.to_main_frame_position(position), hovered_html_element->title());
  184. } else {
  185. page_client.page_did_leave_tooltip_area();
  186. }
  187. }
  188. if (is_hovering_link != was_hovering_link) {
  189. if (is_hovering_link)
  190. page_client.page_did_hover_link(document.complete_url(hovered_link_element->href()));
  191. else
  192. page_client.page_did_unhover_link();
  193. }
  194. return true;
  195. }
  196. void EventHandler::dump_selection(const char* event_name) const
  197. {
  198. UNUSED_PARAM(event_name);
  199. #ifdef SELECTION_DEBUG
  200. dbg() << event_name << " selection start: "
  201. << layout_root()->selection().start().layout_node << ":" << layout_root()->selection().start().index_in_node << ", end: "
  202. << layout_root()->selection().end().layout_node << ":" << layout_root()->selection().end().index_in_node;
  203. #endif
  204. }
  205. }