BrowserWindow.cpp 29 KB

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