EventHandler.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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_x, int wheel_delta_y)
  102. {
  103. if (!layout_root())
  104. return false;
  105. if (modifiers & KeyModifier::Mod_Shift)
  106. swap(wheel_delta_x, wheel_delta_y);
  107. // FIXME: Support wheel events in subframes.
  108. auto result = layout_root()->hit_test(position, Layout::HitTestType::Exact);
  109. if (result.layout_node) {
  110. if (result.layout_node->handle_mousewheel({}, position, buttons, modifiers, wheel_delta_x, wheel_delta_y))
  111. return true;
  112. }
  113. if (auto* page = m_frame.page()) {
  114. page->client().page_did_request_scroll(wheel_delta_x * 20, wheel_delta_y * 20);
  115. return true;
  116. }
  117. return false;
  118. }
  119. bool EventHandler::handle_mouseup(const Gfx::IntPoint& position, unsigned button, unsigned modifiers)
  120. {
  121. if (!layout_root())
  122. return false;
  123. if (m_mouse_event_tracking_layout_node) {
  124. m_mouse_event_tracking_layout_node->handle_mouseup({}, position, button, modifiers);
  125. return true;
  126. }
  127. bool handled_event = false;
  128. auto result = layout_root()->hit_test(position, Layout::HitTestType::Exact);
  129. if (result.layout_node && result.layout_node->wants_mouse_events()) {
  130. result.layout_node->handle_mouseup({}, position, button, modifiers);
  131. // Things may have changed as a consequence of Layout::Node::handle_mouseup(). Hit test again.
  132. if (!layout_root())
  133. return true;
  134. result = layout_root()->hit_test(position, Layout::HitTestType::Exact);
  135. }
  136. if (result.layout_node && result.layout_node->dom_node()) {
  137. RefPtr<DOM::Node> node = result.layout_node->dom_node();
  138. if (is<HTML::HTMLIFrameElement>(*node)) {
  139. if (auto* subframe = verify_cast<HTML::HTMLIFrameElement>(*node).nested_browsing_context())
  140. return subframe->event_handler().handle_mouseup(position.translated(compute_mouse_event_offset({}, *result.layout_node)), button, modifiers);
  141. return false;
  142. }
  143. auto offset = compute_mouse_event_offset(position, *result.layout_node);
  144. node->dispatch_event(UIEvents::MouseEvent::create(UIEvents::EventNames::mouseup, offset.x(), offset.y(), position.x(), position.y()));
  145. handled_event = true;
  146. }
  147. if (button == GUI::MouseButton::Primary)
  148. m_in_mouse_selection = false;
  149. return handled_event;
  150. }
  151. bool EventHandler::handle_mousedown(const Gfx::IntPoint& position, unsigned button, unsigned modifiers)
  152. {
  153. if (!layout_root())
  154. return false;
  155. if (m_mouse_event_tracking_layout_node) {
  156. m_mouse_event_tracking_layout_node->handle_mousedown({}, position, button, modifiers);
  157. return true;
  158. }
  159. NonnullRefPtr document = *m_frame.active_document();
  160. RefPtr<DOM::Node> node;
  161. {
  162. // TODO: Allow selecting element behind if one on top has pointer-events set to none.
  163. auto result = layout_root()->hit_test(position, Layout::HitTestType::Exact);
  164. if (!result.layout_node)
  165. return false;
  166. auto pointer_events = result.layout_node->computed_values().pointer_events();
  167. if (pointer_events == CSS::PointerEvents::None)
  168. return false;
  169. node = result.layout_node->dom_node();
  170. document->set_hovered_node(node);
  171. if (result.layout_node->wants_mouse_events()) {
  172. result.layout_node->handle_mousedown({}, position, button, modifiers);
  173. return true;
  174. }
  175. if (!node)
  176. return false;
  177. if (is<HTML::HTMLIFrameElement>(*node)) {
  178. if (auto* subframe = verify_cast<HTML::HTMLIFrameElement>(*node).nested_browsing_context())
  179. return subframe->event_handler().handle_mousedown(position.translated(compute_mouse_event_offset({}, *result.layout_node)), button, modifiers);
  180. return false;
  181. }
  182. if (auto* page = m_frame.page())
  183. page->set_focused_browsing_context({}, m_frame);
  184. auto offset = compute_mouse_event_offset(position, *result.layout_node);
  185. node->dispatch_event(UIEvents::MouseEvent::create(UIEvents::EventNames::mousedown, offset.x(), offset.y(), position.x(), position.y()));
  186. }
  187. // NOTE: Dispatching an event may have disturbed the world.
  188. if (!layout_root() || layout_root() != node->document().layout_node())
  189. return true;
  190. if (button == GUI::MouseButton::Secondary && is<HTML::HTMLImageElement>(*node)) {
  191. auto& image_element = verify_cast<HTML::HTMLImageElement>(*node);
  192. auto image_url = image_element.document().parse_url(image_element.src());
  193. if (auto* page = m_frame.page())
  194. page->client().page_did_request_image_context_menu(m_frame.to_top_level_position(position), image_url, "", modifiers, image_element.bitmap());
  195. return true;
  196. }
  197. if (RefPtr<HTML::HTMLAnchorElement> link = node->enclosing_link_element()) {
  198. auto href = link->href();
  199. auto url = document->parse_url(href);
  200. dbgln("Web::EventHandler: Clicking on a link to {}", url);
  201. if (button == GUI::MouseButton::Primary) {
  202. if (href.starts_with("javascript:")) {
  203. document->run_javascript(href.substring_view(11, href.length() - 11));
  204. } else if (href.starts_with('#')) {
  205. auto anchor = href.substring_view(1, href.length() - 1);
  206. m_frame.scroll_to_anchor(anchor);
  207. } else {
  208. document->set_active_element(link);
  209. if (m_frame.is_top_level()) {
  210. if (auto* page = m_frame.page())
  211. page->client().page_did_click_link(url, link->target(), modifiers);
  212. } else {
  213. // FIXME: Handle different targets!
  214. m_frame.loader().load(url, FrameLoader::Type::Navigation);
  215. }
  216. }
  217. } else if (button == GUI::MouseButton::Secondary) {
  218. if (auto* page = m_frame.page())
  219. page->client().page_did_request_link_context_menu(m_frame.to_top_level_position(position), url, link->target(), modifiers);
  220. } else if (button == GUI::MouseButton::Middle) {
  221. if (auto* page = m_frame.page())
  222. page->client().page_did_middle_click_link(url, link->target(), modifiers);
  223. }
  224. } else {
  225. if (button == GUI::MouseButton::Primary) {
  226. auto result = layout_root()->hit_test(position, Layout::HitTestType::TextCursor);
  227. if (result.layout_node && result.layout_node->dom_node()) {
  228. m_frame.set_cursor_position(DOM::Position(*result.layout_node->dom_node(), result.index_in_node));
  229. layout_root()->set_selection({ { result.layout_node, result.index_in_node }, {} });
  230. m_in_mouse_selection = true;
  231. }
  232. } else if (button == GUI::MouseButton::Secondary) {
  233. if (auto* page = m_frame.page())
  234. page->client().page_did_request_context_menu(m_frame.to_top_level_position(position));
  235. }
  236. }
  237. return true;
  238. }
  239. bool EventHandler::handle_mousemove(const Gfx::IntPoint& position, unsigned buttons, unsigned modifiers)
  240. {
  241. if (!layout_root())
  242. return false;
  243. if (m_mouse_event_tracking_layout_node) {
  244. m_mouse_event_tracking_layout_node->handle_mousemove({}, position, buttons, modifiers);
  245. return true;
  246. }
  247. auto& document = *m_frame.active_document();
  248. bool hovered_node_changed = false;
  249. bool is_hovering_link = false;
  250. Gfx::StandardCursor hovered_node_cursor = Gfx::StandardCursor::None;
  251. auto result = layout_root()->hit_test(position, Layout::HitTestType::Exact);
  252. const HTML::HTMLAnchorElement* hovered_link_element = nullptr;
  253. if (result.layout_node) {
  254. if (result.layout_node->wants_mouse_events()) {
  255. document.set_hovered_node(result.layout_node->dom_node());
  256. result.layout_node->handle_mousemove({}, position, buttons, modifiers);
  257. // FIXME: It feels a bit aggressive to always update the cursor like this.
  258. if (auto* page = m_frame.page())
  259. page->client().page_did_request_cursor_change(Gfx::StandardCursor::None);
  260. return true;
  261. }
  262. RefPtr<DOM::Node> node = result.layout_node->dom_node();
  263. if (node && is<HTML::HTMLIFrameElement>(*node)) {
  264. if (auto* subframe = verify_cast<HTML::HTMLIFrameElement>(*node).nested_browsing_context())
  265. return subframe->event_handler().handle_mousemove(position.translated(compute_mouse_event_offset({}, *result.layout_node)), buttons, modifiers);
  266. return false;
  267. }
  268. auto pointer_events = result.layout_node->computed_values().pointer_events();
  269. if (pointer_events == CSS::PointerEvents::None)
  270. return false;
  271. hovered_node_changed = node != document.hovered_node();
  272. document.set_hovered_node(node);
  273. if (node) {
  274. hovered_link_element = node->enclosing_link_element();
  275. if (hovered_link_element)
  276. is_hovering_link = true;
  277. if (node->is_text()) {
  278. auto cursor = result.layout_node->computed_values().cursor();
  279. if (cursor == CSS::Cursor::Auto)
  280. hovered_node_cursor = Gfx::StandardCursor::IBeam;
  281. else
  282. hovered_node_cursor = cursor_css_to_gfx(cursor);
  283. }
  284. auto offset = compute_mouse_event_offset(position, *result.layout_node);
  285. node->dispatch_event(UIEvents::MouseEvent::create(UIEvents::EventNames::mousemove, offset.x(), offset.y(), position.x(), position.y()));
  286. // NOTE: Dispatching an event may have disturbed the world.
  287. if (!layout_root() || layout_root() != node->document().layout_node())
  288. return true;
  289. }
  290. if (m_in_mouse_selection) {
  291. auto hit = layout_root()->hit_test(position, Layout::HitTestType::TextCursor);
  292. if (hit.layout_node && hit.layout_node->dom_node()) {
  293. m_frame.set_cursor_position(DOM::Position(*hit.layout_node->dom_node(), result.index_in_node));
  294. layout_root()->set_selection_end({ hit.layout_node, hit.index_in_node });
  295. }
  296. if (auto* page = m_frame.page())
  297. page->client().page_did_change_selection();
  298. }
  299. }
  300. if (auto* page = m_frame.page()) {
  301. page->client().page_did_request_cursor_change(hovered_node_cursor);
  302. if (hovered_node_changed) {
  303. RefPtr<HTML::HTMLElement> hovered_html_element = document.hovered_node() ? document.hovered_node()->enclosing_html_element_with_attribute(HTML::AttributeNames::title) : nullptr;
  304. if (hovered_html_element && !hovered_html_element->title().is_null()) {
  305. page->client().page_did_enter_tooltip_area(m_frame.to_top_level_position(position), hovered_html_element->title());
  306. } else {
  307. page->client().page_did_leave_tooltip_area();
  308. }
  309. if (is_hovering_link)
  310. page->client().page_did_hover_link(document.parse_url(hovered_link_element->href()));
  311. else
  312. page->client().page_did_unhover_link();
  313. }
  314. }
  315. return true;
  316. }
  317. bool EventHandler::focus_next_element()
  318. {
  319. if (!m_frame.active_document())
  320. return false;
  321. auto* element = m_frame.active_document()->focused_element();
  322. if (!element) {
  323. element = m_frame.active_document()->first_child_of_type<DOM::Element>();
  324. if (element && element->is_focusable()) {
  325. m_frame.active_document()->set_focused_element(element);
  326. return true;
  327. }
  328. }
  329. for (element = element->next_element_in_pre_order(); element && !element->is_focusable(); element = element->next_element_in_pre_order())
  330. ;
  331. m_frame.active_document()->set_focused_element(element);
  332. return element;
  333. }
  334. bool EventHandler::focus_previous_element()
  335. {
  336. // FIXME: Implement Shift-Tab cycling backwards through focusable elements!
  337. return false;
  338. }
  339. constexpr bool should_ignore_keydown_event(u32 code_point)
  340. {
  341. // FIXME: There are probably also keys with non-zero code points that should be filtered out.
  342. return code_point == 0;
  343. }
  344. bool EventHandler::handle_keydown(KeyCode key, unsigned modifiers, u32 code_point)
  345. {
  346. if (!m_frame.active_document())
  347. return false;
  348. NonnullRefPtr<DOM::Document> document = *m_frame.active_document();
  349. if (!document->layout_node())
  350. return false;
  351. NonnullRefPtr<Layout::InitialContainingBlock> layout_root = *document->layout_node();
  352. if (key == KeyCode::Key_Tab) {
  353. if (modifiers & KeyModifier::Mod_Shift)
  354. return focus_previous_element();
  355. return focus_next_element();
  356. }
  357. if (layout_root->selection().is_valid()) {
  358. auto range = layout_root->selection().to_dom_range()->normalized();
  359. if (range->start_container()->is_editable()) {
  360. layout_root->set_selection({});
  361. // FIXME: This doesn't work for some reason?
  362. m_frame.set_cursor_position({ *range->start_container(), range->start_offset() });
  363. if (key == KeyCode::Key_Backspace || key == KeyCode::Key_Delete) {
  364. m_edit_event_handler->handle_delete(range);
  365. return true;
  366. }
  367. if (!should_ignore_keydown_event(code_point)) {
  368. m_edit_event_handler->handle_delete(range);
  369. m_edit_event_handler->handle_insert(m_frame.cursor_position(), code_point);
  370. m_frame.increment_cursor_position_offset();
  371. return true;
  372. }
  373. }
  374. }
  375. if (m_frame.cursor_position().is_valid() && m_frame.cursor_position().node()->is_editable()) {
  376. if (key == KeyCode::Key_Backspace) {
  377. if (!m_frame.decrement_cursor_position_offset()) {
  378. // FIXME: Move to the previous node and delete the last character there.
  379. return true;
  380. }
  381. m_edit_event_handler->handle_delete_character_after(m_frame.cursor_position());
  382. return true;
  383. }
  384. if (key == KeyCode::Key_Delete) {
  385. if (m_frame.cursor_position().offset_is_at_end_of_node()) {
  386. // FIXME: Move to the next node and delete the first character there.
  387. return true;
  388. }
  389. m_edit_event_handler->handle_delete_character_after(m_frame.cursor_position());
  390. return true;
  391. }
  392. if (key == KeyCode::Key_Right) {
  393. if (!m_frame.increment_cursor_position_offset()) {
  394. // FIXME: Move to the next node.
  395. }
  396. return true;
  397. }
  398. if (key == KeyCode::Key_Left) {
  399. if (!m_frame.decrement_cursor_position_offset()) {
  400. // FIXME: Move to the previous node.
  401. }
  402. return true;
  403. }
  404. if (!should_ignore_keydown_event(code_point)) {
  405. m_edit_event_handler->handle_insert(m_frame.cursor_position(), code_point);
  406. m_frame.increment_cursor_position_offset();
  407. return true;
  408. }
  409. // NOTE: Because modifier keys should be ignored, we need to return true.
  410. return true;
  411. }
  412. auto event = UIEvents::KeyboardEvent::create_from_platform_event(UIEvents::EventNames::keydown, key, modifiers, code_point);
  413. if (RefPtr<DOM::Element> focused_element = document->focused_element())
  414. return focused_element->dispatch_event(move(event));
  415. if (RefPtr<HTML::HTMLElement> body = m_frame.active_document()->body())
  416. return body->dispatch_event(move(event));
  417. return document->root().dispatch_event(move(event));
  418. }
  419. bool EventHandler::handle_keyup(KeyCode key, unsigned modifiers, u32 code_point)
  420. {
  421. RefPtr<DOM::Document> document = m_frame.active_document();
  422. if (!document)
  423. return false;
  424. auto event = UIEvents::KeyboardEvent::create_from_platform_event(UIEvents::EventNames::keyup, key, modifiers, code_point);
  425. if (RefPtr<DOM::Element> focused_element = document->focused_element())
  426. return document->focused_element()->dispatch_event(move(event));
  427. if (RefPtr<HTML::HTMLElement> body = document->body())
  428. return body->dispatch_event(move(event));
  429. return document->root().dispatch_event(move(event));
  430. }
  431. void EventHandler::set_mouse_event_tracking_layout_node(Layout::Node* layout_node)
  432. {
  433. if (layout_node)
  434. m_mouse_event_tracking_layout_node = layout_node->make_weak_ptr();
  435. else
  436. m_mouse_event_tracking_layout_node = nullptr;
  437. }
  438. }