EventHandler.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. /*
  2. * Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <LibGUI/Event.h>
  27. #include <LibGUI/Window.h>
  28. #include <LibWeb/DOM/Range.h>
  29. #include <LibWeb/DOM/Text.h>
  30. #include <LibWeb/HTML/HTMLAnchorElement.h>
  31. #include <LibWeb/HTML/HTMLIFrameElement.h>
  32. #include <LibWeb/HTML/HTMLImageElement.h>
  33. #include <LibWeb/InProcessWebView.h>
  34. #include <LibWeb/Layout/InitialContainingBlockBox.h>
  35. #include <LibWeb/Page/EventHandler.h>
  36. #include <LibWeb/Page/Frame.h>
  37. #include <LibWeb/UIEvents/EventNames.h>
  38. #include <LibWeb/UIEvents/MouseEvent.h>
  39. namespace Web {
  40. static Gfx::StandardCursor cursor_css_to_gfx(Optional<CSS::Cursor> cursor)
  41. {
  42. if (!cursor.has_value()) {
  43. return Gfx::StandardCursor::None;
  44. }
  45. switch (cursor.value()) {
  46. case CSS::Cursor::Crosshair:
  47. case CSS::Cursor::Cell:
  48. return Gfx::StandardCursor::Crosshair;
  49. case CSS::Cursor::Grab:
  50. case CSS::Cursor::Grabbing:
  51. return Gfx::StandardCursor::Drag;
  52. case CSS::Cursor::Pointer:
  53. return Gfx::StandardCursor::Hand;
  54. case CSS::Cursor::Help:
  55. return Gfx::StandardCursor::Help;
  56. case CSS::Cursor::None:
  57. return Gfx::StandardCursor::Hidden;
  58. case CSS::Cursor::Text:
  59. case CSS::Cursor::VerticalText:
  60. return Gfx::StandardCursor::IBeam;
  61. case CSS::Cursor::Move:
  62. case CSS::Cursor::AllScroll:
  63. return Gfx::StandardCursor::Move;
  64. case CSS::Cursor::Progress:
  65. case CSS::Cursor::Wait:
  66. return Gfx::StandardCursor::Wait;
  67. case CSS::Cursor::ColResize:
  68. return Gfx::StandardCursor::ResizeColumn;
  69. case CSS::Cursor::EResize:
  70. case CSS::Cursor::WResize:
  71. case CSS::Cursor::EwResize:
  72. return Gfx::StandardCursor::ResizeHorizontal;
  73. case CSS::Cursor::RowResize:
  74. return Gfx::StandardCursor::ResizeRow;
  75. case CSS::Cursor::NResize:
  76. case CSS::Cursor::SResize:
  77. case CSS::Cursor::NsResize:
  78. return Gfx::StandardCursor::ResizeVertical;
  79. case CSS::Cursor::NeResize:
  80. case CSS::Cursor::SwResize:
  81. case CSS::Cursor::NeswResize:
  82. return Gfx::StandardCursor::ResizeDiagonalBLTR;
  83. case CSS::Cursor::NwResize:
  84. case CSS::Cursor::SeResize:
  85. case CSS::Cursor::NwseResize:
  86. return Gfx::StandardCursor::ResizeDiagonalTLBR;
  87. default:
  88. return Gfx::StandardCursor::None;
  89. }
  90. }
  91. static Gfx::IntPoint compute_mouse_event_offset(const Gfx::IntPoint& position, const Layout::Node& layout_node)
  92. {
  93. auto top_left_of_layout_node = layout_node.box_type_agnostic_position();
  94. return {
  95. position.x() - static_cast<int>(top_left_of_layout_node.x()),
  96. position.y() - static_cast<int>(top_left_of_layout_node.y())
  97. };
  98. }
  99. EventHandler::EventHandler(Badge<Frame>, Frame& frame)
  100. : m_frame(frame)
  101. , m_edit_event_handler(make<EditEventHandler>(frame))
  102. {
  103. }
  104. EventHandler::~EventHandler()
  105. {
  106. }
  107. const Layout::InitialContainingBlockBox* EventHandler::layout_root() const
  108. {
  109. if (!m_frame.document())
  110. return nullptr;
  111. return m_frame.document()->layout_node();
  112. }
  113. Layout::InitialContainingBlockBox* EventHandler::layout_root()
  114. {
  115. if (!m_frame.document())
  116. return nullptr;
  117. return m_frame.document()->layout_node();
  118. }
  119. bool EventHandler::handle_mousewheel(const Gfx::IntPoint& position, unsigned int buttons, unsigned int modifiers, int wheel_delta)
  120. {
  121. if (!layout_root())
  122. return false;
  123. // FIXME: Support wheel events in subframes.
  124. auto result = layout_root()->hit_test(position, Layout::HitTestType::Exact);
  125. if (result.layout_node) {
  126. if (result.layout_node->handle_mousewheel({}, position, buttons, modifiers, wheel_delta))
  127. return true;
  128. }
  129. if (auto* page = m_frame.page()) {
  130. page->client().page_did_request_scroll(wheel_delta);
  131. return true;
  132. }
  133. return false;
  134. }
  135. bool EventHandler::handle_mouseup(const Gfx::IntPoint& position, unsigned button, unsigned modifiers)
  136. {
  137. if (!layout_root())
  138. return false;
  139. if (m_mouse_event_tracking_layout_node) {
  140. m_mouse_event_tracking_layout_node->handle_mouseup({}, position, button, modifiers);
  141. return true;
  142. }
  143. bool handled_event = false;
  144. auto result = layout_root()->hit_test(position, Layout::HitTestType::Exact);
  145. if (result.layout_node && result.layout_node->wants_mouse_events()) {
  146. result.layout_node->handle_mouseup({}, position, button, modifiers);
  147. // Things may have changed as a consequence of Layout::Node::handle_mouseup(). Hit test again.
  148. if (!layout_root())
  149. return true;
  150. result = layout_root()->hit_test(position, Layout::HitTestType::Exact);
  151. }
  152. if (result.layout_node && result.layout_node->dom_node()) {
  153. RefPtr<DOM::Node> node = result.layout_node->dom_node();
  154. if (is<HTML::HTMLIFrameElement>(*node)) {
  155. if (auto* subframe = downcast<HTML::HTMLIFrameElement>(*node).content_frame())
  156. return subframe->event_handler().handle_mouseup(position.translated(compute_mouse_event_offset({}, *result.layout_node)), button, modifiers);
  157. return false;
  158. }
  159. auto offset = compute_mouse_event_offset(position, *result.layout_node);
  160. node->dispatch_event(UIEvents::MouseEvent::create(UIEvents::EventNames::mouseup, offset.x(), offset.y(), position.x(), position.y()));
  161. handled_event = true;
  162. }
  163. if (button == GUI::MouseButton::Left)
  164. m_in_mouse_selection = false;
  165. return handled_event;
  166. }
  167. bool EventHandler::handle_mousedown(const Gfx::IntPoint& position, unsigned button, unsigned modifiers)
  168. {
  169. if (!layout_root())
  170. return false;
  171. if (m_mouse_event_tracking_layout_node) {
  172. m_mouse_event_tracking_layout_node->handle_mousedown({}, position, button, modifiers);
  173. return true;
  174. }
  175. NonnullRefPtr document = *m_frame.document();
  176. RefPtr<DOM::Node> node;
  177. {
  178. auto result = layout_root()->hit_test(position, Layout::HitTestType::Exact);
  179. if (!result.layout_node)
  180. return false;
  181. node = result.layout_node->dom_node();
  182. document->set_hovered_node(node);
  183. if (result.layout_node->wants_mouse_events()) {
  184. result.layout_node->handle_mousedown({}, position, button, modifiers);
  185. return true;
  186. }
  187. if (!node)
  188. return false;
  189. if (is<HTML::HTMLIFrameElement>(*node)) {
  190. if (auto* subframe = downcast<HTML::HTMLIFrameElement>(*node).content_frame())
  191. return subframe->event_handler().handle_mousedown(position.translated(compute_mouse_event_offset({}, *result.layout_node)), button, modifiers);
  192. return false;
  193. }
  194. if (auto* page = m_frame.page())
  195. page->set_focused_frame({}, m_frame);
  196. auto offset = compute_mouse_event_offset(position, *result.layout_node);
  197. node->dispatch_event(UIEvents::MouseEvent::create(UIEvents::EventNames::mousedown, offset.x(), offset.y(), position.x(), position.y()));
  198. }
  199. // NOTE: Dispatching an event may have disturbed the world.
  200. if (!layout_root() || layout_root() != node->document().layout_node())
  201. return true;
  202. if (button == GUI::MouseButton::Right && is<HTML::HTMLImageElement>(*node)) {
  203. auto& image_element = downcast<HTML::HTMLImageElement>(*node);
  204. auto image_url = image_element.document().complete_url(image_element.src());
  205. if (auto* page = m_frame.page())
  206. page->client().page_did_request_image_context_menu(m_frame.to_main_frame_position(position), image_url, "", modifiers, image_element.bitmap());
  207. return true;
  208. }
  209. if (RefPtr<HTML::HTMLAnchorElement> link = node->enclosing_link_element()) {
  210. auto href = link->href();
  211. auto url = document->complete_url(href);
  212. dbgln("Web::EventHandler: Clicking on a link to {}", url);
  213. if (button == GUI::MouseButton::Left) {
  214. if (href.starts_with("javascript:")) {
  215. document->run_javascript(href.substring_view(11, href.length() - 11));
  216. } else if (href.starts_with('#')) {
  217. auto anchor = href.substring_view(1, href.length() - 1);
  218. m_frame.scroll_to_anchor(anchor);
  219. } else {
  220. if (m_frame.is_main_frame()) {
  221. if (auto* page = m_frame.page())
  222. page->client().page_did_click_link(url, link->target(), modifiers);
  223. } else {
  224. // FIXME: Handle different targets!
  225. m_frame.loader().load(url, FrameLoader::Type::Navigation);
  226. }
  227. }
  228. } else if (button == GUI::MouseButton::Right) {
  229. if (auto* page = m_frame.page())
  230. page->client().page_did_request_link_context_menu(m_frame.to_main_frame_position(position), url, link->target(), modifiers);
  231. } else if (button == GUI::MouseButton::Middle) {
  232. if (auto* page = m_frame.page())
  233. page->client().page_did_middle_click_link(url, link->target(), modifiers);
  234. }
  235. } else {
  236. if (button == GUI::MouseButton::Left) {
  237. auto result = layout_root()->hit_test(position, Layout::HitTestType::TextCursor);
  238. if (result.layout_node && result.layout_node->dom_node()) {
  239. m_frame.set_cursor_position(DOM::Position(*result.layout_node->dom_node(), result.index_in_node));
  240. layout_root()->set_selection({ { result.layout_node, result.index_in_node }, {} });
  241. m_in_mouse_selection = true;
  242. }
  243. } else if (button == GUI::MouseButton::Right) {
  244. if (auto* page = m_frame.page())
  245. page->client().page_did_request_context_menu(m_frame.to_main_frame_position(position));
  246. }
  247. }
  248. return true;
  249. }
  250. bool EventHandler::handle_mousemove(const Gfx::IntPoint& position, unsigned buttons, unsigned modifiers)
  251. {
  252. if (!layout_root())
  253. return false;
  254. if (m_mouse_event_tracking_layout_node) {
  255. m_mouse_event_tracking_layout_node->handle_mousemove({}, position, buttons, modifiers);
  256. return true;
  257. }
  258. auto& document = *m_frame.document();
  259. bool hovered_node_changed = false;
  260. bool is_hovering_link = false;
  261. Gfx::StandardCursor hovered_node_cursor = Gfx::StandardCursor::None;
  262. auto result = layout_root()->hit_test(position, Layout::HitTestType::Exact);
  263. const HTML::HTMLAnchorElement* hovered_link_element = nullptr;
  264. if (result.layout_node) {
  265. if (result.layout_node->wants_mouse_events()) {
  266. document.set_hovered_node(result.layout_node->dom_node());
  267. result.layout_node->handle_mousemove({}, position, buttons, modifiers);
  268. // FIXME: It feels a bit aggressive to always update the cursor like this.
  269. if (auto* page = m_frame.page())
  270. page->client().page_did_request_cursor_change(Gfx::StandardCursor::None);
  271. return true;
  272. }
  273. RefPtr<DOM::Node> node = result.layout_node->dom_node();
  274. if (node && is<HTML::HTMLIFrameElement>(*node)) {
  275. if (auto* subframe = downcast<HTML::HTMLIFrameElement>(*node).content_frame())
  276. return subframe->event_handler().handle_mousemove(position.translated(compute_mouse_event_offset({}, *result.layout_node)), buttons, modifiers);
  277. return false;
  278. }
  279. hovered_node_changed = node != document.hovered_node();
  280. document.set_hovered_node(node);
  281. if (node) {
  282. hovered_link_element = node->enclosing_link_element();
  283. if (hovered_link_element)
  284. is_hovering_link = true;
  285. auto cursor = result.layout_node->computed_values().cursor();
  286. if (node->is_text() && cursor == CSS::Cursor::Auto)
  287. hovered_node_cursor = Gfx::StandardCursor::IBeam;
  288. else
  289. hovered_node_cursor = cursor_css_to_gfx(cursor);
  290. auto offset = compute_mouse_event_offset(position, *result.layout_node);
  291. node->dispatch_event(UIEvents::MouseEvent::create(UIEvents::EventNames::mousemove, offset.x(), offset.y(), position.x(), position.y()));
  292. // NOTE: Dispatching an event may have disturbed the world.
  293. if (!layout_root() || layout_root() != node->document().layout_node())
  294. return true;
  295. }
  296. if (m_in_mouse_selection) {
  297. auto hit = layout_root()->hit_test(position, Layout::HitTestType::TextCursor);
  298. if (hit.layout_node && hit.layout_node->dom_node()) {
  299. m_frame.set_cursor_position(DOM::Position(*hit.layout_node->dom_node(), result.index_in_node));
  300. layout_root()->set_selection_end({ hit.layout_node, hit.index_in_node });
  301. }
  302. if (auto* page = m_frame.page())
  303. page->client().page_did_change_selection();
  304. }
  305. }
  306. if (auto* page = m_frame.page()) {
  307. page->client().page_did_request_cursor_change(hovered_node_cursor);
  308. if (hovered_node_changed) {
  309. RefPtr<HTML::HTMLElement> hovered_html_element = document.hovered_node() ? document.hovered_node()->enclosing_html_element_with_attribute(HTML::AttributeNames::title) : nullptr;
  310. if (hovered_html_element && !hovered_html_element->title().is_null()) {
  311. page->client().page_did_enter_tooltip_area(m_frame.to_main_frame_position(position), hovered_html_element->title());
  312. } else {
  313. page->client().page_did_leave_tooltip_area();
  314. }
  315. if (is_hovering_link)
  316. page->client().page_did_hover_link(document.complete_url(hovered_link_element->href()));
  317. else
  318. page->client().page_did_unhover_link();
  319. }
  320. }
  321. return true;
  322. }
  323. bool EventHandler::focus_next_element()
  324. {
  325. if (!m_frame.document())
  326. return false;
  327. auto* element = m_frame.document()->focused_element();
  328. if (!element) {
  329. element = m_frame.document()->first_child_of_type<DOM::Element>();
  330. if (element && element->is_focusable()) {
  331. m_frame.document()->set_focused_element(element);
  332. return true;
  333. }
  334. }
  335. for (element = element->next_element_in_pre_order(); element && !element->is_focusable(); element = element->next_element_in_pre_order())
  336. ;
  337. m_frame.document()->set_focused_element(element);
  338. return element;
  339. }
  340. bool EventHandler::focus_previous_element()
  341. {
  342. // FIXME: Implement Shift-Tab cycling backwards through focusable elements!
  343. return false;
  344. }
  345. bool EventHandler::handle_keydown(KeyCode key, unsigned modifiers, u32 code_point)
  346. {
  347. if (key == KeyCode::Key_Tab) {
  348. if (modifiers & KeyModifier::Mod_Shift)
  349. return focus_previous_element();
  350. else
  351. return focus_next_element();
  352. }
  353. if (layout_root()->selection().is_valid()) {
  354. auto range = layout_root()->selection().to_dom_range()->normalized();
  355. if (range->start_container()->is_editable()) {
  356. m_frame.document()->layout_node()->set_selection({});
  357. // FIXME: This doesn't work for some reason?
  358. m_frame.set_cursor_position({ *range->start_container(), range->start_offset() });
  359. if (key == KeyCode::Key_Backspace || key == KeyCode::Key_Delete) {
  360. m_edit_event_handler->handle_delete(range);
  361. return true;
  362. } else {
  363. m_edit_event_handler->handle_delete(range);
  364. m_edit_event_handler->handle_insert(m_frame.cursor_position(), code_point);
  365. auto new_position = m_frame.cursor_position();
  366. new_position.set_offset(new_position.offset() + 1);
  367. m_frame.set_cursor_position(move(new_position));
  368. return true;
  369. }
  370. }
  371. }
  372. if (m_frame.cursor_position().is_valid() && m_frame.cursor_position().node()->is_editable()) {
  373. if (key == KeyCode::Key_Backspace) {
  374. auto position = m_frame.cursor_position();
  375. if (position.offset() == 0)
  376. TODO();
  377. auto new_position = m_frame.cursor_position();
  378. new_position.set_offset(position.offset() - 1);
  379. m_frame.set_cursor_position(move(new_position));
  380. m_edit_event_handler->handle_delete(DOM::Range::create(*position.node(), position.offset() - 1, *position.node(), position.offset()));
  381. return true;
  382. } else if (key == KeyCode::Key_Delete) {
  383. auto position = m_frame.cursor_position();
  384. if (position.offset() >= downcast<DOM::Text>(position.node())->data().length())
  385. TODO();
  386. m_edit_event_handler->handle_delete(DOM::Range::create(*position.node(), position.offset(), *position.node(), position.offset() + 1));
  387. return true;
  388. } else if (key == KeyCode::Key_Right) {
  389. auto position = m_frame.cursor_position();
  390. if (position.offset() >= downcast<DOM::Text>(position.node())->data().length())
  391. TODO();
  392. auto new_position = m_frame.cursor_position();
  393. new_position.set_offset(position.offset() + 1);
  394. m_frame.set_cursor_position(move(new_position));
  395. return true;
  396. } else if (key == KeyCode::Key_Left) {
  397. auto position = m_frame.cursor_position();
  398. if (position.offset() == 0)
  399. TODO();
  400. auto new_position = m_frame.cursor_position();
  401. new_position.set_offset(new_position.offset() - 1);
  402. m_frame.set_cursor_position(move(new_position));
  403. return true;
  404. } else {
  405. m_edit_event_handler->handle_insert(m_frame.cursor_position(), code_point);
  406. auto new_position = m_frame.cursor_position();
  407. new_position.set_offset(new_position.offset() + 1);
  408. m_frame.set_cursor_position(move(new_position));
  409. return true;
  410. }
  411. }
  412. return false;
  413. }
  414. void EventHandler::set_mouse_event_tracking_layout_node(Layout::Node* layout_node)
  415. {
  416. if (layout_node)
  417. m_mouse_event_tracking_layout_node = layout_node->make_weak_ptr();
  418. else
  419. m_mouse_event_tracking_layout_node = nullptr;
  420. }
  421. }