EventHandler.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. NonnullRefPtr 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. auto href = link->href();
  114. auto url = document->complete_url(href);
  115. dbg() << "Web::EventHandler: Clicking on a link to " << url;
  116. if (button == GUI::MouseButton::Left) {
  117. auto href = link->href();
  118. auto url = document->complete_url(href);
  119. if (href.starts_with("javascript:")) {
  120. document->run_javascript(href.substring_view(11, href.length() - 11));
  121. } else if (href.starts_with('#')) {
  122. auto anchor = href.substring_view(1, href.length() - 1);
  123. m_frame.scroll_to_anchor(anchor);
  124. } else {
  125. if (m_frame.is_main_frame()) {
  126. page_client.page_did_click_link(url, link->target(), modifiers);
  127. } else {
  128. // FIXME: Handle different targets!
  129. m_frame.loader().load(url);
  130. }
  131. }
  132. } else if (button == GUI::MouseButton::Right) {
  133. page_client.page_did_request_link_context_menu(m_frame.to_main_frame_position(position), url, link->target(), modifiers);
  134. } else if (button == GUI::MouseButton::Middle) {
  135. page_client.page_did_middle_click_link(url, link->target(), modifiers);
  136. }
  137. } else {
  138. if (button == GUI::MouseButton::Left) {
  139. layout_root.selection().set({ result.layout_node, result.index_in_node }, {});
  140. dump_selection("MouseDown");
  141. m_in_mouse_selection = true;
  142. }
  143. else if (button == GUI::MouseButton::Right) {
  144. page_client.page_did_request_context_menu(m_frame.to_main_frame_position(position));
  145. }
  146. }
  147. return true;
  148. }
  149. bool EventHandler::handle_mousemove(const Gfx::IntPoint& position, unsigned buttons, unsigned modifiers)
  150. {
  151. auto* layout_root_ptr = this->layout_root();
  152. if (!layout_root_ptr)
  153. return false;
  154. auto& layout_root = *layout_root_ptr;
  155. auto& document = *m_frame.document();
  156. auto& page_client = m_frame.page().client();
  157. bool hovered_node_changed = false;
  158. bool is_hovering_link = false;
  159. auto result = layout_root.hit_test(position);
  160. const HTMLAnchorElement* hovered_link_element = nullptr;
  161. if (result.layout_node) {
  162. RefPtr<Node> node = result.layout_node->node();
  163. if (node && is<HTMLIFrameElement>(*node)) {
  164. if (auto* subframe = to<HTMLIFrameElement>(*node).hosted_frame())
  165. return subframe->event_handler().handle_mousemove(position.translated(compute_mouse_event_offset({}, *result.layout_node)), buttons, modifiers);
  166. return false;
  167. }
  168. hovered_node_changed = node != document.hovered_node();
  169. document.set_hovered_node(node);
  170. if (node) {
  171. hovered_link_element = node->enclosing_link_element();
  172. if (hovered_link_element) {
  173. #ifdef HTML_DEBUG
  174. dbg() << "PageView: hovering over a link to " << hovered_link_element->href();
  175. #endif
  176. is_hovering_link = true;
  177. }
  178. auto offset = compute_mouse_event_offset(position, *result.layout_node);
  179. node->dispatch_event(MouseEvent::create("mousemove", offset.x(), offset.y()));
  180. }
  181. if (m_in_mouse_selection) {
  182. layout_root.selection().set_end({ result.layout_node, result.index_in_node });
  183. dump_selection("MouseMove");
  184. page_client.page_did_change_selection();
  185. }
  186. }
  187. page_client.page_did_request_cursor_change(is_hovering_link ? GUI::StandardCursor::Hand : GUI::StandardCursor::None);
  188. if (hovered_node_changed) {
  189. RefPtr<HTMLElement> hovered_html_element = document.hovered_node() ? document.hovered_node()->enclosing_html_element() : nullptr;
  190. if (hovered_html_element && !hovered_html_element->title().is_null()) {
  191. page_client.page_did_enter_tooltip_area(m_frame.to_main_frame_position(position), hovered_html_element->title());
  192. } else {
  193. page_client.page_did_leave_tooltip_area();
  194. }
  195. if (is_hovering_link)
  196. page_client.page_did_hover_link(document.complete_url(hovered_link_element->href()));
  197. else
  198. page_client.page_did_unhover_link();
  199. }
  200. return true;
  201. }
  202. void EventHandler::dump_selection(const char* event_name) const
  203. {
  204. UNUSED_PARAM(event_name);
  205. #ifdef SELECTION_DEBUG
  206. dbg() << event_name << " selection start: "
  207. << layout_root()->selection().start().layout_node << ":" << layout_root()->selection().start().index_in_node << ", end: "
  208. << layout_root()->selection().end().layout_node << ":" << layout_root()->selection().end().index_in_node;
  209. #endif
  210. }
  211. }