EventHandler.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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/HTML/HTMLImageElement.h>
  34. #include <LibWeb/InProcessWebView.h>
  35. #include <LibWeb/Layout/LayoutDocument.h>
  36. #include <LibWeb/Page/EventHandler.h>
  37. #include <LibWeb/Page/Frame.h>
  38. #include <LibWeb/UIEvents/MouseEvent.h>
  39. namespace Web {
  40. static Gfx::IntPoint compute_mouse_event_offset(const Gfx::IntPoint& position, const LayoutNode& layout_node)
  41. {
  42. auto top_left_of_layout_node = layout_node.box_type_agnostic_position();
  43. return {
  44. position.x() - static_cast<int>(top_left_of_layout_node.x()),
  45. position.y() - static_cast<int>(top_left_of_layout_node.y())
  46. };
  47. }
  48. EventHandler::EventHandler(Badge<Frame>, Frame& frame)
  49. : m_frame(frame)
  50. {
  51. }
  52. EventHandler::~EventHandler()
  53. {
  54. }
  55. const LayoutDocument* EventHandler::layout_root() const
  56. {
  57. if (!m_frame.document())
  58. return nullptr;
  59. return m_frame.document()->layout_node();
  60. }
  61. LayoutDocument* EventHandler::layout_root()
  62. {
  63. if (!m_frame.document())
  64. return nullptr;
  65. return m_frame.document()->layout_node();
  66. }
  67. bool EventHandler::handle_mouseup(const Gfx::IntPoint& position, unsigned button, unsigned modifiers)
  68. {
  69. if (!layout_root())
  70. return false;
  71. if (m_mouse_event_tracking_layout_node) {
  72. m_mouse_event_tracking_layout_node->handle_mouseup({}, position, button, modifiers);
  73. return true;
  74. }
  75. bool handled_event = false;
  76. auto result = layout_root()->hit_test(position, HitTestType::Exact);
  77. if (result.layout_node && result.layout_node->wants_mouse_events()) {
  78. result.layout_node->handle_mouseup({}, position, button, modifiers);
  79. // Things may have changed as a consequence of LayoutNode::handle_mouseup(). Hit test again.
  80. if (!layout_root())
  81. return true;
  82. result = layout_root()->hit_test(position, HitTestType::Exact);
  83. }
  84. if (result.layout_node && result.layout_node->node()) {
  85. RefPtr<DOM::Node> node = result.layout_node->node();
  86. if (is<HTML::HTMLIFrameElement>(*node)) {
  87. if (auto* subframe = downcast<HTML::HTMLIFrameElement>(*node).content_frame())
  88. return subframe->event_handler().handle_mouseup(position.translated(compute_mouse_event_offset({}, *result.layout_node)), button, modifiers);
  89. return false;
  90. }
  91. auto offset = compute_mouse_event_offset(position, *result.layout_node);
  92. node->dispatch_event(UIEvents::MouseEvent::create("mouseup", offset.x(), offset.y()));
  93. handled_event = true;
  94. }
  95. if (button == GUI::MouseButton::Left) {
  96. dump_selection("MouseUp");
  97. m_in_mouse_selection = false;
  98. }
  99. return handled_event;
  100. }
  101. bool EventHandler::handle_mousedown(const Gfx::IntPoint& position, unsigned button, unsigned modifiers)
  102. {
  103. if (!layout_root())
  104. return false;
  105. if (m_mouse_event_tracking_layout_node) {
  106. m_mouse_event_tracking_layout_node->handle_mousedown({}, position, button, modifiers);
  107. return true;
  108. }
  109. NonnullRefPtr document = *m_frame.document();
  110. auto& page_client = m_frame.page().client();
  111. auto result = layout_root()->hit_test(position, HitTestType::Exact);
  112. if (!result.layout_node)
  113. return false;
  114. RefPtr<DOM::Node> node = result.layout_node->node();
  115. document->set_hovered_node(node);
  116. if (result.layout_node->wants_mouse_events()) {
  117. result.layout_node->handle_mousedown({}, position, button, modifiers);
  118. return true;
  119. }
  120. if (!node)
  121. return false;
  122. if (is<HTML::HTMLIFrameElement>(*node)) {
  123. if (auto* subframe = downcast<HTML::HTMLIFrameElement>(*node).content_frame())
  124. return subframe->event_handler().handle_mousedown(position.translated(compute_mouse_event_offset({}, *result.layout_node)), button, modifiers);
  125. return false;
  126. }
  127. m_frame.page().set_focused_frame({}, m_frame);
  128. auto offset = compute_mouse_event_offset(position, *result.layout_node);
  129. node->dispatch_event(UIEvents::MouseEvent::create("mousedown", offset.x(), offset.y()));
  130. if (!layout_root())
  131. return true;
  132. if (button == GUI::MouseButton::Right && is<HTML::HTMLImageElement>(*node)) {
  133. auto& image_element = downcast<HTML::HTMLImageElement>(*node);
  134. auto image_url = image_element.document().complete_url(image_element.src());
  135. page_client.page_did_request_image_context_menu(m_frame.to_main_frame_position(position), image_url, "", modifiers, image_element.bitmap());
  136. return true;
  137. }
  138. if (RefPtr<HTML::HTMLAnchorElement> link = node->enclosing_link_element()) {
  139. auto href = link->href();
  140. auto url = document->complete_url(href);
  141. dbg() << "Web::EventHandler: Clicking on a link to " << url;
  142. if (button == GUI::MouseButton::Left) {
  143. auto href = link->href();
  144. auto url = document->complete_url(href);
  145. if (href.starts_with("javascript:")) {
  146. document->run_javascript(href.substring_view(11, href.length() - 11));
  147. } else if (href.starts_with('#')) {
  148. auto anchor = href.substring_view(1, href.length() - 1);
  149. m_frame.scroll_to_anchor(anchor);
  150. } else {
  151. if (m_frame.is_main_frame()) {
  152. page_client.page_did_click_link(url, link->target(), modifiers);
  153. } else {
  154. // FIXME: Handle different targets!
  155. m_frame.loader().load(url, FrameLoader::Type::Navigation);
  156. }
  157. }
  158. } else if (button == GUI::MouseButton::Right) {
  159. page_client.page_did_request_link_context_menu(m_frame.to_main_frame_position(position), url, link->target(), modifiers);
  160. } else if (button == GUI::MouseButton::Middle) {
  161. page_client.page_did_middle_click_link(url, link->target(), modifiers);
  162. }
  163. } else {
  164. if (button == GUI::MouseButton::Left) {
  165. auto result = layout_root()->hit_test(position, HitTestType::TextCursor);
  166. if (result.layout_node && result.layout_node->node()) {
  167. m_frame.set_cursor_position(DOM::Position(*node, result.index_in_node));
  168. layout_root()->set_selection({ { result.layout_node, result.index_in_node }, {} });
  169. dump_selection("MouseDown");
  170. m_in_mouse_selection = true;
  171. }
  172. } else if (button == GUI::MouseButton::Right) {
  173. page_client.page_did_request_context_menu(m_frame.to_main_frame_position(position));
  174. }
  175. }
  176. return true;
  177. }
  178. bool EventHandler::handle_mousemove(const Gfx::IntPoint& position, unsigned buttons, unsigned modifiers)
  179. {
  180. if (!layout_root())
  181. return false;
  182. if (m_mouse_event_tracking_layout_node) {
  183. m_mouse_event_tracking_layout_node->handle_mousemove({}, position, buttons, modifiers);
  184. return true;
  185. }
  186. auto& document = *m_frame.document();
  187. auto& page_client = m_frame.page().client();
  188. bool hovered_node_changed = false;
  189. bool is_hovering_link = false;
  190. bool is_hovering_text = false;
  191. auto result = layout_root()->hit_test(position, HitTestType::Exact);
  192. const HTML::HTMLAnchorElement* hovered_link_element = nullptr;
  193. if (result.layout_node) {
  194. if (result.layout_node->wants_mouse_events()) {
  195. document.set_hovered_node(result.layout_node->node());
  196. result.layout_node->handle_mousemove({}, position, buttons, modifiers);
  197. // FIXME: It feels a bit aggressive to always update the cursor like this.
  198. page_client.page_did_request_cursor_change(Gfx::StandardCursor::None);
  199. return true;
  200. }
  201. RefPtr<DOM::Node> node = result.layout_node->node();
  202. if (node && is<HTML::HTMLIFrameElement>(*node)) {
  203. if (auto* subframe = downcast<HTML::HTMLIFrameElement>(*node).content_frame())
  204. return subframe->event_handler().handle_mousemove(position.translated(compute_mouse_event_offset({}, *result.layout_node)), buttons, modifiers);
  205. return false;
  206. }
  207. hovered_node_changed = node != document.hovered_node();
  208. document.set_hovered_node(node);
  209. if (node) {
  210. if (node->is_text())
  211. is_hovering_text = true;
  212. hovered_link_element = node->enclosing_link_element();
  213. if (hovered_link_element) {
  214. #ifdef HTML_DEBUG
  215. dbg() << "InProcessWebView: hovering over a link to " << hovered_link_element->href();
  216. #endif
  217. is_hovering_link = true;
  218. }
  219. auto offset = compute_mouse_event_offset(position, *result.layout_node);
  220. node->dispatch_event(UIEvents::MouseEvent::create("mousemove", offset.x(), offset.y()));
  221. if (!layout_root())
  222. return true;
  223. }
  224. if (m_in_mouse_selection) {
  225. auto hit = layout_root()->hit_test(position, HitTestType::TextCursor);
  226. if (hit.layout_node && hit.layout_node->node()) {
  227. layout_root()->set_selection_end({ hit.layout_node, hit.index_in_node });
  228. }
  229. dump_selection("MouseMove");
  230. page_client.page_did_change_selection();
  231. }
  232. }
  233. if (is_hovering_link)
  234. page_client.page_did_request_cursor_change(Gfx::StandardCursor::Hand);
  235. else if (is_hovering_text)
  236. page_client.page_did_request_cursor_change(Gfx::StandardCursor::IBeam);
  237. else
  238. page_client.page_did_request_cursor_change(Gfx::StandardCursor::None);
  239. if (hovered_node_changed) {
  240. RefPtr<HTML::HTMLElement> hovered_html_element = document.hovered_node() ? document.hovered_node()->enclosing_html_element() : nullptr;
  241. if (hovered_html_element && !hovered_html_element->title().is_null()) {
  242. page_client.page_did_enter_tooltip_area(m_frame.to_main_frame_position(position), hovered_html_element->title());
  243. } else {
  244. page_client.page_did_leave_tooltip_area();
  245. }
  246. if (is_hovering_link)
  247. page_client.page_did_hover_link(document.complete_url(hovered_link_element->href()));
  248. else
  249. page_client.page_did_unhover_link();
  250. }
  251. return true;
  252. }
  253. void EventHandler::dump_selection(const char* event_name) const
  254. {
  255. UNUSED_PARAM(event_name);
  256. #ifdef SELECTION_DEBUG
  257. dbg() << event_name << " selection start: "
  258. << layout_root()->selection().start().layout_node << ":" << layout_root()->selection().start().index_in_node << ", end: "
  259. << layout_root()->selection().end().layout_node << ":" << layout_root()->selection().end().index_in_node;
  260. #endif
  261. }
  262. bool EventHandler::focus_next_element()
  263. {
  264. if (!m_frame.document())
  265. return false;
  266. auto* element = m_frame.document()->focused_element();
  267. if (!element) {
  268. element = m_frame.document()->first_child_of_type<DOM::Element>();
  269. if (element && element->is_focusable()) {
  270. m_frame.document()->set_focused_element(element);
  271. return true;
  272. }
  273. }
  274. for (element = element->next_element_in_pre_order(); element && !element->is_focusable(); element = element->next_element_in_pre_order())
  275. ;
  276. m_frame.document()->set_focused_element(element);
  277. return element;
  278. }
  279. bool EventHandler::focus_previous_element()
  280. {
  281. // FIXME: Implement Shift-Tab cycling backwards through focusable elements!
  282. return false;
  283. }
  284. bool EventHandler::handle_keydown(KeyCode key, unsigned modifiers, u32 code_point)
  285. {
  286. if (key == KeyCode::Key_Tab) {
  287. if (modifiers & KeyModifier::Mod_Shift)
  288. return focus_previous_element();
  289. else
  290. return focus_next_element();
  291. }
  292. if (m_frame.cursor_position().node() && m_frame.cursor_position().node()->is_editable()) {
  293. // FIXME: Support backspacing across DOM node boundaries.
  294. if (key == KeyCode::Key_Backspace && m_frame.cursor_position().offset() > 0) {
  295. auto& text_node = downcast<DOM::Text>(*m_frame.cursor_position().node());
  296. StringBuilder builder;
  297. builder.append(text_node.data().substring_view(0, m_frame.cursor_position().offset() - 1));
  298. builder.append(text_node.data().substring_view(m_frame.cursor_position().offset(), text_node.data().length() - m_frame.cursor_position().offset()));
  299. text_node.set_data(builder.to_string());
  300. m_frame.set_cursor_position({ *m_frame.cursor_position().node(), m_frame.cursor_position().offset() - 1 });
  301. // FIXME: This should definitely use incremental layout invalidation instead!
  302. text_node.document().force_layout();
  303. return true;
  304. }
  305. if (code_point && m_frame.cursor_position().is_valid() && is<DOM::Text>(*m_frame.cursor_position().node())) {
  306. auto& text_node = downcast<DOM::Text>(*m_frame.cursor_position().node());
  307. StringBuilder builder;
  308. builder.append(text_node.data().substring_view(0, m_frame.cursor_position().offset()));
  309. builder.append_code_point(code_point);
  310. builder.append(text_node.data().substring_view(m_frame.cursor_position().offset(), text_node.data().length() - m_frame.cursor_position().offset()));
  311. text_node.set_data(builder.to_string());
  312. // FIXME: This will advance the cursor incorrectly when inserting multiple whitespaces (DOM vs layout whitespace collapse difference.)
  313. m_frame.set_cursor_position({ *m_frame.cursor_position().node(), m_frame.cursor_position().offset() + 1 });
  314. // FIXME: This should definitely use incremental layout invalidation instead!
  315. text_node.document().force_layout();
  316. return true;
  317. }
  318. }
  319. return false;
  320. }
  321. void EventHandler::set_mouse_event_tracking_layout_node(LayoutNode* layout_node)
  322. {
  323. if (layout_node)
  324. m_mouse_event_tracking_layout_node = layout_node->make_weak_ptr();
  325. else
  326. m_mouse_event_tracking_layout_node = nullptr;
  327. }
  328. }