WebContentView.cpp 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941
  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 "Application.h"
  9. #include "StringUtils.h"
  10. #include <AK/Assertions.h>
  11. #include <AK/ByteBuffer.h>
  12. #include <AK/Format.h>
  13. #include <AK/LexicalPath.h>
  14. #include <AK/NonnullOwnPtr.h>
  15. #include <AK/Types.h>
  16. #include <Ladybird/HelperProcess.h>
  17. #include <Ladybird/Utilities.h>
  18. #include <LibCore/EventLoop.h>
  19. #include <LibCore/Resource.h>
  20. #include <LibCore/Timer.h>
  21. #include <LibGfx/Bitmap.h>
  22. #include <LibGfx/DeprecatedPainter.h>
  23. #include <LibGfx/Font/FontDatabase.h>
  24. #include <LibGfx/ImageFormats/PNGWriter.h>
  25. #include <LibGfx/Palette.h>
  26. #include <LibGfx/Rect.h>
  27. #include <LibGfx/SystemTheme.h>
  28. #include <LibWeb/Crypto/Crypto.h>
  29. #include <LibWeb/UIEvents/KeyCode.h>
  30. #include <LibWeb/UIEvents/MouseButton.h>
  31. #include <LibWeb/Worker/WebWorkerClient.h>
  32. #include <LibWebView/Application.h>
  33. #include <LibWebView/WebContentClient.h>
  34. #include <QApplication>
  35. #include <QCursor>
  36. #include <QGuiApplication>
  37. #include <QIcon>
  38. #include <QMimeData>
  39. #include <QMouseEvent>
  40. #include <QPaintEvent>
  41. #include <QPainter>
  42. #include <QPalette>
  43. #include <QScrollBar>
  44. #include <QTextEdit>
  45. #include <QTimer>
  46. #include <QToolTip>
  47. namespace Ladybird {
  48. bool is_using_dark_system_theme(QWidget&);
  49. WebContentView::WebContentView(QWidget* window, RefPtr<WebView::WebContentClient> parent_client, size_t page_index)
  50. : QAbstractScrollArea(window)
  51. {
  52. setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  53. setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  54. m_client_state.client = parent_client;
  55. m_client_state.page_index = page_index;
  56. setAttribute(Qt::WA_InputMethodEnabled, true);
  57. setMouseTracking(true);
  58. setAcceptDrops(true);
  59. setFocusPolicy(Qt::FocusPolicy::StrongFocus);
  60. m_device_pixel_ratio = devicePixelRatio();
  61. verticalScrollBar()->setSingleStep(24);
  62. horizontalScrollBar()->setSingleStep(24);
  63. QObject::connect(qGuiApp, &QGuiApplication::screenRemoved, [this](QScreen*) {
  64. update_screen_rects();
  65. });
  66. QObject::connect(qGuiApp, &QGuiApplication::screenAdded, [this](QScreen*) {
  67. update_screen_rects();
  68. });
  69. m_tooltip_hover_timer.setSingleShot(true);
  70. QObject::connect(&m_tooltip_hover_timer, &QTimer::timeout, [this] {
  71. if (m_tooltip_text.has_value())
  72. QToolTip::showText(
  73. QCursor::pos(),
  74. qstring_from_ak_string(m_tooltip_text.value()),
  75. this);
  76. });
  77. initialize_client((parent_client == nullptr) ? CreateNewClient::Yes : CreateNewClient::No);
  78. on_ready_to_paint = [this]() {
  79. viewport()->update();
  80. };
  81. on_cursor_change = [this](auto cursor) {
  82. update_cursor(cursor);
  83. };
  84. on_request_tooltip_override = [this](auto position, auto const& tooltip) {
  85. m_tooltip_override = true;
  86. if (m_tooltip_hover_timer.isActive())
  87. m_tooltip_hover_timer.stop();
  88. auto tooltip_without_carriage_return = tooltip.contains("\r"sv)
  89. ? tooltip.replace("\r\n"sv, "\n"sv, ReplaceMode::All).replace("\r"sv, "\n"sv, ReplaceMode::All)
  90. : tooltip;
  91. QToolTip::showText(
  92. mapToGlobal(QPoint(position.x(), position.y())),
  93. qstring_from_ak_string(tooltip_without_carriage_return),
  94. this);
  95. };
  96. on_stop_tooltip_override = [this]() {
  97. m_tooltip_override = false;
  98. };
  99. on_enter_tooltip_area = [this](auto const& tooltip) {
  100. m_tooltip_text = tooltip.contains("\r"sv)
  101. ? tooltip.replace("\r\n"sv, "\n"sv, ReplaceMode::All).replace("\r"sv, "\n"sv, ReplaceMode::All)
  102. : tooltip;
  103. };
  104. on_leave_tooltip_area = [this]() {
  105. m_tooltip_text.clear();
  106. };
  107. on_finish_handling_key_event = [this](auto const& event) {
  108. finish_handling_key_event(event);
  109. };
  110. on_finish_handling_drag_event = [this](auto const& event) {
  111. finish_handling_drag_event(event);
  112. };
  113. on_request_worker_agent = [&]() {
  114. RefPtr<Requests::RequestClient> request_server_client {};
  115. if (WebView::Application::web_content_options().use_lagom_networking == WebView::UseLagomNetworking::Yes)
  116. request_server_client = static_cast<Ladybird::Application*>(QApplication::instance())->request_server_client;
  117. auto worker_client = MUST(launch_web_worker_process(MUST(get_paths_for_helper_process("WebWorker"sv)), request_server_client));
  118. return worker_client->dup_socket();
  119. };
  120. m_select_dropdown = new QMenu("Select Dropdown", this);
  121. QObject::connect(m_select_dropdown, &QMenu::aboutToHide, this, [this]() {
  122. if (!m_select_dropdown->activeAction())
  123. select_dropdown_closed({});
  124. });
  125. on_request_select_dropdown = [this](Gfx::IntPoint content_position, i32 minimum_width, Vector<Web::HTML::SelectItem> items) {
  126. m_select_dropdown->clear();
  127. m_select_dropdown->setMinimumWidth(minimum_width / device_pixel_ratio());
  128. auto add_menu_item = [this](Web::HTML::SelectItemOption const& item_option, bool in_option_group) {
  129. QAction* action = new QAction(qstring_from_ak_string(in_option_group ? MUST(String::formatted(" {}", item_option.label)) : item_option.label), this);
  130. action->setCheckable(true);
  131. action->setChecked(item_option.selected);
  132. action->setDisabled(item_option.disabled);
  133. action->setData(QVariant(static_cast<uint>(item_option.id)));
  134. QObject::connect(action, &QAction::triggered, this, &WebContentView::select_dropdown_action);
  135. m_select_dropdown->addAction(action);
  136. };
  137. for (auto const& item : items) {
  138. if (item.has<Web::HTML::SelectItemOptionGroup>()) {
  139. auto const& item_option_group = item.get<Web::HTML::SelectItemOptionGroup>();
  140. QAction* subtitle = new QAction(qstring_from_ak_string(item_option_group.label), this);
  141. subtitle->setDisabled(true);
  142. m_select_dropdown->addAction(subtitle);
  143. for (auto const& item_option : item_option_group.items)
  144. add_menu_item(item_option, true);
  145. }
  146. if (item.has<Web::HTML::SelectItemOption>())
  147. add_menu_item(item.get<Web::HTML::SelectItemOption>(), false);
  148. if (item.has<Web::HTML::SelectItemSeparator>())
  149. m_select_dropdown->addSeparator();
  150. }
  151. m_select_dropdown->exec(map_point_to_global_position(content_position));
  152. };
  153. }
  154. WebContentView::~WebContentView() = default;
  155. void WebContentView::select_dropdown_action()
  156. {
  157. QAction* action = qobject_cast<QAction*>(sender());
  158. select_dropdown_closed(action->data().value<uint>());
  159. }
  160. static Web::UIEvents::MouseButton get_button_from_qt_mouse_button(Qt::MouseButton button)
  161. {
  162. if (button == Qt::MouseButton::LeftButton)
  163. return Web::UIEvents::MouseButton::Primary;
  164. if (button == Qt::MouseButton::RightButton)
  165. return Web::UIEvents::MouseButton::Secondary;
  166. if (button == Qt::MouseButton::MiddleButton)
  167. return Web::UIEvents::MouseButton::Middle;
  168. if (button == Qt::MouseButton::BackButton)
  169. return Web::UIEvents::MouseButton::Backward;
  170. if (button == Qt::MouseButton::ForwardButton)
  171. return Web::UIEvents::MouseButton::Forward;
  172. return Web::UIEvents::MouseButton::None;
  173. }
  174. static Web::UIEvents::MouseButton get_buttons_from_qt_mouse_buttons(Qt::MouseButtons buttons)
  175. {
  176. auto result = Web::UIEvents::MouseButton::None;
  177. if (buttons.testFlag(Qt::MouseButton::LeftButton))
  178. result |= Web::UIEvents::MouseButton::Primary;
  179. if (buttons.testFlag(Qt::MouseButton::RightButton))
  180. result |= Web::UIEvents::MouseButton::Secondary;
  181. if (buttons.testFlag(Qt::MouseButton::MiddleButton))
  182. result |= Web::UIEvents::MouseButton::Middle;
  183. if (buttons.testFlag(Qt::MouseButton::BackButton))
  184. result |= Web::UIEvents::MouseButton::Backward;
  185. if (buttons.testFlag(Qt::MouseButton::ForwardButton))
  186. result |= Web::UIEvents::MouseButton::Forward;
  187. return result;
  188. }
  189. static Web::UIEvents::KeyModifier get_modifiers_from_qt_keyboard_modifiers(Qt::KeyboardModifiers modifiers)
  190. {
  191. auto result = Web::UIEvents::KeyModifier::Mod_None;
  192. if (modifiers.testFlag(Qt::AltModifier))
  193. result |= Web::UIEvents::KeyModifier::Mod_Alt;
  194. if (modifiers.testFlag(Qt::ControlModifier))
  195. result |= Web::UIEvents::KeyModifier::Mod_Ctrl;
  196. if (modifiers.testFlag(Qt::ShiftModifier))
  197. result |= Web::UIEvents::KeyModifier::Mod_Shift;
  198. return result;
  199. }
  200. static Web::UIEvents::KeyModifier get_modifiers_from_qt_key_event(QKeyEvent const& event)
  201. {
  202. auto modifiers = Web::UIEvents::KeyModifier::Mod_None;
  203. if (event.modifiers().testFlag(Qt::AltModifier))
  204. modifiers |= Web::UIEvents::KeyModifier::Mod_Alt;
  205. if (event.modifiers().testFlag(Qt::ControlModifier))
  206. modifiers |= Web::UIEvents::KeyModifier::Mod_Ctrl;
  207. if (event.modifiers().testFlag(Qt::MetaModifier))
  208. modifiers |= Web::UIEvents::KeyModifier::Mod_Super;
  209. if (event.modifiers().testFlag(Qt::ShiftModifier))
  210. modifiers |= Web::UIEvents::KeyModifier::Mod_Shift;
  211. if (event.modifiers().testFlag(Qt::AltModifier))
  212. modifiers |= Web::UIEvents::KeyModifier::Mod_AltGr;
  213. if (event.modifiers().testFlag(Qt::KeypadModifier))
  214. modifiers |= Web::UIEvents::KeyModifier::Mod_Keypad;
  215. return modifiers;
  216. }
  217. static Web::UIEvents::KeyCode get_keycode_from_qt_key_event(QKeyEvent const& event)
  218. {
  219. struct Mapping {
  220. constexpr Mapping(Qt::Key q, Web::UIEvents::KeyCode s)
  221. : qt_key(q)
  222. , serenity_key(s)
  223. {
  224. }
  225. Qt::Key qt_key;
  226. Web::UIEvents::KeyCode serenity_key;
  227. };
  228. // https://doc.qt.io/qt-6/qt.html#Key-enum
  229. constexpr Mapping mappings[] = {
  230. { Qt::Key_0, Web::UIEvents::Key_0 },
  231. { Qt::Key_1, Web::UIEvents::Key_1 },
  232. { Qt::Key_2, Web::UIEvents::Key_2 },
  233. { Qt::Key_3, Web::UIEvents::Key_3 },
  234. { Qt::Key_4, Web::UIEvents::Key_4 },
  235. { Qt::Key_5, Web::UIEvents::Key_5 },
  236. { Qt::Key_6, Web::UIEvents::Key_6 },
  237. { Qt::Key_7, Web::UIEvents::Key_7 },
  238. { Qt::Key_8, Web::UIEvents::Key_8 },
  239. { Qt::Key_9, Web::UIEvents::Key_9 },
  240. { Qt::Key_A, Web::UIEvents::Key_A },
  241. { Qt::Key_Alt, Web::UIEvents::Key_Alt },
  242. { Qt::Key_Ampersand, Web::UIEvents::Key_Ampersand },
  243. { Qt::Key_Apostrophe, Web::UIEvents::Key_Apostrophe },
  244. { Qt::Key_AsciiCircum, Web::UIEvents::Key_Circumflex },
  245. { Qt::Key_AsciiTilde, Web::UIEvents::Key_Tilde },
  246. { Qt::Key_Asterisk, Web::UIEvents::Key_Asterisk },
  247. { Qt::Key_At, Web::UIEvents::Key_AtSign },
  248. { Qt::Key_B, Web::UIEvents::Key_B },
  249. { Qt::Key_Backslash, Web::UIEvents::Key_Backslash },
  250. { Qt::Key_Backspace, Web::UIEvents::Key_Backspace },
  251. { Qt::Key_Bar, Web::UIEvents::Key_Pipe },
  252. { Qt::Key_BraceLeft, Web::UIEvents::Key_LeftBrace },
  253. { Qt::Key_BraceRight, Web::UIEvents::Key_RightBrace },
  254. { Qt::Key_BracketLeft, Web::UIEvents::Key_LeftBracket },
  255. { Qt::Key_BracketRight, Web::UIEvents::Key_RightBracket },
  256. { Qt::Key_C, Web::UIEvents::Key_C },
  257. { Qt::Key_CapsLock, Web::UIEvents::Key_CapsLock },
  258. { Qt::Key_Colon, Web::UIEvents::Key_Colon },
  259. { Qt::Key_Comma, Web::UIEvents::Key_Comma },
  260. { Qt::Key_Control, Web::UIEvents::Key_Control },
  261. { Qt::Key_D, Web::UIEvents::Key_D },
  262. { Qt::Key_Delete, Web::UIEvents::Key_Delete },
  263. { Qt::Key_Dollar, Web::UIEvents::Key_Dollar },
  264. { Qt::Key_Down, Web::UIEvents::Key_Down },
  265. { Qt::Key_E, Web::UIEvents::Key_E },
  266. { Qt::Key_End, Web::UIEvents::Key_End },
  267. { Qt::Key_Equal, Web::UIEvents::Key_Equal },
  268. { Qt::Key_Enter, Web::UIEvents::Key_Return },
  269. { Qt::Key_Escape, Web::UIEvents::Key_Escape },
  270. { Qt::Key_Exclam, Web::UIEvents::Key_ExclamationPoint },
  271. { Qt::Key_exclamdown, Web::UIEvents::Key_ExclamationPoint },
  272. { Qt::Key_F, Web::UIEvents::Key_F },
  273. { Qt::Key_F1, Web::UIEvents::Key_F1 },
  274. { Qt::Key_F10, Web::UIEvents::Key_F10 },
  275. { Qt::Key_F11, Web::UIEvents::Key_F11 },
  276. { Qt::Key_F12, Web::UIEvents::Key_F12 },
  277. { Qt::Key_F2, Web::UIEvents::Key_F2 },
  278. { Qt::Key_F3, Web::UIEvents::Key_F3 },
  279. { Qt::Key_F4, Web::UIEvents::Key_F4 },
  280. { Qt::Key_F5, Web::UIEvents::Key_F5 },
  281. { Qt::Key_F6, Web::UIEvents::Key_F6 },
  282. { Qt::Key_F7, Web::UIEvents::Key_F7 },
  283. { Qt::Key_F8, Web::UIEvents::Key_F8 },
  284. { Qt::Key_F9, Web::UIEvents::Key_F9 },
  285. { Qt::Key_G, Web::UIEvents::Key_G },
  286. { Qt::Key_Greater, Web::UIEvents::Key_GreaterThan },
  287. { Qt::Key_H, Web::UIEvents::Key_H },
  288. { Qt::Key_Home, Web::UIEvents::Key_Home },
  289. { Qt::Key_I, Web::UIEvents::Key_I },
  290. { Qt::Key_Insert, Web::UIEvents::Key_Insert },
  291. { Qt::Key_J, Web::UIEvents::Key_J },
  292. { Qt::Key_K, Web::UIEvents::Key_K },
  293. { Qt::Key_L, Web::UIEvents::Key_L },
  294. { Qt::Key_Left, Web::UIEvents::Key_Left },
  295. { Qt::Key_Less, Web::UIEvents::Key_LessThan },
  296. { Qt::Key_M, Web::UIEvents::Key_M },
  297. { Qt::Key_Menu, Web::UIEvents::Key_Menu },
  298. { Qt::Key_Meta, Web::UIEvents::Key_Super },
  299. { Qt::Key_Minus, Web::UIEvents::Key_Minus },
  300. { Qt::Key_N, Web::UIEvents::Key_N },
  301. { Qt::Key_NumberSign, Web::UIEvents::Key_Hashtag },
  302. { Qt::Key_NumLock, Web::UIEvents::Key_NumLock },
  303. { Qt::Key_O, Web::UIEvents::Key_O },
  304. { Qt::Key_P, Web::UIEvents::Key_P },
  305. { Qt::Key_PageDown, Web::UIEvents::Key_PageDown },
  306. { Qt::Key_PageUp, Web::UIEvents::Key_PageUp },
  307. { Qt::Key_ParenLeft, Web::UIEvents::Key_LeftParen },
  308. { Qt::Key_ParenRight, Web::UIEvents::Key_RightParen },
  309. { Qt::Key_Percent, Web::UIEvents::Key_Percent },
  310. { Qt::Key_Period, Web::UIEvents::Key_Period },
  311. { Qt::Key_Plus, Web::UIEvents::Key_Plus },
  312. { Qt::Key_Print, Web::UIEvents::Key_PrintScreen },
  313. { Qt::Key_Q, Web::UIEvents::Key_Q },
  314. { Qt::Key_Question, Web::UIEvents::Key_QuestionMark },
  315. { Qt::Key_QuoteDbl, Web::UIEvents::Key_DoubleQuote },
  316. { Qt::Key_QuoteLeft, Web::UIEvents::Key_Backtick },
  317. { Qt::Key_R, Web::UIEvents::Key_R },
  318. { Qt::Key_Return, Web::UIEvents::Key_Return },
  319. { Qt::Key_Right, Web::UIEvents::Key_Right },
  320. { Qt::Key_S, Web::UIEvents::Key_S },
  321. { Qt::Key_ScrollLock, Web::UIEvents::Key_ScrollLock },
  322. { Qt::Key_Semicolon, Web::UIEvents::Key_Semicolon },
  323. { Qt::Key_Shift, Web::UIEvents::Key_LeftShift },
  324. { Qt::Key_Slash, Web::UIEvents::Key_Slash },
  325. { Qt::Key_Space, Web::UIEvents::Key_Space },
  326. { Qt::Key_Super_L, Web::UIEvents::Key_Super },
  327. { Qt::Key_Super_R, Web::UIEvents::Key_Super },
  328. { Qt::Key_SysReq, Web::UIEvents::Key_SysRq },
  329. { Qt::Key_T, Web::UIEvents::Key_T },
  330. { Qt::Key_Tab, Web::UIEvents::Key_Tab },
  331. { Qt::Key_U, Web::UIEvents::Key_U },
  332. { Qt::Key_Underscore, Web::UIEvents::Key_Underscore },
  333. { Qt::Key_Up, Web::UIEvents::Key_Up },
  334. { Qt::Key_V, Web::UIEvents::Key_V },
  335. { Qt::Key_W, Web::UIEvents::Key_W },
  336. { Qt::Key_X, Web::UIEvents::Key_X },
  337. { Qt::Key_Y, Web::UIEvents::Key_Y },
  338. { Qt::Key_Z, Web::UIEvents::Key_Z },
  339. };
  340. for (auto const& mapping : mappings) {
  341. if (event.key() == mapping.qt_key)
  342. return mapping.serenity_key;
  343. }
  344. return Web::UIEvents::Key_Invalid;
  345. }
  346. void WebContentView::keyPressEvent(QKeyEvent* event)
  347. {
  348. enqueue_native_event(Web::KeyEvent::Type::KeyDown, *event);
  349. }
  350. void WebContentView::keyReleaseEvent(QKeyEvent* event)
  351. {
  352. enqueue_native_event(Web::KeyEvent::Type::KeyUp, *event);
  353. }
  354. void WebContentView::inputMethodEvent(QInputMethodEvent* event)
  355. {
  356. if (!event->commitString().isEmpty()) {
  357. QKeyEvent keyEvent(QEvent::KeyPress, 0, Qt::NoModifier, event->commitString());
  358. keyPressEvent(&keyEvent);
  359. }
  360. event->accept();
  361. }
  362. QVariant WebContentView::inputMethodQuery(Qt::InputMethodQuery) const
  363. {
  364. return QVariant();
  365. }
  366. void WebContentView::mouseMoveEvent(QMouseEvent* event)
  367. {
  368. if (!m_tooltip_override) {
  369. if (QToolTip::isVisible())
  370. QToolTip::hideText();
  371. m_tooltip_hover_timer.start(600);
  372. }
  373. enqueue_native_event(Web::MouseEvent::Type::MouseMove, *event);
  374. }
  375. void WebContentView::mousePressEvent(QMouseEvent* event)
  376. {
  377. enqueue_native_event(Web::MouseEvent::Type::MouseDown, *event);
  378. }
  379. void WebContentView::mouseReleaseEvent(QMouseEvent* event)
  380. {
  381. enqueue_native_event(Web::MouseEvent::Type::MouseUp, *event);
  382. if (event->button() == Qt::MouseButton::BackButton)
  383. traverse_the_history_by_delta(-1);
  384. else if (event->button() == Qt::MouseButton::ForwardButton)
  385. traverse_the_history_by_delta(1);
  386. }
  387. void WebContentView::wheelEvent(QWheelEvent* event)
  388. {
  389. if (event->modifiers().testFlag(Qt::ControlModifier)) {
  390. event->ignore();
  391. return;
  392. }
  393. enqueue_native_event(Web::MouseEvent::Type::MouseWheel, *event);
  394. }
  395. void WebContentView::mouseDoubleClickEvent(QMouseEvent* event)
  396. {
  397. enqueue_native_event(Web::MouseEvent::Type::DoubleClick, *event);
  398. }
  399. void WebContentView::dragEnterEvent(QDragEnterEvent* event)
  400. {
  401. if (!event->mimeData()->hasUrls())
  402. return;
  403. enqueue_native_event(Web::DragEvent::Type::DragStart, *event);
  404. event->acceptProposedAction();
  405. }
  406. void WebContentView::dragMoveEvent(QDragMoveEvent* event)
  407. {
  408. enqueue_native_event(Web::DragEvent::Type::DragMove, *event);
  409. event->acceptProposedAction();
  410. }
  411. void WebContentView::dragLeaveEvent(QDragLeaveEvent*)
  412. {
  413. // QDragLeaveEvent does not contain any mouse position or button information.
  414. Web::DragEvent event {};
  415. event.type = Web::DragEvent::Type::DragEnd;
  416. enqueue_input_event(AK::move(event));
  417. }
  418. void WebContentView::dropEvent(QDropEvent* event)
  419. {
  420. enqueue_native_event(Web::DragEvent::Type::Drop, *event);
  421. event->acceptProposedAction();
  422. }
  423. void WebContentView::focusInEvent(QFocusEvent*)
  424. {
  425. client().async_set_has_focus(m_client_state.page_index, true);
  426. }
  427. void WebContentView::focusOutEvent(QFocusEvent*)
  428. {
  429. client().async_set_has_focus(m_client_state.page_index, false);
  430. }
  431. void WebContentView::paintEvent(QPaintEvent*)
  432. {
  433. QPainter painter(viewport());
  434. painter.scale(1 / m_device_pixel_ratio, 1 / m_device_pixel_ratio);
  435. Gfx::Bitmap const* bitmap = nullptr;
  436. Gfx::IntSize bitmap_size;
  437. if (m_client_state.has_usable_bitmap) {
  438. bitmap = m_client_state.front_bitmap.bitmap.ptr();
  439. bitmap_size = m_client_state.front_bitmap.last_painted_size.to_type<int>();
  440. } else {
  441. bitmap = m_backup_bitmap.ptr();
  442. bitmap_size = m_backup_bitmap_size.to_type<int>();
  443. }
  444. if (bitmap) {
  445. QImage q_image(bitmap->scanline_u8(0), bitmap->width(), bitmap->height(), bitmap->pitch(), QImage::Format_RGB32);
  446. painter.drawImage(QPoint(0, 0), q_image, QRect(0, 0, bitmap_size.width(), bitmap_size.height()));
  447. if (bitmap_size.width() < width()) {
  448. painter.fillRect(bitmap_size.width(), 0, width() - bitmap_size.width(), bitmap->height(), palette().base());
  449. }
  450. if (bitmap_size.height() < height()) {
  451. painter.fillRect(0, bitmap_size.height(), width(), height() - bitmap_size.height(), palette().base());
  452. }
  453. return;
  454. }
  455. painter.fillRect(rect(), palette().base());
  456. }
  457. void WebContentView::resizeEvent(QResizeEvent* event)
  458. {
  459. QAbstractScrollArea::resizeEvent(event);
  460. update_viewport_size();
  461. handle_resize();
  462. }
  463. void WebContentView::set_viewport_rect(Gfx::IntRect rect)
  464. {
  465. m_viewport_size = rect.size();
  466. client().async_set_viewport_size(m_client_state.page_index, rect.size().to_type<Web::DevicePixels>());
  467. }
  468. void WebContentView::set_window_size(Gfx::IntSize size)
  469. {
  470. client().async_set_window_size(m_client_state.page_index, size.to_type<Web::DevicePixels>());
  471. }
  472. void WebContentView::set_window_position(Gfx::IntPoint position)
  473. {
  474. client().async_set_window_position(m_client_state.page_index, position.to_type<Web::DevicePixels>());
  475. }
  476. void WebContentView::set_device_pixel_ratio(double device_pixel_ratio)
  477. {
  478. m_device_pixel_ratio = device_pixel_ratio;
  479. client().async_set_device_pixels_per_css_pixel(m_client_state.page_index, m_device_pixel_ratio * m_zoom_level);
  480. update_viewport_size();
  481. handle_resize();
  482. }
  483. void WebContentView::update_viewport_size()
  484. {
  485. auto scaled_width = int(viewport()->width() * m_device_pixel_ratio);
  486. auto scaled_height = int(viewport()->height() * m_device_pixel_ratio);
  487. Gfx::IntRect rect(0, 0, scaled_width, scaled_height);
  488. set_viewport_rect(rect);
  489. }
  490. void WebContentView::update_zoom()
  491. {
  492. client().async_set_device_pixels_per_css_pixel(m_client_state.page_index, m_device_pixel_ratio * m_zoom_level);
  493. update_viewport_size();
  494. }
  495. void WebContentView::showEvent(QShowEvent* event)
  496. {
  497. QAbstractScrollArea::showEvent(event);
  498. client().async_set_system_visibility_state(m_client_state.page_index, true);
  499. }
  500. void WebContentView::hideEvent(QHideEvent* event)
  501. {
  502. QAbstractScrollArea::hideEvent(event);
  503. client().async_set_system_visibility_state(m_client_state.page_index, false);
  504. }
  505. static Core::AnonymousBuffer make_system_theme_from_qt_palette(QWidget& widget, WebContentView::PaletteMode mode)
  506. {
  507. auto qt_palette = widget.palette();
  508. auto theme_file = mode == WebContentView::PaletteMode::Default ? "Default"sv : "Dark"sv;
  509. auto theme_ini = MUST(Core::Resource::load_from_uri(MUST(String::formatted("resource://themes/{}.ini", theme_file))));
  510. auto theme = Gfx::load_system_theme(theme_ini->filesystem_path().to_byte_string()).release_value_but_fixme_should_propagate_errors();
  511. auto palette_impl = Gfx::PaletteImpl::create_with_anonymous_buffer(theme);
  512. auto palette = Gfx::Palette(move(palette_impl));
  513. auto translate = [&](Gfx::ColorRole gfx_color_role, QPalette::ColorRole qt_color_role) {
  514. auto new_color = Gfx::Color::from_argb(qt_palette.color(qt_color_role).rgba());
  515. palette.set_color(gfx_color_role, new_color);
  516. };
  517. translate(Gfx::ColorRole::ThreedHighlight, QPalette::ColorRole::Light);
  518. translate(Gfx::ColorRole::ThreedShadow1, QPalette::ColorRole::Mid);
  519. translate(Gfx::ColorRole::ThreedShadow2, QPalette::ColorRole::Dark);
  520. translate(Gfx::ColorRole::HoverHighlight, QPalette::ColorRole::Light);
  521. translate(Gfx::ColorRole::Link, QPalette::ColorRole::Link);
  522. translate(Gfx::ColorRole::VisitedLink, QPalette::ColorRole::LinkVisited);
  523. translate(Gfx::ColorRole::Button, QPalette::ColorRole::Button);
  524. translate(Gfx::ColorRole::ButtonText, QPalette::ColorRole::ButtonText);
  525. translate(Gfx::ColorRole::Selection, QPalette::ColorRole::Highlight);
  526. translate(Gfx::ColorRole::SelectionText, QPalette::ColorRole::HighlightedText);
  527. palette.set_flag(Gfx::FlagRole::IsDark, is_using_dark_system_theme(widget));
  528. return theme;
  529. }
  530. void WebContentView::update_palette(PaletteMode mode)
  531. {
  532. client().async_update_system_theme(m_client_state.page_index, make_system_theme_from_qt_palette(*this, mode));
  533. }
  534. void WebContentView::update_screen_rects()
  535. {
  536. auto screens = QGuiApplication::screens();
  537. if (!screens.empty()) {
  538. Vector<Web::DevicePixelRect> screen_rects;
  539. for (auto const& screen : screens) {
  540. // NOTE: QScreen::geometry() returns the 'device-independent pixels', we multiply
  541. // by the device pixel ratio to get the 'physical pixels' of the display.
  542. auto geometry = screen->geometry();
  543. auto device_pixel_ratio = screen->devicePixelRatio();
  544. screen_rects.append(Web::DevicePixelRect(geometry.x(), geometry.y(), geometry.width() * device_pixel_ratio, geometry.height() * device_pixel_ratio));
  545. }
  546. // NOTE: The first item in QGuiApplication::screens is always the primary screen.
  547. // This is not specified in the documentation but QGuiApplication::primaryScreen
  548. // always returns the first item in the list if it isn't empty.
  549. client().async_update_screen_rects(m_client_state.page_index, screen_rects, 0);
  550. }
  551. }
  552. void WebContentView::initialize_client(WebView::ViewImplementation::CreateNewClient create_new_client)
  553. {
  554. if (create_new_client == CreateNewClient::Yes) {
  555. m_client_state = {};
  556. Optional<IPC::File> request_server_socket;
  557. if (WebView::Application::web_content_options().use_lagom_networking == WebView::UseLagomNetworking::Yes) {
  558. auto& protocol = static_cast<Ladybird::Application*>(QApplication::instance())->request_server_client;
  559. // FIXME: Fail to open the tab, rather than crashing the whole application if this fails
  560. auto socket = connect_new_request_server_client(*protocol).release_value_but_fixme_should_propagate_errors();
  561. request_server_socket = AK::move(socket);
  562. }
  563. auto image_decoder = static_cast<Ladybird::Application*>(QApplication::instance())->image_decoder_client();
  564. auto image_decoder_socket = connect_new_image_decoder_client(*image_decoder).release_value_but_fixme_should_propagate_errors();
  565. auto candidate_web_content_paths = get_paths_for_helper_process("WebContent"sv).release_value_but_fixme_should_propagate_errors();
  566. auto new_client = launch_web_content_process(*this, candidate_web_content_paths, AK::move(image_decoder_socket), AK::move(request_server_socket)).release_value_but_fixme_should_propagate_errors();
  567. m_client_state.client = new_client;
  568. } else {
  569. m_client_state.client->register_view(m_client_state.page_index, *this);
  570. }
  571. m_client_state.client->on_web_content_process_crash = [this] {
  572. Core::deferred_invoke([this] {
  573. handle_web_content_process_crash();
  574. });
  575. };
  576. m_client_state.client_handle = Web::Crypto::generate_random_uuid().release_value_but_fixme_should_propagate_errors();
  577. client().async_set_window_handle(m_client_state.page_index, m_client_state.client_handle);
  578. client().async_set_device_pixels_per_css_pixel(m_client_state.page_index, m_device_pixel_ratio);
  579. update_palette();
  580. update_screen_rects();
  581. if (auto webdriver_content_ipc_path = WebView::Application::chrome_options().webdriver_content_ipc_path; webdriver_content_ipc_path.has_value())
  582. client().async_connect_to_webdriver(m_client_state.page_index, *webdriver_content_ipc_path);
  583. }
  584. void WebContentView::update_cursor(Gfx::StandardCursor cursor)
  585. {
  586. switch (cursor) {
  587. case Gfx::StandardCursor::Hidden:
  588. setCursor(Qt::BlankCursor);
  589. break;
  590. case Gfx::StandardCursor::Arrow:
  591. setCursor(Qt::ArrowCursor);
  592. break;
  593. case Gfx::StandardCursor::Crosshair:
  594. setCursor(Qt::CrossCursor);
  595. break;
  596. case Gfx::StandardCursor::IBeam:
  597. setCursor(Qt::IBeamCursor);
  598. break;
  599. case Gfx::StandardCursor::ResizeHorizontal:
  600. setCursor(Qt::SizeHorCursor);
  601. break;
  602. case Gfx::StandardCursor::ResizeVertical:
  603. setCursor(Qt::SizeVerCursor);
  604. break;
  605. case Gfx::StandardCursor::ResizeDiagonalTLBR:
  606. setCursor(Qt::SizeFDiagCursor);
  607. break;
  608. case Gfx::StandardCursor::ResizeDiagonalBLTR:
  609. setCursor(Qt::SizeBDiagCursor);
  610. break;
  611. case Gfx::StandardCursor::ResizeColumn:
  612. setCursor(Qt::SplitHCursor);
  613. break;
  614. case Gfx::StandardCursor::ResizeRow:
  615. setCursor(Qt::SplitVCursor);
  616. break;
  617. case Gfx::StandardCursor::Hand:
  618. setCursor(Qt::PointingHandCursor);
  619. break;
  620. case Gfx::StandardCursor::Help:
  621. setCursor(Qt::WhatsThisCursor);
  622. break;
  623. case Gfx::StandardCursor::Drag:
  624. setCursor(Qt::ClosedHandCursor);
  625. break;
  626. case Gfx::StandardCursor::DragCopy:
  627. setCursor(Qt::DragCopyCursor);
  628. break;
  629. case Gfx::StandardCursor::Move:
  630. setCursor(Qt::DragMoveCursor);
  631. break;
  632. case Gfx::StandardCursor::Wait:
  633. setCursor(Qt::BusyCursor);
  634. break;
  635. case Gfx::StandardCursor::Disallowed:
  636. setCursor(Qt::ForbiddenCursor);
  637. break;
  638. case Gfx::StandardCursor::Eyedropper:
  639. case Gfx::StandardCursor::Zoom:
  640. // FIXME: No corresponding Qt cursors, default to Arrow
  641. default:
  642. setCursor(Qt::ArrowCursor);
  643. break;
  644. }
  645. }
  646. Web::DevicePixelSize WebContentView::viewport_size() const
  647. {
  648. return m_viewport_size.to_type<Web::DevicePixels>();
  649. }
  650. QPoint WebContentView::map_point_to_global_position(Gfx::IntPoint position) const
  651. {
  652. return mapToGlobal(QPoint { position.x(), position.y() } / device_pixel_ratio());
  653. }
  654. Gfx::IntPoint WebContentView::to_content_position(Gfx::IntPoint widget_position) const
  655. {
  656. return widget_position;
  657. }
  658. Gfx::IntPoint WebContentView::to_widget_position(Gfx::IntPoint content_position) const
  659. {
  660. return content_position;
  661. }
  662. bool WebContentView::event(QEvent* event)
  663. {
  664. // NOTE: We have to implement event() manually as Qt's focus navigation mechanism
  665. // eats all the Tab key presses by default.
  666. if (event->type() == QEvent::KeyPress) {
  667. keyPressEvent(static_cast<QKeyEvent*>(event));
  668. return true;
  669. }
  670. if (event->type() == QEvent::KeyRelease) {
  671. keyReleaseEvent(static_cast<QKeyEvent*>(event));
  672. return true;
  673. }
  674. if (event->type() == QEvent::PaletteChange) {
  675. update_palette();
  676. return QAbstractScrollArea::event(event);
  677. }
  678. if (event->type() == QEvent::ShortcutOverride) {
  679. event->accept();
  680. return true;
  681. }
  682. return QAbstractScrollArea::event(event);
  683. }
  684. void WebContentView::enqueue_native_event(Web::MouseEvent::Type type, QSinglePointEvent const& event)
  685. {
  686. Web::DevicePixelPoint position = { event.position().x() * m_device_pixel_ratio, event.position().y() * m_device_pixel_ratio };
  687. auto screen_position = Gfx::IntPoint { event.globalPosition().x() * m_device_pixel_ratio, event.globalPosition().y() * m_device_pixel_ratio };
  688. auto button = get_button_from_qt_mouse_button(event.button());
  689. auto buttons = get_buttons_from_qt_mouse_buttons(event.buttons());
  690. auto modifiers = get_modifiers_from_qt_keyboard_modifiers(event.modifiers());
  691. if (button == 0 && (type == Web::MouseEvent::Type::MouseDown || type == Web::MouseEvent::Type::MouseUp)) {
  692. // We could not convert Qt buttons to something that LibWeb can recognize - don't even bother propagating this
  693. // to the web engine as it will not handle it anyway, and it will (currently) assert.
  694. return;
  695. }
  696. int wheel_delta_x = 0;
  697. int wheel_delta_y = 0;
  698. if (type == Web::MouseEvent::Type::MouseWheel) {
  699. auto const& wheel_event = static_cast<QWheelEvent const&>(event);
  700. if (auto pixel_delta = -wheel_event.pixelDelta(); !pixel_delta.isNull()) {
  701. wheel_delta_x = pixel_delta.x();
  702. wheel_delta_y = pixel_delta.y();
  703. } else {
  704. auto angle_delta = -wheel_event.angleDelta();
  705. float delta_x = -static_cast<float>(angle_delta.x()) / 120.0f;
  706. float delta_y = static_cast<float>(angle_delta.y()) / 120.0f;
  707. auto step_x = delta_x * static_cast<float>(QApplication::wheelScrollLines()) * m_device_pixel_ratio;
  708. auto step_y = delta_y * static_cast<float>(QApplication::wheelScrollLines()) * m_device_pixel_ratio;
  709. auto scroll_step_size = static_cast<float>(verticalScrollBar()->singleStep());
  710. wheel_delta_x = static_cast<int>(step_x * scroll_step_size);
  711. wheel_delta_y = static_cast<int>(step_y * scroll_step_size);
  712. }
  713. }
  714. enqueue_input_event(Web::MouseEvent { type, position, screen_position.to_type<Web::DevicePixels>(), button, buttons, modifiers, wheel_delta_x, wheel_delta_y, nullptr });
  715. }
  716. struct DragData : Web::ChromeInputData {
  717. explicit DragData(QDropEvent const& event)
  718. : urls(event.mimeData()->urls())
  719. {
  720. }
  721. QList<QUrl> urls;
  722. };
  723. void WebContentView::enqueue_native_event(Web::DragEvent::Type type, QDropEvent const& event)
  724. {
  725. Web::DevicePixelPoint position = { event.position().x() * m_device_pixel_ratio, event.position().y() * m_device_pixel_ratio };
  726. auto global_position = mapToGlobal(event.position());
  727. auto screen_position = Gfx::IntPoint { global_position.x() * m_device_pixel_ratio, global_position.y() * m_device_pixel_ratio };
  728. auto button = get_button_from_qt_mouse_button(Qt::LeftButton);
  729. auto buttons = get_buttons_from_qt_mouse_buttons(event.buttons());
  730. auto modifiers = get_modifiers_from_qt_keyboard_modifiers(event.modifiers());
  731. Vector<Web::HTML::SelectedFile> files;
  732. OwnPtr<DragData> chrome_data;
  733. if (type == Web::DragEvent::Type::DragStart) {
  734. VERIFY(event.mimeData()->hasUrls());
  735. for (auto const& url : event.mimeData()->urls()) {
  736. auto file_path = ak_byte_string_from_qstring(url.toLocalFile());
  737. if (auto file = Web::HTML::SelectedFile::from_file_path(file_path); file.is_error())
  738. warnln("Unable to open file {}: {}", file_path, file.error());
  739. else
  740. files.append(file.release_value());
  741. }
  742. } else if (type == Web::DragEvent::Type::Drop) {
  743. chrome_data = make<DragData>(event);
  744. }
  745. enqueue_input_event(Web::DragEvent { type, position, screen_position.to_type<Web::DevicePixels>(), button, buttons, modifiers, AK::move(files), AK::move(chrome_data) });
  746. }
  747. void WebContentView::finish_handling_drag_event(Web::DragEvent const& event)
  748. {
  749. if (event.type != Web::DragEvent::Type::Drop)
  750. return;
  751. auto const& chrome_data = verify_cast<DragData>(*event.chrome_data);
  752. emit urls_dropped(chrome_data.urls);
  753. }
  754. struct KeyData : Web::ChromeInputData {
  755. explicit KeyData(QKeyEvent const& event)
  756. : event(adopt_own(*event.clone()))
  757. {
  758. }
  759. NonnullOwnPtr<QKeyEvent> event;
  760. };
  761. void WebContentView::enqueue_native_event(Web::KeyEvent::Type type, QKeyEvent const& event)
  762. {
  763. auto keycode = get_keycode_from_qt_key_event(event);
  764. auto modifiers = get_modifiers_from_qt_key_event(event);
  765. auto text = event.text();
  766. auto code_point = text.isEmpty() ? 0u : event.text()[0].unicode();
  767. auto to_web_event = [&]() -> Web::KeyEvent {
  768. if (event.key() == Qt::Key_Backtab) {
  769. // Qt transforms Shift+Tab into a "Backtab", so we undo that transformation here.
  770. return { type, Web::UIEvents::KeyCode::Key_Tab, Web::UIEvents::Mod_Shift, '\t', make<KeyData>(event) };
  771. }
  772. if (event.key() == Qt::Key_Enter || event.key() == Qt::Key_Return) {
  773. // This ensures consistent behavior between systems that treat Enter as '\n' and '\r\n'
  774. return { type, Web::UIEvents::KeyCode::Key_Return, Web::UIEvents::Mod_Shift, '\n', make<KeyData>(event) };
  775. }
  776. return { type, keycode, modifiers, code_point, make<KeyData>(event) };
  777. };
  778. enqueue_input_event(to_web_event());
  779. }
  780. void WebContentView::finish_handling_key_event(Web::KeyEvent const& key_event)
  781. {
  782. auto& chrome_data = verify_cast<KeyData>(*key_event.chrome_data);
  783. auto& event = *chrome_data.event;
  784. switch (key_event.type) {
  785. case Web::KeyEvent::Type::KeyDown:
  786. QAbstractScrollArea::keyPressEvent(&event);
  787. break;
  788. case Web::KeyEvent::Type::KeyUp:
  789. QAbstractScrollArea::keyReleaseEvent(&event);
  790. break;
  791. }
  792. if (!event.isAccepted())
  793. QApplication::sendEvent(parent(), &event);
  794. }
  795. }