BrowserWindow.cpp 29 KB

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