EventHandler.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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/InProcessWebView.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, HitTestType::Exact);
  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, HitTestType::Exact);
  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. m_frame.page().set_focused_frame({}, m_frame);
  108. auto offset = compute_mouse_event_offset(position, *result.layout_node);
  109. node->dispatch_event(UIEvents::MouseEvent::create("mousedown", offset.x(), offset.y()));
  110. if (!layout_root())
  111. return true;
  112. if (RefPtr<HTML::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, FrameLoader::Type::Navigation);
  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. auto result = layout_root()->hit_test(position, HitTestType::TextCursor);
  140. if (result.layout_node && result.layout_node->node()) {
  141. m_frame.set_cursor_position(DOM::Position(*node, result.index_in_node));
  142. layout_root()->selection().set({ result.layout_node, result.index_in_node }, {});
  143. layout_root()->recompute_selection_states();
  144. dump_selection("MouseDown");
  145. m_in_mouse_selection = true;
  146. }
  147. } else if (button == GUI::MouseButton::Right) {
  148. page_client.page_did_request_context_menu(m_frame.to_main_frame_position(position));
  149. }
  150. }
  151. return true;
  152. }
  153. bool EventHandler::handle_mousemove(const Gfx::IntPoint& position, unsigned buttons, unsigned modifiers)
  154. {
  155. if (!layout_root())
  156. return false;
  157. auto& document = *m_frame.document();
  158. auto& page_client = m_frame.page().client();
  159. bool hovered_node_changed = false;
  160. bool is_hovering_link = false;
  161. bool is_hovering_text = false;
  162. auto result = layout_root()->hit_test(position, HitTestType::Exact);
  163. const HTML::HTMLAnchorElement* hovered_link_element = nullptr;
  164. if (result.layout_node) {
  165. RefPtr<DOM::Node> node = result.layout_node->node();
  166. if (node && is<HTML::HTMLIFrameElement>(*node)) {
  167. if (auto* subframe = downcast<HTML::HTMLIFrameElement>(*node).hosted_frame())
  168. return subframe->event_handler().handle_mousemove(position.translated(compute_mouse_event_offset({}, *result.layout_node)), buttons, modifiers);
  169. return false;
  170. }
  171. hovered_node_changed = node != document.hovered_node();
  172. document.set_hovered_node(node);
  173. if (node) {
  174. if (node->is_text())
  175. is_hovering_text = true;
  176. hovered_link_element = node->enclosing_link_element();
  177. if (hovered_link_element) {
  178. #ifdef HTML_DEBUG
  179. dbg() << "InProcessWebView: hovering over a link to " << hovered_link_element->href();
  180. #endif
  181. is_hovering_link = true;
  182. }
  183. auto offset = compute_mouse_event_offset(position, *result.layout_node);
  184. node->dispatch_event(UIEvents::MouseEvent::create("mousemove", offset.x(), offset.y()));
  185. if (!layout_root())
  186. return true;
  187. }
  188. if (m_in_mouse_selection) {
  189. auto hit = layout_root()->hit_test(position, HitTestType::TextCursor);
  190. if (hit.layout_node && hit.layout_node->node()) {
  191. layout_root()->selection().set_end({ hit.layout_node, hit.index_in_node });
  192. layout_root()->recompute_selection_states();
  193. }
  194. dump_selection("MouseMove");
  195. page_client.page_did_change_selection();
  196. }
  197. }
  198. if (is_hovering_link)
  199. page_client.page_did_request_cursor_change(GUI::StandardCursor::Hand);
  200. else if (is_hovering_text)
  201. page_client.page_did_request_cursor_change(GUI::StandardCursor::IBeam);
  202. else
  203. page_client.page_did_request_cursor_change(GUI::StandardCursor::None);
  204. if (hovered_node_changed) {
  205. RefPtr<HTML::HTMLElement> hovered_html_element = document.hovered_node() ? document.hovered_node()->enclosing_html_element() : nullptr;
  206. if (hovered_html_element && !hovered_html_element->title().is_null()) {
  207. page_client.page_did_enter_tooltip_area(m_frame.to_main_frame_position(position), hovered_html_element->title());
  208. } else {
  209. page_client.page_did_leave_tooltip_area();
  210. }
  211. if (is_hovering_link)
  212. page_client.page_did_hover_link(document.complete_url(hovered_link_element->href()));
  213. else
  214. page_client.page_did_unhover_link();
  215. }
  216. return true;
  217. }
  218. void EventHandler::dump_selection(const char* event_name) const
  219. {
  220. UNUSED_PARAM(event_name);
  221. #ifdef SELECTION_DEBUG
  222. dbg() << event_name << " selection start: "
  223. << layout_root()->selection().start().layout_node << ":" << layout_root()->selection().start().index_in_node << ", end: "
  224. << layout_root()->selection().end().layout_node << ":" << layout_root()->selection().end().index_in_node;
  225. #endif
  226. }
  227. bool EventHandler::focus_next_element()
  228. {
  229. if (!m_frame.document())
  230. return false;
  231. auto* element = m_frame.document()->focused_element();
  232. if (!element) {
  233. element = m_frame.document()->first_child_of_type<DOM::Element>();
  234. if (element && element->is_focusable()) {
  235. m_frame.document()->set_focused_element(element);
  236. return true;
  237. }
  238. }
  239. for (element = element->next_element_in_pre_order(); element && !element->is_focusable(); element = element->next_element_in_pre_order())
  240. ;
  241. m_frame.document()->set_focused_element(element);
  242. return element;
  243. }
  244. bool EventHandler::focus_previous_element()
  245. {
  246. // FIXME: Implement Shift-Tab cycling backwards through focusable elements!
  247. return false;
  248. }
  249. bool EventHandler::handle_keydown(KeyCode key, unsigned modifiers, u32 code_point)
  250. {
  251. if (key == KeyCode::Key_Tab) {
  252. if (modifiers & KeyModifier::Mod_Shift)
  253. return focus_previous_element();
  254. else
  255. return focus_next_element();
  256. }
  257. if (m_frame.cursor_position().node() && m_frame.cursor_position().node()->is_editable()) {
  258. // FIXME: Support backspacing across DOM node boundaries.
  259. if (key == KeyCode::Key_Backspace && m_frame.cursor_position().offset() > 0) {
  260. auto& text_node = downcast<DOM::Text>(*m_frame.cursor_position().node());
  261. StringBuilder builder;
  262. builder.append(text_node.data().substring_view(0, m_frame.cursor_position().offset() - 1));
  263. builder.append(text_node.data().substring_view(m_frame.cursor_position().offset(), text_node.data().length() - m_frame.cursor_position().offset()));
  264. text_node.set_data(builder.to_string());
  265. m_frame.set_cursor_position({ *m_frame.cursor_position().node(), m_frame.cursor_position().offset() - 1 });
  266. // FIXME: This should definitely use incremental layout invalidation instead!
  267. text_node.document().force_layout();
  268. return true;
  269. }
  270. if (code_point && m_frame.cursor_position().is_valid() && is<DOM::Text>(*m_frame.cursor_position().node())) {
  271. auto& text_node = downcast<DOM::Text>(*m_frame.cursor_position().node());
  272. StringBuilder builder;
  273. builder.append(text_node.data().substring_view(0, m_frame.cursor_position().offset()));
  274. builder.append_code_point(code_point);
  275. builder.append(text_node.data().substring_view(m_frame.cursor_position().offset(), text_node.data().length() - m_frame.cursor_position().offset()));
  276. text_node.set_data(builder.to_string());
  277. // FIXME: This will advance the cursor incorrectly when inserting multiple whitespaces (DOM vs layout whitespace collapse difference.)
  278. m_frame.set_cursor_position({ *m_frame.cursor_position().node(), m_frame.cursor_position().offset() + 1 });
  279. // FIXME: This should definitely use incremental layout invalidation instead!
  280. text_node.document().force_layout();
  281. return true;
  282. }
  283. }
  284. return false;
  285. }
  286. }