EventHandler.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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/Text.h>
  31. #include <LibWeb/HTML/HTMLAnchorElement.h>
  32. #include <LibWeb/HTML/HTMLIFrameElement.h>
  33. #include <LibWeb/Layout/LayoutDocument.h>
  34. #include <LibWeb/Page/EventHandler.h>
  35. #include <LibWeb/Page/Frame.h>
  36. #include <LibWeb/PageView.h>
  37. #include <LibWeb/UIEvents/MouseEvent.h>
  38. namespace Web {
  39. static Gfx::IntPoint compute_mouse_event_offset(const Gfx::IntPoint& position, const LayoutNode& layout_node)
  40. {
  41. auto top_left_of_layout_node = layout_node.box_type_agnostic_position();
  42. return {
  43. position.x() - static_cast<int>(top_left_of_layout_node.x()),
  44. position.y() - static_cast<int>(top_left_of_layout_node.y())
  45. };
  46. }
  47. EventHandler::EventHandler(Badge<Frame>, Frame& frame)
  48. : m_frame(frame)
  49. {
  50. }
  51. EventHandler::~EventHandler()
  52. {
  53. }
  54. const LayoutDocument* EventHandler::layout_root() const
  55. {
  56. if (!m_frame.document())
  57. return nullptr;
  58. return m_frame.document()->layout_node();
  59. }
  60. LayoutDocument* EventHandler::layout_root()
  61. {
  62. if (!m_frame.document())
  63. return nullptr;
  64. return m_frame.document()->layout_node();
  65. }
  66. bool EventHandler::handle_mouseup(const Gfx::IntPoint& position, unsigned button, unsigned modifiers)
  67. {
  68. if (!layout_root())
  69. return false;
  70. bool handled_event = false;
  71. auto result = layout_root()->hit_test(position);
  72. if (result.layout_node && result.layout_node->node()) {
  73. RefPtr<DOM::Node> node = result.layout_node->node();
  74. if (is<HTML::HTMLIFrameElement>(*node)) {
  75. if (auto* subframe = downcast<HTML::HTMLIFrameElement>(*node).hosted_frame())
  76. return subframe->event_handler().handle_mouseup(position.translated(compute_mouse_event_offset({}, *result.layout_node)), button, modifiers);
  77. return false;
  78. }
  79. auto offset = compute_mouse_event_offset(position, *result.layout_node);
  80. node->dispatch_event(UIEvents::MouseEvent::create("mouseup", offset.x(), offset.y()));
  81. handled_event = true;
  82. }
  83. if (button == GUI::MouseButton::Left) {
  84. dump_selection("MouseUp");
  85. m_in_mouse_selection = false;
  86. }
  87. return handled_event;
  88. }
  89. bool EventHandler::handle_mousedown(const Gfx::IntPoint& position, unsigned button, unsigned modifiers)
  90. {
  91. if (!layout_root())
  92. return false;
  93. NonnullRefPtr document = *m_frame.document();
  94. auto& page_client = m_frame.page().client();
  95. auto result = layout_root()->hit_test(position);
  96. if (!result.layout_node)
  97. return false;
  98. RefPtr<DOM::Node> node = result.layout_node->node();
  99. document->set_hovered_node(node);
  100. if (!node)
  101. return false;
  102. if (is<HTML::HTMLIFrameElement>(*node)) {
  103. if (auto* subframe = downcast<HTML::HTMLIFrameElement>(*node).hosted_frame())
  104. return subframe->event_handler().handle_mousedown(position.translated(compute_mouse_event_offset({}, *result.layout_node)), button, modifiers);
  105. return false;
  106. }
  107. auto offset = compute_mouse_event_offset(position, *result.layout_node);
  108. node->dispatch_event(UIEvents::MouseEvent::create("mousedown", offset.x(), offset.y()));
  109. if (!layout_root())
  110. return true;
  111. if (RefPtr<HTML::HTMLAnchorElement> link = node->enclosing_link_element()) {
  112. auto href = link->href();
  113. auto url = document->complete_url(href);
  114. dbg() << "Web::EventHandler: Clicking on a link to " << url;
  115. if (button == GUI::MouseButton::Left) {
  116. auto href = link->href();
  117. auto url = document->complete_url(href);
  118. if (href.starts_with("javascript:")) {
  119. document->run_javascript(href.substring_view(11, href.length() - 11));
  120. } else if (href.starts_with('#')) {
  121. auto anchor = href.substring_view(1, href.length() - 1);
  122. m_frame.scroll_to_anchor(anchor);
  123. } else {
  124. if (m_frame.is_main_frame()) {
  125. page_client.page_did_click_link(url, link->target(), modifiers);
  126. } else {
  127. // FIXME: Handle different targets!
  128. m_frame.loader().load(url, FrameLoader::Type::Navigation);
  129. }
  130. }
  131. } else if (button == GUI::MouseButton::Right) {
  132. page_client.page_did_request_link_context_menu(m_frame.to_main_frame_position(position), url, link->target(), modifiers);
  133. } else if (button == GUI::MouseButton::Middle) {
  134. page_client.page_did_middle_click_link(url, link->target(), modifiers);
  135. }
  136. } else {
  137. if (button == GUI::MouseButton::Left) {
  138. m_frame.set_cursor_position(DOM::Position(*node, result.index_in_node));
  139. layout_root()->selection().set({ result.layout_node, result.index_in_node }, {});
  140. dump_selection("MouseDown");
  141. m_in_mouse_selection = true;
  142. } else if (button == GUI::MouseButton::Right) {
  143. page_client.page_did_request_context_menu(m_frame.to_main_frame_position(position));
  144. }
  145. }
  146. return true;
  147. }
  148. bool EventHandler::handle_mousemove(const Gfx::IntPoint& position, unsigned buttons, unsigned modifiers)
  149. {
  150. if (!layout_root())
  151. return false;
  152. auto& document = *m_frame.document();
  153. auto& page_client = m_frame.page().client();
  154. bool hovered_node_changed = false;
  155. bool is_hovering_link = false;
  156. auto result = layout_root()->hit_test(position);
  157. const HTML::HTMLAnchorElement* hovered_link_element = nullptr;
  158. if (result.layout_node) {
  159. RefPtr<DOM::Node> node = result.layout_node->node();
  160. if (node && is<HTML::HTMLIFrameElement>(*node)) {
  161. if (auto* subframe = downcast<HTML::HTMLIFrameElement>(*node).hosted_frame())
  162. return subframe->event_handler().handle_mousemove(position.translated(compute_mouse_event_offset({}, *result.layout_node)), buttons, modifiers);
  163. return false;
  164. }
  165. hovered_node_changed = node != document.hovered_node();
  166. document.set_hovered_node(node);
  167. if (node) {
  168. hovered_link_element = node->enclosing_link_element();
  169. if (hovered_link_element) {
  170. #ifdef HTML_DEBUG
  171. dbg() << "PageView: hovering over a link to " << hovered_link_element->href();
  172. #endif
  173. is_hovering_link = true;
  174. }
  175. auto offset = compute_mouse_event_offset(position, *result.layout_node);
  176. node->dispatch_event(UIEvents::MouseEvent::create("mousemove", offset.x(), offset.y()));
  177. if (!layout_root())
  178. return true;
  179. }
  180. if (m_in_mouse_selection) {
  181. layout_root()->selection().set_end({ result.layout_node, result.index_in_node });
  182. dump_selection("MouseMove");
  183. page_client.page_did_change_selection();
  184. }
  185. }
  186. page_client.page_did_request_cursor_change(is_hovering_link ? GUI::StandardCursor::Hand : GUI::StandardCursor::None);
  187. if (hovered_node_changed) {
  188. RefPtr<HTML::HTMLElement> hovered_html_element = document.hovered_node() ? document.hovered_node()->enclosing_html_element() : nullptr;
  189. if (hovered_html_element && !hovered_html_element->title().is_null()) {
  190. page_client.page_did_enter_tooltip_area(m_frame.to_main_frame_position(position), hovered_html_element->title());
  191. } else {
  192. page_client.page_did_leave_tooltip_area();
  193. }
  194. if (is_hovering_link)
  195. page_client.page_did_hover_link(document.complete_url(hovered_link_element->href()));
  196. else
  197. page_client.page_did_unhover_link();
  198. }
  199. return true;
  200. }
  201. void EventHandler::dump_selection(const char* event_name) const
  202. {
  203. UNUSED_PARAM(event_name);
  204. #ifdef SELECTION_DEBUG
  205. dbg() << event_name << " selection start: "
  206. << layout_root()->selection().start().layout_node << ":" << layout_root()->selection().start().index_in_node << ", end: "
  207. << layout_root()->selection().end().layout_node << ":" << layout_root()->selection().end().index_in_node;
  208. #endif
  209. }
  210. bool EventHandler::handle_keydown(KeyCode, unsigned, u32 code_point)
  211. {
  212. if (code_point && m_frame.cursor_position().is_valid() && is<DOM::Text>(*m_frame.cursor_position().node())) {
  213. auto& text_node = downcast<DOM::Text>(*m_frame.cursor_position().node());
  214. StringBuilder builder;
  215. builder.append(text_node.data().substring_view(0, m_frame.cursor_position().offset()));
  216. builder.append_codepoint(code_point);
  217. builder.append(text_node.data().substring_view(m_frame.cursor_position().offset(), text_node.data().length() - m_frame.cursor_position().offset()));
  218. text_node.set_data(builder.to_string());
  219. // FIXME: This will advance the cursor incorrectly when inserting multiple whitespaces (DOM vs layout whitespace collapse difference.)
  220. m_frame.set_cursor_position({ *m_frame.cursor_position().node(), m_frame.cursor_position().offset() + 1 });
  221. // FIXME: This should definitely use incremental layout invalidation instead!
  222. text_node.document().force_layout();
  223. return true;
  224. }
  225. return true;
  226. }
  227. }