EventHandler.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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/HTML/HTMLAnchorElement.h>
  31. #include <LibWeb/HTML/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 m_frame.document()->layout_node();
  58. }
  59. LayoutDocument* EventHandler::layout_root()
  60. {
  61. if (!m_frame.document())
  62. return nullptr;
  63. return m_frame.document()->layout_node();
  64. }
  65. bool EventHandler::handle_mouseup(const Gfx::IntPoint& position, unsigned button, unsigned modifiers)
  66. {
  67. if (!layout_root())
  68. return false;
  69. bool handled_event = false;
  70. auto result = layout_root()->hit_test(position);
  71. if (result.layout_node && result.layout_node->node()) {
  72. RefPtr<Node> node = result.layout_node->node();
  73. if (is<HTMLIFrameElement>(*node)) {
  74. if (auto* subframe = downcast<HTMLIFrameElement>(*node).hosted_frame())
  75. return subframe->event_handler().handle_mouseup(position.translated(compute_mouse_event_offset({}, *result.layout_node)), button, modifiers);
  76. return false;
  77. }
  78. auto offset = compute_mouse_event_offset(position, *result.layout_node);
  79. node->dispatch_event(MouseEvent::create("mouseup", offset.x(), offset.y()));
  80. handled_event = true;
  81. }
  82. if (button == GUI::MouseButton::Left) {
  83. dump_selection("MouseUp");
  84. m_in_mouse_selection = false;
  85. }
  86. return handled_event;
  87. }
  88. bool EventHandler::handle_mousedown(const Gfx::IntPoint& position, unsigned button, unsigned modifiers)
  89. {
  90. if (!layout_root())
  91. return false;
  92. NonnullRefPtr document = *m_frame.document();
  93. auto& page_client = m_frame.page().client();
  94. auto result = layout_root()->hit_test(position);
  95. if (!result.layout_node)
  96. return false;
  97. RefPtr<Node> node = result.layout_node->node();
  98. document->set_hovered_node(node);
  99. if (!node)
  100. return false;
  101. if (is<HTMLIFrameElement>(*node)) {
  102. if (auto* subframe = downcast<HTMLIFrameElement>(*node).hosted_frame())
  103. return subframe->event_handler().handle_mousedown(position.translated(compute_mouse_event_offset({}, *result.layout_node)), button, modifiers);
  104. return false;
  105. }
  106. auto offset = compute_mouse_event_offset(position, *result.layout_node);
  107. node->dispatch_event(MouseEvent::create("mousedown", offset.x(), offset.y()));
  108. if (!layout_root())
  109. return true;
  110. if (RefPtr<HTMLAnchorElement> link = node->enclosing_link_element()) {
  111. auto href = link->href();
  112. auto url = document->complete_url(href);
  113. dbg() << "Web::EventHandler: Clicking on a link to " << url;
  114. if (button == GUI::MouseButton::Left) {
  115. auto href = link->href();
  116. auto url = document->complete_url(href);
  117. if (href.starts_with("javascript:")) {
  118. document->run_javascript(href.substring_view(11, href.length() - 11));
  119. } else if (href.starts_with('#')) {
  120. auto anchor = href.substring_view(1, href.length() - 1);
  121. m_frame.scroll_to_anchor(anchor);
  122. } else {
  123. if (m_frame.is_main_frame()) {
  124. page_client.page_did_click_link(url, link->target(), modifiers);
  125. } else {
  126. // FIXME: Handle different targets!
  127. m_frame.loader().load(url, FrameLoader::Type::Navigation);
  128. }
  129. }
  130. } else if (button == GUI::MouseButton::Right) {
  131. page_client.page_did_request_link_context_menu(m_frame.to_main_frame_position(position), url, link->target(), modifiers);
  132. } else if (button == GUI::MouseButton::Middle) {
  133. page_client.page_did_middle_click_link(url, link->target(), modifiers);
  134. }
  135. } else {
  136. if (button == GUI::MouseButton::Left) {
  137. layout_root()->selection().set({ result.layout_node, result.index_in_node }, {});
  138. dump_selection("MouseDown");
  139. m_in_mouse_selection = true;
  140. }
  141. else if (button == GUI::MouseButton::Right) {
  142. page_client.page_did_request_context_menu(m_frame.to_main_frame_position(position));
  143. }
  144. }
  145. return true;
  146. }
  147. bool EventHandler::handle_mousemove(const Gfx::IntPoint& position, unsigned buttons, unsigned modifiers)
  148. {
  149. if (!layout_root())
  150. return false;
  151. auto& document = *m_frame.document();
  152. auto& page_client = m_frame.page().client();
  153. bool hovered_node_changed = false;
  154. bool is_hovering_link = false;
  155. auto result = layout_root()->hit_test(position);
  156. const HTMLAnchorElement* hovered_link_element = nullptr;
  157. if (result.layout_node) {
  158. RefPtr<Node> node = result.layout_node->node();
  159. if (node && is<HTMLIFrameElement>(*node)) {
  160. if (auto* subframe = downcast<HTMLIFrameElement>(*node).hosted_frame())
  161. return subframe->event_handler().handle_mousemove(position.translated(compute_mouse_event_offset({}, *result.layout_node)), buttons, modifiers);
  162. return false;
  163. }
  164. hovered_node_changed = node != document.hovered_node();
  165. document.set_hovered_node(node);
  166. if (node) {
  167. hovered_link_element = node->enclosing_link_element();
  168. if (hovered_link_element) {
  169. #ifdef HTML_DEBUG
  170. dbg() << "PageView: hovering over a link to " << hovered_link_element->href();
  171. #endif
  172. is_hovering_link = true;
  173. }
  174. auto offset = compute_mouse_event_offset(position, *result.layout_node);
  175. node->dispatch_event(MouseEvent::create("mousemove", offset.x(), offset.y()));
  176. if (!layout_root())
  177. return true;
  178. }
  179. if (m_in_mouse_selection) {
  180. layout_root()->selection().set_end({ result.layout_node, result.index_in_node });
  181. dump_selection("MouseMove");
  182. page_client.page_did_change_selection();
  183. }
  184. }
  185. page_client.page_did_request_cursor_change(is_hovering_link ? GUI::StandardCursor::Hand : GUI::StandardCursor::None);
  186. if (hovered_node_changed) {
  187. RefPtr<HTMLElement> hovered_html_element = document.hovered_node() ? document.hovered_node()->enclosing_html_element() : nullptr;
  188. if (hovered_html_element && !hovered_html_element->title().is_null()) {
  189. page_client.page_did_enter_tooltip_area(m_frame.to_main_frame_position(position), hovered_html_element->title());
  190. } else {
  191. page_client.page_did_leave_tooltip_area();
  192. }
  193. if (is_hovering_link)
  194. page_client.page_did_hover_link(document.complete_url(hovered_link_element->href()));
  195. else
  196. page_client.page_did_unhover_link();
  197. }
  198. return true;
  199. }
  200. void EventHandler::dump_selection(const char* event_name) const
  201. {
  202. UNUSED_PARAM(event_name);
  203. #ifdef SELECTION_DEBUG
  204. dbg() << event_name << " selection start: "
  205. << layout_root()->selection().start().layout_node << ":" << layout_root()->selection().start().index_in_node << ", end: "
  206. << layout_root()->selection().end().layout_node << ":" << layout_root()->selection().end().index_in_node;
  207. #endif
  208. }
  209. }