BrowserWindow.cpp 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224
  1. /*
  2. * Copyright (c) 2022-2023, Andreas Kling <andreas@ladybird.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. * Copyright (c) 2024, Sam Atkins <sam@ladybird.org>
  7. *
  8. * SPDX-License-Identifier: BSD-2-Clause
  9. */
  10. #include "BrowserWindow.h"
  11. #include "Application.h"
  12. #include "Icon.h"
  13. #include "Settings.h"
  14. #include "SettingsDialog.h"
  15. #include "StringUtils.h"
  16. #include "TaskManagerWindow.h"
  17. #include "WebContentView.h"
  18. #include <AK/TypeCasts.h>
  19. #include <Ladybird/Qt/TabBar.h>
  20. #include <Ladybird/Utilities.h>
  21. #include <LibWeb/CSS/PreferredColorScheme.h>
  22. #include <LibWeb/CSS/PreferredContrast.h>
  23. #include <LibWeb/CSS/PreferredMotion.h>
  24. #include <LibWeb/Loader/UserAgent.h>
  25. #include <LibWebView/Application.h>
  26. #include <LibWebView/CookieJar.h>
  27. #include <LibWebView/UserAgent.h>
  28. #include <QAction>
  29. #include <QActionGroup>
  30. #include <QApplication>
  31. #include <QClipboard>
  32. #include <QGuiApplication>
  33. #include <QInputDialog>
  34. #include <QPlainTextEdit>
  35. #include <QShortcut>
  36. #include <QTabBar>
  37. #include <QWindow>
  38. namespace Ladybird {
  39. static QIcon const& app_icon()
  40. {
  41. static QIcon icon;
  42. if (icon.isNull()) {
  43. QPixmap pixmap;
  44. pixmap.load(":/Icons/ladybird.png");
  45. icon = QIcon(pixmap);
  46. }
  47. return icon;
  48. }
  49. class HamburgerMenu : public QMenu {
  50. public:
  51. using QMenu::QMenu;
  52. virtual ~HamburgerMenu() override = default;
  53. virtual void showEvent(QShowEvent*) override
  54. {
  55. if (!isVisible())
  56. return;
  57. auto* browser_window = verify_cast<BrowserWindow>(parentWidget());
  58. if (!browser_window)
  59. return;
  60. auto* current_tab = browser_window->current_tab();
  61. if (!current_tab)
  62. return;
  63. // Ensure the hamburger menu placed within the browser window.
  64. auto* hamburger_button = current_tab->hamburger_button();
  65. auto button_top_right = hamburger_button->mapToGlobal(hamburger_button->rect().bottomRight());
  66. move(button_top_right - QPoint(rect().width(), 0));
  67. }
  68. };
  69. BrowserWindow::BrowserWindow(Vector<URL::URL> const& initial_urls, IsPopupWindow is_popup_window, Tab* parent_tab, Optional<u64> page_index)
  70. : m_tabs_container(new TabWidget(this))
  71. , m_new_tab_button_toolbar(new QToolBar("New Tab", m_tabs_container))
  72. , m_is_popup_window(is_popup_window)
  73. {
  74. setWindowIcon(app_icon());
  75. // Listen for DPI changes
  76. m_device_pixel_ratio = devicePixelRatio();
  77. m_current_screen = screen();
  78. if (QT_VERSION < QT_VERSION_CHECK(6, 6, 0) || QGuiApplication::platformName() != "wayland") {
  79. setAttribute(Qt::WA_NativeWindow);
  80. setAttribute(Qt::WA_DontCreateNativeAncestors);
  81. QObject::connect(m_current_screen, &QScreen::logicalDotsPerInchChanged, this, &BrowserWindow::device_pixel_ratio_changed);
  82. QObject::connect(windowHandle(), &QWindow::screenChanged, this, [this](QScreen* screen) {
  83. if (m_device_pixel_ratio != devicePixelRatio())
  84. device_pixel_ratio_changed(devicePixelRatio());
  85. // Listen for logicalDotsPerInchChanged signals on new screen
  86. QObject::disconnect(m_current_screen, &QScreen::logicalDotsPerInchChanged, nullptr, nullptr);
  87. m_current_screen = screen;
  88. QObject::connect(m_current_screen, &QScreen::logicalDotsPerInchChanged, this, &BrowserWindow::device_pixel_ratio_changed);
  89. });
  90. }
  91. QObject::connect(Settings::the(), &Settings::enable_do_not_track_changed, this, [this](bool enable) {
  92. for_each_tab([enable](auto& tab) {
  93. tab.set_enable_do_not_track(enable);
  94. });
  95. });
  96. QObject::connect(Settings::the(), &Settings::enable_autoplay_changed, this, [this](bool enable) {
  97. for_each_tab([enable](auto& tab) {
  98. tab.set_enable_autoplay(enable);
  99. });
  100. });
  101. QObject::connect(Settings::the(), &Settings::preferred_languages_changed, this, [this](QStringList languages) {
  102. Vector<String> preferred_languages;
  103. preferred_languages.ensure_capacity(languages.length());
  104. for (auto& language : languages) {
  105. preferred_languages.append(ak_string_from_qstring(language));
  106. }
  107. for_each_tab([preferred_languages](auto& tab) {
  108. tab.set_preferred_languages(preferred_languages);
  109. });
  110. });
  111. m_hamburger_menu = new HamburgerMenu(this);
  112. if (!Settings::the()->show_menubar())
  113. menuBar()->hide();
  114. QObject::connect(Settings::the(), &Settings::show_menubar_changed, this, [this](bool show_menubar) {
  115. menuBar()->setVisible(show_menubar);
  116. });
  117. auto* file_menu = menuBar()->addMenu("&File");
  118. m_new_tab_action = new QAction("New &Tab", this);
  119. m_new_tab_action->setShortcuts(QKeySequence::keyBindings(QKeySequence::StandardKey::AddTab));
  120. m_hamburger_menu->addAction(m_new_tab_action);
  121. file_menu->addAction(m_new_tab_action);
  122. m_new_window_action = new QAction("New &Window", this);
  123. m_new_window_action->setShortcuts(QKeySequence::keyBindings(QKeySequence::StandardKey::New));
  124. m_hamburger_menu->addAction(m_new_window_action);
  125. file_menu->addAction(m_new_window_action);
  126. auto* close_current_tab_action = new QAction("&Close Current Tab", this);
  127. close_current_tab_action->setIcon(load_icon_from_uri("resource://icons/16x16/close-tab.png"sv));
  128. close_current_tab_action->setShortcuts(QKeySequence::keyBindings(QKeySequence::StandardKey::Close));
  129. m_hamburger_menu->addAction(close_current_tab_action);
  130. file_menu->addAction(close_current_tab_action);
  131. auto* open_file_action = new QAction("&Open File...", this);
  132. open_file_action->setIcon(load_icon_from_uri("resource://icons/16x16/filetype-folder-open.png"sv));
  133. open_file_action->setShortcut(QKeySequence(QKeySequence::StandardKey::Open));
  134. m_hamburger_menu->addAction(open_file_action);
  135. file_menu->addAction(open_file_action);
  136. m_hamburger_menu->addSeparator();
  137. auto* edit_menu = m_hamburger_menu->addMenu("&Edit");
  138. menuBar()->addMenu(edit_menu);
  139. m_copy_selection_action = new QAction("&Copy", this);
  140. m_copy_selection_action->setIcon(load_icon_from_uri("resource://icons/16x16/edit-copy.png"sv));
  141. m_copy_selection_action->setShortcuts(QKeySequence::keyBindings(QKeySequence::StandardKey::Copy));
  142. edit_menu->addAction(m_copy_selection_action);
  143. QObject::connect(m_copy_selection_action, &QAction::triggered, this, &BrowserWindow::copy_selected_text);
  144. m_paste_action = new QAction("&Paste", this);
  145. m_paste_action->setIcon(load_icon_from_uri("resource://icons/16x16/paste.png"sv));
  146. m_paste_action->setShortcuts(QKeySequence::keyBindings(QKeySequence::StandardKey::Paste));
  147. edit_menu->addAction(m_paste_action);
  148. QObject::connect(m_paste_action, &QAction::triggered, this, &BrowserWindow::paste);
  149. m_select_all_action = new QAction("Select &All", this);
  150. m_select_all_action->setIcon(load_icon_from_uri("resource://icons/16x16/select-all.png"sv));
  151. m_select_all_action->setShortcuts(QKeySequence::keyBindings(QKeySequence::StandardKey::SelectAll));
  152. edit_menu->addAction(m_select_all_action);
  153. QObject::connect(m_select_all_action, &QAction::triggered, this, &BrowserWindow::select_all);
  154. edit_menu->addSeparator();
  155. m_find_in_page_action = new QAction("&Find in Page...", this);
  156. m_find_in_page_action->setIcon(load_icon_from_uri("resource://icons/16x16/find.png"sv));
  157. m_find_in_page_action->setShortcuts(QKeySequence::keyBindings(QKeySequence::StandardKey::Find));
  158. auto find_previous_shortcuts = QKeySequence::keyBindings(QKeySequence::StandardKey::FindPrevious);
  159. for (auto const& shortcut : find_previous_shortcuts)
  160. new QShortcut(shortcut, this, [this] {
  161. if (m_current_tab)
  162. m_current_tab->find_previous();
  163. });
  164. auto find_next_shortcuts = QKeySequence::keyBindings(QKeySequence::StandardKey::FindNext);
  165. for (auto const& shortcut : find_next_shortcuts)
  166. new QShortcut(shortcut, this, [this] {
  167. if (m_current_tab)
  168. m_current_tab->find_next();
  169. });
  170. edit_menu->addAction(m_find_in_page_action);
  171. QObject::connect(m_find_in_page_action, &QAction::triggered, this, &BrowserWindow::show_find_in_page);
  172. edit_menu->addSeparator();
  173. auto* settings_action = new QAction("&Settings", this);
  174. settings_action->setIcon(load_icon_from_uri("resource://icons/16x16/settings.png"sv));
  175. settings_action->setShortcuts(QKeySequence::keyBindings(QKeySequence::StandardKey::Preferences));
  176. edit_menu->addAction(settings_action);
  177. auto* view_menu = m_hamburger_menu->addMenu("&View");
  178. menuBar()->addMenu(view_menu);
  179. auto* open_next_tab_action = new QAction("Open &Next Tab", this);
  180. open_next_tab_action->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_PageDown));
  181. view_menu->addAction(open_next_tab_action);
  182. QObject::connect(open_next_tab_action, &QAction::triggered, this, &BrowserWindow::open_next_tab);
  183. auto* open_previous_tab_action = new QAction("Open &Previous Tab", this);
  184. open_previous_tab_action->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_PageUp));
  185. view_menu->addAction(open_previous_tab_action);
  186. QObject::connect(open_previous_tab_action, &QAction::triggered, this, &BrowserWindow::open_previous_tab);
  187. view_menu->addSeparator();
  188. m_zoom_menu = view_menu->addMenu("&Zoom");
  189. auto* zoom_in_action = new QAction("Zoom &In", this);
  190. zoom_in_action->setIcon(load_icon_from_uri("resource://icons/16x16/zoom-in.png"sv));
  191. auto zoom_in_shortcuts = QKeySequence::keyBindings(QKeySequence::StandardKey::ZoomIn);
  192. auto secondary_zoom_shortcut = QKeySequence(Qt::CTRL | Qt::Key_Equal);
  193. if (!zoom_in_shortcuts.contains(secondary_zoom_shortcut))
  194. zoom_in_shortcuts.append(AK::move(secondary_zoom_shortcut));
  195. zoom_in_action->setShortcuts(zoom_in_shortcuts);
  196. m_zoom_menu->addAction(zoom_in_action);
  197. QObject::connect(zoom_in_action, &QAction::triggered, this, &BrowserWindow::zoom_in);
  198. auto* zoom_out_action = new QAction("Zoom &Out", this);
  199. zoom_out_action->setIcon(load_icon_from_uri("resource://icons/16x16/zoom-out.png"sv));
  200. zoom_out_action->setShortcuts(QKeySequence::keyBindings(QKeySequence::StandardKey::ZoomOut));
  201. m_zoom_menu->addAction(zoom_out_action);
  202. QObject::connect(zoom_out_action, &QAction::triggered, this, &BrowserWindow::zoom_out);
  203. auto* reset_zoom_action = new QAction("&Reset Zoom", this);
  204. reset_zoom_action->setIcon(load_icon_from_uri("resource://icons/16x16/zoom-reset.png"sv));
  205. reset_zoom_action->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_0));
  206. m_zoom_menu->addAction(reset_zoom_action);
  207. QObject::connect(reset_zoom_action, &QAction::triggered, this, &BrowserWindow::reset_zoom);
  208. view_menu->addSeparator();
  209. auto* color_scheme_menu = view_menu->addMenu("&Color Scheme");
  210. auto* color_scheme_group = new QActionGroup(this);
  211. auto* auto_color_scheme = new QAction("&Auto", this);
  212. auto_color_scheme->setCheckable(true);
  213. color_scheme_group->addAction(auto_color_scheme);
  214. color_scheme_menu->addAction(auto_color_scheme);
  215. QObject::connect(auto_color_scheme, &QAction::triggered, this, [this] {
  216. set_preferred_color_scheme(Web::CSS::PreferredColorScheme::Auto);
  217. });
  218. auto* light_color_scheme = new QAction("&Light", this);
  219. light_color_scheme->setCheckable(true);
  220. color_scheme_group->addAction(light_color_scheme);
  221. color_scheme_menu->addAction(light_color_scheme);
  222. QObject::connect(light_color_scheme, &QAction::triggered, this, [this] {
  223. set_preferred_color_scheme(Web::CSS::PreferredColorScheme::Light);
  224. });
  225. auto* dark_color_scheme = new QAction("&Dark", this);
  226. dark_color_scheme->setCheckable(true);
  227. color_scheme_group->addAction(dark_color_scheme);
  228. color_scheme_menu->addAction(dark_color_scheme);
  229. QObject::connect(dark_color_scheme, &QAction::triggered, this, [this] {
  230. set_preferred_color_scheme(Web::CSS::PreferredColorScheme::Dark);
  231. });
  232. auto_color_scheme->setChecked(true);
  233. auto* contrast_menu = view_menu->addMenu("&Contrast");
  234. auto* contrast_group = new QActionGroup(this);
  235. auto* auto_contrast = new QAction("&Auto", this);
  236. auto_contrast->setCheckable(true);
  237. contrast_group->addAction(auto_contrast);
  238. contrast_menu->addAction(auto_contrast);
  239. QObject::connect(auto_contrast, &QAction::triggered, this, &BrowserWindow::enable_auto_contrast);
  240. auto* less_contrast = new QAction("&Less", this);
  241. less_contrast->setCheckable(true);
  242. contrast_group->addAction(less_contrast);
  243. contrast_menu->addAction(less_contrast);
  244. QObject::connect(less_contrast, &QAction::triggered, this, &BrowserWindow::enable_less_contrast);
  245. auto* more_contrast = new QAction("&More", this);
  246. more_contrast->setCheckable(true);
  247. contrast_group->addAction(more_contrast);
  248. contrast_menu->addAction(more_contrast);
  249. QObject::connect(more_contrast, &QAction::triggered, this, &BrowserWindow::enable_more_contrast);
  250. auto* no_preference_contrast = new QAction("&No Preference", this);
  251. no_preference_contrast->setCheckable(true);
  252. contrast_group->addAction(no_preference_contrast);
  253. contrast_menu->addAction(no_preference_contrast);
  254. QObject::connect(no_preference_contrast, &QAction::triggered, this, &BrowserWindow::enable_no_preference_contrast);
  255. auto_contrast->setChecked(true);
  256. auto* motion_menu = view_menu->addMenu("&Motion");
  257. auto* motion_group = new QActionGroup(this);
  258. auto* auto_motion = new QAction("&Auto", this);
  259. auto_motion->setCheckable(true);
  260. motion_group->addAction(auto_motion);
  261. motion_menu->addAction(auto_motion);
  262. QObject::connect(auto_motion, &QAction::triggered, this, &BrowserWindow::enable_auto_motion);
  263. auto* reduce_motion = new QAction("&Reduce", this);
  264. reduce_motion->setCheckable(true);
  265. motion_group->addAction(reduce_motion);
  266. motion_menu->addAction(reduce_motion);
  267. QObject::connect(reduce_motion, &QAction::triggered, this, &BrowserWindow::enable_reduce_motion);
  268. auto* no_preference_motion = new QAction("&No Preference", this);
  269. no_preference_motion->setCheckable(true);
  270. motion_group->addAction(no_preference_motion);
  271. motion_menu->addAction(no_preference_motion);
  272. QObject::connect(no_preference_motion, &QAction::triggered, this, &BrowserWindow::enable_no_preference_motion);
  273. auto_motion->setChecked(true);
  274. auto* show_menubar = new QAction("Show &Menubar", this);
  275. show_menubar->setCheckable(true);
  276. show_menubar->setChecked(Settings::the()->show_menubar());
  277. view_menu->addAction(show_menubar);
  278. QObject::connect(show_menubar, &QAction::triggered, this, [](bool checked) {
  279. Settings::the()->set_show_menubar(checked);
  280. });
  281. auto* inspect_menu = m_hamburger_menu->addMenu("&Inspect");
  282. menuBar()->addMenu(inspect_menu);
  283. m_view_source_action = new QAction("View &Source", this);
  284. m_view_source_action->setIcon(load_icon_from_uri("resource://icons/16x16/filetype-html.png"sv));
  285. m_view_source_action->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_U));
  286. inspect_menu->addAction(m_view_source_action);
  287. QObject::connect(m_view_source_action, &QAction::triggered, this, [this] {
  288. if (m_current_tab) {
  289. m_current_tab->view().get_source();
  290. }
  291. });
  292. auto* inspector_action = new QAction("Open &Inspector", this);
  293. inspector_action->setIcon(load_icon_from_uri("resource://icons/browser/dom-tree.png"sv));
  294. inspector_action->setShortcuts({ QKeySequence("Ctrl+Shift+I"), QKeySequence("F12") });
  295. inspect_menu->addAction(inspector_action);
  296. QObject::connect(inspector_action, &QAction::triggered, this, [this] {
  297. if (m_current_tab) {
  298. m_current_tab->show_inspector_window();
  299. }
  300. });
  301. auto* task_manager_action = new QAction("Open Task &Manager", this);
  302. task_manager_action->setIcon(load_icon_from_uri("resource://icons/16x16/app-system-monitor.png"sv));
  303. task_manager_action->setShortcuts({ QKeySequence("Ctrl+Shift+M") });
  304. inspect_menu->addAction(task_manager_action);
  305. QObject::connect(task_manager_action, &QAction::triggered, this, [&] {
  306. static_cast<Ladybird::Application*>(QApplication::instance())->show_task_manager_window();
  307. });
  308. auto* debug_menu = m_hamburger_menu->addMenu("&Debug");
  309. menuBar()->addMenu(debug_menu);
  310. auto* dump_session_history_tree_action = new QAction("Dump Session History Tree", this);
  311. dump_session_history_tree_action->setIcon(load_icon_from_uri("resource://icons/16x16/history.png"sv));
  312. debug_menu->addAction(dump_session_history_tree_action);
  313. QObject::connect(dump_session_history_tree_action, &QAction::triggered, this, [this] {
  314. debug_request("dump-session-history");
  315. });
  316. auto* dump_dom_tree_action = new QAction("Dump &DOM Tree", this);
  317. dump_dom_tree_action->setIcon(load_icon_from_uri("resource://icons/browser/dom-tree.png"sv));
  318. debug_menu->addAction(dump_dom_tree_action);
  319. QObject::connect(dump_dom_tree_action, &QAction::triggered, this, [this] {
  320. debug_request("dump-dom-tree");
  321. });
  322. auto* dump_layout_tree_action = new QAction("Dump &Layout Tree", this);
  323. dump_layout_tree_action->setIcon(load_icon_from_uri("resource://icons/16x16/layout.png"sv));
  324. debug_menu->addAction(dump_layout_tree_action);
  325. QObject::connect(dump_layout_tree_action, &QAction::triggered, this, [this] {
  326. debug_request("dump-layout-tree");
  327. });
  328. auto* dump_paint_tree_action = new QAction("Dump &Paint Tree", this);
  329. dump_paint_tree_action->setIcon(load_icon_from_uri("resource://icons/16x16/layout.png"sv));
  330. debug_menu->addAction(dump_paint_tree_action);
  331. QObject::connect(dump_paint_tree_action, &QAction::triggered, this, [this] {
  332. debug_request("dump-paint-tree");
  333. });
  334. auto* dump_stacking_context_tree_action = new QAction("Dump S&tacking Context Tree", this);
  335. dump_stacking_context_tree_action->setIcon(load_icon_from_uri("resource://icons/16x16/layers.png"sv));
  336. debug_menu->addAction(dump_stacking_context_tree_action);
  337. QObject::connect(dump_stacking_context_tree_action, &QAction::triggered, this, [this] {
  338. debug_request("dump-stacking-context-tree");
  339. });
  340. auto* dump_style_sheets_action = new QAction("Dump &Style Sheets", this);
  341. dump_style_sheets_action->setIcon(load_icon_from_uri("resource://icons/16x16/filetype-css.png"sv));
  342. debug_menu->addAction(dump_style_sheets_action);
  343. QObject::connect(dump_style_sheets_action, &QAction::triggered, this, [this] {
  344. debug_request("dump-style-sheets");
  345. });
  346. auto* dump_styles_action = new QAction("Dump &All Resolved Styles", this);
  347. dump_styles_action->setIcon(load_icon_from_uri("resource://icons/16x16/filetype-css.png"sv));
  348. debug_menu->addAction(dump_styles_action);
  349. QObject::connect(dump_styles_action, &QAction::triggered, this, [this] {
  350. debug_request("dump-all-resolved-styles");
  351. });
  352. auto* dump_cookies_action = new QAction("Dump C&ookies", this);
  353. dump_cookies_action->setIcon(load_icon_from_uri("resource://icons/browser/cookie.png"sv));
  354. debug_menu->addAction(dump_cookies_action);
  355. QObject::connect(dump_cookies_action, &QAction::triggered, this, [] {
  356. WebView::Application::cookie_jar().dump_cookies();
  357. });
  358. auto* dump_local_storage_action = new QAction("Dump Loc&al Storage", this);
  359. dump_local_storage_action->setIcon(load_icon_from_uri("resource://icons/browser/local-storage.png"sv));
  360. debug_menu->addAction(dump_local_storage_action);
  361. QObject::connect(dump_local_storage_action, &QAction::triggered, this, [this] {
  362. debug_request("dump-local-storage");
  363. });
  364. debug_menu->addSeparator();
  365. m_show_line_box_borders_action = new QAction("Show Line Box Borders", this);
  366. m_show_line_box_borders_action->setCheckable(true);
  367. debug_menu->addAction(m_show_line_box_borders_action);
  368. QObject::connect(m_show_line_box_borders_action, &QAction::triggered, this, [this] {
  369. bool state = m_show_line_box_borders_action->isChecked();
  370. for_each_tab([state](auto& tab) {
  371. tab.set_line_box_borders(state);
  372. });
  373. });
  374. debug_menu->addSeparator();
  375. auto* collect_garbage_action = new QAction("Collect &Garbage", this);
  376. collect_garbage_action->setShortcut(QKeySequence(Qt::CTRL | Qt::SHIFT | Qt::Key_G));
  377. collect_garbage_action->setIcon(load_icon_from_uri("resource://icons/16x16/trash-can.png"sv));
  378. debug_menu->addAction(collect_garbage_action);
  379. QObject::connect(collect_garbage_action, &QAction::triggered, this, [this] {
  380. debug_request("collect-garbage");
  381. });
  382. auto* dump_gc_graph_action = new QAction("Dump GC graph", this);
  383. debug_menu->addAction(dump_gc_graph_action);
  384. QObject::connect(dump_gc_graph_action, &QAction::triggered, this, [this] {
  385. if (m_current_tab) {
  386. auto gc_graph_path = m_current_tab->view().dump_gc_graph();
  387. warnln("\033[33;1mDumped GC-graph into {}"
  388. "\033[0m",
  389. gc_graph_path);
  390. }
  391. });
  392. auto* clear_cache_action = new QAction("Clear &Cache", this);
  393. clear_cache_action->setShortcut(QKeySequence(Qt::CTRL | Qt::SHIFT | Qt::Key_C));
  394. clear_cache_action->setIcon(load_icon_from_uri("resource://icons/browser/clear-cache.png"sv));
  395. debug_menu->addAction(clear_cache_action);
  396. QObject::connect(clear_cache_action, &QAction::triggered, this, [this] {
  397. debug_request("clear-cache");
  398. });
  399. auto* spoof_user_agent_menu = debug_menu->addMenu("Spoof &User Agent");
  400. spoof_user_agent_menu->setIcon(load_icon_from_uri("resource://icons/16x16/spoof.png"sv));
  401. auto* user_agent_group = new QActionGroup(this);
  402. auto add_user_agent = [this, &user_agent_group, &spoof_user_agent_menu](auto name, auto const& user_agent) {
  403. auto* action = new QAction(qstring_from_ak_string(name), this);
  404. action->setCheckable(true);
  405. user_agent_group->addAction(action);
  406. spoof_user_agent_menu->addAction(action);
  407. QObject::connect(action, &QAction::triggered, this, [this, user_agent] {
  408. for_each_tab([user_agent](auto& tab) {
  409. tab.set_user_agent_string(user_agent);
  410. });
  411. set_user_agent_string(user_agent);
  412. });
  413. return action;
  414. };
  415. auto const& user_agent_preset = WebView::Application::web_content_options().user_agent_preset;
  416. set_user_agent_string(user_agent_preset.has_value() ? *WebView::user_agents.get(*user_agent_preset) : Web::default_user_agent);
  417. auto* disable_spoofing = add_user_agent("Disabled"sv, Web::default_user_agent);
  418. disable_spoofing->setChecked(!user_agent_preset.has_value());
  419. for (auto const& user_agent : WebView::user_agents) {
  420. auto* spoofed_user_agent = add_user_agent(user_agent.key, user_agent.value.to_byte_string());
  421. spoofed_user_agent->setChecked(user_agent.key == user_agent_preset);
  422. }
  423. auto* custom_user_agent_action = new QAction("Custom...", this);
  424. custom_user_agent_action->setCheckable(true);
  425. user_agent_group->addAction(custom_user_agent_action);
  426. spoof_user_agent_menu->addAction(custom_user_agent_action);
  427. QObject::connect(custom_user_agent_action, &QAction::triggered, this, [this, disable_spoofing] {
  428. auto user_agent = QInputDialog::getText(this, "Custom User Agent", "Enter User Agent:");
  429. if (!user_agent.isEmpty()) {
  430. auto user_agent_byte_string = ak_byte_string_from_qstring(user_agent);
  431. for_each_tab([&](auto& tab) {
  432. tab.set_user_agent_string(user_agent_byte_string);
  433. });
  434. set_user_agent_string(user_agent_byte_string);
  435. } else {
  436. disable_spoofing->activate(QAction::Trigger);
  437. }
  438. });
  439. auto* navigator_compatibility_mode_menu = debug_menu->addMenu("Navigator Compatibility Mode");
  440. navigator_compatibility_mode_menu->setIcon(load_icon_from_uri("resource://icons/16x16/spoof.png"sv));
  441. auto* navigator_compatibility_mode_group = new QActionGroup(this);
  442. auto add_navigator_compatibility_mode = [this, &navigator_compatibility_mode_group, &navigator_compatibility_mode_menu](auto name, auto const& compatibility_mode) {
  443. auto* action = new QAction(qstring_from_ak_string(name), this);
  444. action->setCheckable(true);
  445. navigator_compatibility_mode_group->addAction(action);
  446. navigator_compatibility_mode_menu->addAction(action);
  447. QObject::connect(action, &QAction::triggered, this, [this, compatibility_mode] {
  448. for_each_tab([compatibility_mode](auto& tab) {
  449. tab.set_navigator_compatibility_mode(compatibility_mode);
  450. });
  451. set_navigator_compatibility_mode(compatibility_mode);
  452. });
  453. return action;
  454. };
  455. auto* chrome_compatibility_mode = add_navigator_compatibility_mode("Chrome"_string, "chrome"sv.to_byte_string());
  456. chrome_compatibility_mode->setChecked(true);
  457. add_navigator_compatibility_mode("Gecko"_string, "gecko"sv.to_byte_string());
  458. add_navigator_compatibility_mode("WebKit"_string, "webkit"sv.to_byte_string());
  459. set_navigator_compatibility_mode("chrome");
  460. debug_menu->addSeparator();
  461. m_enable_scripting_action = new QAction("Enable Scripting", this);
  462. m_enable_scripting_action->setCheckable(true);
  463. m_enable_scripting_action->setChecked(WebView::Application::chrome_options().disable_scripting == WebView::DisableScripting::No);
  464. debug_menu->addAction(m_enable_scripting_action);
  465. QObject::connect(m_enable_scripting_action, &QAction::triggered, this, [this] {
  466. bool state = m_enable_scripting_action->isChecked();
  467. for_each_tab([state](auto& tab) {
  468. tab.set_scripting(state);
  469. });
  470. });
  471. m_block_pop_ups_action = new QAction("Block Pop-ups", this);
  472. m_block_pop_ups_action->setCheckable(true);
  473. m_block_pop_ups_action->setChecked(WebView::Application::chrome_options().allow_popups == WebView::AllowPopups::No);
  474. debug_menu->addAction(m_block_pop_ups_action);
  475. QObject::connect(m_block_pop_ups_action, &QAction::triggered, this, [this] {
  476. bool state = m_block_pop_ups_action->isChecked();
  477. for_each_tab([state](auto& tab) {
  478. tab.set_block_popups(state);
  479. });
  480. });
  481. m_enable_same_origin_policy_action = new QAction("Enable Same-Origin Policy", this);
  482. m_enable_same_origin_policy_action->setCheckable(true);
  483. debug_menu->addAction(m_enable_same_origin_policy_action);
  484. QObject::connect(m_enable_same_origin_policy_action, &QAction::triggered, this, [this] {
  485. bool state = m_enable_same_origin_policy_action->isChecked();
  486. for_each_tab([state](auto& tab) {
  487. tab.set_same_origin_policy(state);
  488. });
  489. });
  490. auto* help_menu = m_hamburger_menu->addMenu("&Help");
  491. menuBar()->addMenu(help_menu);
  492. auto* about_action = new QAction("&About Ladybird", this);
  493. help_menu->addAction(about_action);
  494. QObject::connect(about_action, &QAction::triggered, this, [this] {
  495. new_tab_from_url("about:version"sv, Web::HTML::ActivateTab::Yes);
  496. });
  497. m_hamburger_menu->addSeparator();
  498. file_menu->addSeparator();
  499. auto* quit_action = new QAction("&Quit", this);
  500. quit_action->setShortcuts(QKeySequence::keyBindings(QKeySequence::StandardKey::Quit));
  501. m_hamburger_menu->addAction(quit_action);
  502. file_menu->addAction(quit_action);
  503. QObject::connect(quit_action, &QAction::triggered, this, &QMainWindow::close);
  504. QObject::connect(m_new_tab_action, &QAction::triggered, this, [this] {
  505. auto& tab = new_tab_from_url(ak_url_from_qstring(Settings::the()->new_tab_page()), Web::HTML::ActivateTab::Yes);
  506. tab.set_url_is_hidden(true);
  507. tab.focus_location_editor();
  508. });
  509. QObject::connect(m_new_window_action, &QAction::triggered, this, [] {
  510. (void)static_cast<Ladybird::Application*>(QApplication::instance())->new_window({});
  511. });
  512. QObject::connect(open_file_action, &QAction::triggered, this, &BrowserWindow::open_file);
  513. QObject::connect(settings_action, &QAction::triggered, this, [this] {
  514. if (!m_settings_dialog) {
  515. m_settings_dialog = new SettingsDialog(this);
  516. }
  517. m_settings_dialog->show();
  518. m_settings_dialog->setFocus();
  519. });
  520. QObject::connect(m_tabs_container, &QTabWidget::currentChanged, [this](int index) {
  521. auto* tab = verify_cast<Tab>(m_tabs_container->widget(index));
  522. if (tab)
  523. setWindowTitle(QString("%1 - Ladybird").arg(tab->title()));
  524. set_current_tab(tab);
  525. });
  526. QObject::connect(m_tabs_container, &QTabWidget::tabCloseRequested, this, &BrowserWindow::close_tab);
  527. QObject::connect(close_current_tab_action, &QAction::triggered, this, &BrowserWindow::close_current_tab);
  528. m_inspect_dom_node_action = new QAction("&Inspect Element", this);
  529. connect(m_inspect_dom_node_action, &QAction::triggered, this, [this] {
  530. if (m_current_tab)
  531. m_current_tab->show_inspector_window(Tab::InspectorTarget::HoveredElement);
  532. });
  533. m_go_back_action = new QAction("Go Back", this);
  534. connect(m_go_back_action, &QAction::triggered, this, [this] {
  535. if (m_current_tab)
  536. m_current_tab->back();
  537. });
  538. m_go_forward_action = new QAction("Go Forward", this);
  539. connect(m_go_forward_action, &QAction::triggered, this, [this] {
  540. if (m_current_tab)
  541. m_current_tab->forward();
  542. });
  543. m_reload_action = new QAction("&Reload", this);
  544. connect(m_reload_action, &QAction::triggered, this, [this] {
  545. if (m_current_tab)
  546. m_current_tab->reload();
  547. });
  548. m_go_back_action->setShortcuts(QKeySequence::keyBindings(QKeySequence::StandardKey::Back));
  549. m_go_forward_action->setShortcuts(QKeySequence::keyBindings(QKeySequence::StandardKey::Forward));
  550. m_reload_action->setShortcuts(QKeySequence::keyBindings(QKeySequence::StandardKey::Refresh));
  551. m_go_back_action->setEnabled(false);
  552. m_go_forward_action->setEnabled(false);
  553. m_reload_action->setEnabled(true);
  554. for (int i = 0; i <= 7; ++i) {
  555. new QShortcut(QKeySequence(Qt::CTRL | static_cast<Qt::Key>(Qt::Key_1 + i)), this, [this, i] {
  556. if (m_tabs_container->count() <= 1)
  557. return;
  558. m_tabs_container->setCurrentIndex(min(i, m_tabs_container->count() - 1));
  559. });
  560. }
  561. new QShortcut(QKeySequence(Qt::CTRL | Qt::Key_9), this, [this] {
  562. if (m_tabs_container->count() <= 1)
  563. return;
  564. m_tabs_container->setCurrentIndex(m_tabs_container->count() - 1);
  565. });
  566. if (parent_tab) {
  567. new_child_tab(Web::HTML::ActivateTab::Yes, *parent_tab, AK::move(page_index));
  568. } else {
  569. for (size_t i = 0; i < initial_urls.size(); ++i) {
  570. new_tab_from_url(initial_urls[i], (i == 0) ? Web::HTML::ActivateTab::Yes : Web::HTML::ActivateTab::No);
  571. }
  572. }
  573. m_new_tab_button_toolbar->addAction(m_new_tab_action);
  574. m_new_tab_button_toolbar->setMovable(false);
  575. m_new_tab_button_toolbar->setStyleSheet("QToolBar { background: transparent; }");
  576. m_new_tab_button_toolbar->setIconSize(QSize(16, 16));
  577. m_tabs_container->setCornerWidget(m_new_tab_button_toolbar, Qt::TopRightCorner);
  578. setCentralWidget(m_tabs_container);
  579. setContextMenuPolicy(Qt::PreventContextMenu);
  580. }
  581. void BrowserWindow::set_current_tab(Tab* tab)
  582. {
  583. m_current_tab = tab;
  584. if (tab) {
  585. update_displayed_zoom_level();
  586. tab->update_navigation_buttons_state();
  587. }
  588. }
  589. void BrowserWindow::debug_request(ByteString const& request, ByteString const& argument)
  590. {
  591. if (!m_current_tab)
  592. return;
  593. m_current_tab->debug_request(request, argument);
  594. }
  595. Tab& BrowserWindow::new_tab_from_url(URL::URL const& url, Web::HTML::ActivateTab activate_tab)
  596. {
  597. auto& tab = create_new_tab(activate_tab);
  598. tab.navigate(url);
  599. return tab;
  600. }
  601. Tab& BrowserWindow::new_tab_from_content(StringView html, Web::HTML::ActivateTab activate_tab)
  602. {
  603. auto& tab = create_new_tab(activate_tab);
  604. tab.load_html(html);
  605. return tab;
  606. }
  607. Tab& BrowserWindow::new_child_tab(Web::HTML::ActivateTab activate_tab, Tab& parent, Optional<u64> page_index)
  608. {
  609. return create_new_tab(activate_tab, parent, page_index);
  610. }
  611. Tab& BrowserWindow::create_new_tab(Web::HTML::ActivateTab activate_tab, Tab& parent, Optional<u64> page_index)
  612. {
  613. if (!page_index.has_value())
  614. return create_new_tab(activate_tab);
  615. auto* tab = new Tab(this, parent.view().client(), page_index.value());
  616. // FIXME: Merge with other overload
  617. if (m_current_tab == nullptr) {
  618. set_current_tab(tab);
  619. }
  620. m_tabs_container->addTab(tab, "New Tab");
  621. if (activate_tab == Web::HTML::ActivateTab::Yes)
  622. m_tabs_container->setCurrentWidget(tab);
  623. initialize_tab(tab);
  624. return *tab;
  625. }
  626. Tab& BrowserWindow::create_new_tab(Web::HTML::ActivateTab activate_tab)
  627. {
  628. auto* tab = new Tab(this);
  629. if (m_current_tab == nullptr) {
  630. set_current_tab(tab);
  631. }
  632. m_tabs_container->addTab(tab, "New Tab");
  633. if (activate_tab == Web::HTML::ActivateTab::Yes)
  634. m_tabs_container->setCurrentWidget(tab);
  635. initialize_tab(tab);
  636. return *tab;
  637. }
  638. void BrowserWindow::initialize_tab(Tab* tab)
  639. {
  640. QObject::connect(tab, &Tab::title_changed, this, &BrowserWindow::tab_title_changed);
  641. QObject::connect(tab, &Tab::favicon_changed, this, &BrowserWindow::tab_favicon_changed);
  642. QObject::connect(tab, &Tab::audio_play_state_changed, this, &BrowserWindow::tab_audio_play_state_changed);
  643. QObject::connect(tab, &Tab::navigation_buttons_state_changed, this, &BrowserWindow::tab_navigation_buttons_state_changed);
  644. QObject::connect(&tab->view(), &WebContentView::urls_dropped, this, [this](auto& urls) {
  645. VERIFY(urls.size());
  646. m_current_tab->navigate(ak_url_from_qurl(urls[0]));
  647. for (qsizetype i = 1; i < urls.size(); ++i)
  648. new_tab_from_url(ak_url_from_qurl(urls[i]), Web::HTML::ActivateTab::No);
  649. });
  650. tab->view().on_new_web_view = [this, tab](auto activate_tab, Web::HTML::WebViewHints hints, Optional<u64> page_index) {
  651. if (hints.popup) {
  652. auto& window = static_cast<Ladybird::Application*>(QApplication::instance())->new_window({}, IsPopupWindow::Yes, tab, AK::move(page_index));
  653. window.set_window_rect(hints.screen_x, hints.screen_y, hints.width, hints.height);
  654. return window.current_tab()->view().handle();
  655. }
  656. auto& new_tab = new_child_tab(activate_tab, *tab, page_index);
  657. return new_tab.view().handle();
  658. };
  659. tab->view().on_tab_open_request = [this](auto url, auto activate_tab) {
  660. auto& tab = new_tab_from_url(url, activate_tab);
  661. return tab.view().handle();
  662. };
  663. tab->view().on_link_click = [this](auto url, auto target, unsigned modifiers) {
  664. // TODO: maybe activate tabs according to some configuration, this is just normal current browser behavior
  665. if (modifiers == Web::UIEvents::Mod_Ctrl) {
  666. m_current_tab->view().on_tab_open_request(url, Web::HTML::ActivateTab::No);
  667. } else if (target == "_blank") {
  668. m_current_tab->view().on_tab_open_request(url, Web::HTML::ActivateTab::Yes);
  669. } else {
  670. m_current_tab->view().load(url);
  671. }
  672. };
  673. tab->view().on_link_middle_click = [this](auto url, auto target, unsigned modifiers) {
  674. m_current_tab->view().on_link_click(url, target, Web::UIEvents::Mod_Ctrl);
  675. (void)modifiers;
  676. };
  677. m_tabs_container->setTabIcon(m_tabs_container->indexOf(tab), tab->favicon());
  678. create_close_button_for_tab(tab);
  679. Vector<String> preferred_languages;
  680. preferred_languages.ensure_capacity(Settings::the()->preferred_languages().length());
  681. for (auto& language : Settings::the()->preferred_languages()) {
  682. preferred_languages.append(ak_string_from_qstring(language));
  683. }
  684. tab->set_line_box_borders(m_show_line_box_borders_action->isChecked());
  685. tab->set_scripting(m_enable_scripting_action->isChecked());
  686. tab->set_block_popups(m_block_pop_ups_action->isChecked());
  687. tab->set_same_origin_policy(m_enable_same_origin_policy_action->isChecked());
  688. tab->set_user_agent_string(user_agent_string());
  689. tab->set_preferred_languages(preferred_languages);
  690. tab->set_navigator_compatibility_mode(navigator_compatibility_mode());
  691. tab->set_enable_do_not_track(Settings::the()->enable_do_not_track());
  692. tab->set_enable_autoplay(WebView::Application::web_content_options().enable_autoplay == WebView::EnableAutoplay::Yes || Settings::the()->enable_autoplay());
  693. tab->view().set_preferred_color_scheme(m_preferred_color_scheme);
  694. }
  695. void BrowserWindow::activate_tab(int index)
  696. {
  697. m_tabs_container->setCurrentIndex(index);
  698. }
  699. void BrowserWindow::close_tab(int index)
  700. {
  701. auto* tab = m_tabs_container->widget(index);
  702. m_tabs_container->removeTab(index);
  703. tab->deleteLater();
  704. if (m_tabs_container->count() == 0)
  705. close();
  706. }
  707. void BrowserWindow::move_tab(int old_index, int new_index)
  708. {
  709. m_tabs_container->tabBar()->moveTab(old_index, new_index);
  710. }
  711. void BrowserWindow::open_file()
  712. {
  713. m_current_tab->open_file();
  714. }
  715. void BrowserWindow::close_current_tab()
  716. {
  717. close_tab(m_tabs_container->currentIndex());
  718. }
  719. int BrowserWindow::tab_index(Tab* tab)
  720. {
  721. return m_tabs_container->indexOf(tab);
  722. }
  723. void BrowserWindow::device_pixel_ratio_changed(qreal dpi)
  724. {
  725. m_device_pixel_ratio = dpi;
  726. for_each_tab([this](auto& tab) {
  727. tab.view().set_device_pixel_ratio(m_device_pixel_ratio);
  728. });
  729. }
  730. void BrowserWindow::tab_title_changed(int index, QString const& title)
  731. {
  732. // NOTE: Qt uses ampersands for shortcut keys in tab titles, so we need to escape them.
  733. QString title_escaped = title;
  734. title_escaped.replace("&", "&&");
  735. m_tabs_container->setTabText(index, title_escaped);
  736. m_tabs_container->setTabToolTip(index, title);
  737. if (m_tabs_container->currentIndex() == index)
  738. setWindowTitle(QString("%1 - Ladybird").arg(title));
  739. }
  740. void BrowserWindow::tab_favicon_changed(int index, QIcon const& icon)
  741. {
  742. m_tabs_container->setTabIcon(index, icon);
  743. }
  744. void BrowserWindow::create_close_button_for_tab(Tab* tab)
  745. {
  746. auto index = m_tabs_container->indexOf(tab);
  747. m_tabs_container->setTabIcon(index, tab->favicon());
  748. auto* button = new TabBarButton(create_tvg_icon_with_theme_colors("close", palette()));
  749. auto position = audio_button_position_for_tab(index) == QTabBar::LeftSide ? QTabBar::RightSide : QTabBar::LeftSide;
  750. connect(button, &QPushButton::clicked, this, [this, tab]() {
  751. auto index = m_tabs_container->indexOf(tab);
  752. close_tab(index);
  753. });
  754. m_tabs_container->tabBar()->setTabButton(index, position, button);
  755. }
  756. void BrowserWindow::tab_audio_play_state_changed(int index, Web::HTML::AudioPlayState play_state)
  757. {
  758. auto* tab = verify_cast<Tab>(m_tabs_container->widget(index));
  759. auto position = audio_button_position_for_tab(index);
  760. switch (play_state) {
  761. case Web::HTML::AudioPlayState::Paused:
  762. if (tab->view().page_mute_state() == Web::HTML::MuteState::Unmuted)
  763. m_tabs_container->tabBar()->setTabButton(index, position, nullptr);
  764. break;
  765. case Web::HTML::AudioPlayState::Playing:
  766. auto* button = new TabBarButton(icon_for_page_mute_state(*tab));
  767. button->setToolTip(tool_tip_for_page_mute_state(*tab));
  768. button->setObjectName("LadybirdAudioState");
  769. connect(button, &QPushButton::clicked, this, [this, tab, position]() {
  770. tab->view().toggle_page_mute_state();
  771. auto index = tab_index(tab);
  772. switch (tab->view().audio_play_state()) {
  773. case Web::HTML::AudioPlayState::Paused:
  774. m_tabs_container->tabBar()->setTabButton(index, position, nullptr);
  775. break;
  776. case Web::HTML::AudioPlayState::Playing:
  777. auto* button = m_tabs_container->tabBar()->tabButton(index, position);
  778. verify_cast<TabBarButton>(button)->setIcon(icon_for_page_mute_state(*tab));
  779. button->setToolTip(tool_tip_for_page_mute_state(*tab));
  780. break;
  781. }
  782. });
  783. m_tabs_container->tabBar()->setTabButton(index, position, button);
  784. break;
  785. }
  786. }
  787. void BrowserWindow::tab_navigation_buttons_state_changed(int index)
  788. {
  789. auto* tab = verify_cast<Tab>(m_tabs_container->widget(index));
  790. tab->update_navigation_buttons_state();
  791. }
  792. QIcon BrowserWindow::icon_for_page_mute_state(Tab& tab) const
  793. {
  794. switch (tab.view().page_mute_state()) {
  795. case Web::HTML::MuteState::Muted:
  796. return style()->standardIcon(QStyle::SP_MediaVolumeMuted);
  797. case Web::HTML::MuteState::Unmuted:
  798. return style()->standardIcon(QStyle::SP_MediaVolume);
  799. }
  800. VERIFY_NOT_REACHED();
  801. }
  802. QString BrowserWindow::tool_tip_for_page_mute_state(Tab& tab) const
  803. {
  804. switch (tab.view().page_mute_state()) {
  805. case Web::HTML::MuteState::Muted:
  806. return "Unmute tab";
  807. case Web::HTML::MuteState::Unmuted:
  808. return "Mute tab";
  809. }
  810. VERIFY_NOT_REACHED();
  811. }
  812. QTabBar::ButtonPosition BrowserWindow::audio_button_position_for_tab(int tab_index) const
  813. {
  814. if (auto* button = m_tabs_container->tabBar()->tabButton(tab_index, QTabBar::LeftSide)) {
  815. if (button->objectName() != "LadybirdAudioState")
  816. return QTabBar::RightSide;
  817. }
  818. return QTabBar::LeftSide;
  819. }
  820. void BrowserWindow::open_next_tab()
  821. {
  822. if (m_tabs_container->count() <= 1)
  823. return;
  824. auto next_index = m_tabs_container->currentIndex() + 1;
  825. if (next_index >= m_tabs_container->count())
  826. next_index = 0;
  827. m_tabs_container->setCurrentIndex(next_index);
  828. }
  829. void BrowserWindow::open_previous_tab()
  830. {
  831. if (m_tabs_container->count() <= 1)
  832. return;
  833. auto next_index = m_tabs_container->currentIndex() - 1;
  834. if (next_index < 0)
  835. next_index = m_tabs_container->count() - 1;
  836. m_tabs_container->setCurrentIndex(next_index);
  837. }
  838. void BrowserWindow::enable_auto_contrast()
  839. {
  840. for_each_tab([](auto& tab) {
  841. tab.view().set_preferred_contrast(Web::CSS::PreferredContrast::Auto);
  842. });
  843. }
  844. void BrowserWindow::enable_less_contrast()
  845. {
  846. for_each_tab([](auto& tab) {
  847. tab.view().set_preferred_contrast(Web::CSS::PreferredContrast::Less);
  848. });
  849. }
  850. void BrowserWindow::enable_more_contrast()
  851. {
  852. for_each_tab([](auto& tab) {
  853. tab.view().set_preferred_contrast(Web::CSS::PreferredContrast::More);
  854. });
  855. }
  856. void BrowserWindow::enable_no_preference_contrast()
  857. {
  858. for_each_tab([](auto& tab) {
  859. tab.view().set_preferred_contrast(Web::CSS::PreferredContrast::NoPreference);
  860. });
  861. }
  862. void BrowserWindow::enable_auto_motion()
  863. {
  864. for_each_tab([](auto& tab) {
  865. tab.view().set_preferred_motion(Web::CSS::PreferredMotion::Auto);
  866. });
  867. }
  868. void BrowserWindow::enable_no_preference_motion()
  869. {
  870. for_each_tab([](auto& tab) {
  871. tab.view().set_preferred_motion(Web::CSS::PreferredMotion::NoPreference);
  872. });
  873. }
  874. void BrowserWindow::enable_reduce_motion()
  875. {
  876. for_each_tab([](auto& tab) {
  877. tab.view().set_preferred_motion(Web::CSS::PreferredMotion::Reduce);
  878. });
  879. }
  880. void BrowserWindow::zoom_in()
  881. {
  882. if (!m_current_tab)
  883. return;
  884. m_current_tab->view().zoom_in();
  885. update_displayed_zoom_level();
  886. }
  887. void BrowserWindow::zoom_out()
  888. {
  889. if (!m_current_tab)
  890. return;
  891. m_current_tab->view().zoom_out();
  892. update_displayed_zoom_level();
  893. }
  894. void BrowserWindow::reset_zoom()
  895. {
  896. if (!m_current_tab)
  897. return;
  898. m_current_tab->view().reset_zoom();
  899. update_displayed_zoom_level();
  900. }
  901. void BrowserWindow::update_zoom_menu()
  902. {
  903. VERIFY(m_zoom_menu);
  904. auto zoom_level_text = MUST(String::formatted("&Zoom ({}%)", round_to<int>(m_current_tab->view().zoom_level() * 100)));
  905. m_zoom_menu->setTitle(qstring_from_ak_string(zoom_level_text));
  906. }
  907. void BrowserWindow::select_all()
  908. {
  909. if (!m_current_tab)
  910. return;
  911. m_current_tab->view().select_all();
  912. }
  913. void BrowserWindow::show_find_in_page()
  914. {
  915. if (!m_current_tab)
  916. return;
  917. m_current_tab->show_find_in_page();
  918. }
  919. void BrowserWindow::paste()
  920. {
  921. if (!m_current_tab)
  922. return;
  923. auto* clipboard = QGuiApplication::clipboard();
  924. m_current_tab->view().paste(ak_string_from_qstring(clipboard->text()));
  925. }
  926. void BrowserWindow::update_displayed_zoom_level()
  927. {
  928. VERIFY(m_current_tab);
  929. update_zoom_menu();
  930. m_current_tab->update_reset_zoom_button();
  931. }
  932. void BrowserWindow::set_window_rect(Optional<Web::DevicePixels> x, Optional<Web::DevicePixels> y, Optional<Web::DevicePixels> width, Optional<Web::DevicePixels> height)
  933. {
  934. x = x.value_or(0);
  935. y = y.value_or(0);
  936. if (!width.has_value() || width.value() == 0)
  937. width = 800;
  938. if (!height.has_value() || height.value() == 0)
  939. height = 600;
  940. setGeometry(x.value().value(), y.value().value(), width.value().value(), height.value().value());
  941. }
  942. void BrowserWindow::set_preferred_color_scheme(Web::CSS::PreferredColorScheme color_scheme)
  943. {
  944. m_preferred_color_scheme = color_scheme;
  945. for_each_tab([color_scheme](auto& tab) {
  946. tab.view().set_preferred_color_scheme(color_scheme);
  947. });
  948. }
  949. void BrowserWindow::copy_selected_text()
  950. {
  951. if (!m_current_tab)
  952. return;
  953. auto text = m_current_tab->view().selected_text();
  954. auto* clipboard = QGuiApplication::clipboard();
  955. clipboard->setText(qstring_from_ak_string(text));
  956. }
  957. bool BrowserWindow::event(QEvent* event)
  958. {
  959. #if QT_VERSION >= QT_VERSION_CHECK(6, 6, 0)
  960. if (event->type() == QEvent::DevicePixelRatioChange) {
  961. if (m_device_pixel_ratio != devicePixelRatio())
  962. device_pixel_ratio_changed(devicePixelRatio());
  963. }
  964. #endif
  965. if (event->type() == QEvent::WindowActivate)
  966. static_cast<Ladybird::Application*>(QApplication::instance())->set_active_window(*this);
  967. return QMainWindow::event(event);
  968. }
  969. void BrowserWindow::resizeEvent(QResizeEvent* event)
  970. {
  971. QWidget::resizeEvent(event);
  972. for_each_tab([&](auto& tab) {
  973. tab.view().set_window_size({ frameSize().width() * m_device_pixel_ratio, frameSize().height() * m_device_pixel_ratio });
  974. });
  975. }
  976. void BrowserWindow::moveEvent(QMoveEvent* event)
  977. {
  978. QWidget::moveEvent(event);
  979. for_each_tab([&](auto& tab) {
  980. tab.view().set_window_position({ event->pos().x() * m_device_pixel_ratio, event->pos().y() * m_device_pixel_ratio });
  981. });
  982. }
  983. void BrowserWindow::wheelEvent(QWheelEvent* event)
  984. {
  985. if ((event->modifiers() & Qt::ControlModifier) != 0) {
  986. if (event->angleDelta().y() > 0)
  987. zoom_in();
  988. else if (event->angleDelta().y() < 0)
  989. zoom_out();
  990. }
  991. }
  992. bool BrowserWindow::eventFilter(QObject* obj, QEvent* event)
  993. {
  994. if (event->type() == QEvent::MouseButtonRelease) {
  995. auto const* const mouse_event = static_cast<QMouseEvent*>(event);
  996. if (mouse_event->button() == Qt::MouseButton::MiddleButton) {
  997. if (obj == m_tabs_container) {
  998. auto const tab_index = m_tabs_container->tabBar()->tabAt(mouse_event->pos());
  999. if (tab_index != -1) {
  1000. close_tab(tab_index);
  1001. return true;
  1002. }
  1003. }
  1004. }
  1005. }
  1006. return QMainWindow::eventFilter(obj, event);
  1007. }
  1008. void BrowserWindow::closeEvent(QCloseEvent* event)
  1009. {
  1010. if (m_is_popup_window == IsPopupWindow::No) {
  1011. Settings::the()->set_last_position(pos());
  1012. Settings::the()->set_last_size(size());
  1013. Settings::the()->set_is_maximized(isMaximized());
  1014. }
  1015. QObject::deleteLater();
  1016. QMainWindow::closeEvent(event);
  1017. }
  1018. }