BrowserWindow.cpp 47 KB

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