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