EventHandler.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  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(Gfx::IntPoint const& position, Layout::Node const& 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. Layout::InitialContainingBlock const* 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(Gfx::IntPoint const& 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.has_value() && 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(Gfx::IntPoint const& 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. if (auto result = paint_root()->hit_test(position.to_type<float>(), Painting::HitTestType::Exact); result.has_value())
  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. if (auto result = paint_root()->hit_test(position.to_type<float>(), Painting::HitTestType::Exact); result.has_value())
  151. paintable = result->paintable;
  152. }
  153. if (paintable) {
  154. RefPtr<DOM::Node> node = paintable->mouse_event_target();
  155. if (!node)
  156. node = paintable->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. bool run_activation_behavior = true;
  167. if (node.ptr() == m_mousedown_target && button == GUI::MouseButton::Primary) {
  168. run_activation_behavior = node->dispatch_event(UIEvents::MouseEvent::create(UIEvents::EventNames::click, offset.x(), offset.y(), position.x(), position.y()));
  169. }
  170. if (run_activation_behavior) {
  171. // FIXME: This is ad-hoc and incorrect. The reason this exists is
  172. // because we are missing browsing context navigation:
  173. //
  174. // https://html.spec.whatwg.org/multipage/browsing-the-web.html#navigate
  175. //
  176. // Additionally, we currently cannot spawn a new top-level
  177. // browsing context for new tab operations, because the new
  178. // top-level browsing context would be in another process. To
  179. // fix this, there needs to be some way to be able to
  180. // communicate with browsing contexts in remote WebContent
  181. // processes, and then step 8 of this algorithm needs to be
  182. // implemented in BrowsingContext::choose_a_browsing_context:
  183. //
  184. // https://html.spec.whatwg.org/multipage/browsers.html#the-rules-for-choosing-a-browsing-context-given-a-browsing-context-name
  185. if (RefPtr<HTML::HTMLAnchorElement> link = node->enclosing_link_element()) {
  186. NonnullRefPtr document = *m_browsing_context.active_document();
  187. auto href = link->href();
  188. auto url = document->parse_url(href);
  189. dbgln("Web::EventHandler: Clicking on a link to {}", url);
  190. if (button == GUI::MouseButton::Primary) {
  191. if (href.starts_with("javascript:")) {
  192. document->run_javascript(href.substring_view(11, href.length() - 11));
  193. } else if (!url.fragment().is_null() && url.equals(document->url(), AK::URL::ExcludeFragment::Yes)) {
  194. m_browsing_context.scroll_to_anchor(url.fragment());
  195. } else {
  196. if (m_browsing_context.is_top_level()) {
  197. if (auto* page = m_browsing_context.page())
  198. page->client().page_did_click_link(url, link->target(), modifiers);
  199. }
  200. }
  201. } else if (button == GUI::MouseButton::Middle) {
  202. if (auto* page = m_browsing_context.page())
  203. page->client().page_did_middle_click_link(url, link->target(), modifiers);
  204. } else if (button == GUI::MouseButton::Secondary) {
  205. if (auto* page = m_browsing_context.page())
  206. page->client().page_did_request_link_context_menu(m_browsing_context.to_top_level_position(position), url, link->target(), modifiers);
  207. }
  208. } else if (button == GUI::MouseButton::Secondary) {
  209. if (is<HTML::HTMLImageElement>(*node)) {
  210. auto& image_element = verify_cast<HTML::HTMLImageElement>(*node);
  211. auto image_url = image_element.document().parse_url(image_element.src());
  212. if (auto* page = m_browsing_context.page())
  213. page->client().page_did_request_image_context_menu(m_browsing_context.to_top_level_position(position), image_url, "", modifiers, image_element.bitmap());
  214. } else if (auto* page = m_browsing_context.page()) {
  215. page->client().page_did_request_context_menu(m_browsing_context.to_top_level_position(position));
  216. }
  217. }
  218. }
  219. }
  220. }
  221. if (button == GUI::MouseButton::Primary)
  222. m_in_mouse_selection = false;
  223. return handled_event;
  224. }
  225. bool EventHandler::handle_mousedown(Gfx::IntPoint const& position, unsigned button, unsigned modifiers)
  226. {
  227. if (m_browsing_context.active_document())
  228. m_browsing_context.active_document()->update_layout();
  229. if (!paint_root())
  230. return false;
  231. NonnullRefPtr document = *m_browsing_context.active_document();
  232. RefPtr<DOM::Node> node;
  233. {
  234. // TODO: Allow selecting element behind if one on top has pointer-events set to none.
  235. RefPtr<Painting::Paintable> paintable;
  236. if (m_mouse_event_tracking_layout_node) {
  237. paintable = m_mouse_event_tracking_layout_node->paintable();
  238. } else {
  239. auto result = paint_root()->hit_test(position.to_type<float>(), Painting::HitTestType::Exact);
  240. if (!result.has_value())
  241. return false;
  242. paintable = result->paintable;
  243. }
  244. auto pointer_events = paintable->computed_values().pointer_events();
  245. // FIXME: Handle other values for pointer-events.
  246. if (pointer_events == CSS::PointerEvents::None)
  247. return false;
  248. node = paintable->mouse_event_target();
  249. if (!node)
  250. node = paintable->dom_node();
  251. document->set_hovered_node(node);
  252. if (paintable->wants_mouse_events()) {
  253. if (paintable->handle_mousedown({}, position, button, modifiers) == Painting::Paintable::DispatchEventOfSameName::No)
  254. return false;
  255. }
  256. if (!node)
  257. return false;
  258. if (is<HTML::HTMLIFrameElement>(*node)) {
  259. if (auto* nested_browsing_context = static_cast<HTML::HTMLIFrameElement&>(*node).nested_browsing_context())
  260. return nested_browsing_context->event_handler().handle_mousedown(position.translated(compute_mouse_event_offset({}, paintable->layout_node())), button, modifiers);
  261. return false;
  262. }
  263. if (auto* page = m_browsing_context.page())
  264. page->set_focused_browsing_context({}, m_browsing_context);
  265. m_mousedown_target = node;
  266. auto offset = compute_mouse_event_offset(position, paintable->layout_node());
  267. node->dispatch_event(UIEvents::MouseEvent::create(UIEvents::EventNames::mousedown, offset.x(), offset.y(), position.x(), position.y()));
  268. }
  269. // NOTE: Dispatching an event may have disturbed the world.
  270. if (!paint_root() || paint_root() != node->document().paint_box())
  271. return true;
  272. if (button == GUI::MouseButton::Primary) {
  273. if (auto result = paint_root()->hit_test(position.to_type<float>(), Painting::HitTestType::TextCursor); result.has_value()) {
  274. auto paintable = result->paintable;
  275. if (paintable->dom_node()) {
  276. // See if we want to focus something.
  277. bool did_focus_something = false;
  278. for (auto candidate = node; candidate; candidate = candidate->parent()) {
  279. if (candidate->is_focusable()) {
  280. document->set_focused_element(verify_cast<DOM::Element>(candidate.ptr()));
  281. did_focus_something = true;
  282. break;
  283. }
  284. }
  285. // If we didn't focus anything, place the document text cursor at the mouse position.
  286. // FIXME: This is all rather strange. Find a better solution.
  287. if (!did_focus_something) {
  288. m_browsing_context.set_cursor_position(DOM::Position(*paintable->dom_node(), result->index_in_node));
  289. layout_root()->set_selection({ { paintable->layout_node(), result->index_in_node }, {} });
  290. m_in_mouse_selection = true;
  291. }
  292. }
  293. }
  294. }
  295. return true;
  296. }
  297. bool EventHandler::handle_mousemove(Gfx::IntPoint const& position, unsigned buttons, unsigned modifiers)
  298. {
  299. if (m_browsing_context.active_document())
  300. m_browsing_context.active_document()->update_layout();
  301. if (!paint_root())
  302. return false;
  303. auto& document = *m_browsing_context.active_document();
  304. bool hovered_node_changed = false;
  305. bool is_hovering_link = false;
  306. Gfx::StandardCursor hovered_node_cursor = Gfx::StandardCursor::None;
  307. RefPtr<Painting::Paintable> paintable;
  308. Optional<int> start_index;
  309. if (m_mouse_event_tracking_layout_node) {
  310. paintable = m_mouse_event_tracking_layout_node->paintable();
  311. } else {
  312. if (auto result = paint_root()->hit_test(position.to_type<float>(), Painting::HitTestType::Exact); result.has_value()) {
  313. paintable = result->paintable;
  314. start_index = result->index_in_node;
  315. }
  316. }
  317. const HTML::HTMLAnchorElement* hovered_link_element = nullptr;
  318. if (paintable) {
  319. if (paintable->wants_mouse_events()) {
  320. document.set_hovered_node(paintable->dom_node());
  321. if (paintable->handle_mousemove({}, position, buttons, modifiers) == Painting::Paintable::DispatchEventOfSameName::No)
  322. return false;
  323. // FIXME: It feels a bit aggressive to always update the cursor like this.
  324. if (auto* page = m_browsing_context.page())
  325. page->client().page_did_request_cursor_change(Gfx::StandardCursor::None);
  326. }
  327. RefPtr<DOM::Node> node = paintable->mouse_event_target();
  328. if (!node)
  329. node = paintable->dom_node();
  330. if (node && is<HTML::HTMLIFrameElement>(*node)) {
  331. if (auto* nested_browsing_context = static_cast<HTML::HTMLIFrameElement&>(*node).nested_browsing_context())
  332. return nested_browsing_context->event_handler().handle_mousemove(position.translated(compute_mouse_event_offset({}, paintable->layout_node())), buttons, modifiers);
  333. return false;
  334. }
  335. auto pointer_events = paintable->computed_values().pointer_events();
  336. // FIXME: Handle other values for pointer-events.
  337. if (pointer_events == CSS::PointerEvents::None)
  338. return false;
  339. hovered_node_changed = node != document.hovered_node();
  340. document.set_hovered_node(node);
  341. if (node) {
  342. hovered_link_element = node->enclosing_link_element();
  343. if (hovered_link_element)
  344. is_hovering_link = true;
  345. if (node->is_text()) {
  346. auto cursor = paintable->computed_values().cursor();
  347. if (cursor == CSS::Cursor::Auto)
  348. hovered_node_cursor = Gfx::StandardCursor::IBeam;
  349. else
  350. hovered_node_cursor = cursor_css_to_gfx(cursor);
  351. } else if (node->is_element()) {
  352. auto cursor = paintable->computed_values().cursor();
  353. if (cursor == CSS::Cursor::Auto)
  354. hovered_node_cursor = Gfx::StandardCursor::Arrow;
  355. else
  356. hovered_node_cursor = cursor_css_to_gfx(cursor);
  357. }
  358. auto offset = compute_mouse_event_offset(position, paintable->layout_node());
  359. node->dispatch_event(UIEvents::MouseEvent::create(UIEvents::EventNames::mousemove, offset.x(), offset.y(), position.x(), position.y()));
  360. // NOTE: Dispatching an event may have disturbed the world.
  361. if (!paint_root() || paint_root() != node->document().paint_box())
  362. return true;
  363. }
  364. if (m_in_mouse_selection) {
  365. auto hit = paint_root()->hit_test(position.to_type<float>(), Painting::HitTestType::TextCursor);
  366. if (start_index.has_value() && hit.has_value() && hit->dom_node()) {
  367. m_browsing_context.set_cursor_position(DOM::Position(*hit->dom_node(), *start_index));
  368. layout_root()->set_selection_end({ hit->paintable->layout_node(), hit->index_in_node });
  369. }
  370. if (auto* page = m_browsing_context.page())
  371. page->client().page_did_change_selection();
  372. }
  373. }
  374. if (auto* page = m_browsing_context.page()) {
  375. page->client().page_did_request_cursor_change(hovered_node_cursor);
  376. if (hovered_node_changed) {
  377. RefPtr<HTML::HTMLElement> hovered_html_element = document.hovered_node() ? document.hovered_node()->enclosing_html_element_with_attribute(HTML::AttributeNames::title) : nullptr;
  378. if (hovered_html_element && !hovered_html_element->title().is_null()) {
  379. page->client().page_did_enter_tooltip_area(m_browsing_context.to_top_level_position(position), hovered_html_element->title());
  380. } else {
  381. page->client().page_did_leave_tooltip_area();
  382. }
  383. if (is_hovering_link)
  384. page->client().page_did_hover_link(document.parse_url(hovered_link_element->href()));
  385. else
  386. page->client().page_did_unhover_link();
  387. }
  388. }
  389. return true;
  390. }
  391. bool EventHandler::focus_next_element()
  392. {
  393. if (!m_browsing_context.active_document())
  394. return false;
  395. auto* element = m_browsing_context.active_document()->focused_element();
  396. if (!element) {
  397. element = m_browsing_context.active_document()->first_child_of_type<DOM::Element>();
  398. if (element && element->is_focusable()) {
  399. m_browsing_context.active_document()->set_focused_element(element);
  400. return true;
  401. }
  402. }
  403. for (element = element->next_element_in_pre_order(); element && !element->is_focusable(); element = element->next_element_in_pre_order())
  404. ;
  405. m_browsing_context.active_document()->set_focused_element(element);
  406. return element;
  407. }
  408. bool EventHandler::focus_previous_element()
  409. {
  410. if (!m_browsing_context.active_document())
  411. return false;
  412. auto* element = m_browsing_context.active_document()->focused_element();
  413. if (!element) {
  414. element = m_browsing_context.active_document()->last_child_of_type<DOM::Element>();
  415. if (element && element->is_focusable()) {
  416. m_browsing_context.active_document()->set_focused_element(element);
  417. return true;
  418. }
  419. }
  420. for (element = element->previous_element_in_pre_order(); element && !element->is_focusable(); element = element->previous_element_in_pre_order())
  421. ;
  422. m_browsing_context.active_document()->set_focused_element(element);
  423. return element;
  424. }
  425. constexpr bool should_ignore_keydown_event(u32 code_point)
  426. {
  427. // FIXME: There are probably also keys with non-zero code points that should be filtered out.
  428. return code_point == 0 || code_point == 27;
  429. }
  430. bool EventHandler::handle_keydown(KeyCode key, unsigned modifiers, u32 code_point)
  431. {
  432. if (!m_browsing_context.active_document())
  433. return false;
  434. NonnullRefPtr<DOM::Document> document = *m_browsing_context.active_document();
  435. if (!document->layout_node())
  436. return false;
  437. NonnullRefPtr<Layout::InitialContainingBlock> layout_root = *document->layout_node();
  438. if (key == KeyCode::Key_Tab) {
  439. if (modifiers & KeyModifier::Mod_Shift)
  440. return focus_previous_element();
  441. return focus_next_element();
  442. }
  443. if (layout_root->selection().is_valid()) {
  444. auto range = layout_root->selection().to_dom_range()->normalized();
  445. if (range->start_container()->is_editable()) {
  446. layout_root->set_selection({});
  447. // FIXME: This doesn't work for some reason?
  448. m_browsing_context.set_cursor_position({ *range->start_container(), range->start_offset() });
  449. if (key == KeyCode::Key_Backspace || key == KeyCode::Key_Delete) {
  450. m_edit_event_handler->handle_delete(range);
  451. return true;
  452. }
  453. if (!should_ignore_keydown_event(code_point)) {
  454. m_edit_event_handler->handle_delete(range);
  455. m_edit_event_handler->handle_insert(m_browsing_context.cursor_position(), code_point);
  456. m_browsing_context.increment_cursor_position_offset();
  457. return true;
  458. }
  459. }
  460. }
  461. if (m_browsing_context.cursor_position().is_valid() && m_browsing_context.cursor_position().node()->is_editable()) {
  462. if (key == KeyCode::Key_Backspace) {
  463. if (!m_browsing_context.decrement_cursor_position_offset()) {
  464. // FIXME: Move to the previous node and delete the last character there.
  465. return true;
  466. }
  467. m_edit_event_handler->handle_delete_character_after(m_browsing_context.cursor_position());
  468. return true;
  469. }
  470. if (key == KeyCode::Key_Delete) {
  471. if (m_browsing_context.cursor_position().offset_is_at_end_of_node()) {
  472. // FIXME: Move to the next node and delete the first character there.
  473. return true;
  474. }
  475. m_edit_event_handler->handle_delete_character_after(m_browsing_context.cursor_position());
  476. return true;
  477. }
  478. if (key == KeyCode::Key_Right) {
  479. if (!m_browsing_context.increment_cursor_position_offset()) {
  480. // FIXME: Move to the next node.
  481. }
  482. return true;
  483. }
  484. if (key == KeyCode::Key_Left) {
  485. if (!m_browsing_context.decrement_cursor_position_offset()) {
  486. // FIXME: Move to the previous node.
  487. }
  488. return true;
  489. }
  490. if (key == KeyCode::Key_Home) {
  491. auto& node = *static_cast<DOM::Text*>(const_cast<DOM::Node*>(m_browsing_context.cursor_position().node()));
  492. m_browsing_context.set_cursor_position(DOM::Position { node, 0 });
  493. return true;
  494. }
  495. if (key == KeyCode::Key_End) {
  496. auto& node = *static_cast<DOM::Text*>(const_cast<DOM::Node*>(m_browsing_context.cursor_position().node()));
  497. m_browsing_context.set_cursor_position(DOM::Position { node, (unsigned)node.data().length() });
  498. return true;
  499. }
  500. if (!should_ignore_keydown_event(code_point)) {
  501. m_edit_event_handler->handle_insert(m_browsing_context.cursor_position(), code_point);
  502. m_browsing_context.increment_cursor_position_offset();
  503. return true;
  504. }
  505. // NOTE: Because modifier keys should be ignored, we need to return true.
  506. return true;
  507. }
  508. auto event = UIEvents::KeyboardEvent::create_from_platform_event(UIEvents::EventNames::keydown, key, modifiers, code_point);
  509. if (RefPtr<DOM::Element> focused_element = document->focused_element())
  510. return focused_element->dispatch_event(move(event));
  511. if (RefPtr<HTML::HTMLElement> body = m_browsing_context.active_document()->body())
  512. return body->dispatch_event(move(event));
  513. return document->root().dispatch_event(move(event));
  514. }
  515. bool EventHandler::handle_keyup(KeyCode key, unsigned modifiers, u32 code_point)
  516. {
  517. RefPtr<DOM::Document> document = m_browsing_context.active_document();
  518. if (!document)
  519. return false;
  520. auto event = UIEvents::KeyboardEvent::create_from_platform_event(UIEvents::EventNames::keyup, key, modifiers, code_point);
  521. if (RefPtr<DOM::Element> focused_element = document->focused_element())
  522. return document->focused_element()->dispatch_event(move(event));
  523. if (RefPtr<HTML::HTMLElement> body = document->body())
  524. return body->dispatch_event(move(event));
  525. return document->root().dispatch_event(move(event));
  526. }
  527. void EventHandler::set_mouse_event_tracking_layout_node(Layout::Node* layout_node)
  528. {
  529. if (layout_node)
  530. m_mouse_event_tracking_layout_node = layout_node->make_weak_ptr();
  531. else
  532. m_mouse_event_tracking_layout_node = nullptr;
  533. }
  534. }