BrowserWindow.cpp 35 KB

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