EventHandler.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. /*
  2. * Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Max Wipfli <mail@maxwipfli.ch>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibGUI/Event.h>
  8. #include <LibWeb/DOM/Range.h>
  9. #include <LibWeb/DOM/Text.h>
  10. #include <LibWeb/DOM/Window.h>
  11. #include <LibWeb/HTML/BrowsingContext.h>
  12. #include <LibWeb/HTML/HTMLAnchorElement.h>
  13. #include <LibWeb/HTML/HTMLIFrameElement.h>
  14. #include <LibWeb/HTML/HTMLImageElement.h>
  15. #include <LibWeb/Layout/InitialContainingBlock.h>
  16. #include <LibWeb/Page/EventHandler.h>
  17. #include <LibWeb/Page/Page.h>
  18. #include <LibWeb/UIEvents/EventNames.h>
  19. #include <LibWeb/UIEvents/KeyboardEvent.h>
  20. #include <LibWeb/UIEvents/MouseEvent.h>
  21. namespace Web {
  22. static Gfx::StandardCursor cursor_css_to_gfx(Optional<CSS::Cursor> cursor)
  23. {
  24. if (!cursor.has_value()) {
  25. return Gfx::StandardCursor::None;
  26. }
  27. switch (cursor.value()) {
  28. case CSS::Cursor::Crosshair:
  29. case CSS::Cursor::Cell:
  30. return Gfx::StandardCursor::Crosshair;
  31. case CSS::Cursor::Grab:
  32. case CSS::Cursor::Grabbing:
  33. return Gfx::StandardCursor::Drag;
  34. case CSS::Cursor::Pointer:
  35. return Gfx::StandardCursor::Hand;
  36. case CSS::Cursor::Help:
  37. return Gfx::StandardCursor::Help;
  38. case CSS::Cursor::None:
  39. return Gfx::StandardCursor::Hidden;
  40. case CSS::Cursor::Text:
  41. case CSS::Cursor::VerticalText:
  42. return Gfx::StandardCursor::IBeam;
  43. case CSS::Cursor::Move:
  44. case CSS::Cursor::AllScroll:
  45. return Gfx::StandardCursor::Move;
  46. case CSS::Cursor::Progress:
  47. case CSS::Cursor::Wait:
  48. return Gfx::StandardCursor::Wait;
  49. case CSS::Cursor::ColResize:
  50. return Gfx::StandardCursor::ResizeColumn;
  51. case CSS::Cursor::EResize:
  52. case CSS::Cursor::WResize:
  53. case CSS::Cursor::EwResize:
  54. return Gfx::StandardCursor::ResizeHorizontal;
  55. case CSS::Cursor::RowResize:
  56. return Gfx::StandardCursor::ResizeRow;
  57. case CSS::Cursor::NResize:
  58. case CSS::Cursor::SResize:
  59. case CSS::Cursor::NsResize:
  60. return Gfx::StandardCursor::ResizeVertical;
  61. case CSS::Cursor::NeResize:
  62. case CSS::Cursor::SwResize:
  63. case CSS::Cursor::NeswResize:
  64. return Gfx::StandardCursor::ResizeDiagonalBLTR;
  65. case CSS::Cursor::NwResize:
  66. case CSS::Cursor::SeResize:
  67. case CSS::Cursor::NwseResize:
  68. return Gfx::StandardCursor::ResizeDiagonalTLBR;
  69. default:
  70. return Gfx::StandardCursor::None;
  71. }
  72. }
  73. static Gfx::IntPoint compute_mouse_event_offset(const Gfx::IntPoint& position, const Layout::Node& layout_node)
  74. {
  75. auto top_left_of_layout_node = layout_node.box_type_agnostic_position();
  76. return {
  77. position.x() - static_cast<int>(top_left_of_layout_node.x()),
  78. position.y() - static_cast<int>(top_left_of_layout_node.y())
  79. };
  80. }
  81. EventHandler::EventHandler(Badge<HTML::BrowsingContext>, HTML::BrowsingContext& frame)
  82. : m_frame(frame)
  83. , m_edit_event_handler(make<EditEventHandler>(frame))
  84. {
  85. }
  86. EventHandler::~EventHandler()
  87. {
  88. }
  89. const Layout::InitialContainingBlock* EventHandler::layout_root() const
  90. {
  91. if (!m_frame.active_document())
  92. return nullptr;
  93. return m_frame.active_document()->layout_node();
  94. }
  95. Layout::InitialContainingBlock* EventHandler::layout_root()
  96. {
  97. if (!m_frame.active_document())
  98. return nullptr;
  99. return m_frame.active_document()->layout_node();
  100. }
  101. bool EventHandler::handle_mousewheel(const Gfx::IntPoint& position, unsigned int buttons, unsigned int modifiers, int wheel_delta)
  102. {
  103. if (!layout_root())
  104. return false;
  105. // FIXME: Support wheel events in subframes.
  106. auto result = layout_root()->hit_test(position, Layout::HitTestType::Exact);
  107. if (result.layout_node) {
  108. if (result.layout_node->handle_mousewheel({}, position, buttons, modifiers, wheel_delta))
  109. return true;
  110. }
  111. if (auto* page = m_frame.page()) {
  112. page->client().page_did_request_scroll(0, wheel_delta * 20);
  113. return true;
  114. }
  115. return false;
  116. }
  117. bool EventHandler::handle_mouseup(const Gfx::IntPoint& position, unsigned button, unsigned modifiers)
  118. {
  119. if (!layout_root())
  120. return false;
  121. if (m_mouse_event_tracking_layout_node) {
  122. m_mouse_event_tracking_layout_node->handle_mouseup({}, position, button, modifiers);
  123. return true;
  124. }
  125. bool handled_event = false;
  126. auto result = layout_root()->hit_test(position, Layout::HitTestType::Exact);
  127. if (result.layout_node && result.layout_node->wants_mouse_events()) {
  128. result.layout_node->handle_mouseup({}, position, button, modifiers);
  129. // Things may have changed as a consequence of Layout::Node::handle_mouseup(). Hit test again.
  130. if (!layout_root())
  131. return true;
  132. result = layout_root()->hit_test(position, Layout::HitTestType::Exact);
  133. }
  134. if (result.layout_node && result.layout_node->dom_node()) {
  135. RefPtr<DOM::Node> node = result.layout_node->dom_node();
  136. if (is<HTML::HTMLIFrameElement>(*node)) {
  137. if (auto* subframe = verify_cast<HTML::HTMLIFrameElement>(*node).nested_browsing_context())
  138. return subframe->event_handler().handle_mouseup(position.translated(compute_mouse_event_offset({}, *result.layout_node)), button, modifiers);
  139. return false;
  140. }
  141. auto offset = compute_mouse_event_offset(position, *result.layout_node);
  142. node->dispatch_event(UIEvents::MouseEvent::create(UIEvents::EventNames::mouseup, offset.x(), offset.y(), position.x(), position.y()));
  143. handled_event = true;
  144. }
  145. if (button == GUI::MouseButton::Primary)
  146. m_in_mouse_selection = false;
  147. return handled_event;
  148. }
  149. bool EventHandler::handle_mousedown(const Gfx::IntPoint& position, unsigned button, unsigned modifiers)
  150. {
  151. if (!layout_root())
  152. return false;
  153. if (m_mouse_event_tracking_layout_node) {
  154. m_mouse_event_tracking_layout_node->handle_mousedown({}, position, button, modifiers);
  155. return true;
  156. }
  157. NonnullRefPtr document = *m_frame.active_document();
  158. RefPtr<DOM::Node> node;
  159. {
  160. // TODO: Allow selecting element behind if one on top has pointer-events set to none.
  161. auto result = layout_root()->hit_test(position, Layout::HitTestType::Exact);
  162. if (!result.layout_node)
  163. return false;
  164. auto pointer_events = result.layout_node->computed_values().pointer_events();
  165. if (pointer_events == CSS::PointerEvents::None)
  166. return false;
  167. node = result.layout_node->dom_node();
  168. document->set_hovered_node(node);
  169. if (result.layout_node->wants_mouse_events()) {
  170. result.layout_node->handle_mousedown({}, position, button, modifiers);
  171. return true;
  172. }
  173. if (!node)
  174. return false;
  175. if (is<HTML::HTMLIFrameElement>(*node)) {
  176. if (auto* subframe = verify_cast<HTML::HTMLIFrameElement>(*node).nested_browsing_context())
  177. return subframe->event_handler().handle_mousedown(position.translated(compute_mouse_event_offset({}, *result.layout_node)), button, modifiers);
  178. return false;
  179. }
  180. if (auto* page = m_frame.page())
  181. page->set_focused_browsing_context({}, m_frame);
  182. auto offset = compute_mouse_event_offset(position, *result.layout_node);
  183. node->dispatch_event(UIEvents::MouseEvent::create(UIEvents::EventNames::mousedown, offset.x(), offset.y(), position.x(), position.y()));
  184. }
  185. // NOTE: Dispatching an event may have disturbed the world.
  186. if (!layout_root() || layout_root() != node->document().layout_node())
  187. return true;
  188. if (button == GUI::MouseButton::Secondary && is<HTML::HTMLImageElement>(*node)) {
  189. auto& image_element = verify_cast<HTML::HTMLImageElement>(*node);
  190. auto image_url = image_element.document().parse_url(image_element.src());
  191. if (auto* page = m_frame.page())
  192. page->client().page_did_request_image_context_menu(m_frame.to_top_level_position(position), image_url, "", modifiers, image_element.bitmap());
  193. return true;
  194. }
  195. if (RefPtr<HTML::HTMLAnchorElement> link = node->enclosing_link_element()) {
  196. auto href = link->href();
  197. auto url = document->parse_url(href);
  198. dbgln("Web::EventHandler: Clicking on a link to {}", url);
  199. if (button == GUI::MouseButton::Primary) {
  200. if (href.starts_with("javascript:")) {
  201. document->run_javascript(href.substring_view(11, href.length() - 11));
  202. } else if (href.starts_with('#')) {
  203. auto anchor = href.substring_view(1, href.length() - 1);
  204. m_frame.scroll_to_anchor(anchor);
  205. } else {
  206. document->set_active_element(link);
  207. if (m_frame.is_top_level()) {
  208. if (auto* page = m_frame.page())
  209. page->client().page_did_click_link(url, link->target(), modifiers);
  210. } else {
  211. // FIXME: Handle different targets!
  212. m_frame.loader().load(url, FrameLoader::Type::Navigation);
  213. }
  214. }
  215. } else if (button == GUI::MouseButton::Secondary) {
  216. if (auto* page = m_frame.page())
  217. page->client().page_did_request_link_context_menu(m_frame.to_top_level_position(position), url, link->target(), modifiers);
  218. } else if (button == GUI::MouseButton::Middle) {
  219. if (auto* page = m_frame.page())
  220. page->client().page_did_middle_click_link(url, link->target(), modifiers);
  221. }
  222. } else {
  223. if (button == GUI::MouseButton::Primary) {
  224. auto result = layout_root()->hit_test(position, Layout::HitTestType::TextCursor);
  225. if (result.layout_node && result.layout_node->dom_node()) {
  226. m_frame.set_cursor_position(DOM::Position(*result.layout_node->dom_node(), result.index_in_node));
  227. layout_root()->set_selection({ { result.layout_node, result.index_in_node }, {} });
  228. m_in_mouse_selection = true;
  229. }
  230. } else if (button == GUI::MouseButton::Secondary) {
  231. if (auto* page = m_frame.page())
  232. page->client().page_did_request_context_menu(m_frame.to_top_level_position(position));
  233. }
  234. }
  235. return true;
  236. }
  237. bool EventHandler::handle_mousemove(const Gfx::IntPoint& position, unsigned buttons, unsigned modifiers)
  238. {
  239. if (!layout_root())
  240. return false;
  241. if (m_mouse_event_tracking_layout_node) {
  242. m_mouse_event_tracking_layout_node->handle_mousemove({}, position, buttons, modifiers);
  243. return true;
  244. }
  245. auto& document = *m_frame.active_document();
  246. bool hovered_node_changed = false;
  247. bool is_hovering_link = false;
  248. Gfx::StandardCursor hovered_node_cursor = Gfx::StandardCursor::None;
  249. auto result = layout_root()->hit_test(position, Layout::HitTestType::Exact);
  250. const HTML::HTMLAnchorElement* hovered_link_element = nullptr;
  251. if (result.layout_node) {
  252. if (result.layout_node->wants_mouse_events()) {
  253. document.set_hovered_node(result.layout_node->dom_node());
  254. result.layout_node->handle_mousemove({}, position, buttons, modifiers);
  255. // FIXME: It feels a bit aggressive to always update the cursor like this.
  256. if (auto* page = m_frame.page())
  257. page->client().page_did_request_cursor_change(Gfx::StandardCursor::None);
  258. return true;
  259. }
  260. RefPtr<DOM::Node> node = result.layout_node->dom_node();
  261. if (node && is<HTML::HTMLIFrameElement>(*node)) {
  262. if (auto* subframe = verify_cast<HTML::HTMLIFrameElement>(*node).nested_browsing_context())
  263. return subframe->event_handler().handle_mousemove(position.translated(compute_mouse_event_offset({}, *result.layout_node)), buttons, modifiers);
  264. return false;
  265. }
  266. auto pointer_events = result.layout_node->computed_values().pointer_events();
  267. if (pointer_events == CSS::PointerEvents::None)
  268. return false;
  269. hovered_node_changed = node != document.hovered_node();
  270. document.set_hovered_node(node);
  271. if (node) {
  272. hovered_link_element = node->enclosing_link_element();
  273. if (hovered_link_element)
  274. is_hovering_link = true;
  275. if (node->is_text()) {
  276. auto cursor = result.layout_node->computed_values().cursor();
  277. if (cursor == CSS::Cursor::Auto)
  278. hovered_node_cursor = Gfx::StandardCursor::IBeam;
  279. else
  280. hovered_node_cursor = cursor_css_to_gfx(cursor);
  281. }
  282. auto offset = compute_mouse_event_offset(position, *result.layout_node);
  283. node->dispatch_event(UIEvents::MouseEvent::create(UIEvents::EventNames::mousemove, offset.x(), offset.y(), position.x(), position.y()));
  284. // NOTE: Dispatching an event may have disturbed the world.
  285. if (!layout_root() || layout_root() != node->document().layout_node())
  286. return true;
  287. }
  288. if (m_in_mouse_selection) {
  289. auto hit = layout_root()->hit_test(position, Layout::HitTestType::TextCursor);
  290. if (hit.layout_node && hit.layout_node->dom_node()) {
  291. m_frame.set_cursor_position(DOM::Position(*hit.layout_node->dom_node(), result.index_in_node));
  292. layout_root()->set_selection_end({ hit.layout_node, hit.index_in_node });
  293. }
  294. if (auto* page = m_frame.page())
  295. page->client().page_did_change_selection();
  296. }
  297. }
  298. if (auto* page = m_frame.page()) {
  299. page->client().page_did_request_cursor_change(hovered_node_cursor);
  300. if (hovered_node_changed) {
  301. RefPtr<HTML::HTMLElement> hovered_html_element = document.hovered_node() ? document.hovered_node()->enclosing_html_element_with_attribute(HTML::AttributeNames::title) : nullptr;
  302. if (hovered_html_element && !hovered_html_element->title().is_null()) {
  303. page->client().page_did_enter_tooltip_area(m_frame.to_top_level_position(position), hovered_html_element->title());
  304. } else {
  305. page->client().page_did_leave_tooltip_area();
  306. }
  307. if (is_hovering_link)
  308. page->client().page_did_hover_link(document.parse_url(hovered_link_element->href()));
  309. else
  310. page->client().page_did_unhover_link();
  311. }
  312. }
  313. return true;
  314. }
  315. bool EventHandler::focus_next_element()
  316. {
  317. if (!m_frame.active_document())
  318. return false;
  319. auto* element = m_frame.active_document()->focused_element();
  320. if (!element) {
  321. element = m_frame.active_document()->first_child_of_type<DOM::Element>();
  322. if (element && element->is_focusable()) {
  323. m_frame.active_document()->set_focused_element(element);
  324. return true;
  325. }
  326. }
  327. for (element = element->next_element_in_pre_order(); element && !element->is_focusable(); element = element->next_element_in_pre_order())
  328. ;
  329. m_frame.active_document()->set_focused_element(element);
  330. return element;
  331. }
  332. bool EventHandler::focus_previous_element()
  333. {
  334. // FIXME: Implement Shift-Tab cycling backwards through focusable elements!
  335. return false;
  336. }
  337. constexpr bool should_ignore_keydown_event(u32 code_point)
  338. {
  339. // FIXME: There are probably also keys with non-zero code points that should be filtered out.
  340. return code_point == 0;
  341. }
  342. bool EventHandler::handle_keydown(KeyCode key, unsigned modifiers, u32 code_point)
  343. {
  344. if (!m_frame.active_document())
  345. return false;
  346. NonnullRefPtr<DOM::Document> document = *m_frame.active_document();
  347. if (!document->layout_node())
  348. return false;
  349. NonnullRefPtr<Layout::InitialContainingBlock> layout_root = *document->layout_node();
  350. if (key == KeyCode::Key_Tab) {
  351. if (modifiers & KeyModifier::Mod_Shift)
  352. return focus_previous_element();
  353. return focus_next_element();
  354. }
  355. if (layout_root->selection().is_valid()) {
  356. auto range = layout_root->selection().to_dom_range()->normalized();
  357. if (range->start_container()->is_editable()) {
  358. layout_root->set_selection({});
  359. // FIXME: This doesn't work for some reason?
  360. m_frame.set_cursor_position({ *range->start_container(), range->start_offset() });
  361. if (key == KeyCode::Key_Backspace || key == KeyCode::Key_Delete) {
  362. m_edit_event_handler->handle_delete(range);
  363. return true;
  364. }
  365. if (!should_ignore_keydown_event(code_point)) {
  366. m_edit_event_handler->handle_delete(range);
  367. m_edit_event_handler->handle_insert(m_frame.cursor_position(), code_point);
  368. m_frame.increment_cursor_position_offset();
  369. return true;
  370. }
  371. }
  372. }
  373. if (m_frame.cursor_position().is_valid() && m_frame.cursor_position().node()->is_editable()) {
  374. if (key == KeyCode::Key_Backspace) {
  375. if (!m_frame.decrement_cursor_position_offset()) {
  376. // FIXME: Move to the previous node and delete the last character there.
  377. return true;
  378. }
  379. m_edit_event_handler->handle_delete_character_after(m_frame.cursor_position());
  380. return true;
  381. }
  382. if (key == KeyCode::Key_Delete) {
  383. if (m_frame.cursor_position().offset_is_at_end_of_node()) {
  384. // FIXME: Move to the next node and delete the first character there.
  385. return true;
  386. }
  387. m_edit_event_handler->handle_delete_character_after(m_frame.cursor_position());
  388. return true;
  389. }
  390. if (key == KeyCode::Key_Right) {
  391. if (!m_frame.increment_cursor_position_offset()) {
  392. // FIXME: Move to the next node.
  393. }
  394. return true;
  395. }
  396. if (key == KeyCode::Key_Left) {
  397. if (!m_frame.decrement_cursor_position_offset()) {
  398. // FIXME: Move to the previous node.
  399. }
  400. return true;
  401. }
  402. if (!should_ignore_keydown_event(code_point)) {
  403. m_edit_event_handler->handle_insert(m_frame.cursor_position(), code_point);
  404. m_frame.increment_cursor_position_offset();
  405. return true;
  406. }
  407. // NOTE: Because modifier keys should be ignored, we need to return true.
  408. return true;
  409. }
  410. auto event = UIEvents::KeyboardEvent::create_from_platform_event(UIEvents::EventNames::keydown, key, modifiers, code_point);
  411. if (RefPtr<DOM::Element> focused_element = document->focused_element())
  412. return focused_element->dispatch_event(move(event));
  413. if (RefPtr<HTML::HTMLElement> body = m_frame.active_document()->body())
  414. return body->dispatch_event(move(event));
  415. return document->root().dispatch_event(move(event));
  416. }
  417. bool EventHandler::handle_keyup(KeyCode key, unsigned modifiers, u32 code_point)
  418. {
  419. RefPtr<DOM::Document> document = m_frame.active_document();
  420. if (!document)
  421. return false;
  422. auto event = UIEvents::KeyboardEvent::create_from_platform_event(UIEvents::EventNames::keyup, key, modifiers, code_point);
  423. if (RefPtr<DOM::Element> focused_element = document->focused_element())
  424. return document->focused_element()->dispatch_event(move(event));
  425. if (RefPtr<HTML::HTMLElement> body = document->body())
  426. return body->dispatch_event(move(event));
  427. return document->root().dispatch_event(move(event));
  428. }
  429. void EventHandler::set_mouse_event_tracking_layout_node(Layout::Node* layout_node)
  430. {
  431. if (layout_node)
  432. m_mouse_event_tracking_layout_node = layout_node->make_weak_ptr();
  433. else
  434. m_mouse_event_tracking_layout_node = nullptr;
  435. }
  436. }