WebContentView.cpp 28 KB

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