InProcessWebView.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /*
  2. * Copyright (c) 2018-2022, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/URL.h>
  7. #include <LibCore/MimeData.h>
  8. #include <LibGUI/Application.h>
  9. #include <LibGUI/InputBox.h>
  10. #include <LibGUI/MessageBox.h>
  11. #include <LibGUI/Painter.h>
  12. #include <LibGUI/Scrollbar.h>
  13. #include <LibGUI/Window.h>
  14. #include <LibGfx/ShareableBitmap.h>
  15. #include <LibWeb/HTML/BrowsingContext.h>
  16. #include <LibWeb/HTML/HTMLAnchorElement.h>
  17. #include <LibWeb/HTML/Parser/HTMLParser.h>
  18. #include <LibWeb/InProcessWebView.h>
  19. #include <LibWeb/Layout/InitialContainingBlock.h>
  20. #include <LibWeb/Layout/TextNode.h>
  21. #include <LibWeb/Loader/ResourceLoader.h>
  22. #include <LibWeb/Page/EventHandler.h>
  23. #include <LibWeb/Painting/PaintContext.h>
  24. #include <LibWeb/Painting/Paintable.h>
  25. #include <LibWeb/UIEvents/MouseEvent.h>
  26. REGISTER_WIDGET(Web, InProcessWebView)
  27. namespace Web {
  28. InProcessWebView::InProcessWebView()
  29. : m_page(make<Page>(*this))
  30. {
  31. set_should_hide_unnecessary_scrollbars(true);
  32. set_background_role(ColorRole::Base);
  33. set_focus_policy(GUI::FocusPolicy::StrongFocus);
  34. }
  35. InProcessWebView::~InProcessWebView()
  36. {
  37. }
  38. void InProcessWebView::select_all()
  39. {
  40. page().focused_context().select_all();
  41. update();
  42. }
  43. String InProcessWebView::selected_text() const
  44. {
  45. return page().focused_context().selected_text();
  46. }
  47. void InProcessWebView::set_preferred_color_scheme(CSS::PreferredColorScheme color_scheme)
  48. {
  49. m_preferred_color_scheme = color_scheme;
  50. if (auto* document = page().top_level_browsing_context().active_document())
  51. document->invalidate_style();
  52. }
  53. void InProcessWebView::page_did_layout()
  54. {
  55. VERIFY(layout_root());
  56. VERIFY(layout_root()->m_paint_box);
  57. set_content_size(layout_root()->m_paint_box->content_size().to_type<int>());
  58. }
  59. void InProcessWebView::page_did_change_title(const String& title)
  60. {
  61. if (on_title_change)
  62. on_title_change(title);
  63. }
  64. void InProcessWebView::page_did_set_document_in_top_level_browsing_context(DOM::Document* document)
  65. {
  66. if (on_set_document)
  67. on_set_document(document);
  68. layout_and_sync_size();
  69. scroll_to_top();
  70. update();
  71. }
  72. void InProcessWebView::page_did_start_loading(const AK::URL& url)
  73. {
  74. if (on_load_start)
  75. on_load_start(url);
  76. }
  77. void InProcessWebView::page_did_finish_loading(const AK::URL& url)
  78. {
  79. if (on_load_finish)
  80. on_load_finish(url);
  81. }
  82. void InProcessWebView::page_did_change_selection()
  83. {
  84. update();
  85. }
  86. void InProcessWebView::page_did_request_cursor_change(Gfx::StandardCursor cursor)
  87. {
  88. set_override_cursor(cursor);
  89. }
  90. void InProcessWebView::page_did_request_context_menu(const Gfx::IntPoint& content_position)
  91. {
  92. if (on_context_menu_request)
  93. on_context_menu_request(screen_relative_rect().location().translated(to_widget_position(content_position)));
  94. }
  95. void InProcessWebView::page_did_request_link_context_menu(const Gfx::IntPoint& content_position, const AK::URL& url, [[maybe_unused]] const String& target, [[maybe_unused]] unsigned modifiers)
  96. {
  97. if (on_link_context_menu_request)
  98. on_link_context_menu_request(url, screen_relative_rect().location().translated(to_widget_position(content_position)));
  99. }
  100. void InProcessWebView::page_did_request_image_context_menu(const Gfx::IntPoint& content_position, const AK::URL& url, [[maybe_unused]] const String& target, [[maybe_unused]] unsigned modifiers, const Gfx::Bitmap* bitmap)
  101. {
  102. if (!on_image_context_menu_request)
  103. return;
  104. Gfx::ShareableBitmap shareable_bitmap;
  105. if (bitmap)
  106. shareable_bitmap = bitmap->to_shareable_bitmap();
  107. on_image_context_menu_request(url, screen_relative_rect().location().translated(to_widget_position(content_position)), move(shareable_bitmap));
  108. }
  109. void InProcessWebView::page_did_click_link(const AK::URL& url, const String& target, unsigned modifiers)
  110. {
  111. if (on_link_click)
  112. on_link_click(url, target, modifiers);
  113. }
  114. void InProcessWebView::page_did_middle_click_link(const AK::URL& url, const String& target, unsigned modifiers)
  115. {
  116. if (on_link_middle_click)
  117. on_link_middle_click(url, target, modifiers);
  118. }
  119. void InProcessWebView::page_did_enter_tooltip_area([[maybe_unused]] const Gfx::IntPoint& content_position, const String& title)
  120. {
  121. GUI::Application::the()->show_tooltip(title, nullptr);
  122. }
  123. void InProcessWebView::page_did_leave_tooltip_area()
  124. {
  125. GUI::Application::the()->hide_tooltip();
  126. }
  127. void InProcessWebView::page_did_hover_link(const AK::URL& url)
  128. {
  129. if (on_link_hover)
  130. on_link_hover(url);
  131. }
  132. void InProcessWebView::page_did_unhover_link()
  133. {
  134. if (on_link_hover)
  135. on_link_hover({});
  136. }
  137. void InProcessWebView::page_did_invalidate(const Gfx::IntRect&)
  138. {
  139. update();
  140. }
  141. void InProcessWebView::page_did_change_favicon(const Gfx::Bitmap& bitmap)
  142. {
  143. if (on_favicon_change)
  144. on_favicon_change(bitmap);
  145. }
  146. void InProcessWebView::layout_and_sync_size()
  147. {
  148. if (!document())
  149. return;
  150. bool had_vertical_scrollbar = vertical_scrollbar().is_visible();
  151. bool had_horizontal_scrollbar = horizontal_scrollbar().is_visible();
  152. page().top_level_browsing_context().set_size(available_size());
  153. set_content_size(layout_root()->m_paint_box->content_size().to_type<int>());
  154. // NOTE: If layout caused us to gain or lose scrollbars, we have to lay out again
  155. // since the scrollbars now take up some of the available space.
  156. if (had_vertical_scrollbar != vertical_scrollbar().is_visible() || had_horizontal_scrollbar != horizontal_scrollbar().is_visible()) {
  157. page().top_level_browsing_context().set_size(available_size());
  158. set_content_size(layout_root()->m_paint_box->content_size().to_type<int>());
  159. }
  160. page().top_level_browsing_context().set_viewport_scroll_offset({ horizontal_scrollbar().value(), vertical_scrollbar().value() });
  161. }
  162. void InProcessWebView::resize_event(GUI::ResizeEvent& event)
  163. {
  164. GUI::AbstractScrollableWidget::resize_event(event);
  165. layout_and_sync_size();
  166. }
  167. void InProcessWebView::paint_event(GUI::PaintEvent& event)
  168. {
  169. GUI::Frame::paint_event(event);
  170. GUI::Painter painter(*this);
  171. painter.add_clip_rect(widget_inner_rect());
  172. painter.add_clip_rect(event.rect());
  173. if (!layout_root()) {
  174. painter.fill_rect(event.rect(), palette().color(background_role()));
  175. return;
  176. }
  177. painter.translate(frame_thickness(), frame_thickness());
  178. PaintContext context(painter, palette(), { horizontal_scrollbar().value(), vertical_scrollbar().value() });
  179. context.set_should_show_line_box_borders(m_should_show_line_box_borders);
  180. context.set_viewport_rect(viewport_rect_in_content_coordinates());
  181. context.set_has_focus(is_focused());
  182. layout_root()->paint_all_phases(context);
  183. }
  184. void InProcessWebView::mousemove_event(GUI::MouseEvent& event)
  185. {
  186. page().handle_mousemove(to_content_position(event.position()), event.buttons(), event.modifiers());
  187. GUI::AbstractScrollableWidget::mousemove_event(event);
  188. }
  189. void InProcessWebView::mousedown_event(GUI::MouseEvent& event)
  190. {
  191. page().handle_mousedown(to_content_position(event.position()), event.button(), event.modifiers());
  192. GUI::AbstractScrollableWidget::mousedown_event(event);
  193. }
  194. void InProcessWebView::mouseup_event(GUI::MouseEvent& event)
  195. {
  196. page().handle_mouseup(to_content_position(event.position()), event.button(), event.modifiers());
  197. GUI::AbstractScrollableWidget::mouseup_event(event);
  198. }
  199. void InProcessWebView::mousewheel_event(GUI::MouseEvent& event)
  200. {
  201. page().handle_mousewheel(to_content_position(event.position()), event.button(), event.modifiers(), event.wheel_delta_x(), event.wheel_delta_y());
  202. GUI::AbstractScrollableWidget::mousewheel_event(event);
  203. }
  204. void InProcessWebView::keydown_event(GUI::KeyEvent& event)
  205. {
  206. bool page_accepted_event = page().handle_keydown(event.key(), event.modifiers(), event.code_point());
  207. if (event.modifiers() == 0) {
  208. switch (event.key()) {
  209. case Key_Home:
  210. vertical_scrollbar().set_value(0);
  211. break;
  212. case Key_End:
  213. vertical_scrollbar().set_value(vertical_scrollbar().max());
  214. break;
  215. case Key_Down:
  216. vertical_scrollbar().increase_slider_by_steps(1);
  217. break;
  218. case Key_Up:
  219. vertical_scrollbar().decrease_slider_by_steps(1);
  220. break;
  221. case Key_Left:
  222. horizontal_scrollbar().increase_slider_by_steps(1);
  223. break;
  224. case Key_Right:
  225. horizontal_scrollbar().decrease_slider_by_steps(1);
  226. break;
  227. case Key_PageDown:
  228. vertical_scrollbar().increase_slider_by(frame_inner_rect().height());
  229. break;
  230. case Key_PageUp:
  231. vertical_scrollbar().decrease_slider_by(frame_inner_rect().height());
  232. break;
  233. default:
  234. if (!page_accepted_event) {
  235. AbstractScrollableWidget::keydown_event(event);
  236. return;
  237. }
  238. break;
  239. }
  240. }
  241. event.accept();
  242. }
  243. AK::URL InProcessWebView::url() const
  244. {
  245. if (!page().top_level_browsing_context().active_document())
  246. return {};
  247. return page().top_level_browsing_context().active_document()->url();
  248. }
  249. void InProcessWebView::reload()
  250. {
  251. load(url());
  252. }
  253. void InProcessWebView::load_html(StringView html, const AK::URL& url)
  254. {
  255. page().top_level_browsing_context().loader().load_html(html, url);
  256. }
  257. bool InProcessWebView::load(const AK::URL& url)
  258. {
  259. set_override_cursor(Gfx::StandardCursor::None);
  260. return page().top_level_browsing_context().loader().load(url, FrameLoader::Type::Navigation);
  261. }
  262. const Layout::InitialContainingBlock* InProcessWebView::layout_root() const
  263. {
  264. return document() ? document()->layout_node() : nullptr;
  265. }
  266. Layout::InitialContainingBlock* InProcessWebView::layout_root()
  267. {
  268. if (!document())
  269. return nullptr;
  270. return const_cast<Layout::InitialContainingBlock*>(document()->layout_node());
  271. }
  272. void InProcessWebView::page_did_request_scroll_into_view(const Gfx::IntRect& rect)
  273. {
  274. scroll_into_view(rect, true, true);
  275. set_override_cursor(Gfx::StandardCursor::None);
  276. }
  277. void InProcessWebView::load_empty_document()
  278. {
  279. page().top_level_browsing_context().set_active_document(nullptr);
  280. }
  281. DOM::Document* InProcessWebView::document()
  282. {
  283. return page().top_level_browsing_context().active_document();
  284. }
  285. const DOM::Document* InProcessWebView::document() const
  286. {
  287. return page().top_level_browsing_context().active_document();
  288. }
  289. void InProcessWebView::set_document(DOM::Document* document)
  290. {
  291. page().top_level_browsing_context().set_active_document(document);
  292. }
  293. void InProcessWebView::did_scroll()
  294. {
  295. page().top_level_browsing_context().set_viewport_scroll_offset({ horizontal_scrollbar().value(), vertical_scrollbar().value() });
  296. }
  297. void InProcessWebView::drop_event(GUI::DropEvent& event)
  298. {
  299. if (event.mime_data().has_urls()) {
  300. if (on_url_drop) {
  301. on_url_drop(event.mime_data().urls().first());
  302. return;
  303. }
  304. }
  305. AbstractScrollableWidget::drop_event(event);
  306. }
  307. void InProcessWebView::page_did_request_alert(const String& message)
  308. {
  309. GUI::MessageBox::show(window(), message, "Alert", GUI::MessageBox::Type::Information);
  310. }
  311. bool InProcessWebView::page_did_request_confirm(const String& message)
  312. {
  313. auto confirm_result = GUI::MessageBox::show(window(), message, "Confirm", GUI::MessageBox::Type::Warning, GUI::MessageBox::InputType::OKCancel);
  314. return confirm_result == GUI::Dialog::ExecResult::ExecOK;
  315. }
  316. String InProcessWebView::page_did_request_prompt(const String& message, const String& default_)
  317. {
  318. String value { default_ };
  319. if (GUI::InputBox::show(window(), value, message, "Prompt") == GUI::InputBox::ExecOK)
  320. return value;
  321. return {};
  322. }
  323. String InProcessWebView::page_did_request_cookie(const AK::URL& url, Cookie::Source source)
  324. {
  325. if (on_get_cookie)
  326. return on_get_cookie(url, source);
  327. return {};
  328. }
  329. void InProcessWebView::page_did_set_cookie(const AK::URL& url, const Cookie::ParsedCookie& cookie, Cookie::Source source)
  330. {
  331. if (on_set_cookie)
  332. on_set_cookie(url, cookie, source);
  333. }
  334. }