WebContentView.cpp 28 KB

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