BrowserWindow.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  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 "ConsoleWidget.h"
  11. #include "Settings.h"
  12. #include "SettingsDialog.h"
  13. #include "Utilities.h"
  14. #include "WebContentView.h"
  15. #include <AK/TypeCasts.h>
  16. #include <Browser/CookieJar.h>
  17. #include <LibWeb/CSS/PreferredColorScheme.h>
  18. #include <LibWeb/Loader/ResourceLoader.h>
  19. #include <QAction>
  20. #include <QActionGroup>
  21. #include <QClipboard>
  22. #include <QGuiApplication>
  23. #include <QInputDialog>
  24. #include <QPlainTextEdit>
  25. #include <QTabBar>
  26. extern DeprecatedString s_serenity_resource_root;
  27. extern Browser::Settings* s_settings;
  28. BrowserWindow::BrowserWindow(Browser::CookieJar& cookie_jar, StringView webdriver_content_ipc_path, WebView::EnableCallgrindProfiling enable_callgrind_profiling)
  29. : m_cookie_jar(cookie_jar)
  30. , m_webdriver_content_ipc_path(webdriver_content_ipc_path)
  31. , m_enable_callgrind_profiling(enable_callgrind_profiling)
  32. {
  33. m_tabs_container = new QTabWidget(this);
  34. m_tabs_container->installEventFilter(this);
  35. m_tabs_container->setElideMode(Qt::TextElideMode::ElideRight);
  36. m_tabs_container->setMovable(true);
  37. m_tabs_container->setTabsClosable(true);
  38. m_tabs_container->setDocumentMode(true);
  39. m_tabs_container->setTabBarAutoHide(true);
  40. auto* menu = menuBar()->addMenu("&File");
  41. auto* new_tab_action = new QAction("New &Tab", this);
  42. new_tab_action->setShortcuts(QKeySequence::keyBindings(QKeySequence::StandardKey::AddTab));
  43. menu->addAction(new_tab_action);
  44. auto* settings_action = new QAction("&Settings", this);
  45. settings_action->setShortcuts(QKeySequence::keyBindings(QKeySequence::StandardKey::Preferences));
  46. menu->addAction(settings_action);
  47. auto* close_current_tab_action = new QAction("Close Current Tab", this);
  48. close_current_tab_action->setShortcuts(QKeySequence::keyBindings(QKeySequence::StandardKey::Close));
  49. menu->addAction(close_current_tab_action);
  50. auto* quit_action = new QAction("&Quit", this);
  51. quit_action->setShortcuts(QKeySequence::keyBindings(QKeySequence::StandardKey::Quit));
  52. menu->addAction(quit_action);
  53. auto* edit_menu = menuBar()->addMenu("&Edit");
  54. m_copy_selection_action = make<QAction>("&Copy", this);
  55. m_copy_selection_action->setShortcuts(QKeySequence::keyBindings(QKeySequence::StandardKey::Copy));
  56. edit_menu->addAction(m_copy_selection_action);
  57. QObject::connect(m_copy_selection_action, &QAction::triggered, this, &BrowserWindow::copy_selected_text);
  58. m_select_all_action = make<QAction>("Select &All", this);
  59. m_select_all_action->setShortcuts(QKeySequence::keyBindings(QKeySequence::StandardKey::SelectAll));
  60. edit_menu->addAction(m_select_all_action);
  61. QObject::connect(m_select_all_action, &QAction::triggered, this, &BrowserWindow::select_all);
  62. auto* view_menu = menuBar()->addMenu("&View");
  63. auto* open_next_tab_action = new QAction("Open &Next Tab", this);
  64. open_next_tab_action->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_PageDown));
  65. view_menu->addAction(open_next_tab_action);
  66. QObject::connect(open_next_tab_action, &QAction::triggered, this, &BrowserWindow::open_next_tab);
  67. auto* open_previous_tab_action = new QAction("Open &Previous Tab", this);
  68. open_previous_tab_action->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_PageUp));
  69. view_menu->addAction(open_previous_tab_action);
  70. QObject::connect(open_previous_tab_action, &QAction::triggered, this, &BrowserWindow::open_previous_tab);
  71. view_menu->addSeparator();
  72. m_zoom_menu = view_menu->addMenu("&Zoom");
  73. auto* zoom_in_action = new QAction("Zoom &In", this);
  74. auto zoom_in_shortcuts = QKeySequence::keyBindings(QKeySequence::StandardKey::ZoomIn);
  75. zoom_in_shortcuts.append(QKeySequence(Qt::CTRL | Qt::Key_Equal));
  76. zoom_in_action->setShortcuts(zoom_in_shortcuts);
  77. m_zoom_menu->addAction(zoom_in_action);
  78. QObject::connect(zoom_in_action, &QAction::triggered, this, &BrowserWindow::zoom_in);
  79. auto* zoom_out_action = new QAction("Zoom &Out", this);
  80. zoom_out_action->setShortcuts(QKeySequence::keyBindings(QKeySequence::StandardKey::ZoomOut));
  81. m_zoom_menu->addAction(zoom_out_action);
  82. QObject::connect(zoom_out_action, &QAction::triggered, this, &BrowserWindow::zoom_out);
  83. auto* reset_zoom_action = new QAction("&Reset Zoom", this);
  84. reset_zoom_action->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_0));
  85. m_zoom_menu->addAction(reset_zoom_action);
  86. QObject::connect(reset_zoom_action, &QAction::triggered, this, &BrowserWindow::reset_zoom);
  87. view_menu->addSeparator();
  88. auto* color_scheme_menu = view_menu->addMenu("&Color Scheme");
  89. auto* color_scheme_group = new QActionGroup(this);
  90. auto* auto_color_scheme = new QAction("&Auto", this);
  91. auto_color_scheme->setCheckable(true);
  92. color_scheme_group->addAction(auto_color_scheme);
  93. color_scheme_menu->addAction(auto_color_scheme);
  94. QObject::connect(auto_color_scheme, &QAction::triggered, this, &BrowserWindow::enable_auto_color_scheme);
  95. auto* light_color_scheme = new QAction("&Light", this);
  96. light_color_scheme->setCheckable(true);
  97. color_scheme_group->addAction(light_color_scheme);
  98. color_scheme_menu->addAction(light_color_scheme);
  99. QObject::connect(light_color_scheme, &QAction::triggered, this, &BrowserWindow::enable_light_color_scheme);
  100. auto* dark_color_scheme = new QAction("&Dark", this);
  101. dark_color_scheme->setCheckable(true);
  102. color_scheme_group->addAction(dark_color_scheme);
  103. color_scheme_menu->addAction(dark_color_scheme);
  104. QObject::connect(dark_color_scheme, &QAction::triggered, this, &BrowserWindow::enable_dark_color_scheme);
  105. auto_color_scheme->setChecked(true);
  106. auto* inspect_menu = menuBar()->addMenu("&Inspect");
  107. m_view_source_action = make<QAction>("View &Source", this);
  108. m_view_source_action->setIcon(QIcon(QString("%1/res/icons/16x16/filetype-html.png").arg(s_serenity_resource_root.characters())));
  109. m_view_source_action->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_U));
  110. inspect_menu->addAction(m_view_source_action);
  111. QObject::connect(m_view_source_action, &QAction::triggered, this, [this] {
  112. if (m_current_tab) {
  113. m_current_tab->view().get_source();
  114. }
  115. });
  116. auto* js_console_action = new QAction("Show &JS Console", this);
  117. js_console_action->setIcon(QIcon(QString("%1/res/icons/16x16/filetype-javascript.png").arg(s_serenity_resource_root.characters())));
  118. js_console_action->setShortcut(QKeySequence(Qt::CTRL | Qt::SHIFT | Qt::Key_J));
  119. inspect_menu->addAction(js_console_action);
  120. QObject::connect(js_console_action, &QAction::triggered, this, [this] {
  121. if (m_current_tab) {
  122. m_current_tab->view().show_js_console();
  123. }
  124. });
  125. auto* inspector_action = new QAction("Open &Inspector");
  126. inspector_action->setIcon(QIcon(QString("%1/res/icons/browser/dom-tree.png").arg(s_serenity_resource_root.characters())));
  127. inspector_action->setShortcut(QKeySequence("Ctrl+Shift+I"));
  128. inspect_menu->addAction(inspector_action);
  129. QObject::connect(inspector_action, &QAction::triggered, this, [this] {
  130. if (m_current_tab) {
  131. m_current_tab->view().show_inspector();
  132. }
  133. });
  134. auto* debug_menu = menuBar()->addMenu("&Debug");
  135. auto* dump_dom_tree_action = new QAction("Dump DOM Tree", this);
  136. dump_dom_tree_action->setIcon(QIcon(QString("%1/res/icons/browser/dom-tree.png").arg(s_serenity_resource_root.characters())));
  137. debug_menu->addAction(dump_dom_tree_action);
  138. QObject::connect(dump_dom_tree_action, &QAction::triggered, this, [this] {
  139. debug_request("dump-dom-tree");
  140. });
  141. auto* dump_layout_tree_action = new QAction("Dump Layout Tree", this);
  142. dump_layout_tree_action->setIcon(QIcon(QString("%1/res/icons/16x16/layout.png").arg(s_serenity_resource_root.characters())));
  143. debug_menu->addAction(dump_layout_tree_action);
  144. QObject::connect(dump_layout_tree_action, &QAction::triggered, this, [this] {
  145. debug_request("dump-layout-tree");
  146. });
  147. auto* dump_paint_tree_action = new QAction("Dump Paint Tree", this);
  148. debug_menu->addAction(dump_paint_tree_action);
  149. QObject::connect(dump_paint_tree_action, &QAction::triggered, this, [this] {
  150. debug_request("dump-paint-tree");
  151. });
  152. auto* dump_stacking_context_tree_action = new QAction("Dump Stacking Context Tree", this);
  153. dump_stacking_context_tree_action->setIcon(QIcon(QString("%1/res/icons/16x16/layers.png").arg(s_serenity_resource_root.characters())));
  154. debug_menu->addAction(dump_stacking_context_tree_action);
  155. QObject::connect(dump_stacking_context_tree_action, &QAction::triggered, this, [this] {
  156. debug_request("dump-stacking-context-tree");
  157. });
  158. auto* dump_style_sheets_action = new QAction("Dump Style Sheets", this);
  159. dump_style_sheets_action->setIcon(QIcon(QString("%1/res/icons/16x16/filetype-css.png").arg(s_serenity_resource_root.characters())));
  160. debug_menu->addAction(dump_style_sheets_action);
  161. QObject::connect(dump_style_sheets_action, &QAction::triggered, this, [this] {
  162. debug_request("dump-style-sheets");
  163. });
  164. auto* dump_history_action = new QAction("Dump History", this);
  165. dump_history_action->setIcon(QIcon(QString("%1/res/icons/16x16/history.png").arg(s_serenity_resource_root.characters())));
  166. debug_menu->addAction(dump_history_action);
  167. QObject::connect(dump_history_action, &QAction::triggered, this, [this] {
  168. debug_request("dump-history");
  169. });
  170. auto* dump_cookies_action = new QAction("Dump Cookies", this);
  171. dump_cookies_action->setIcon(QIcon(QString("%1/res/icons/browser/cookie.png").arg(s_serenity_resource_root.characters())));
  172. debug_menu->addAction(dump_cookies_action);
  173. QObject::connect(dump_cookies_action, &QAction::triggered, this, [this] {
  174. m_cookie_jar.dump_cookies();
  175. });
  176. auto* dump_local_storage_action = new QAction("Dump Local Storage", this);
  177. dump_local_storage_action->setIcon(QIcon(QString("%1/res/icons/browser/local-storage.png").arg(s_serenity_resource_root.characters())));
  178. debug_menu->addAction(dump_local_storage_action);
  179. QObject::connect(dump_local_storage_action, &QAction::triggered, this, [this] {
  180. debug_request("dump-local-storage");
  181. });
  182. debug_menu->addSeparator();
  183. auto* show_line_box_borders_action = new QAction("Show Line Box Borders", this);
  184. show_line_box_borders_action->setCheckable(true);
  185. debug_menu->addAction(show_line_box_borders_action);
  186. QObject::connect(show_line_box_borders_action, &QAction::triggered, this, [this, show_line_box_borders_action] {
  187. bool state = show_line_box_borders_action->isChecked();
  188. debug_request("set-line-box-borders", state ? "on" : "off");
  189. });
  190. debug_menu->addSeparator();
  191. auto* collect_garbage_action = new QAction("Collect Garbage", this);
  192. collect_garbage_action->setShortcut(QKeySequence(Qt::CTRL | Qt::SHIFT | Qt::Key_G));
  193. collect_garbage_action->setIcon(QIcon(QString("%1/res/icons/16x16/trash-can.png").arg(s_serenity_resource_root.characters())));
  194. debug_menu->addAction(collect_garbage_action);
  195. QObject::connect(collect_garbage_action, &QAction::triggered, this, [this] {
  196. debug_request("collect-garbage");
  197. });
  198. auto* clear_cache_action = new QAction("Clear Cache", this);
  199. clear_cache_action->setIcon(QIcon(QString("%1/res/icons/browser/clear-cache.png").arg(s_serenity_resource_root.characters())));
  200. debug_menu->addAction(clear_cache_action);
  201. QObject::connect(clear_cache_action, &QAction::triggered, this, [this] {
  202. debug_request("clear-cache");
  203. });
  204. auto* spoof_user_agent_menu = debug_menu->addMenu("Spoof User Agent");
  205. spoof_user_agent_menu->setIcon(QIcon(QString("%1/res/icons/16x16/spoof.png").arg(s_serenity_resource_root.characters())));
  206. auto* user_agent_group = new QActionGroup(this);
  207. auto add_user_agent = [this, &user_agent_group, &spoof_user_agent_menu](auto& name, auto& user_agent) {
  208. auto* action = new QAction(name);
  209. action->setCheckable(true);
  210. user_agent_group->addAction(action);
  211. spoof_user_agent_menu->addAction(action);
  212. QObject::connect(action, &QAction::triggered, this, [this, user_agent] {
  213. debug_request("spoof-user-agent", user_agent);
  214. debug_request("clear-cache"); // clear the cache to ensure requests are re-done with the new user agent
  215. });
  216. return action;
  217. };
  218. auto* disable_spoofing = add_user_agent("Disabled", Web::default_user_agent);
  219. disable_spoofing->setChecked(true);
  220. add_user_agent("Chrome Linux Desktop", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.128 Safari/537.36");
  221. add_user_agent("Firefox Linux Desktop", "Mozilla/5.0 (X11; Linux i686; rv:87.0) Gecko/20100101 Firefox/87.0");
  222. add_user_agent("Safari macOS Desktop", "Mozilla/5.0 (Macintosh; Intel Mac OS X 11_2_3) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0.3 Safari/605.1.15");
  223. add_user_agent("Chrome Android Mobile", "Mozilla/5.0 (Linux; Android 10) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.66 Mobile Safari/537.36");
  224. add_user_agent("Firefox Android Mobile", "Mozilla/5.0 (Android 11; Mobile; rv:68.0) Gecko/68.0 Firefox/86.0");
  225. add_user_agent("Safari iOS Mobile", "Mozilla/5.0 (iPhone; CPU iPhone OS 14_4_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0 Mobile/15E148 Safari/604.1");
  226. auto* custom_user_agent_action = new QAction("Custom...");
  227. custom_user_agent_action->setCheckable(true);
  228. user_agent_group->addAction(custom_user_agent_action);
  229. spoof_user_agent_menu->addAction(custom_user_agent_action);
  230. QObject::connect(custom_user_agent_action, &QAction::triggered, this, [this, disable_spoofing] {
  231. auto user_agent = QInputDialog::getText(this, "Custom User Agent", "Enter User Agent:");
  232. if (!user_agent.isEmpty()) {
  233. debug_request("spoof-user-agent", ak_deprecated_string_from_qstring(user_agent));
  234. debug_request("clear-cache"); // clear the cache to ensure requests are re-done with the new user agent
  235. } else {
  236. disable_spoofing->activate(QAction::Trigger);
  237. }
  238. });
  239. debug_menu->addSeparator();
  240. auto* enable_scripting_action = new QAction("Enable Scripting", this);
  241. enable_scripting_action->setCheckable(true);
  242. enable_scripting_action->setChecked(true);
  243. debug_menu->addAction(enable_scripting_action);
  244. QObject::connect(enable_scripting_action, &QAction::triggered, this, [this, enable_scripting_action] {
  245. bool state = enable_scripting_action->isChecked();
  246. debug_request("scripting", state ? "on" : "off");
  247. });
  248. auto* block_pop_ups_action = new QAction("Block Pop-ups", this);
  249. block_pop_ups_action->setCheckable(true);
  250. block_pop_ups_action->setChecked(true);
  251. debug_menu->addAction(block_pop_ups_action);
  252. QObject::connect(block_pop_ups_action, &QAction::triggered, this, [this, block_pop_ups_action] {
  253. bool state = block_pop_ups_action->isChecked();
  254. debug_request("block-pop-ups", state ? "on" : "off");
  255. });
  256. auto* enable_same_origin_policy_action = new QAction("Enable Same-Origin Policy", this);
  257. enable_same_origin_policy_action->setCheckable(true);
  258. debug_menu->addAction(enable_same_origin_policy_action);
  259. QObject::connect(enable_same_origin_policy_action, &QAction::triggered, this, [this, enable_same_origin_policy_action] {
  260. bool state = enable_same_origin_policy_action->isChecked();
  261. debug_request("same-origin-policy", state ? "on" : "off");
  262. });
  263. QObject::connect(new_tab_action, &QAction::triggered, this, [this] {
  264. new_tab(s_settings->new_tab_page(), Web::HTML::ActivateTab::Yes);
  265. });
  266. QObject::connect(settings_action, &QAction::triggered, this, [this] {
  267. new SettingsDialog(this);
  268. });
  269. QObject::connect(quit_action, &QAction::triggered, this, &QMainWindow::close);
  270. QObject::connect(m_tabs_container, &QTabWidget::currentChanged, [this](int index) {
  271. setWindowTitle(QString("%1 - Ladybird").arg(m_tabs_container->tabText(index)));
  272. setWindowIcon(m_tabs_container->tabIcon(index));
  273. set_current_tab(verify_cast<Tab>(m_tabs_container->widget(index)));
  274. });
  275. QObject::connect(m_tabs_container, &QTabWidget::tabCloseRequested, this, &BrowserWindow::close_tab);
  276. QObject::connect(close_current_tab_action, &QAction::triggered, this, &BrowserWindow::close_current_tab);
  277. m_inspect_dom_node_action = make<QAction>("&Inspect Element", this);
  278. connect(m_inspect_dom_node_action, &QAction::triggered, this, [this] {
  279. if (m_current_tab)
  280. m_current_tab->view().show_inspector(WebContentView::InspectorTarget::HoveredElement);
  281. });
  282. m_go_back_action = make<QAction>("Go Back");
  283. connect(m_go_back_action, &QAction::triggered, this, [this] {
  284. if (m_current_tab)
  285. m_current_tab->back();
  286. });
  287. m_go_forward_action = make<QAction>("Go Forward");
  288. connect(m_go_forward_action, &QAction::triggered, this, [this] {
  289. if (m_current_tab)
  290. m_current_tab->forward();
  291. });
  292. m_reload_action = make<QAction>("&Reload");
  293. connect(m_reload_action, &QAction::triggered, this, [this] {
  294. if (m_current_tab)
  295. m_current_tab->reload();
  296. });
  297. m_go_back_action->setShortcuts(QKeySequence::keyBindings(QKeySequence::StandardKey::Back));
  298. m_go_forward_action->setShortcuts(QKeySequence::keyBindings(QKeySequence::StandardKey::Forward));
  299. m_reload_action->setShortcuts(QKeySequence::keyBindings(QKeySequence::StandardKey::Refresh));
  300. m_go_back_action->setEnabled(false);
  301. m_go_forward_action->setEnabled(false);
  302. new_tab(s_settings->new_tab_page(), Web::HTML::ActivateTab::Yes);
  303. setCentralWidget(m_tabs_container);
  304. setContextMenuPolicy(Qt::PreventContextMenu);
  305. }
  306. void BrowserWindow::set_current_tab(Tab* tab)
  307. {
  308. m_current_tab = tab;
  309. if (tab)
  310. update_displayed_zoom_level();
  311. }
  312. void BrowserWindow::debug_request(DeprecatedString const& request, DeprecatedString const& argument)
  313. {
  314. if (!m_current_tab)
  315. return;
  316. m_current_tab->debug_request(request, argument);
  317. }
  318. Tab& BrowserWindow::new_tab(QString const& url, Web::HTML::ActivateTab activate_tab)
  319. {
  320. auto tab = make<Tab>(this, m_webdriver_content_ipc_path, m_enable_callgrind_profiling);
  321. auto tab_ptr = tab.ptr();
  322. m_tabs.append(std::move(tab));
  323. if (m_current_tab == nullptr) {
  324. set_current_tab(tab_ptr);
  325. }
  326. m_tabs_container->addTab(tab_ptr, "New Tab");
  327. if (activate_tab == Web::HTML::ActivateTab::Yes)
  328. m_tabs_container->setCurrentWidget(tab_ptr);
  329. QObject::connect(tab_ptr, &Tab::title_changed, this, &BrowserWindow::tab_title_changed);
  330. QObject::connect(tab_ptr, &Tab::favicon_changed, this, &BrowserWindow::tab_favicon_changed);
  331. QObject::connect(&tab_ptr->view(), &WebContentView::urls_dropped, this, [this](auto& urls) {
  332. VERIFY(urls.size());
  333. m_current_tab->navigate(urls[0].toString());
  334. for (qsizetype i = 1; i < urls.size(); ++i)
  335. new_tab(urls[i].toString(), Web::HTML::ActivateTab::No);
  336. });
  337. tab_ptr->view().on_new_tab = [this](auto activate_tab) {
  338. auto& tab = new_tab("about:blank", activate_tab);
  339. return tab.view().handle();
  340. };
  341. tab_ptr->view().on_tab_open_request = [this](auto url, auto activate_tab) {
  342. auto& tab = new_tab(qstring_from_ak_deprecated_string(url.to_deprecated_string()), activate_tab);
  343. return tab.view().handle();
  344. };
  345. tab_ptr->view().on_link_click = [this](auto url, auto target, unsigned modifiers) {
  346. // TODO: maybe activate tabs according to some configuration, this is just normal current browser behavior
  347. if (modifiers == Mod_Ctrl) {
  348. m_current_tab->view().on_tab_open_request(url, Web::HTML::ActivateTab::No);
  349. } else if (target == "_blank") {
  350. m_current_tab->view().on_tab_open_request(url, Web::HTML::ActivateTab::Yes);
  351. }
  352. };
  353. tab_ptr->view().on_link_middle_click = [this](auto url, auto target, unsigned modifiers) {
  354. m_current_tab->view().on_link_click(url, target, Mod_Ctrl);
  355. (void)modifiers;
  356. };
  357. tab_ptr->view().on_get_all_cookies = [this](auto const& url) {
  358. return m_cookie_jar.get_all_cookies(url);
  359. };
  360. tab_ptr->view().on_get_named_cookie = [this](auto const& url, auto const& name) {
  361. return m_cookie_jar.get_named_cookie(url, name);
  362. };
  363. tab_ptr->view().on_get_cookie = [this](auto& url, auto source) -> DeprecatedString {
  364. return m_cookie_jar.get_cookie(url, source);
  365. };
  366. tab_ptr->view().on_set_cookie = [this](auto& url, auto& cookie, auto source) {
  367. m_cookie_jar.set_cookie(url, cookie, source);
  368. };
  369. tab_ptr->view().on_update_cookie = [this](auto const& cookie) {
  370. m_cookie_jar.update_cookie(cookie);
  371. };
  372. tab_ptr->focus_location_editor();
  373. // We *don't* load the initial page if we are connected to a WebDriver, as the Set URL command may come in very
  374. // quickly, and become replaced by this load.
  375. if (m_webdriver_content_ipc_path.is_empty()) {
  376. // We make it HistoryNavigation so that the initial page doesn't get added to the history.
  377. tab_ptr->navigate(url, Tab::LoadType::HistoryNavigation);
  378. }
  379. return *tab_ptr;
  380. }
  381. void BrowserWindow::activate_tab(int index)
  382. {
  383. m_tabs_container->setCurrentIndex(index);
  384. }
  385. void BrowserWindow::close_tab(int index)
  386. {
  387. auto* tab = m_tabs_container->widget(index);
  388. m_tabs_container->removeTab(index);
  389. m_tabs.remove_first_matching([&](auto& entry) {
  390. return entry == tab;
  391. });
  392. }
  393. void BrowserWindow::close_current_tab()
  394. {
  395. auto count = m_tabs_container->count() - 1;
  396. if (!count)
  397. close();
  398. else
  399. close_tab(m_tabs_container->currentIndex());
  400. }
  401. int BrowserWindow::tab_index(Tab* tab)
  402. {
  403. return m_tabs_container->indexOf(tab);
  404. }
  405. void BrowserWindow::tab_title_changed(int index, QString const& title)
  406. {
  407. if (title.isEmpty()) {
  408. m_tabs_container->setTabText(index, "...");
  409. if (m_tabs_container->currentIndex() == index)
  410. setWindowTitle("Ladybird");
  411. } else {
  412. m_tabs_container->setTabText(index, title);
  413. if (m_tabs_container->currentIndex() == index)
  414. setWindowTitle(QString("%1 - Ladybird").arg(title));
  415. }
  416. }
  417. void BrowserWindow::tab_favicon_changed(int index, QIcon icon)
  418. {
  419. m_tabs_container->setTabIcon(index, icon);
  420. if (m_tabs_container->currentIndex() == index)
  421. setWindowIcon(icon);
  422. }
  423. void BrowserWindow::open_next_tab()
  424. {
  425. if (m_tabs_container->count() <= 1)
  426. return;
  427. auto next_index = m_tabs_container->currentIndex() + 1;
  428. if (next_index >= m_tabs_container->count())
  429. next_index = 0;
  430. m_tabs_container->setCurrentIndex(next_index);
  431. }
  432. void BrowserWindow::open_previous_tab()
  433. {
  434. if (m_tabs_container->count() <= 1)
  435. return;
  436. auto next_index = m_tabs_container->currentIndex() - 1;
  437. if (next_index < 0)
  438. next_index = m_tabs_container->count() - 1;
  439. m_tabs_container->setCurrentIndex(next_index);
  440. }
  441. void BrowserWindow::enable_auto_color_scheme()
  442. {
  443. for (auto& tab : m_tabs) {
  444. tab->view().set_preferred_color_scheme(Web::CSS::PreferredColorScheme::Auto);
  445. }
  446. }
  447. void BrowserWindow::enable_light_color_scheme()
  448. {
  449. for (auto& tab : m_tabs) {
  450. tab->view().set_preferred_color_scheme(Web::CSS::PreferredColorScheme::Light);
  451. }
  452. }
  453. void BrowserWindow::enable_dark_color_scheme()
  454. {
  455. for (auto& tab : m_tabs) {
  456. tab->view().set_preferred_color_scheme(Web::CSS::PreferredColorScheme::Dark);
  457. }
  458. }
  459. void BrowserWindow::zoom_in()
  460. {
  461. if (!m_current_tab)
  462. return;
  463. m_current_tab->view().zoom_in();
  464. update_displayed_zoom_level();
  465. }
  466. void BrowserWindow::zoom_out()
  467. {
  468. if (!m_current_tab)
  469. return;
  470. m_current_tab->view().zoom_out();
  471. update_displayed_zoom_level();
  472. }
  473. void BrowserWindow::reset_zoom()
  474. {
  475. if (!m_current_tab)
  476. return;
  477. m_current_tab->view().reset_zoom();
  478. update_displayed_zoom_level();
  479. }
  480. void BrowserWindow::select_all()
  481. {
  482. if (!m_current_tab)
  483. return;
  484. if (auto* console = m_current_tab->view().console(); console && console->isActiveWindow())
  485. console->view().select_all();
  486. else
  487. m_current_tab->view().select_all();
  488. }
  489. void BrowserWindow::update_displayed_zoom_level()
  490. {
  491. VERIFY(m_zoom_menu && m_current_tab);
  492. auto zoom_level_text = MUST(String::formatted("&Zoom ({}%)", round_to<int>(m_current_tab->view().zoom_level() * 100)));
  493. m_zoom_menu->setTitle(qstring_from_ak_string(zoom_level_text));
  494. m_current_tab->update_reset_zoom_button();
  495. }
  496. void BrowserWindow::copy_selected_text()
  497. {
  498. if (!m_current_tab)
  499. return;
  500. DeprecatedString text;
  501. if (auto* console = m_current_tab->view().console(); console && console->isActiveWindow())
  502. text = console->view().selected_text();
  503. else
  504. text = m_current_tab->view().selected_text();
  505. auto* clipboard = QGuiApplication::clipboard();
  506. clipboard->setText(qstring_from_ak_deprecated_string(text));
  507. }
  508. void BrowserWindow::resizeEvent(QResizeEvent* event)
  509. {
  510. QWidget::resizeEvent(event);
  511. for (auto& tab : m_tabs) {
  512. tab->view().set_window_size({ frameSize().width(), frameSize().height() });
  513. }
  514. }
  515. void BrowserWindow::moveEvent(QMoveEvent* event)
  516. {
  517. QWidget::moveEvent(event);
  518. for (auto& tab : m_tabs) {
  519. tab->view().set_window_position({ event->pos().x(), event->pos().y() });
  520. }
  521. }
  522. bool BrowserWindow::eventFilter(QObject* obj, QEvent* event)
  523. {
  524. if (event->type() == QEvent::MouseButtonRelease) {
  525. auto const* const mouse_event = static_cast<QMouseEvent*>(event);
  526. if (mouse_event->button() == Qt::MouseButton::MiddleButton) {
  527. if (obj == m_tabs_container) {
  528. auto const tab_index = m_tabs_container->tabBar()->tabAt(mouse_event->pos());
  529. close_tab(tab_index);
  530. return true;
  531. }
  532. }
  533. }
  534. return QMainWindow::eventFilter(obj, event);
  535. }