BrowserWindow.cpp 31 KB

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