EventHandler.cpp 17 KB

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