EventHandler.cpp 16 KB

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