WebContentView.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  1. /*
  2. * Copyright (c) 2022-2023, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2023, Linus Groh <linusg@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include "WebContentView.h"
  8. #include "StringUtils.h"
  9. #include <AK/Assertions.h>
  10. #include <AK/ByteBuffer.h>
  11. #include <AK/Format.h>
  12. #include <AK/HashTable.h>
  13. #include <AK/LexicalPath.h>
  14. #include <AK/NonnullOwnPtr.h>
  15. #include <AK/StringBuilder.h>
  16. #include <AK/Types.h>
  17. #include <Kernel/API/KeyCode.h>
  18. #include <Ladybird/HelperProcess.h>
  19. #include <Ladybird/Utilities.h>
  20. #include <LibCore/ArgsParser.h>
  21. #include <LibCore/EventLoop.h>
  22. #include <LibCore/System.h>
  23. #include <LibCore/Timer.h>
  24. #include <LibGfx/Bitmap.h>
  25. #include <LibGfx/Font/FontDatabase.h>
  26. #include <LibGfx/ImageFormats/PNGWriter.h>
  27. #include <LibGfx/Painter.h>
  28. #include <LibGfx/Palette.h>
  29. #include <LibGfx/Rect.h>
  30. #include <LibGfx/SystemTheme.h>
  31. #include <LibMain/Main.h>
  32. #include <LibWeb/Crypto/Crypto.h>
  33. #include <LibWeb/Loader/ContentFilter.h>
  34. #include <LibWebView/WebContentClient.h>
  35. #include <QApplication>
  36. #include <QCursor>
  37. #include <QGuiApplication>
  38. #include <QIcon>
  39. #include <QLineEdit>
  40. #include <QMimeData>
  41. #include <QMouseEvent>
  42. #include <QPaintEvent>
  43. #include <QPainter>
  44. #include <QPalette>
  45. #include <QScrollBar>
  46. #include <QTextEdit>
  47. #include <QTimer>
  48. #include <QToolTip>
  49. namespace Ladybird {
  50. bool is_using_dark_system_theme(QWidget&);
  51. WebContentView::WebContentView(StringView webdriver_content_ipc_path, WebView::EnableCallgrindProfiling enable_callgrind_profiling, UseLagomNetworking use_lagom_networking)
  52. : m_use_lagom_networking(use_lagom_networking)
  53. , m_webdriver_content_ipc_path(webdriver_content_ipc_path)
  54. {
  55. setMouseTracking(true);
  56. setAcceptDrops(true);
  57. setFocusPolicy(Qt::FocusPolicy::StrongFocus);
  58. m_device_pixel_ratio = devicePixelRatio();
  59. m_inverse_pixel_scaling_ratio = 1.0 / m_device_pixel_ratio;
  60. verticalScrollBar()->setSingleStep(24);
  61. horizontalScrollBar()->setSingleStep(24);
  62. QObject::connect(verticalScrollBar(), &QScrollBar::valueChanged, [this](int) {
  63. update_viewport_rect();
  64. });
  65. QObject::connect(horizontalScrollBar(), &QScrollBar::valueChanged, [this](int) {
  66. update_viewport_rect();
  67. });
  68. create_client(enable_callgrind_profiling);
  69. on_did_layout = [this](auto content_size) {
  70. verticalScrollBar()->setMinimum(0);
  71. verticalScrollBar()->setMaximum(content_size.height() - m_viewport_rect.height());
  72. verticalScrollBar()->setPageStep(m_viewport_rect.height());
  73. horizontalScrollBar()->setMinimum(0);
  74. horizontalScrollBar()->setMaximum(content_size.width() - m_viewport_rect.width());
  75. horizontalScrollBar()->setPageStep(m_viewport_rect.width());
  76. };
  77. on_ready_to_paint = [this]() {
  78. viewport()->update();
  79. };
  80. on_scroll_by_delta = [this](auto x_delta, auto y_delta) {
  81. horizontalScrollBar()->setValue(max(0, horizontalScrollBar()->value() + x_delta));
  82. verticalScrollBar()->setValue(max(0, verticalScrollBar()->value() + y_delta));
  83. };
  84. on_scroll_to_point = [this](auto position) {
  85. horizontalScrollBar()->setValue(position.x());
  86. verticalScrollBar()->setValue(position.y());
  87. };
  88. on_scroll_into_view = [this](auto rect) {
  89. if (m_viewport_rect.contains(rect))
  90. return;
  91. if (rect.top() < m_viewport_rect.top())
  92. verticalScrollBar()->setValue(rect.top());
  93. else if (rect.top() > m_viewport_rect.top() && rect.bottom() > m_viewport_rect.bottom())
  94. verticalScrollBar()->setValue(rect.bottom() - m_viewport_rect.height());
  95. };
  96. on_cursor_change = [this](auto cursor) {
  97. update_cursor(cursor);
  98. };
  99. }
  100. WebContentView::~WebContentView() = default;
  101. unsigned get_button_from_qt_event(QSinglePointEvent const& event)
  102. {
  103. if (event.button() == Qt::MouseButton::LeftButton)
  104. return 1;
  105. if (event.button() == Qt::MouseButton::RightButton)
  106. return 2;
  107. if (event.button() == Qt::MouseButton::MiddleButton)
  108. return 4;
  109. if (event.button() == Qt::MouseButton::BackButton)
  110. return 8;
  111. if (event.buttons() == Qt::MouseButton::ForwardButton)
  112. return 16;
  113. return 0;
  114. }
  115. unsigned get_buttons_from_qt_event(QSinglePointEvent const& event)
  116. {
  117. unsigned buttons = 0;
  118. if (event.buttons() & Qt::MouseButton::LeftButton)
  119. buttons |= 1;
  120. if (event.buttons() & Qt::MouseButton::RightButton)
  121. buttons |= 2;
  122. if (event.buttons() & Qt::MouseButton::MiddleButton)
  123. buttons |= 4;
  124. if (event.buttons() & Qt::MouseButton::BackButton)
  125. buttons |= 8;
  126. if (event.buttons() & Qt::MouseButton::ForwardButton)
  127. buttons |= 16;
  128. return buttons;
  129. }
  130. unsigned get_modifiers_from_qt_mouse_event(QSinglePointEvent const& event)
  131. {
  132. unsigned modifiers = 0;
  133. if (event.modifiers() & Qt::Modifier::ALT)
  134. modifiers |= 1;
  135. if (event.modifiers() & Qt::Modifier::CTRL)
  136. modifiers |= 2;
  137. if (event.modifiers() & Qt::Modifier::SHIFT)
  138. modifiers |= 4;
  139. return modifiers;
  140. }
  141. unsigned get_modifiers_from_qt_keyboard_event(QKeyEvent const& event)
  142. {
  143. auto modifiers = 0;
  144. if (event.modifiers().testFlag(Qt::AltModifier))
  145. modifiers |= KeyModifier::Mod_Alt;
  146. if (event.modifiers().testFlag(Qt::ControlModifier))
  147. modifiers |= KeyModifier::Mod_Ctrl;
  148. if (event.modifiers().testFlag(Qt::MetaModifier))
  149. modifiers |= KeyModifier::Mod_Super;
  150. if (event.modifiers().testFlag(Qt::ShiftModifier))
  151. modifiers |= KeyModifier::Mod_Shift;
  152. if (event.modifiers().testFlag(Qt::AltModifier))
  153. modifiers |= KeyModifier::Mod_AltGr;
  154. if (event.modifiers().testFlag(Qt::KeypadModifier))
  155. modifiers |= KeyModifier::Mod_Keypad;
  156. return modifiers;
  157. }
  158. KeyCode get_keycode_from_qt_keyboard_event(QKeyEvent const& event)
  159. {
  160. struct Mapping {
  161. constexpr Mapping(Qt::Key q, KeyCode s)
  162. : qt_key(q)
  163. , serenity_key(s)
  164. {
  165. }
  166. Qt::Key qt_key;
  167. KeyCode serenity_key;
  168. };
  169. // https://doc.qt.io/qt-6/qt.html#Key-enum
  170. constexpr Mapping mappings[] = {
  171. { Qt::Key_0, Key_0 },
  172. { Qt::Key_1, Key_1 },
  173. { Qt::Key_2, Key_2 },
  174. { Qt::Key_3, Key_3 },
  175. { Qt::Key_4, Key_4 },
  176. { Qt::Key_5, Key_5 },
  177. { Qt::Key_6, Key_6 },
  178. { Qt::Key_7, Key_7 },
  179. { Qt::Key_8, Key_8 },
  180. { Qt::Key_9, Key_9 },
  181. { Qt::Key_A, Key_A },
  182. { Qt::Key_Alt, Key_Alt },
  183. { Qt::Key_Ampersand, Key_Ampersand },
  184. { Qt::Key_Apostrophe, Key_Apostrophe },
  185. { Qt::Key_AsciiCircum, Key_Circumflex },
  186. { Qt::Key_AsciiTilde, Key_Tilde },
  187. { Qt::Key_Asterisk, Key_Asterisk },
  188. { Qt::Key_At, Key_AtSign },
  189. { Qt::Key_B, Key_B },
  190. { Qt::Key_Backslash, Key_Backslash },
  191. { Qt::Key_Backspace, Key_Backspace },
  192. { Qt::Key_Bar, Key_Pipe },
  193. { Qt::Key_BraceLeft, Key_LeftBrace },
  194. { Qt::Key_BraceRight, Key_RightBrace },
  195. { Qt::Key_BracketLeft, Key_LeftBracket },
  196. { Qt::Key_BracketRight, Key_RightBracket },
  197. { Qt::Key_C, Key_C },
  198. { Qt::Key_CapsLock, Key_CapsLock },
  199. { Qt::Key_Colon, Key_Colon },
  200. { Qt::Key_Comma, Key_Comma },
  201. { Qt::Key_Control, Key_Control },
  202. { Qt::Key_D, Key_D },
  203. { Qt::Key_Delete, Key_Delete },
  204. { Qt::Key_Dollar, Key_Dollar },
  205. { Qt::Key_Down, Key_Down },
  206. { Qt::Key_E, Key_E },
  207. { Qt::Key_End, Key_End },
  208. { Qt::Key_Equal, Key_Equal },
  209. { Qt::Key_Enter, Key_Return },
  210. { Qt::Key_Escape, Key_Escape },
  211. { Qt::Key_Exclam, Key_ExclamationPoint },
  212. { Qt::Key_exclamdown, Key_ExclamationPoint },
  213. { Qt::Key_F, Key_F },
  214. { Qt::Key_F1, Key_F1 },
  215. { Qt::Key_F10, Key_F10 },
  216. { Qt::Key_F11, Key_F11 },
  217. { Qt::Key_F12, Key_F12 },
  218. { Qt::Key_F2, Key_F2 },
  219. { Qt::Key_F3, Key_F3 },
  220. { Qt::Key_F4, Key_F4 },
  221. { Qt::Key_F5, Key_F5 },
  222. { Qt::Key_F6, Key_F6 },
  223. { Qt::Key_F7, Key_F7 },
  224. { Qt::Key_F8, Key_F8 },
  225. { Qt::Key_F9, Key_F9 },
  226. { Qt::Key_G, Key_G },
  227. { Qt::Key_Greater, Key_GreaterThan },
  228. { Qt::Key_H, Key_H },
  229. { Qt::Key_Home, Key_Home },
  230. { Qt::Key_I, Key_I },
  231. { Qt::Key_Insert, Key_Insert },
  232. { Qt::Key_J, Key_J },
  233. { Qt::Key_K, Key_K },
  234. { Qt::Key_L, Key_L },
  235. { Qt::Key_Left, Key_Left },
  236. { Qt::Key_Less, Key_LessThan },
  237. { Qt::Key_M, Key_M },
  238. { Qt::Key_Menu, Key_Menu },
  239. { Qt::Key_Meta, Key_Super },
  240. { Qt::Key_Minus, Key_Minus },
  241. { Qt::Key_N, Key_N },
  242. { Qt::Key_NumberSign, Key_Hashtag },
  243. { Qt::Key_NumLock, Key_NumLock },
  244. { Qt::Key_O, Key_O },
  245. { Qt::Key_P, Key_P },
  246. { Qt::Key_PageDown, Key_PageDown },
  247. { Qt::Key_PageUp, Key_PageUp },
  248. { Qt::Key_ParenLeft, Key_LeftParen },
  249. { Qt::Key_ParenRight, Key_RightParen },
  250. { Qt::Key_Percent, Key_Percent },
  251. { Qt::Key_Period, Key_Period },
  252. { Qt::Key_Plus, Key_Plus },
  253. { Qt::Key_Print, Key_PrintScreen },
  254. { Qt::Key_Q, Key_Q },
  255. { Qt::Key_Question, Key_QuestionMark },
  256. { Qt::Key_QuoteDbl, Key_DoubleQuote },
  257. { Qt::Key_QuoteLeft, Key_Backtick },
  258. { Qt::Key_R, Key_R },
  259. { Qt::Key_Return, Key_Return },
  260. { Qt::Key_Right, Key_Right },
  261. { Qt::Key_S, Key_S },
  262. { Qt::Key_ScrollLock, Key_ScrollLock },
  263. { Qt::Key_Semicolon, Key_Semicolon },
  264. { Qt::Key_Shift, Key_LeftShift },
  265. { Qt::Key_Slash, Key_Slash },
  266. { Qt::Key_Space, Key_Space },
  267. { Qt::Key_Super_L, Key_Super },
  268. { Qt::Key_Super_R, Key_Super },
  269. { Qt::Key_SysReq, Key_SysRq },
  270. { Qt::Key_T, Key_T },
  271. { Qt::Key_Tab, Key_Tab },
  272. { Qt::Key_U, Key_U },
  273. { Qt::Key_Underscore, Key_Underscore },
  274. { Qt::Key_Up, Key_Up },
  275. { Qt::Key_V, Key_V },
  276. { Qt::Key_W, Key_W },
  277. { Qt::Key_X, Key_X },
  278. { Qt::Key_Y, Key_Y },
  279. { Qt::Key_Z, Key_Z },
  280. };
  281. for (auto const& mapping : mappings) {
  282. if (event.key() == mapping.qt_key)
  283. return mapping.serenity_key;
  284. }
  285. return Key_Invalid;
  286. }
  287. void WebContentView::wheelEvent(QWheelEvent* event)
  288. {
  289. if (!event->modifiers().testFlag(Qt::ControlModifier)) {
  290. Gfx::IntPoint position(event->position().x() / m_inverse_pixel_scaling_ratio, event->position().y() / m_inverse_pixel_scaling_ratio);
  291. auto button = get_button_from_qt_event(*event);
  292. auto buttons = get_buttons_from_qt_event(*event);
  293. auto modifiers = get_modifiers_from_qt_mouse_event(*event);
  294. auto num_pixels = -event->pixelDelta();
  295. if (!num_pixels.isNull()) {
  296. client().async_mouse_wheel(to_content_position(position), button, buttons, modifiers, num_pixels.x(), num_pixels.y());
  297. } else {
  298. auto num_degrees = -event->angleDelta();
  299. float delta_x = -num_degrees.x() / 120;
  300. float delta_y = num_degrees.y() / 120;
  301. auto step_x = delta_x * QApplication::wheelScrollLines() * devicePixelRatio();
  302. auto step_y = delta_y * QApplication::wheelScrollLines() * devicePixelRatio();
  303. int scroll_step_size = verticalScrollBar()->singleStep();
  304. client().async_mouse_wheel(to_content_position(position), button, buttons, modifiers, step_x * scroll_step_size, step_y * scroll_step_size);
  305. }
  306. event->accept();
  307. return;
  308. }
  309. event->ignore();
  310. }
  311. void WebContentView::mouseMoveEvent(QMouseEvent* event)
  312. {
  313. Gfx::IntPoint position(event->position().x() / m_inverse_pixel_scaling_ratio, event->position().y() / m_inverse_pixel_scaling_ratio);
  314. auto buttons = get_buttons_from_qt_event(*event);
  315. auto modifiers = get_modifiers_from_qt_mouse_event(*event);
  316. client().async_mouse_move(to_content_position(position), 0, buttons, modifiers);
  317. }
  318. void WebContentView::mousePressEvent(QMouseEvent* event)
  319. {
  320. Gfx::IntPoint position(event->position().x() / m_inverse_pixel_scaling_ratio, event->position().y() / m_inverse_pixel_scaling_ratio);
  321. auto button = get_button_from_qt_event(*event);
  322. if (button == 0) {
  323. // We could not convert Qt buttons to something that Lagom can
  324. // recognize - don't even bother propagating this to the web engine
  325. // as it will not handle it anyway, and it will (currently) assert
  326. return;
  327. }
  328. auto modifiers = get_modifiers_from_qt_mouse_event(*event);
  329. auto buttons = get_buttons_from_qt_event(*event);
  330. client().async_mouse_down(to_content_position(position), button, buttons, modifiers);
  331. }
  332. void WebContentView::mouseReleaseEvent(QMouseEvent* event)
  333. {
  334. Gfx::IntPoint position(event->position().x() / m_inverse_pixel_scaling_ratio, event->position().y() / m_inverse_pixel_scaling_ratio);
  335. auto button = get_button_from_qt_event(*event);
  336. if (event->button() & Qt::MouseButton::BackButton) {
  337. if (on_navigate_back)
  338. on_navigate_back();
  339. } else if (event->button() & Qt::MouseButton::ForwardButton) {
  340. if (on_navigate_forward)
  341. on_navigate_forward();
  342. }
  343. if (button == 0) {
  344. // We could not convert Qt buttons to something that Lagom can
  345. // recognize - don't even bother propagating this to the web engine
  346. // as it will not handle it anyway, and it will (currently) assert
  347. return;
  348. }
  349. auto modifiers = get_modifiers_from_qt_mouse_event(*event);
  350. auto buttons = get_buttons_from_qt_event(*event);
  351. client().async_mouse_up(to_content_position(position), button, buttons, modifiers);
  352. }
  353. void WebContentView::mouseDoubleClickEvent(QMouseEvent* event)
  354. {
  355. Gfx::IntPoint position(event->position().x() / m_inverse_pixel_scaling_ratio, event->position().y() / m_inverse_pixel_scaling_ratio);
  356. auto button = get_button_from_qt_event(*event);
  357. if (button == 0) {
  358. // We could not convert Qt buttons to something that Lagom can
  359. // recognize - don't even bother propagating this to the web engine
  360. // as it will not handle it anyway, and it will (currently) assert
  361. return;
  362. }
  363. auto modifiers = get_modifiers_from_qt_mouse_event(*event);
  364. auto buttons = get_buttons_from_qt_event(*event);
  365. client().async_doubleclick(to_content_position(position), button, buttons, modifiers);
  366. }
  367. void WebContentView::dragEnterEvent(QDragEnterEvent* event)
  368. {
  369. if (event->mimeData()->hasUrls())
  370. event->acceptProposedAction();
  371. }
  372. void WebContentView::dropEvent(QDropEvent* event)
  373. {
  374. VERIFY(event->mimeData()->hasUrls());
  375. emit urls_dropped(event->mimeData()->urls());
  376. event->acceptProposedAction();
  377. }
  378. void WebContentView::keyPressEvent(QKeyEvent* event)
  379. {
  380. switch (event->key()) {
  381. case Qt::Key_Left:
  382. case Qt::Key_Right:
  383. case Qt::Key_Up:
  384. case Qt::Key_Down:
  385. case Qt::Key_PageUp:
  386. case Qt::Key_PageDown:
  387. QAbstractScrollArea::keyPressEvent(event);
  388. break;
  389. default:
  390. break;
  391. }
  392. if (event->key() == Qt::Key_Backtab) {
  393. // NOTE: Qt transforms Shift+Tab into a "Backtab", so we undo that transformation here.
  394. client().async_key_down(KeyCode::Key_Tab, Mod_Shift, '\t');
  395. return;
  396. }
  397. auto text = event->text();
  398. auto point = text.isEmpty() ? 0u : event->text()[0].unicode();
  399. auto keycode = get_keycode_from_qt_keyboard_event(*event);
  400. auto modifiers = get_modifiers_from_qt_keyboard_event(*event);
  401. client().async_key_down(keycode, modifiers, point);
  402. }
  403. void WebContentView::keyReleaseEvent(QKeyEvent* event)
  404. {
  405. auto text = event->text();
  406. auto point = text.isEmpty() ? 0u : event->text()[0].unicode();
  407. auto keycode = get_keycode_from_qt_keyboard_event(*event);
  408. auto modifiers = get_modifiers_from_qt_keyboard_event(*event);
  409. client().async_key_up(keycode, modifiers, point);
  410. }
  411. void WebContentView::focusInEvent(QFocusEvent*)
  412. {
  413. client().async_set_has_focus(true);
  414. }
  415. void WebContentView::focusOutEvent(QFocusEvent*)
  416. {
  417. client().async_set_has_focus(false);
  418. }
  419. void WebContentView::paintEvent(QPaintEvent*)
  420. {
  421. QPainter painter(viewport());
  422. painter.scale(m_inverse_pixel_scaling_ratio, m_inverse_pixel_scaling_ratio);
  423. Gfx::Bitmap const* bitmap = nullptr;
  424. Gfx::IntSize bitmap_size;
  425. if (m_client_state.has_usable_bitmap) {
  426. bitmap = m_client_state.front_bitmap.bitmap.ptr();
  427. bitmap_size = m_client_state.front_bitmap.last_painted_size;
  428. } else {
  429. bitmap = m_backup_bitmap.ptr();
  430. bitmap_size = m_backup_bitmap_size;
  431. }
  432. if (bitmap) {
  433. QImage q_image(bitmap->scanline_u8(0), bitmap->width(), bitmap->height(), QImage::Format_RGB32);
  434. painter.drawImage(QPoint(0, 0), q_image, QRect(0, 0, bitmap_size.width(), bitmap_size.height()));
  435. if (bitmap_size.width() < width()) {
  436. painter.fillRect(bitmap_size.width(), 0, width() - bitmap_size.width(), bitmap->height(), palette().base());
  437. }
  438. if (bitmap_size.height() < height()) {
  439. painter.fillRect(0, bitmap_size.height(), width(), height() - bitmap_size.height(), palette().base());
  440. }
  441. return;
  442. }
  443. painter.fillRect(rect(), palette().base());
  444. }
  445. void WebContentView::resizeEvent(QResizeEvent* event)
  446. {
  447. QAbstractScrollArea::resizeEvent(event);
  448. update_viewport_rect();
  449. handle_resize();
  450. }
  451. void WebContentView::set_viewport_rect(Gfx::IntRect rect)
  452. {
  453. m_viewport_rect = rect;
  454. client().async_set_viewport_rect(rect);
  455. }
  456. void WebContentView::set_window_size(Gfx::IntSize size)
  457. {
  458. client().async_set_window_size(size);
  459. }
  460. void WebContentView::set_window_position(Gfx::IntPoint position)
  461. {
  462. client().async_set_window_position(position);
  463. }
  464. void WebContentView::update_viewport_rect()
  465. {
  466. auto scaled_width = int(viewport()->width() / m_inverse_pixel_scaling_ratio);
  467. auto scaled_height = int(viewport()->height() / m_inverse_pixel_scaling_ratio);
  468. Gfx::IntRect rect(max(0, horizontalScrollBar()->value()), max(0, verticalScrollBar()->value()), scaled_width, scaled_height);
  469. set_viewport_rect(rect);
  470. request_repaint();
  471. }
  472. void WebContentView::update_zoom()
  473. {
  474. client().async_set_device_pixels_per_css_pixel(m_device_pixel_ratio * m_zoom_level);
  475. update_viewport_rect();
  476. request_repaint();
  477. }
  478. void WebContentView::showEvent(QShowEvent* event)
  479. {
  480. QAbstractScrollArea::showEvent(event);
  481. client().async_set_system_visibility_state(true);
  482. }
  483. void WebContentView::hideEvent(QHideEvent* event)
  484. {
  485. QAbstractScrollArea::hideEvent(event);
  486. client().async_set_system_visibility_state(false);
  487. }
  488. static Core::AnonymousBuffer make_system_theme_from_qt_palette(QWidget& widget, WebContentView::PaletteMode mode)
  489. {
  490. auto qt_palette = widget.palette();
  491. auto theme_file = mode == WebContentView::PaletteMode::Default ? "Default"sv : "Dark"sv;
  492. auto theme = Gfx::load_system_theme(DeprecatedString::formatted("{}/res/themes/{}.ini", s_serenity_resource_root, theme_file)).release_value_but_fixme_should_propagate_errors();
  493. auto palette_impl = Gfx::PaletteImpl::create_with_anonymous_buffer(theme);
  494. auto palette = Gfx::Palette(move(palette_impl));
  495. auto translate = [&](Gfx::ColorRole gfx_color_role, QPalette::ColorRole qt_color_role) {
  496. auto new_color = Gfx::Color::from_argb(qt_palette.color(qt_color_role).rgba());
  497. palette.set_color(gfx_color_role, new_color);
  498. };
  499. translate(Gfx::ColorRole::ThreedHighlight, QPalette::ColorRole::Light);
  500. translate(Gfx::ColorRole::ThreedShadow1, QPalette::ColorRole::Mid);
  501. translate(Gfx::ColorRole::ThreedShadow2, QPalette::ColorRole::Dark);
  502. translate(Gfx::ColorRole::HoverHighlight, QPalette::ColorRole::Light);
  503. translate(Gfx::ColorRole::Link, QPalette::ColorRole::Link);
  504. translate(Gfx::ColorRole::VisitedLink, QPalette::ColorRole::LinkVisited);
  505. translate(Gfx::ColorRole::Button, QPalette::ColorRole::Button);
  506. translate(Gfx::ColorRole::ButtonText, QPalette::ColorRole::ButtonText);
  507. translate(Gfx::ColorRole::Selection, QPalette::ColorRole::Highlight);
  508. translate(Gfx::ColorRole::SelectionText, QPalette::ColorRole::HighlightedText);
  509. palette.set_flag(Gfx::FlagRole::IsDark, is_using_dark_system_theme(widget));
  510. return theme;
  511. }
  512. void WebContentView::update_palette(PaletteMode mode)
  513. {
  514. client().async_update_system_theme(make_system_theme_from_qt_palette(*this, mode));
  515. }
  516. void WebContentView::create_client(WebView::EnableCallgrindProfiling enable_callgrind_profiling)
  517. {
  518. m_client_state = {};
  519. auto candidate_web_content_paths = get_paths_for_helper_process("WebContent"sv).release_value_but_fixme_should_propagate_errors();
  520. auto new_client = launch_web_content_process(*this, candidate_web_content_paths, enable_callgrind_profiling, WebView::IsLayoutTestMode::No, m_use_lagom_networking).release_value_but_fixme_should_propagate_errors();
  521. m_client_state.client = new_client;
  522. m_client_state.client->on_web_content_process_crash = [this] {
  523. Core::deferred_invoke([this] {
  524. handle_web_content_process_crash();
  525. });
  526. };
  527. m_client_state.client_handle = Web::Crypto::generate_random_uuid().release_value_but_fixme_should_propagate_errors();
  528. client().async_set_window_handle(m_client_state.client_handle);
  529. client().async_set_device_pixels_per_css_pixel(m_device_pixel_ratio);
  530. update_palette();
  531. client().async_update_system_fonts(Gfx::FontDatabase::default_font_query(), Gfx::FontDatabase::fixed_width_font_query(), Gfx::FontDatabase::window_title_font_query());
  532. auto screens = QGuiApplication::screens();
  533. if (!screens.empty()) {
  534. Vector<Gfx::IntRect> screen_rects;
  535. for (auto const& screen : screens) {
  536. auto geometry = screen->geometry();
  537. screen_rects.append(Gfx::IntRect(geometry.x(), geometry.y(), geometry.width(), geometry.height()));
  538. }
  539. // FIXME: Update the screens again when QGuiApplication::screenAdded/Removed signals are emitted
  540. // NOTE: The first item in QGuiApplication::screens is always the primary screen.
  541. // This is not specified in the documentation but QGuiApplication::primaryScreen
  542. // always returns the first item in the list if it isn't empty.
  543. client().async_update_screen_rects(screen_rects, 0);
  544. }
  545. if (!m_webdriver_content_ipc_path.is_empty())
  546. client().async_connect_to_webdriver(m_webdriver_content_ipc_path);
  547. }
  548. void WebContentView::update_cursor(Gfx::StandardCursor cursor)
  549. {
  550. switch (cursor) {
  551. case Gfx::StandardCursor::Hidden:
  552. setCursor(Qt::BlankCursor);
  553. break;
  554. case Gfx::StandardCursor::Arrow:
  555. setCursor(Qt::ArrowCursor);
  556. break;
  557. case Gfx::StandardCursor::Crosshair:
  558. setCursor(Qt::CrossCursor);
  559. break;
  560. case Gfx::StandardCursor::IBeam:
  561. setCursor(Qt::IBeamCursor);
  562. break;
  563. case Gfx::StandardCursor::ResizeHorizontal:
  564. setCursor(Qt::SizeHorCursor);
  565. break;
  566. case Gfx::StandardCursor::ResizeVertical:
  567. setCursor(Qt::SizeVerCursor);
  568. break;
  569. case Gfx::StandardCursor::ResizeDiagonalTLBR:
  570. setCursor(Qt::SizeFDiagCursor);
  571. break;
  572. case Gfx::StandardCursor::ResizeDiagonalBLTR:
  573. setCursor(Qt::SizeBDiagCursor);
  574. break;
  575. case Gfx::StandardCursor::ResizeColumn:
  576. setCursor(Qt::SplitHCursor);
  577. break;
  578. case Gfx::StandardCursor::ResizeRow:
  579. setCursor(Qt::SplitVCursor);
  580. break;
  581. case Gfx::StandardCursor::Hand:
  582. setCursor(Qt::PointingHandCursor);
  583. break;
  584. case Gfx::StandardCursor::Help:
  585. setCursor(Qt::WhatsThisCursor);
  586. break;
  587. case Gfx::StandardCursor::Drag:
  588. setCursor(Qt::ClosedHandCursor);
  589. break;
  590. case Gfx::StandardCursor::DragCopy:
  591. setCursor(Qt::DragCopyCursor);
  592. break;
  593. case Gfx::StandardCursor::Move:
  594. setCursor(Qt::DragMoveCursor);
  595. break;
  596. case Gfx::StandardCursor::Wait:
  597. setCursor(Qt::BusyCursor);
  598. break;
  599. case Gfx::StandardCursor::Disallowed:
  600. setCursor(Qt::ForbiddenCursor);
  601. break;
  602. case Gfx::StandardCursor::Eyedropper:
  603. case Gfx::StandardCursor::Zoom:
  604. // FIXME: No corresponding Qt cursors, default to Arrow
  605. default:
  606. setCursor(Qt::ArrowCursor);
  607. break;
  608. }
  609. }
  610. void WebContentView::notify_server_did_enter_tooltip_area(Badge<WebContentClient>, Gfx::IntPoint content_position, DeprecatedString const& tooltip)
  611. {
  612. auto widget_position = to_widget_position(content_position);
  613. QToolTip::showText(
  614. mapToGlobal(QPoint(widget_position.x(), widget_position.y())),
  615. qstring_from_ak_deprecated_string(tooltip),
  616. this);
  617. }
  618. void WebContentView::notify_server_did_leave_tooltip_area(Badge<WebContentClient>)
  619. {
  620. QToolTip::hideText();
  621. }
  622. Gfx::IntRect WebContentView::viewport_rect() const
  623. {
  624. return m_viewport_rect;
  625. }
  626. Gfx::IntPoint WebContentView::to_content_position(Gfx::IntPoint widget_position) const
  627. {
  628. return widget_position.translated(max(0, horizontalScrollBar()->value()), max(0, verticalScrollBar()->value()));
  629. }
  630. Gfx::IntPoint WebContentView::to_widget_position(Gfx::IntPoint content_position) const
  631. {
  632. return content_position.translated(-(max(0, horizontalScrollBar()->value())), -(max(0, verticalScrollBar()->value())));
  633. }
  634. bool WebContentView::event(QEvent* event)
  635. {
  636. // NOTE: We have to implement event() manually as Qt's focus navigation mechanism
  637. // eats all the Tab key presses by default.
  638. if (event->type() == QEvent::KeyPress) {
  639. keyPressEvent(static_cast<QKeyEvent*>(event));
  640. return true;
  641. }
  642. if (event->type() == QEvent::KeyRelease) {
  643. keyReleaseEvent(static_cast<QKeyEvent*>(event));
  644. return true;
  645. }
  646. if (event->type() == QEvent::PaletteChange) {
  647. update_palette();
  648. request_repaint();
  649. return QAbstractScrollArea::event(event);
  650. }
  651. return QAbstractScrollArea::event(event);
  652. }
  653. void WebContentView::notify_server_did_finish_handling_input_event(bool event_was_accepted)
  654. {
  655. // FIXME: Currently Ladybird handles the keyboard shortcuts before passing the event to web content, so
  656. // we don't need to do anything here. But we'll need to once we start asking web content first.
  657. (void)event_was_accepted;
  658. }
  659. ErrorOr<String> WebContentView::dump_layout_tree()
  660. {
  661. return String::from_deprecated_string(client().dump_layout_tree());
  662. }
  663. }