BrowserWindow.cpp 29 KB

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