EventHandler.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  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/HTML/BrowsingContext.h>
  11. #include <LibWeb/HTML/HTMLAnchorElement.h>
  12. #include <LibWeb/HTML/HTMLIFrameElement.h>
  13. #include <LibWeb/HTML/HTMLImageElement.h>
  14. #include <LibWeb/HTML/Window.h>
  15. #include <LibWeb/Layout/InitialContainingBlock.h>
  16. #include <LibWeb/Page/EventHandler.h>
  17. #include <LibWeb/Page/Page.h>
  18. #include <LibWeb/Painting/PaintableBox.h>
  19. #include <LibWeb/UIEvents/EventNames.h>
  20. #include <LibWeb/UIEvents/KeyboardEvent.h>
  21. #include <LibWeb/UIEvents/MouseEvent.h>
  22. namespace Web {
  23. static Gfx::StandardCursor cursor_css_to_gfx(Optional<CSS::Cursor> cursor)
  24. {
  25. if (!cursor.has_value()) {
  26. return Gfx::StandardCursor::None;
  27. }
  28. switch (cursor.value()) {
  29. case CSS::Cursor::Crosshair:
  30. case CSS::Cursor::Cell:
  31. return Gfx::StandardCursor::Crosshair;
  32. case CSS::Cursor::Grab:
  33. case CSS::Cursor::Grabbing:
  34. return Gfx::StandardCursor::Drag;
  35. case CSS::Cursor::Pointer:
  36. return Gfx::StandardCursor::Hand;
  37. case CSS::Cursor::Help:
  38. return Gfx::StandardCursor::Help;
  39. case CSS::Cursor::None:
  40. return Gfx::StandardCursor::Hidden;
  41. case CSS::Cursor::Text:
  42. case CSS::Cursor::VerticalText:
  43. return Gfx::StandardCursor::IBeam;
  44. case CSS::Cursor::Move:
  45. case CSS::Cursor::AllScroll:
  46. return Gfx::StandardCursor::Move;
  47. case CSS::Cursor::Progress:
  48. case CSS::Cursor::Wait:
  49. return Gfx::StandardCursor::Wait;
  50. case CSS::Cursor::ColResize:
  51. return Gfx::StandardCursor::ResizeColumn;
  52. case CSS::Cursor::EResize:
  53. case CSS::Cursor::WResize:
  54. case CSS::Cursor::EwResize:
  55. return Gfx::StandardCursor::ResizeHorizontal;
  56. case CSS::Cursor::RowResize:
  57. return Gfx::StandardCursor::ResizeRow;
  58. case CSS::Cursor::NResize:
  59. case CSS::Cursor::SResize:
  60. case CSS::Cursor::NsResize:
  61. return Gfx::StandardCursor::ResizeVertical;
  62. case CSS::Cursor::NeResize:
  63. case CSS::Cursor::SwResize:
  64. case CSS::Cursor::NeswResize:
  65. return Gfx::StandardCursor::ResizeDiagonalBLTR;
  66. case CSS::Cursor::NwResize:
  67. case CSS::Cursor::SeResize:
  68. case CSS::Cursor::NwseResize:
  69. return Gfx::StandardCursor::ResizeDiagonalTLBR;
  70. default:
  71. return Gfx::StandardCursor::None;
  72. }
  73. }
  74. static Gfx::IntPoint compute_mouse_event_offset(const Gfx::IntPoint& position, const Layout::Node& layout_node)
  75. {
  76. auto top_left_of_layout_node = layout_node.box_type_agnostic_position();
  77. return {
  78. position.x() - static_cast<int>(top_left_of_layout_node.x()),
  79. position.y() - static_cast<int>(top_left_of_layout_node.y())
  80. };
  81. }
  82. EventHandler::EventHandler(Badge<HTML::BrowsingContext>, HTML::BrowsingContext& browsing_context)
  83. : m_browsing_context(browsing_context)
  84. , m_edit_event_handler(make<EditEventHandler>(browsing_context))
  85. {
  86. }
  87. EventHandler::~EventHandler() = default;
  88. const Layout::InitialContainingBlock* EventHandler::layout_root() const
  89. {
  90. if (!m_browsing_context.active_document())
  91. return nullptr;
  92. return m_browsing_context.active_document()->layout_node();
  93. }
  94. Layout::InitialContainingBlock* EventHandler::layout_root()
  95. {
  96. if (!m_browsing_context.active_document())
  97. return nullptr;
  98. return m_browsing_context.active_document()->layout_node();
  99. }
  100. Painting::PaintableBox* EventHandler::paint_root()
  101. {
  102. if (!m_browsing_context.active_document())
  103. return nullptr;
  104. return const_cast<Painting::PaintableBox*>(m_browsing_context.active_document()->paint_box());
  105. }
  106. Painting::PaintableBox const* EventHandler::paint_root() const
  107. {
  108. if (!m_browsing_context.active_document())
  109. return nullptr;
  110. return const_cast<Painting::PaintableBox*>(m_browsing_context.active_document()->paint_box());
  111. }
  112. bool EventHandler::handle_mousewheel(const Gfx::IntPoint& position, unsigned int buttons, unsigned int modifiers, int wheel_delta_x, int wheel_delta_y)
  113. {
  114. if (m_browsing_context.active_document())
  115. m_browsing_context.active_document()->update_layout();
  116. if (!paint_root())
  117. return false;
  118. if (modifiers & KeyModifier::Mod_Shift)
  119. swap(wheel_delta_x, wheel_delta_y);
  120. // FIXME: Support wheel events in nested browsing contexts.
  121. auto result = paint_root()->hit_test(position.to_type<float>(), Painting::HitTestType::Exact);
  122. if (result.paintable && result.paintable->handle_mousewheel({}, position, buttons, modifiers, wheel_delta_x, wheel_delta_y))
  123. return true;
  124. if (auto* page = m_browsing_context.page()) {
  125. page->client().page_did_request_scroll(wheel_delta_x * 20, wheel_delta_y * 20);
  126. return true;
  127. }
  128. return false;
  129. }
  130. bool EventHandler::handle_mouseup(const Gfx::IntPoint& position, unsigned button, unsigned modifiers)
  131. {
  132. if (m_browsing_context.active_document())
  133. m_browsing_context.active_document()->update_layout();
  134. if (!paint_root())
  135. return false;
  136. bool handled_event = false;
  137. RefPtr<Painting::Paintable> paintable;
  138. if (m_mouse_event_tracking_layout_node) {
  139. paintable = m_mouse_event_tracking_layout_node->paintable();
  140. } else {
  141. auto result = paint_root()->hit_test(position.to_type<float>(), Painting::HitTestType::Exact);
  142. paintable = result.paintable;
  143. }
  144. if (paintable && paintable->wants_mouse_events()) {
  145. if (paintable->handle_mouseup({}, position, button, modifiers) == Painting::Paintable::DispatchEventOfSameName::No)
  146. return false;
  147. // Things may have changed as a consequence of Layout::Node::handle_mouseup(). Hit test again.
  148. if (!paint_root())
  149. return true;
  150. auto result = paint_root()->hit_test(position.to_type<float>(), Painting::HitTestType::Exact);
  151. paintable = result.paintable;
  152. }
  153. if (paintable) {
  154. RefPtr<DOM::Node> node = paintable->mouse_event_target();
  155. if (!node)
  156. node = paintable->layout_node().dom_node();
  157. if (node) {
  158. if (is<HTML::HTMLIFrameElement>(*node)) {
  159. if (auto* nested_browsing_context = static_cast<HTML::HTMLIFrameElement&>(*node).nested_browsing_context())
  160. return nested_browsing_context->event_handler().handle_mouseup(position.translated(compute_mouse_event_offset({}, paintable->layout_node())), button, modifiers);
  161. return false;
  162. }
  163. auto offset = compute_mouse_event_offset(position, paintable->layout_node());
  164. node->dispatch_event(UIEvents::MouseEvent::create(UIEvents::EventNames::mouseup, offset.x(), offset.y(), position.x(), position.y()));
  165. handled_event = true;
  166. if (node.ptr() == m_mousedown_target) {
  167. node->dispatch_event(UIEvents::MouseEvent::create(UIEvents::EventNames::click, offset.x(), offset.y(), position.x(), position.y()));
  168. }
  169. }
  170. }
  171. if (button == GUI::MouseButton::Primary)
  172. m_in_mouse_selection = false;
  173. return handled_event;
  174. }
  175. bool EventHandler::handle_mousedown(const Gfx::IntPoint& position, unsigned button, unsigned modifiers)
  176. {
  177. if (m_browsing_context.active_document())
  178. m_browsing_context.active_document()->update_layout();
  179. if (!paint_root())
  180. return false;
  181. NonnullRefPtr document = *m_browsing_context.active_document();
  182. RefPtr<DOM::Node> node;
  183. {
  184. // TODO: Allow selecting element behind if one on top has pointer-events set to none.
  185. RefPtr<Painting::Paintable> paintable;
  186. if (m_mouse_event_tracking_layout_node) {
  187. paintable = m_mouse_event_tracking_layout_node->paintable();
  188. } else {
  189. auto result = paint_root()->hit_test(position.to_type<float>(), Painting::HitTestType::Exact);
  190. if (!result.paintable)
  191. return false;
  192. paintable = result.paintable;
  193. }
  194. auto pointer_events = paintable->computed_values().pointer_events();
  195. // FIXME: Handle other values for pointer-events.
  196. if (pointer_events == CSS::PointerEvents::None)
  197. return false;
  198. node = paintable->mouse_event_target();
  199. if (!node)
  200. node = paintable->layout_node().dom_node();
  201. document->set_hovered_node(node);
  202. if (paintable->wants_mouse_events()) {
  203. if (paintable->handle_mousedown({}, position, button, modifiers) == Painting::Paintable::DispatchEventOfSameName::No)
  204. return false;
  205. }
  206. if (!node)
  207. return false;
  208. if (is<HTML::HTMLIFrameElement>(*node)) {
  209. if (auto* nested_browsing_context = static_cast<HTML::HTMLIFrameElement&>(*node).nested_browsing_context())
  210. return nested_browsing_context->event_handler().handle_mousedown(position.translated(compute_mouse_event_offset({}, paintable->layout_node())), button, modifiers);
  211. return false;
  212. }
  213. if (auto* page = m_browsing_context.page())
  214. page->set_focused_browsing_context({}, m_browsing_context);
  215. m_mousedown_target = node;
  216. auto offset = compute_mouse_event_offset(position, paintable->layout_node());
  217. node->dispatch_event(UIEvents::MouseEvent::create(UIEvents::EventNames::mousedown, offset.x(), offset.y(), position.x(), position.y()));
  218. }
  219. // NOTE: Dispatching an event may have disturbed the world.
  220. if (!paint_root() || paint_root() != node->document().paint_box())
  221. return true;
  222. if (button == GUI::MouseButton::Secondary && is<HTML::HTMLImageElement>(*node)) {
  223. auto& image_element = verify_cast<HTML::HTMLImageElement>(*node);
  224. auto image_url = image_element.document().parse_url(image_element.src());
  225. if (auto* page = m_browsing_context.page())
  226. page->client().page_did_request_image_context_menu(m_browsing_context.to_top_level_position(position), image_url, "", modifiers, image_element.bitmap());
  227. return true;
  228. }
  229. if (RefPtr<HTML::HTMLAnchorElement> link = node->enclosing_link_element()) {
  230. auto href = link->href();
  231. auto url = document->parse_url(href);
  232. dbgln("Web::EventHandler: Clicking on a link to {}", url);
  233. if (button == GUI::MouseButton::Primary) {
  234. if (href.starts_with("javascript:")) {
  235. document->run_javascript(href.substring_view(11, href.length() - 11));
  236. } else if (!url.fragment().is_null() && url.equals(document->url(), AK::URL::ExcludeFragment::Yes)) {
  237. m_browsing_context.scroll_to_anchor(url.fragment());
  238. } else {
  239. document->set_active_element(link);
  240. if (m_browsing_context.is_top_level()) {
  241. if (auto* page = m_browsing_context.page())
  242. page->client().page_did_click_link(url, link->target(), modifiers);
  243. } else {
  244. // FIXME: Handle different targets!
  245. m_browsing_context.loader().load(url, FrameLoader::Type::Navigation);
  246. }
  247. }
  248. } else if (button == GUI::MouseButton::Secondary) {
  249. if (auto* page = m_browsing_context.page())
  250. page->client().page_did_request_link_context_menu(m_browsing_context.to_top_level_position(position), url, link->target(), modifiers);
  251. } else if (button == GUI::MouseButton::Middle) {
  252. if (auto* page = m_browsing_context.page())
  253. page->client().page_did_middle_click_link(url, link->target(), modifiers);
  254. }
  255. } else {
  256. if (button == GUI::MouseButton::Primary) {
  257. auto result = paint_root()->hit_test(position.to_type<float>(), Painting::HitTestType::TextCursor);
  258. if (result.paintable && result.paintable->layout_node().dom_node()) {
  259. // See if we want to focus something.
  260. bool did_focus_something = false;
  261. for (auto candidate = node; candidate; candidate = candidate->parent()) {
  262. if (candidate->is_focusable()) {
  263. document->set_focused_element(verify_cast<DOM::Element>(candidate.ptr()));
  264. did_focus_something = true;
  265. break;
  266. }
  267. }
  268. // If we didn't focus anything, place the document text cursor at the mouse position.
  269. // FIXME: This is all rather strange. Find a better solution.
  270. if (!did_focus_something) {
  271. m_browsing_context.set_cursor_position(DOM::Position(*result.paintable->layout_node().dom_node(), result.index_in_node));
  272. layout_root()->set_selection({ { result.paintable->layout_node(), result.index_in_node }, {} });
  273. m_in_mouse_selection = true;
  274. }
  275. }
  276. } else if (button == GUI::MouseButton::Secondary) {
  277. if (auto* page = m_browsing_context.page())
  278. page->client().page_did_request_context_menu(m_browsing_context.to_top_level_position(position));
  279. }
  280. }
  281. return true;
  282. }
  283. bool EventHandler::handle_mousemove(const Gfx::IntPoint& position, unsigned buttons, unsigned modifiers)
  284. {
  285. if (m_browsing_context.active_document())
  286. m_browsing_context.active_document()->update_layout();
  287. if (!paint_root())
  288. return false;
  289. auto& document = *m_browsing_context.active_document();
  290. bool hovered_node_changed = false;
  291. bool is_hovering_link = false;
  292. Gfx::StandardCursor hovered_node_cursor = Gfx::StandardCursor::None;
  293. RefPtr<Painting::Paintable> paintable;
  294. Optional<int> start_index;
  295. if (m_mouse_event_tracking_layout_node) {
  296. paintable = m_mouse_event_tracking_layout_node->paintable();
  297. } else {
  298. auto result = paint_root()->hit_test(position.to_type<float>(), Painting::HitTestType::Exact);
  299. paintable = result.paintable;
  300. start_index = result.index_in_node;
  301. }
  302. const HTML::HTMLAnchorElement* hovered_link_element = nullptr;
  303. if (paintable) {
  304. if (paintable->wants_mouse_events()) {
  305. document.set_hovered_node(paintable->layout_node().dom_node());
  306. if (paintable->handle_mousemove({}, position, buttons, modifiers) == Painting::Paintable::DispatchEventOfSameName::No)
  307. return false;
  308. // FIXME: It feels a bit aggressive to always update the cursor like this.
  309. if (auto* page = m_browsing_context.page())
  310. page->client().page_did_request_cursor_change(Gfx::StandardCursor::None);
  311. }
  312. RefPtr<DOM::Node> node = paintable->mouse_event_target();
  313. if (!node)
  314. node = paintable->layout_node().dom_node();
  315. if (node && is<HTML::HTMLIFrameElement>(*node)) {
  316. if (auto* nested_browsing_context = static_cast<HTML::HTMLIFrameElement&>(*node).nested_browsing_context())
  317. return nested_browsing_context->event_handler().handle_mousemove(position.translated(compute_mouse_event_offset({}, paintable->layout_node())), buttons, modifiers);
  318. return false;
  319. }
  320. auto pointer_events = paintable->computed_values().pointer_events();
  321. // FIXME: Handle other values for pointer-events.
  322. if (pointer_events == CSS::PointerEvents::None)
  323. return false;
  324. hovered_node_changed = node != document.hovered_node();
  325. document.set_hovered_node(node);
  326. if (node) {
  327. hovered_link_element = node->enclosing_link_element();
  328. if (hovered_link_element)
  329. is_hovering_link = true;
  330. if (node->is_text()) {
  331. auto cursor = paintable->computed_values().cursor();
  332. if (cursor == CSS::Cursor::Auto)
  333. hovered_node_cursor = Gfx::StandardCursor::IBeam;
  334. else
  335. hovered_node_cursor = cursor_css_to_gfx(cursor);
  336. } else if (node->is_element()) {
  337. auto cursor = paintable->computed_values().cursor();
  338. if (cursor == CSS::Cursor::Auto)
  339. hovered_node_cursor = Gfx::StandardCursor::Arrow;
  340. else
  341. hovered_node_cursor = cursor_css_to_gfx(cursor);
  342. }
  343. auto offset = compute_mouse_event_offset(position, paintable->layout_node());
  344. node->dispatch_event(UIEvents::MouseEvent::create(UIEvents::EventNames::mousemove, offset.x(), offset.y(), position.x(), position.y()));
  345. // NOTE: Dispatching an event may have disturbed the world.
  346. if (!paint_root() || paint_root() != node->document().paint_box())
  347. return true;
  348. }
  349. if (m_in_mouse_selection) {
  350. auto hit = paint_root()->hit_test(position.to_type<float>(), Painting::HitTestType::TextCursor);
  351. if (start_index.has_value() && hit.paintable && hit.paintable->layout_node().dom_node()) {
  352. m_browsing_context.set_cursor_position(DOM::Position(*hit.paintable->layout_node().dom_node(), *start_index));
  353. layout_root()->set_selection_end({ hit.paintable->layout_node(), hit.index_in_node });
  354. }
  355. if (auto* page = m_browsing_context.page())
  356. page->client().page_did_change_selection();
  357. }
  358. }
  359. if (auto* page = m_browsing_context.page()) {
  360. page->client().page_did_request_cursor_change(hovered_node_cursor);
  361. if (hovered_node_changed) {
  362. RefPtr<HTML::HTMLElement> hovered_html_element = document.hovered_node() ? document.hovered_node()->enclosing_html_element_with_attribute(HTML::AttributeNames::title) : nullptr;
  363. if (hovered_html_element && !hovered_html_element->title().is_null()) {
  364. page->client().page_did_enter_tooltip_area(m_browsing_context.to_top_level_position(position), hovered_html_element->title());
  365. } else {
  366. page->client().page_did_leave_tooltip_area();
  367. }
  368. if (is_hovering_link)
  369. page->client().page_did_hover_link(document.parse_url(hovered_link_element->href()));
  370. else
  371. page->client().page_did_unhover_link();
  372. }
  373. }
  374. return true;
  375. }
  376. bool EventHandler::focus_next_element()
  377. {
  378. if (!m_browsing_context.active_document())
  379. return false;
  380. auto* element = m_browsing_context.active_document()->focused_element();
  381. if (!element) {
  382. element = m_browsing_context.active_document()->first_child_of_type<DOM::Element>();
  383. if (element && element->is_focusable()) {
  384. m_browsing_context.active_document()->set_focused_element(element);
  385. return true;
  386. }
  387. }
  388. for (element = element->next_element_in_pre_order(); element && !element->is_focusable(); element = element->next_element_in_pre_order())
  389. ;
  390. m_browsing_context.active_document()->set_focused_element(element);
  391. return element;
  392. }
  393. bool EventHandler::focus_previous_element()
  394. {
  395. if (!m_browsing_context.active_document())
  396. return false;
  397. auto* element = m_browsing_context.active_document()->focused_element();
  398. if (!element) {
  399. element = m_browsing_context.active_document()->last_child_of_type<DOM::Element>();
  400. if (element && element->is_focusable()) {
  401. m_browsing_context.active_document()->set_focused_element(element);
  402. return true;
  403. }
  404. }
  405. for (element = element->previous_element_in_pre_order(); element && !element->is_focusable(); element = element->previous_element_in_pre_order())
  406. ;
  407. m_browsing_context.active_document()->set_focused_element(element);
  408. return element;
  409. }
  410. constexpr bool should_ignore_keydown_event(u32 code_point)
  411. {
  412. // FIXME: There are probably also keys with non-zero code points that should be filtered out.
  413. return code_point == 0 || code_point == 27;
  414. }
  415. bool EventHandler::handle_keydown(KeyCode key, unsigned modifiers, u32 code_point)
  416. {
  417. if (!m_browsing_context.active_document())
  418. return false;
  419. NonnullRefPtr<DOM::Document> document = *m_browsing_context.active_document();
  420. if (!document->layout_node())
  421. return false;
  422. NonnullRefPtr<Layout::InitialContainingBlock> layout_root = *document->layout_node();
  423. if (key == KeyCode::Key_Tab) {
  424. if (modifiers & KeyModifier::Mod_Shift)
  425. return focus_previous_element();
  426. return focus_next_element();
  427. }
  428. if (layout_root->selection().is_valid()) {
  429. auto range = layout_root->selection().to_dom_range()->normalized();
  430. if (range->start_container()->is_editable()) {
  431. layout_root->set_selection({});
  432. // FIXME: This doesn't work for some reason?
  433. m_browsing_context.set_cursor_position({ *range->start_container(), range->start_offset() });
  434. if (key == KeyCode::Key_Backspace || key == KeyCode::Key_Delete) {
  435. m_edit_event_handler->handle_delete(range);
  436. return true;
  437. }
  438. if (!should_ignore_keydown_event(code_point)) {
  439. m_edit_event_handler->handle_delete(range);
  440. m_edit_event_handler->handle_insert(m_browsing_context.cursor_position(), code_point);
  441. m_browsing_context.increment_cursor_position_offset();
  442. return true;
  443. }
  444. }
  445. }
  446. if (m_browsing_context.cursor_position().is_valid() && m_browsing_context.cursor_position().node()->is_editable()) {
  447. if (key == KeyCode::Key_Backspace) {
  448. if (!m_browsing_context.decrement_cursor_position_offset()) {
  449. // FIXME: Move to the previous node and delete the last character there.
  450. return true;
  451. }
  452. m_edit_event_handler->handle_delete_character_after(m_browsing_context.cursor_position());
  453. return true;
  454. }
  455. if (key == KeyCode::Key_Delete) {
  456. if (m_browsing_context.cursor_position().offset_is_at_end_of_node()) {
  457. // FIXME: Move to the next node and delete the first character there.
  458. return true;
  459. }
  460. m_edit_event_handler->handle_delete_character_after(m_browsing_context.cursor_position());
  461. return true;
  462. }
  463. if (key == KeyCode::Key_Right) {
  464. if (!m_browsing_context.increment_cursor_position_offset()) {
  465. // FIXME: Move to the next node.
  466. }
  467. return true;
  468. }
  469. if (key == KeyCode::Key_Left) {
  470. if (!m_browsing_context.decrement_cursor_position_offset()) {
  471. // FIXME: Move to the previous node.
  472. }
  473. return true;
  474. }
  475. if (key == KeyCode::Key_Home) {
  476. auto& node = *static_cast<DOM::Text*>(const_cast<DOM::Node*>(m_browsing_context.cursor_position().node()));
  477. m_browsing_context.set_cursor_position(DOM::Position { node, 0 });
  478. return true;
  479. }
  480. if (key == KeyCode::Key_End) {
  481. auto& node = *static_cast<DOM::Text*>(const_cast<DOM::Node*>(m_browsing_context.cursor_position().node()));
  482. m_browsing_context.set_cursor_position(DOM::Position { node, (unsigned)node.data().length() });
  483. return true;
  484. }
  485. if (!should_ignore_keydown_event(code_point)) {
  486. m_edit_event_handler->handle_insert(m_browsing_context.cursor_position(), code_point);
  487. m_browsing_context.increment_cursor_position_offset();
  488. return true;
  489. }
  490. // NOTE: Because modifier keys should be ignored, we need to return true.
  491. return true;
  492. }
  493. auto event = UIEvents::KeyboardEvent::create_from_platform_event(UIEvents::EventNames::keydown, key, modifiers, code_point);
  494. if (RefPtr<DOM::Element> focused_element = document->focused_element())
  495. return focused_element->dispatch_event(move(event));
  496. if (RefPtr<HTML::HTMLElement> body = m_browsing_context.active_document()->body())
  497. return body->dispatch_event(move(event));
  498. return document->root().dispatch_event(move(event));
  499. }
  500. bool EventHandler::handle_keyup(KeyCode key, unsigned modifiers, u32 code_point)
  501. {
  502. RefPtr<DOM::Document> document = m_browsing_context.active_document();
  503. if (!document)
  504. return false;
  505. auto event = UIEvents::KeyboardEvent::create_from_platform_event(UIEvents::EventNames::keyup, key, modifiers, code_point);
  506. if (RefPtr<DOM::Element> focused_element = document->focused_element())
  507. return document->focused_element()->dispatch_event(move(event));
  508. if (RefPtr<HTML::HTMLElement> body = document->body())
  509. return body->dispatch_event(move(event));
  510. return document->root().dispatch_event(move(event));
  511. }
  512. void EventHandler::set_mouse_event_tracking_layout_node(Layout::Node* layout_node)
  513. {
  514. if (layout_node)
  515. m_mouse_event_tracking_layout_node = layout_node->make_weak_ptr();
  516. else
  517. m_mouse_event_tracking_layout_node = nullptr;
  518. }
  519. }