WebContentView.cpp 28 KB

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