BrowserWindow.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822
  1. /*
  2. * Copyright (c) 2022-2023, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, Matthew Costa <ucosty@gmail.com>
  4. * Copyright (c) 2022, Filiph Sandström <filiph.sandstrom@filfatstudios.com>
  5. * Copyright (c) 2023, Linus Groh <linusg@serenityos.org>
  6. *
  7. * SPDX-License-Identifier: BSD-2-Clause
  8. */
  9. #include "BrowserWindow.h"
  10. #include "Icon.h"
  11. #include "Settings.h"
  12. #include "SettingsDialog.h"
  13. #include "StringUtils.h"
  14. #include "WebContentView.h"
  15. #include <AK/TypeCasts.h>
  16. #include <Ladybird/Utilities.h>
  17. #include <LibWeb/CSS/PreferredColorScheme.h>
  18. #include <LibWeb/Loader/ResourceLoader.h>
  19. #include <LibWebView/CookieJar.h>
  20. #include <LibWebView/UserAgent.h>
  21. #include <QAction>
  22. #include <QActionGroup>
  23. #include <QClipboard>
  24. #include <QGuiApplication>
  25. #include <QInputDialog>
  26. #include <QPlainTextEdit>
  27. #include <QShortcut>
  28. #include <QTabBar>
  29. #include <QWindow>
  30. namespace Ladybird {
  31. static QIcon const& app_icon()
  32. {
  33. static QIcon icon;
  34. if (icon.isNull()) {
  35. QPixmap pixmap;
  36. pixmap.load(":/Icons/ladybird.png");
  37. icon = QIcon(pixmap);
  38. }
  39. return icon;
  40. }
  41. BrowserWindow::BrowserWindow(Vector<URL::URL> const& initial_urls, WebView::CookieJar& cookie_jar, WebContentOptions const& web_content_options, StringView webdriver_content_ipc_path)
  42. : m_cookie_jar(cookie_jar)
  43. , m_web_content_options(web_content_options)
  44. , m_webdriver_content_ipc_path(webdriver_content_ipc_path)
  45. {
  46. setWindowIcon(app_icon());
  47. m_tabs_container = new QTabWidget(this);
  48. m_tabs_container->installEventFilter(this);
  49. m_tabs_container->setElideMode(Qt::TextElideMode::ElideRight);
  50. m_tabs_container->setMovable(true);
  51. m_tabs_container->setTabsClosable(true);
  52. m_tabs_container->setDocumentMode(true);
  53. m_tabs_container->setTabBarAutoHide(true);
  54. // Listen for DPI changes
  55. m_device_pixel_ratio = devicePixelRatio();
  56. m_current_screen = screen();
  57. if (QT_VERSION < QT_VERSION_CHECK(6, 6, 0) || QGuiApplication::platformName() != "wayland") {
  58. setAttribute(Qt::WA_NativeWindow);
  59. setAttribute(Qt::WA_DontCreateNativeAncestors);
  60. QObject::connect(m_current_screen, &QScreen::logicalDotsPerInchChanged, this, &BrowserWindow::device_pixel_ratio_changed);
  61. QObject::connect(windowHandle(), &QWindow::screenChanged, this, [this](QScreen* screen) {
  62. if (m_device_pixel_ratio != devicePixelRatio())
  63. device_pixel_ratio_changed(devicePixelRatio());
  64. // Listen for logicalDotsPerInchChanged signals on new screen
  65. QObject::disconnect(m_current_screen, &QScreen::logicalDotsPerInchChanged, nullptr, nullptr);
  66. m_current_screen = screen;
  67. QObject::connect(m_current_screen, &QScreen::logicalDotsPerInchChanged, this, &BrowserWindow::device_pixel_ratio_changed);
  68. });
  69. }
  70. auto* menu = menuBar()->addMenu("&File");
  71. auto* about_action = new QAction("&About Ladybird", this);
  72. menu->addAction(about_action);
  73. menu->addSeparator();
  74. auto* new_tab_action = new QAction("New &Tab", this);
  75. new_tab_action->setIcon(load_icon_from_uri("resource://icons/16x16/new-tab.png"sv));
  76. new_tab_action->setShortcuts(QKeySequence::keyBindings(QKeySequence::StandardKey::AddTab));
  77. menu->addAction(new_tab_action);
  78. auto* close_current_tab_action = new QAction("&Close Current Tab", this);
  79. close_current_tab_action->setIcon(load_icon_from_uri("resource://icons/16x16/close-tab.png"sv));
  80. close_current_tab_action->setShortcuts(QKeySequence::keyBindings(QKeySequence::StandardKey::Close));
  81. menu->addAction(close_current_tab_action);
  82. auto* open_file_action = new QAction("&Open File...", this);
  83. open_file_action->setIcon(load_icon_from_uri("resource://icons/16x16/filetype-folder-open.png"sv));
  84. open_file_action->setShortcut(QKeySequence(QKeySequence::StandardKey::Open));
  85. menu->addAction(open_file_action);
  86. menu->addSeparator();
  87. auto* quit_action = new QAction("&Quit", this);
  88. quit_action->setShortcuts(QKeySequence::keyBindings(QKeySequence::StandardKey::Quit));
  89. menu->addAction(quit_action);
  90. auto* edit_menu = menuBar()->addMenu("&Edit");
  91. m_copy_selection_action = new QAction("&Copy", this);
  92. m_copy_selection_action->setIcon(load_icon_from_uri("resource://icons/16x16/edit-copy.png"sv));
  93. m_copy_selection_action->setShortcuts(QKeySequence::keyBindings(QKeySequence::StandardKey::Copy));
  94. edit_menu->addAction(m_copy_selection_action);
  95. QObject::connect(m_copy_selection_action, &QAction::triggered, this, &BrowserWindow::copy_selected_text);
  96. m_paste_action = new QAction("&Paste", this);
  97. m_paste_action->setIcon(load_icon_from_uri("resource://icons/16x16/paste.png"sv));
  98. m_paste_action->setShortcuts(QKeySequence::keyBindings(QKeySequence::StandardKey::Paste));
  99. edit_menu->addAction(m_paste_action);
  100. QObject::connect(m_paste_action, &QAction::triggered, this, &BrowserWindow::paste);
  101. m_select_all_action = new QAction("Select &All", this);
  102. m_select_all_action->setIcon(load_icon_from_uri("resource://icons/16x16/select-all.png"sv));
  103. m_select_all_action->setShortcuts(QKeySequence::keyBindings(QKeySequence::StandardKey::SelectAll));
  104. edit_menu->addAction(m_select_all_action);
  105. QObject::connect(m_select_all_action, &QAction::triggered, this, &BrowserWindow::select_all);
  106. edit_menu->addSeparator();
  107. auto* settings_action = new QAction("&Settings", this);
  108. settings_action->setIcon(load_icon_from_uri("resource://icons/16x16/settings.png"sv));
  109. settings_action->setShortcuts(QKeySequence::keyBindings(QKeySequence::StandardKey::Preferences));
  110. edit_menu->addAction(settings_action);
  111. auto* view_menu = menuBar()->addMenu("&View");
  112. auto* open_next_tab_action = new QAction("Open &Next Tab", this);
  113. open_next_tab_action->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_PageDown));
  114. view_menu->addAction(open_next_tab_action);
  115. QObject::connect(open_next_tab_action, &QAction::triggered, this, &BrowserWindow::open_next_tab);
  116. auto* open_previous_tab_action = new QAction("Open &Previous Tab", this);
  117. open_previous_tab_action->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_PageUp));
  118. view_menu->addAction(open_previous_tab_action);
  119. QObject::connect(open_previous_tab_action, &QAction::triggered, this, &BrowserWindow::open_previous_tab);
  120. view_menu->addSeparator();
  121. m_zoom_menu = view_menu->addMenu("&Zoom");
  122. auto* zoom_in_action = new QAction("Zoom &In", this);
  123. zoom_in_action->setIcon(load_icon_from_uri("resource://icons/16x16/zoom-in.png"sv));
  124. auto zoom_in_shortcuts = QKeySequence::keyBindings(QKeySequence::StandardKey::ZoomIn);
  125. zoom_in_shortcuts.append(QKeySequence(Qt::CTRL | Qt::Key_Equal));
  126. zoom_in_action->setShortcuts(zoom_in_shortcuts);
  127. m_zoom_menu->addAction(zoom_in_action);
  128. QObject::connect(zoom_in_action, &QAction::triggered, this, &BrowserWindow::zoom_in);
  129. auto* zoom_out_action = new QAction("Zoom &Out", this);
  130. zoom_out_action->setIcon(load_icon_from_uri("resource://icons/16x16/zoom-out.png"sv));
  131. zoom_out_action->setShortcuts(QKeySequence::keyBindings(QKeySequence::StandardKey::ZoomOut));
  132. m_zoom_menu->addAction(zoom_out_action);
  133. QObject::connect(zoom_out_action, &QAction::triggered, this, &BrowserWindow::zoom_out);
  134. auto* reset_zoom_action = new QAction("&Reset Zoom", this);
  135. reset_zoom_action->setIcon(load_icon_from_uri("resource://icons/16x16/zoom-reset.png"sv));
  136. reset_zoom_action->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_0));
  137. m_zoom_menu->addAction(reset_zoom_action);
  138. QObject::connect(reset_zoom_action, &QAction::triggered, this, &BrowserWindow::reset_zoom);
  139. view_menu->addSeparator();
  140. auto* color_scheme_menu = view_menu->addMenu("&Color Scheme");
  141. auto* color_scheme_group = new QActionGroup(this);
  142. auto* auto_color_scheme = new QAction("&Auto", this);
  143. auto_color_scheme->setCheckable(true);
  144. color_scheme_group->addAction(auto_color_scheme);
  145. color_scheme_menu->addAction(auto_color_scheme);
  146. QObject::connect(auto_color_scheme, &QAction::triggered, this, &BrowserWindow::enable_auto_color_scheme);
  147. auto* light_color_scheme = new QAction("&Light", this);
  148. light_color_scheme->setCheckable(true);
  149. color_scheme_group->addAction(light_color_scheme);
  150. color_scheme_menu->addAction(light_color_scheme);
  151. QObject::connect(light_color_scheme, &QAction::triggered, this, &BrowserWindow::enable_light_color_scheme);
  152. auto* dark_color_scheme = new QAction("&Dark", this);
  153. dark_color_scheme->setCheckable(true);
  154. color_scheme_group->addAction(dark_color_scheme);
  155. color_scheme_menu->addAction(dark_color_scheme);
  156. QObject::connect(dark_color_scheme, &QAction::triggered, this, &BrowserWindow::enable_dark_color_scheme);
  157. auto_color_scheme->setChecked(true);
  158. auto* inspect_menu = menuBar()->addMenu("&Inspect");
  159. m_view_source_action = new QAction("View &Source", this);
  160. m_view_source_action->setIcon(load_icon_from_uri("resource://icons/16x16/filetype-html.png"sv));
  161. m_view_source_action->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_U));
  162. inspect_menu->addAction(m_view_source_action);
  163. QObject::connect(m_view_source_action, &QAction::triggered, this, [this] {
  164. if (m_current_tab) {
  165. m_current_tab->view().get_source();
  166. }
  167. });
  168. auto* inspector_action = new QAction("Open &Inspector", this);
  169. inspector_action->setIcon(load_icon_from_uri("resource://icons/browser/dom-tree.png"sv));
  170. inspector_action->setShortcuts({ QKeySequence("Ctrl+Shift+I"), QKeySequence("F12") });
  171. inspect_menu->addAction(inspector_action);
  172. QObject::connect(inspector_action, &QAction::triggered, this, [this] {
  173. if (m_current_tab) {
  174. m_current_tab->show_inspector_window();
  175. }
  176. });
  177. auto* debug_menu = menuBar()->addMenu("&Debug");
  178. auto* dump_session_history_tree_action = new QAction("Dump Session History Tree", this);
  179. debug_menu->addAction(dump_session_history_tree_action);
  180. QObject::connect(dump_session_history_tree_action, &QAction::triggered, this, [this] {
  181. debug_request("dump-session-history");
  182. });
  183. auto* dump_dom_tree_action = new QAction("Dump &DOM Tree", this);
  184. dump_dom_tree_action->setIcon(load_icon_from_uri("resource://icons/browser/dom-tree.png"sv));
  185. debug_menu->addAction(dump_dom_tree_action);
  186. QObject::connect(dump_dom_tree_action, &QAction::triggered, this, [this] {
  187. debug_request("dump-dom-tree");
  188. });
  189. auto* dump_layout_tree_action = new QAction("Dump &Layout Tree", this);
  190. dump_layout_tree_action->setIcon(load_icon_from_uri("resource://icons/16x16/layout.png"sv));
  191. debug_menu->addAction(dump_layout_tree_action);
  192. QObject::connect(dump_layout_tree_action, &QAction::triggered, this, [this] {
  193. debug_request("dump-layout-tree");
  194. });
  195. auto* dump_paint_tree_action = new QAction("Dump &Paint Tree", this);
  196. dump_paint_tree_action->setIcon(load_icon_from_uri("resource://icons/16x16/layout.png"sv));
  197. debug_menu->addAction(dump_paint_tree_action);
  198. QObject::connect(dump_paint_tree_action, &QAction::triggered, this, [this] {
  199. debug_request("dump-paint-tree");
  200. });
  201. auto* dump_stacking_context_tree_action = new QAction("Dump S&tacking Context Tree", this);
  202. dump_stacking_context_tree_action->setIcon(load_icon_from_uri("resource://icons/16x16/layers.png"sv));
  203. debug_menu->addAction(dump_stacking_context_tree_action);
  204. QObject::connect(dump_stacking_context_tree_action, &QAction::triggered, this, [this] {
  205. debug_request("dump-stacking-context-tree");
  206. });
  207. auto* dump_style_sheets_action = new QAction("Dump &Style Sheets", this);
  208. dump_style_sheets_action->setIcon(load_icon_from_uri("resource://icons/16x16/filetype-css.png"sv));
  209. debug_menu->addAction(dump_style_sheets_action);
  210. QObject::connect(dump_style_sheets_action, &QAction::triggered, this, [this] {
  211. debug_request("dump-style-sheets");
  212. });
  213. auto* dump_styles_action = new QAction("Dump &All Resolved Styles", this);
  214. dump_styles_action->setIcon(load_icon_from_uri("resource://icons/16x16/filetype-css.png"sv));
  215. debug_menu->addAction(dump_styles_action);
  216. QObject::connect(dump_styles_action, &QAction::triggered, this, [this] {
  217. debug_request("dump-all-resolved-styles");
  218. });
  219. auto* dump_history_action = new QAction("Dump &History", this);
  220. dump_history_action->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_H));
  221. dump_history_action->setIcon(load_icon_from_uri("resource://icons/16x16/history.png"sv));
  222. debug_menu->addAction(dump_history_action);
  223. QObject::connect(dump_history_action, &QAction::triggered, this, [this] {
  224. debug_request("dump-history");
  225. });
  226. auto* dump_cookies_action = new QAction("Dump C&ookies", this);
  227. dump_cookies_action->setIcon(load_icon_from_uri("resource://icons/browser/cookie.png"sv));
  228. debug_menu->addAction(dump_cookies_action);
  229. QObject::connect(dump_cookies_action, &QAction::triggered, this, [this] {
  230. m_cookie_jar.dump_cookies();
  231. });
  232. auto* dump_local_storage_action = new QAction("Dump Loc&al Storage", this);
  233. dump_local_storage_action->setIcon(load_icon_from_uri("resource://icons/browser/local-storage.png"sv));
  234. debug_menu->addAction(dump_local_storage_action);
  235. QObject::connect(dump_local_storage_action, &QAction::triggered, this, [this] {
  236. debug_request("dump-local-storage");
  237. });
  238. debug_menu->addSeparator();
  239. auto* show_line_box_borders_action = new QAction("Show Line Box Borders", this);
  240. show_line_box_borders_action->setCheckable(true);
  241. debug_menu->addAction(show_line_box_borders_action);
  242. QObject::connect(show_line_box_borders_action, &QAction::triggered, this, [this, show_line_box_borders_action] {
  243. bool state = show_line_box_borders_action->isChecked();
  244. debug_request("set-line-box-borders", state ? "on" : "off");
  245. });
  246. debug_menu->addSeparator();
  247. auto* collect_garbage_action = new QAction("Collect &Garbage", this);
  248. collect_garbage_action->setShortcut(QKeySequence(Qt::CTRL | Qt::SHIFT | Qt::Key_G));
  249. collect_garbage_action->setIcon(load_icon_from_uri("resource://icons/16x16/trash-can.png"sv));
  250. debug_menu->addAction(collect_garbage_action);
  251. QObject::connect(collect_garbage_action, &QAction::triggered, this, [this] {
  252. debug_request("collect-garbage");
  253. });
  254. auto* dump_gc_graph_action = new QAction("Dump GC graph", this);
  255. debug_menu->addAction(dump_gc_graph_action);
  256. QObject::connect(dump_gc_graph_action, &QAction::triggered, this, [this] {
  257. if (m_current_tab) {
  258. auto gc_graph_path = m_current_tab->view().dump_gc_graph();
  259. warnln("\033[33;1mDumped GC-graph into {}"
  260. "\033[0m",
  261. gc_graph_path);
  262. }
  263. });
  264. auto* clear_cache_action = new QAction("Clear &Cache", this);
  265. clear_cache_action->setShortcut(QKeySequence(Qt::CTRL | Qt::SHIFT | Qt::Key_C));
  266. clear_cache_action->setIcon(load_icon_from_uri("resource://icons/browser/clear-cache.png"sv));
  267. debug_menu->addAction(clear_cache_action);
  268. QObject::connect(clear_cache_action, &QAction::triggered, this, [this] {
  269. debug_request("clear-cache");
  270. });
  271. auto* spoof_user_agent_menu = debug_menu->addMenu("Spoof &User Agent");
  272. spoof_user_agent_menu->setIcon(load_icon_from_uri("resource://icons/16x16/spoof.png"sv));
  273. auto* user_agent_group = new QActionGroup(this);
  274. auto add_user_agent = [this, &user_agent_group, &spoof_user_agent_menu](auto name, auto const& user_agent) {
  275. auto* action = new QAction(qstring_from_ak_string(name), this);
  276. action->setCheckable(true);
  277. user_agent_group->addAction(action);
  278. spoof_user_agent_menu->addAction(action);
  279. QObject::connect(action, &QAction::triggered, this, [this, user_agent] {
  280. debug_request("spoof-user-agent", user_agent);
  281. debug_request("clear-cache"); // clear the cache to ensure requests are re-done with the new user agent
  282. });
  283. return action;
  284. };
  285. auto* disable_spoofing = add_user_agent("Disabled"sv, Web::default_user_agent);
  286. disable_spoofing->setChecked(true);
  287. for (auto const& user_agent : WebView::user_agents)
  288. add_user_agent(user_agent.key, user_agent.value.to_byte_string());
  289. auto* custom_user_agent_action = new QAction("Custom...", this);
  290. custom_user_agent_action->setCheckable(true);
  291. user_agent_group->addAction(custom_user_agent_action);
  292. spoof_user_agent_menu->addAction(custom_user_agent_action);
  293. QObject::connect(custom_user_agent_action, &QAction::triggered, this, [this, disable_spoofing] {
  294. auto user_agent = QInputDialog::getText(this, "Custom User Agent", "Enter User Agent:");
  295. if (!user_agent.isEmpty()) {
  296. debug_request("spoof-user-agent", ak_byte_string_from_qstring(user_agent));
  297. debug_request("clear-cache"); // clear the cache to ensure requests are re-done with the new user agent
  298. } else {
  299. disable_spoofing->activate(QAction::Trigger);
  300. }
  301. });
  302. debug_menu->addSeparator();
  303. auto* enable_scripting_action = new QAction("Enable Scripting", this);
  304. enable_scripting_action->setCheckable(true);
  305. enable_scripting_action->setChecked(true);
  306. debug_menu->addAction(enable_scripting_action);
  307. QObject::connect(enable_scripting_action, &QAction::triggered, this, [this, enable_scripting_action] {
  308. bool state = enable_scripting_action->isChecked();
  309. debug_request("scripting", state ? "on" : "off");
  310. });
  311. auto* block_pop_ups_action = new QAction("Block Pop-ups", this);
  312. block_pop_ups_action->setCheckable(true);
  313. block_pop_ups_action->setChecked(true);
  314. debug_menu->addAction(block_pop_ups_action);
  315. QObject::connect(block_pop_ups_action, &QAction::triggered, this, [this, block_pop_ups_action] {
  316. bool state = block_pop_ups_action->isChecked();
  317. debug_request("block-pop-ups", state ? "on" : "off");
  318. });
  319. auto* enable_same_origin_policy_action = new QAction("Enable Same-Origin Policy", this);
  320. enable_same_origin_policy_action->setCheckable(true);
  321. debug_menu->addAction(enable_same_origin_policy_action);
  322. QObject::connect(enable_same_origin_policy_action, &QAction::triggered, this, [this, enable_same_origin_policy_action] {
  323. bool state = enable_same_origin_policy_action->isChecked();
  324. debug_request("same-origin-policy", state ? "on" : "off");
  325. });
  326. QObject::connect(about_action, &QAction::triggered, this, [this] {
  327. new_tab_from_url("about:version"sv, Web::HTML::ActivateTab::Yes);
  328. });
  329. QObject::connect(new_tab_action, &QAction::triggered, this, [this] {
  330. new_tab_from_url(ak_url_from_qstring(Settings::the()->new_tab_page()), Web::HTML::ActivateTab::Yes);
  331. });
  332. QObject::connect(open_file_action, &QAction::triggered, this, &BrowserWindow::open_file);
  333. QObject::connect(settings_action, &QAction::triggered, this, [this] {
  334. if (!m_settings_dialog) {
  335. m_settings_dialog = new SettingsDialog(this);
  336. }
  337. m_settings_dialog->show();
  338. m_settings_dialog->setFocus();
  339. });
  340. QObject::connect(quit_action, &QAction::triggered, this, &QMainWindow::close);
  341. QObject::connect(m_tabs_container, &QTabWidget::currentChanged, [this](int index) {
  342. setWindowTitle(QString("%1 - Ladybird").arg(m_tabs_container->tabText(index)));
  343. set_current_tab(verify_cast<Tab>(m_tabs_container->widget(index)));
  344. });
  345. QObject::connect(m_tabs_container, &QTabWidget::tabCloseRequested, this, &BrowserWindow::close_tab);
  346. QObject::connect(close_current_tab_action, &QAction::triggered, this, &BrowserWindow::close_current_tab);
  347. m_inspect_dom_node_action = new QAction("&Inspect Element", this);
  348. connect(m_inspect_dom_node_action, &QAction::triggered, this, [this] {
  349. if (m_current_tab)
  350. m_current_tab->show_inspector_window(Tab::InspectorTarget::HoveredElement);
  351. });
  352. m_go_back_action = new QAction("Go Back", this);
  353. connect(m_go_back_action, &QAction::triggered, this, [this] {
  354. if (m_current_tab)
  355. m_current_tab->back();
  356. });
  357. m_go_forward_action = new QAction("Go Forward", this);
  358. connect(m_go_forward_action, &QAction::triggered, this, [this] {
  359. if (m_current_tab)
  360. m_current_tab->forward();
  361. });
  362. m_reload_action = new QAction("&Reload", this);
  363. connect(m_reload_action, &QAction::triggered, this, [this] {
  364. if (m_current_tab)
  365. m_current_tab->reload();
  366. });
  367. m_go_back_action->setShortcuts(QKeySequence::keyBindings(QKeySequence::StandardKey::Back));
  368. m_go_forward_action->setShortcuts(QKeySequence::keyBindings(QKeySequence::StandardKey::Forward));
  369. m_reload_action->setShortcuts(QKeySequence::keyBindings(QKeySequence::StandardKey::Refresh));
  370. m_go_back_action->setEnabled(false);
  371. m_go_forward_action->setEnabled(false);
  372. m_reload_action->setEnabled(false);
  373. for (int i = 0; i <= 7; ++i) {
  374. new QShortcut(QKeySequence(Qt::CTRL | static_cast<Qt::Key>(Qt::Key_1 + i)), this, [this, i] {
  375. if (m_tabs_container->count() <= 1)
  376. return;
  377. m_tabs_container->setCurrentIndex(min(i, m_tabs_container->count() - 1));
  378. });
  379. }
  380. new QShortcut(QKeySequence(Qt::CTRL | Qt::Key_9), this, [this] {
  381. if (m_tabs_container->count() <= 1)
  382. return;
  383. m_tabs_container->setCurrentIndex(m_tabs_container->count() - 1);
  384. });
  385. for (size_t i = 0; i < initial_urls.size(); ++i) {
  386. new_tab_from_url(initial_urls[i], (i == 0) ? Web::HTML::ActivateTab::Yes : Web::HTML::ActivateTab::No);
  387. }
  388. setCentralWidget(m_tabs_container);
  389. setContextMenuPolicy(Qt::PreventContextMenu);
  390. }
  391. void BrowserWindow::set_current_tab(Tab* tab)
  392. {
  393. m_current_tab = tab;
  394. if (tab)
  395. update_displayed_zoom_level();
  396. }
  397. void BrowserWindow::debug_request(ByteString const& request, ByteString const& argument)
  398. {
  399. if (!m_current_tab)
  400. return;
  401. m_current_tab->debug_request(request, argument);
  402. }
  403. Tab& BrowserWindow::new_tab_from_url(URL::URL const& url, Web::HTML::ActivateTab activate_tab)
  404. {
  405. auto& tab = create_new_tab(activate_tab);
  406. tab.navigate(url);
  407. return tab;
  408. }
  409. Tab& BrowserWindow::new_tab_from_content(StringView html, Web::HTML::ActivateTab activate_tab)
  410. {
  411. auto& tab = create_new_tab(activate_tab);
  412. tab.load_html(html);
  413. return tab;
  414. }
  415. Tab& BrowserWindow::new_child_tab(Web::HTML::ActivateTab activate_tab, Tab& parent, Web::HTML::WebViewHints hints, Optional<u64> page_index)
  416. {
  417. return create_new_tab(activate_tab, parent, hints, page_index);
  418. }
  419. Tab& BrowserWindow::create_new_tab(Web::HTML::ActivateTab activate_tab, Tab& parent, Web::HTML::WebViewHints, Optional<u64> page_index)
  420. {
  421. if (!page_index.has_value())
  422. return create_new_tab(activate_tab);
  423. // FIXME: Respect hints for:
  424. // popup: Create new window
  425. // width, height: size of window
  426. // screen_x, screen_y: positioning of window on the screen
  427. auto* tab = new Tab(this, m_web_content_options, m_webdriver_content_ipc_path, parent.view().client(), page_index.value());
  428. VERIFY(m_current_tab != nullptr);
  429. m_tabs_container->addTab(tab, "New Tab");
  430. if (activate_tab == Web::HTML::ActivateTab::Yes)
  431. m_tabs_container->setCurrentWidget(tab);
  432. initialize_tab(tab);
  433. return *tab;
  434. }
  435. Tab& BrowserWindow::create_new_tab(Web::HTML::ActivateTab activate_tab)
  436. {
  437. auto* tab = new Tab(this, m_web_content_options, m_webdriver_content_ipc_path);
  438. if (m_current_tab == nullptr) {
  439. set_current_tab(tab);
  440. }
  441. m_tabs_container->addTab(tab, "New Tab");
  442. if (activate_tab == Web::HTML::ActivateTab::Yes)
  443. m_tabs_container->setCurrentWidget(tab);
  444. initialize_tab(tab);
  445. return *tab;
  446. }
  447. void BrowserWindow::initialize_tab(Tab* tab)
  448. {
  449. QObject::connect(tab, &Tab::title_changed, this, &BrowserWindow::tab_title_changed);
  450. QObject::connect(tab, &Tab::favicon_changed, this, &BrowserWindow::tab_favicon_changed);
  451. QObject::connect(&tab->view(), &WebContentView::urls_dropped, this, [this](auto& urls) {
  452. VERIFY(urls.size());
  453. m_current_tab->navigate(ak_url_from_qurl(urls[0]));
  454. for (qsizetype i = 1; i < urls.size(); ++i)
  455. new_tab_from_url(ak_url_from_qurl(urls[i]), Web::HTML::ActivateTab::No);
  456. });
  457. tab->view().on_new_web_view = [this, tab](auto activate_tab, Web::HTML::WebViewHints hints, Optional<u64> page_index) {
  458. auto& new_tab = new_child_tab(activate_tab, *tab, hints, page_index);
  459. return new_tab.view().handle();
  460. };
  461. tab->view().on_tab_open_request = [this](auto url, auto activate_tab) {
  462. auto& tab = new_tab_from_url(url, activate_tab);
  463. return tab.view().handle();
  464. };
  465. tab->view().on_link_click = [this](auto url, auto target, unsigned modifiers) {
  466. // TODO: maybe activate tabs according to some configuration, this is just normal current browser behavior
  467. if (modifiers == Mod_Ctrl) {
  468. m_current_tab->view().on_tab_open_request(url, Web::HTML::ActivateTab::No);
  469. } else if (target == "_blank") {
  470. m_current_tab->view().on_tab_open_request(url, Web::HTML::ActivateTab::Yes);
  471. } else {
  472. m_current_tab->view().load(url);
  473. }
  474. };
  475. tab->view().on_link_middle_click = [this](auto url, auto target, unsigned modifiers) {
  476. m_current_tab->view().on_link_click(url, target, Mod_Ctrl);
  477. (void)modifiers;
  478. };
  479. tab->view().on_get_all_cookies = [this](auto const& url) {
  480. return m_cookie_jar.get_all_cookies(url);
  481. };
  482. tab->view().on_get_named_cookie = [this](auto const& url, auto const& name) {
  483. return m_cookie_jar.get_named_cookie(url, name);
  484. };
  485. tab->view().on_get_cookie = [this](auto& url, auto source) {
  486. return m_cookie_jar.get_cookie(url, source);
  487. };
  488. tab->view().on_set_cookie = [this](auto& url, auto& cookie, auto source) {
  489. m_cookie_jar.set_cookie(url, cookie, source);
  490. };
  491. tab->view().on_update_cookie = [this](auto const& cookie) {
  492. m_cookie_jar.update_cookie(cookie);
  493. };
  494. tab->focus_location_editor();
  495. }
  496. void BrowserWindow::activate_tab(int index)
  497. {
  498. m_tabs_container->setCurrentIndex(index);
  499. }
  500. void BrowserWindow::close_tab(int index)
  501. {
  502. auto* tab = m_tabs_container->widget(index);
  503. m_tabs_container->removeTab(index);
  504. tab->deleteLater();
  505. }
  506. void BrowserWindow::open_file()
  507. {
  508. m_current_tab->open_file();
  509. }
  510. void BrowserWindow::close_current_tab()
  511. {
  512. close_tab(m_tabs_container->currentIndex());
  513. if (m_tabs_container->count() == 0)
  514. close();
  515. }
  516. int BrowserWindow::tab_index(Tab* tab)
  517. {
  518. return m_tabs_container->indexOf(tab);
  519. }
  520. void BrowserWindow::device_pixel_ratio_changed(qreal dpi)
  521. {
  522. m_device_pixel_ratio = dpi;
  523. for_each_tab([this](auto& tab) {
  524. tab.view().set_device_pixel_ratio(m_device_pixel_ratio);
  525. });
  526. }
  527. void BrowserWindow::tab_title_changed(int index, QString const& title)
  528. {
  529. m_tabs_container->setTabText(index, title);
  530. if (m_tabs_container->currentIndex() == index)
  531. setWindowTitle(QString("%1 - Ladybird").arg(title));
  532. }
  533. void BrowserWindow::tab_favicon_changed(int index, QIcon const& icon)
  534. {
  535. m_tabs_container->setTabIcon(index, icon);
  536. }
  537. void BrowserWindow::open_next_tab()
  538. {
  539. if (m_tabs_container->count() <= 1)
  540. return;
  541. auto next_index = m_tabs_container->currentIndex() + 1;
  542. if (next_index >= m_tabs_container->count())
  543. next_index = 0;
  544. m_tabs_container->setCurrentIndex(next_index);
  545. }
  546. void BrowserWindow::open_previous_tab()
  547. {
  548. if (m_tabs_container->count() <= 1)
  549. return;
  550. auto next_index = m_tabs_container->currentIndex() - 1;
  551. if (next_index < 0)
  552. next_index = m_tabs_container->count() - 1;
  553. m_tabs_container->setCurrentIndex(next_index);
  554. }
  555. void BrowserWindow::enable_auto_color_scheme()
  556. {
  557. for_each_tab([](auto& tab) {
  558. tab.view().set_preferred_color_scheme(Web::CSS::PreferredColorScheme::Auto);
  559. });
  560. }
  561. void BrowserWindow::enable_light_color_scheme()
  562. {
  563. for_each_tab([](auto& tab) {
  564. tab.view().set_preferred_color_scheme(Web::CSS::PreferredColorScheme::Light);
  565. });
  566. }
  567. void BrowserWindow::enable_dark_color_scheme()
  568. {
  569. for_each_tab([](auto& tab) {
  570. tab.view().set_preferred_color_scheme(Web::CSS::PreferredColorScheme::Dark);
  571. });
  572. }
  573. void BrowserWindow::zoom_in()
  574. {
  575. if (!m_current_tab)
  576. return;
  577. m_current_tab->view().zoom_in();
  578. update_displayed_zoom_level();
  579. }
  580. void BrowserWindow::zoom_out()
  581. {
  582. if (!m_current_tab)
  583. return;
  584. m_current_tab->view().zoom_out();
  585. update_displayed_zoom_level();
  586. }
  587. void BrowserWindow::reset_zoom()
  588. {
  589. if (!m_current_tab)
  590. return;
  591. m_current_tab->view().reset_zoom();
  592. update_displayed_zoom_level();
  593. }
  594. void BrowserWindow::update_zoom_menu()
  595. {
  596. VERIFY(m_zoom_menu);
  597. auto zoom_level_text = MUST(String::formatted("&Zoom ({}%)", round_to<int>(m_current_tab->view().zoom_level() * 100)));
  598. m_zoom_menu->setTitle(qstring_from_ak_string(zoom_level_text));
  599. }
  600. void BrowserWindow::select_all()
  601. {
  602. if (!m_current_tab)
  603. return;
  604. m_current_tab->view().select_all();
  605. }
  606. void BrowserWindow::paste()
  607. {
  608. if (!m_current_tab)
  609. return;
  610. auto* clipboard = QGuiApplication::clipboard();
  611. m_current_tab->view().paste(ak_string_from_qstring(clipboard->text()));
  612. }
  613. void BrowserWindow::update_displayed_zoom_level()
  614. {
  615. VERIFY(m_current_tab);
  616. update_zoom_menu();
  617. m_current_tab->update_reset_zoom_button();
  618. }
  619. void BrowserWindow::copy_selected_text()
  620. {
  621. if (!m_current_tab)
  622. return;
  623. auto text = m_current_tab->view().selected_text();
  624. auto* clipboard = QGuiApplication::clipboard();
  625. clipboard->setText(qstring_from_ak_string(text));
  626. }
  627. bool BrowserWindow::event(QEvent* event)
  628. {
  629. #if QT_VERSION >= QT_VERSION_CHECK(6, 6, 0)
  630. if (event->type() == QEvent::DevicePixelRatioChange) {
  631. if (m_device_pixel_ratio != devicePixelRatio())
  632. device_pixel_ratio_changed(devicePixelRatio());
  633. }
  634. #endif
  635. return QMainWindow::event(event);
  636. }
  637. void BrowserWindow::resizeEvent(QResizeEvent* event)
  638. {
  639. QWidget::resizeEvent(event);
  640. for_each_tab([&](auto& tab) {
  641. tab.view().set_window_size({ frameSize().width() * m_device_pixel_ratio, frameSize().height() * m_device_pixel_ratio });
  642. });
  643. }
  644. void BrowserWindow::moveEvent(QMoveEvent* event)
  645. {
  646. QWidget::moveEvent(event);
  647. for_each_tab([&](auto& tab) {
  648. tab.view().set_window_position({ event->pos().x() * m_device_pixel_ratio, event->pos().y() * m_device_pixel_ratio });
  649. });
  650. }
  651. void BrowserWindow::wheelEvent(QWheelEvent* event)
  652. {
  653. if ((event->modifiers() & Qt::ControlModifier) != 0) {
  654. if (event->angleDelta().y() > 0)
  655. zoom_in();
  656. else if (event->angleDelta().y() < 0)
  657. zoom_out();
  658. }
  659. }
  660. bool BrowserWindow::eventFilter(QObject* obj, QEvent* event)
  661. {
  662. if (event->type() == QEvent::MouseButtonRelease) {
  663. auto const* const mouse_event = static_cast<QMouseEvent*>(event);
  664. if (mouse_event->button() == Qt::MouseButton::MiddleButton) {
  665. if (obj == m_tabs_container) {
  666. auto const tab_index = m_tabs_container->tabBar()->tabAt(mouse_event->pos());
  667. close_tab(tab_index);
  668. return true;
  669. }
  670. }
  671. }
  672. return QMainWindow::eventFilter(obj, event);
  673. }
  674. void BrowserWindow::closeEvent(QCloseEvent* event)
  675. {
  676. Settings::the()->set_last_position(pos());
  677. Settings::the()->set_last_size(size());
  678. Settings::the()->set_is_maximized(isMaximized());
  679. QMainWindow::closeEvent(event);
  680. }
  681. }