BrowserWindow.cpp 44 KB

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