WebContentView.cpp 26 KB

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