Tab.cpp 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965
  1. /*
  2. * Copyright (c) 2022, Andreas Kling <andreas@ladybird.org>
  3. * Copyright (c) 2022, Matthew Costa <ucosty@gmail.com>
  4. * Copyright (c) 2024, Jamie Mansfield <jmansfield@cadixdev.org>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include "BrowserWindow.h"
  9. #include "Icon.h"
  10. #include "InspectorWidget.h"
  11. #include "Settings.h"
  12. #include "StringUtils.h"
  13. #include <AK/TemporaryChange.h>
  14. #include <LibGfx/DeprecatedPainter.h>
  15. #include <LibGfx/ImageFormats/BMPWriter.h>
  16. #include <LibWeb/HTML/SelectedFile.h>
  17. #include <LibWebView/SearchEngine.h>
  18. #include <LibWebView/SourceHighlighter.h>
  19. #include <LibWebView/URL.h>
  20. #include <QClipboard>
  21. #include <QColorDialog>
  22. #include <QCoreApplication>
  23. #include <QCursor>
  24. #include <QDesktopServices>
  25. #include <QFileDialog>
  26. #include <QFont>
  27. #include <QFontMetrics>
  28. #include <QGuiApplication>
  29. #include <QImage>
  30. #include <QInputDialog>
  31. #include <QMenu>
  32. #include <QMessageBox>
  33. #include <QMimeData>
  34. #include <QMimeDatabase>
  35. #include <QMimeType>
  36. #include <QPainter>
  37. #include <QPoint>
  38. #include <QPushButton>
  39. #include <QResizeEvent>
  40. namespace Ladybird {
  41. static QIcon default_favicon()
  42. {
  43. static QIcon icon = load_icon_from_uri("resource://icons/48x48/app-browser.png"sv);
  44. return icon;
  45. }
  46. Tab::Tab(BrowserWindow* window, RefPtr<WebView::WebContentClient> parent_client, size_t page_index)
  47. : QWidget(window)
  48. , m_window(window)
  49. {
  50. m_layout = new QBoxLayout(QBoxLayout::Direction::TopToBottom, this);
  51. m_layout->setSpacing(0);
  52. m_layout->setContentsMargins(0, 0, 0, 0);
  53. m_view = new WebContentView(this, parent_client, page_index);
  54. m_find_in_page = new FindInPageWidget(this, m_view);
  55. m_find_in_page->setVisible(false);
  56. m_toolbar = new QToolBar(this);
  57. m_location_edit = new LocationEdit(this);
  58. m_hover_label = new QLabel(this);
  59. m_hover_label->hide();
  60. m_hover_label->setFrameShape(QFrame::Shape::Box);
  61. m_hover_label->setAutoFillBackground(true);
  62. auto* focus_location_editor_action = new QAction("Edit Location", this);
  63. focus_location_editor_action->setShortcut(QKeySequence("Ctrl+L"));
  64. addAction(focus_location_editor_action);
  65. m_layout->addWidget(m_toolbar);
  66. m_layout->addWidget(m_view);
  67. m_layout->addWidget(m_find_in_page);
  68. m_hamburger_button = new QToolButton(m_toolbar);
  69. m_hamburger_button->setText("Show &Menu");
  70. m_hamburger_button->setToolTip("Show Menu");
  71. m_hamburger_button->setIcon(create_tvg_icon_with_theme_colors("hamburger", palette()));
  72. m_hamburger_button->setPopupMode(QToolButton::InstantPopup);
  73. m_hamburger_button->setMenu(&m_window->hamburger_menu());
  74. m_hamburger_button->setStyleSheet(":menu-indicator {image: none}");
  75. recreate_toolbar_icons();
  76. m_favicon = default_favicon();
  77. m_toolbar->addAction(&m_window->go_back_action());
  78. m_toolbar->addAction(&m_window->go_forward_action());
  79. m_toolbar->addAction(&m_window->reload_action());
  80. m_toolbar->addWidget(m_location_edit);
  81. m_toolbar->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed);
  82. m_hamburger_button_action = m_toolbar->addWidget(m_hamburger_button);
  83. m_toolbar->setIconSize({ 16, 16 });
  84. // This is a little awkward, but without this Qt shrinks the button to the size of the icon.
  85. // Note: toolButtonStyle="0" -> ToolButtonIconOnly.
  86. m_toolbar->setStyleSheet("QToolButton[toolButtonStyle=\"0\"]{width:24px;height:24px}");
  87. m_hamburger_button_action->setVisible(!Settings::the()->show_menubar());
  88. QObject::connect(Settings::the(), &Settings::show_menubar_changed, this, [this](bool show_menubar) {
  89. m_hamburger_button_action->setVisible(!show_menubar);
  90. });
  91. m_reset_zoom_button = new QToolButton(m_toolbar);
  92. m_reset_zoom_button->setToolButtonStyle(Qt::ToolButtonTextOnly);
  93. m_reset_zoom_button->setToolTip("Reset zoom level");
  94. m_reset_zoom_button_action = m_toolbar->addWidget(m_reset_zoom_button);
  95. m_reset_zoom_button_action->setVisible(false);
  96. QObject::connect(m_reset_zoom_button, &QAbstractButton::clicked, [this] {
  97. view().reset_zoom();
  98. update_reset_zoom_button();
  99. m_window->update_zoom_menu();
  100. });
  101. view().on_activate_tab = [this] {
  102. m_window->activate_tab(tab_index());
  103. };
  104. view().on_close = [this] {
  105. m_window->close_tab(tab_index());
  106. };
  107. view().on_link_hover = [this](auto const& url) {
  108. m_hover_label->setText(qstring_from_ak_string(url.to_byte_string()));
  109. update_hover_label();
  110. m_hover_label->show();
  111. };
  112. view().on_link_unhover = [this]() {
  113. m_hover_label->hide();
  114. };
  115. view().on_load_start = [this](const URL::URL& url, bool) {
  116. if (m_inspector_widget)
  117. m_inspector_widget->reset();
  118. auto url_serialized = qstring_from_ak_string(url.serialize());
  119. m_title = url_serialized;
  120. emit title_changed(tab_index(), url_serialized);
  121. m_favicon = default_favicon();
  122. emit favicon_changed(tab_index(), m_favicon);
  123. m_location_edit->set_url(url);
  124. m_location_edit->setCursorPosition(0);
  125. };
  126. view().on_load_finish = [this](auto&) {
  127. if (m_inspector_widget != nullptr && m_inspector_widget->isVisible())
  128. m_inspector_widget->inspect();
  129. };
  130. view().on_url_change = [this](auto const& url) {
  131. m_location_edit->set_url(url);
  132. };
  133. QObject::connect(m_location_edit, &QLineEdit::returnPressed, this, &Tab::location_edit_return_pressed);
  134. view().on_title_change = [this](auto const& title) {
  135. m_title = qstring_from_ak_string(title);
  136. emit title_changed(tab_index(), m_title);
  137. };
  138. view().on_favicon_change = [this](auto const& bitmap) {
  139. auto qimage = QImage(bitmap.scanline_u8(0), bitmap.width(), bitmap.height(), QImage::Format_ARGB32);
  140. if (qimage.isNull())
  141. return;
  142. auto qpixmap = QPixmap::fromImage(qimage);
  143. if (qpixmap.isNull())
  144. return;
  145. m_favicon = qpixmap;
  146. emit favicon_changed(tab_index(), m_favicon);
  147. };
  148. view().on_request_alert = [this](auto const& message) {
  149. m_dialog = new QMessageBox(QMessageBox::Icon::Warning, "Ladybird", qstring_from_ak_string(message), QMessageBox::StandardButton::Ok, &view());
  150. m_dialog->exec();
  151. view().alert_closed();
  152. m_dialog = nullptr;
  153. };
  154. view().on_request_confirm = [this](auto const& message) {
  155. m_dialog = new QMessageBox(QMessageBox::Icon::Question, "Ladybird", qstring_from_ak_string(message), QMessageBox::StandardButton::Ok | QMessageBox::StandardButton::Cancel, &view());
  156. auto result = m_dialog->exec();
  157. view().confirm_closed(result == QMessageBox::StandardButton::Ok || result == QDialog::Accepted);
  158. m_dialog = nullptr;
  159. };
  160. view().on_request_prompt = [this](auto const& message, auto const& default_) {
  161. m_dialog = new QInputDialog(&view());
  162. auto& dialog = static_cast<QInputDialog&>(*m_dialog);
  163. dialog.setWindowTitle("Ladybird");
  164. dialog.setLabelText(qstring_from_ak_string(message));
  165. dialog.setTextValue(qstring_from_ak_string(default_));
  166. if (dialog.exec() == QDialog::Accepted)
  167. view().prompt_closed(ak_string_from_qstring(dialog.textValue()));
  168. else
  169. view().prompt_closed({});
  170. m_dialog = nullptr;
  171. };
  172. view().on_request_set_prompt_text = [this](auto const& message) {
  173. if (m_dialog && is<QInputDialog>(*m_dialog))
  174. static_cast<QInputDialog&>(*m_dialog).setTextValue(qstring_from_ak_string(message));
  175. };
  176. view().on_request_accept_dialog = [this]() {
  177. if (m_dialog)
  178. m_dialog->accept();
  179. };
  180. view().on_request_dismiss_dialog = [this]() {
  181. if (m_dialog)
  182. m_dialog->reject();
  183. };
  184. view().on_request_color_picker = [this](Color current_color) {
  185. m_dialog = new QColorDialog(QColor(current_color.red(), current_color.green(), current_color.blue()), &view());
  186. auto& dialog = static_cast<QColorDialog&>(*m_dialog);
  187. dialog.setWindowTitle("Ladybird");
  188. dialog.setOption(QColorDialog::ShowAlphaChannel, false);
  189. QObject::connect(&dialog, &QColorDialog::currentColorChanged, this, [this](QColor const& color) {
  190. view().color_picker_update(Color(color.red(), color.green(), color.blue()), Web::HTML::ColorPickerUpdateState::Update);
  191. });
  192. if (dialog.exec() == QDialog::Accepted)
  193. view().color_picker_update(Color(dialog.selectedColor().red(), dialog.selectedColor().green(), dialog.selectedColor().blue()), Web::HTML::ColorPickerUpdateState::Closed);
  194. else
  195. view().color_picker_update({}, Web::HTML::ColorPickerUpdateState::Closed);
  196. m_dialog = nullptr;
  197. };
  198. view().on_request_file_picker = [this](auto const& accepted_file_types, auto allow_multiple_files) {
  199. Vector<Web::HTML::SelectedFile> selected_files;
  200. auto create_selected_file = [&](auto const& qfile_path) {
  201. auto file_path = ak_byte_string_from_qstring(qfile_path);
  202. if (auto file = Web::HTML::SelectedFile::from_file_path(file_path); file.is_error())
  203. warnln("Unable to open file {}: {}", file_path, file.error());
  204. else
  205. selected_files.append(file.release_value());
  206. };
  207. QStringList accepted_file_filters;
  208. QMimeDatabase mime_database;
  209. for (auto const& filter : accepted_file_types.filters) {
  210. filter.visit(
  211. [&](Web::HTML::FileFilter::FileType type) {
  212. QString title;
  213. QString filter;
  214. switch (type) {
  215. case Web::HTML::FileFilter::FileType::Audio:
  216. title = "Audio files";
  217. filter = "audio/";
  218. break;
  219. case Web::HTML::FileFilter::FileType::Image:
  220. title = "Image files";
  221. filter = "image/";
  222. break;
  223. case Web::HTML::FileFilter::FileType::Video:
  224. title = "Video files";
  225. filter = "video/";
  226. break;
  227. }
  228. QStringList extensions;
  229. for (auto const& mime_type : mime_database.allMimeTypes()) {
  230. if (mime_type.name().startsWith(filter))
  231. extensions.append(mime_type.globPatterns());
  232. }
  233. accepted_file_filters.append(QString("%1 (%2)").arg(title, extensions.join(" ")));
  234. },
  235. [&](Web::HTML::FileFilter::MimeType const& filter) {
  236. if (auto mime_type = mime_database.mimeTypeForName(qstring_from_ak_string(filter.value)); mime_type.isValid())
  237. accepted_file_filters.append(mime_type.filterString());
  238. },
  239. [&](Web::HTML::FileFilter::Extension const& filter) {
  240. auto extension = MUST(String::formatted("*.{}", filter.value));
  241. accepted_file_filters.append(qstring_from_ak_string(extension));
  242. });
  243. }
  244. accepted_file_filters.size() > 1 ? accepted_file_filters.prepend("All files (*)") : accepted_file_filters.append("All files (*)");
  245. auto filters = accepted_file_filters.join(";;");
  246. if (allow_multiple_files == Web::HTML::AllowMultipleFiles::Yes) {
  247. auto paths = QFileDialog::getOpenFileNames(this, "Select files", QDir::homePath(), filters);
  248. selected_files.ensure_capacity(static_cast<size_t>(paths.size()));
  249. for (auto const& path : paths)
  250. create_selected_file(path);
  251. } else {
  252. auto path = QFileDialog::getOpenFileName(this, "Select file", QDir::homePath(), filters);
  253. create_selected_file(path);
  254. }
  255. view().file_picker_closed(std::move(selected_files));
  256. };
  257. view().on_find_in_page = [this](auto current_match_index, auto const& total_match_count) {
  258. m_find_in_page->update_result_label(current_match_index, total_match_count);
  259. };
  260. QObject::connect(focus_location_editor_action, &QAction::triggered, this, &Tab::focus_location_editor);
  261. view().on_received_source = [this](auto const& url, auto const& base_url, auto const& source) {
  262. auto html = WebView::highlight_source(url, base_url, source, Syntax::Language::HTML, WebView::HighlightOutputMode::FullDocument);
  263. m_window->new_tab_from_content(html, Web::HTML::ActivateTab::Yes);
  264. };
  265. view().on_inspector_requested_style_sheet_source = [this](auto const& identifier) {
  266. view().request_style_sheet_source(identifier);
  267. };
  268. view().on_restore_window = [this]() {
  269. m_window->showNormal();
  270. };
  271. view().on_reposition_window = [this](auto const& position) {
  272. m_window->move(position.x(), position.y());
  273. return Gfx::IntPoint { m_window->x(), m_window->y() };
  274. };
  275. view().on_resize_window = [this](auto const& size) {
  276. m_window->resize(size.width(), size.height());
  277. return Gfx::IntSize { m_window->width(), m_window->height() };
  278. };
  279. view().on_maximize_window = [this]() {
  280. m_window->showMaximized();
  281. return Gfx::IntRect { m_window->x(), m_window->y(), m_window->width(), m_window->height() };
  282. };
  283. view().on_minimize_window = [this]() {
  284. m_window->showMinimized();
  285. return Gfx::IntRect { m_window->x(), m_window->y(), m_window->width(), m_window->height() };
  286. };
  287. view().on_fullscreen_window = [this]() {
  288. m_window->showFullScreen();
  289. return Gfx::IntRect { m_window->x(), m_window->y(), m_window->width(), m_window->height() };
  290. };
  291. view().on_insert_clipboard_entry = [](auto const& data, auto const&, auto const& mime_type) {
  292. QByteArray qdata { data.bytes_as_string_view().characters_without_null_termination(), static_cast<qsizetype>(data.bytes_as_string_view().length()) };
  293. auto* mime_data = new QMimeData();
  294. mime_data->setData(qstring_from_ak_string(mime_type), qdata);
  295. auto* clipboard = QGuiApplication::clipboard();
  296. clipboard->setMimeData(mime_data);
  297. };
  298. view().on_audio_play_state_changed = [this](auto play_state) {
  299. emit audio_play_state_changed(tab_index(), play_state);
  300. };
  301. view().on_navigation_buttons_state_changed = [this](auto back_enabled, auto forward_enabled) {
  302. m_can_navigate_back = back_enabled;
  303. m_can_navigate_forward = forward_enabled;
  304. emit navigation_buttons_state_changed(tab_index());
  305. };
  306. auto* reload_tab_action = new QAction("&Reload Tab", this);
  307. QObject::connect(reload_tab_action, &QAction::triggered, this, [this]() {
  308. reload();
  309. });
  310. auto* duplicate_tab_action = new QAction("&Duplicate Tab", this);
  311. QObject::connect(duplicate_tab_action, &QAction::triggered, this, [this]() {
  312. m_window->new_tab_from_url(view().url(), Web::HTML::ActivateTab::Yes);
  313. });
  314. auto* move_to_start_action = new QAction("Move to &Start", this);
  315. QObject::connect(move_to_start_action, &QAction::triggered, this, [this]() {
  316. m_window->move_tab(tab_index(), 0);
  317. });
  318. auto* move_to_end_action = new QAction("Move to &End", this);
  319. QObject::connect(move_to_end_action, &QAction::triggered, this, [this]() {
  320. m_window->move_tab(tab_index(), m_window->tab_count() - 1);
  321. });
  322. auto* close_tab_action = new QAction("&Close Tab", this);
  323. QObject::connect(close_tab_action, &QAction::triggered, this, [this]() {
  324. view().on_close();
  325. });
  326. auto* close_tabs_to_left_action = new QAction("C&lose Tabs to Left", this);
  327. QObject::connect(close_tabs_to_left_action, &QAction::triggered, this, [this]() {
  328. for (auto i = tab_index() - 1; i >= 0; i--) {
  329. m_window->close_tab(i);
  330. }
  331. });
  332. auto* close_tabs_to_right_action = new QAction("Close Tabs to R&ight", this);
  333. QObject::connect(close_tabs_to_right_action, &QAction::triggered, this, [this]() {
  334. for (auto i = m_window->tab_count() - 1; i > tab_index(); i--) {
  335. m_window->close_tab(i);
  336. }
  337. });
  338. auto* close_other_tabs_action = new QAction("Cl&ose Other Tabs", this);
  339. QObject::connect(close_other_tabs_action, &QAction::triggered, this, [this]() {
  340. for (auto i = m_window->tab_count() - 1; i >= 0; i--) {
  341. if (i == tab_index())
  342. continue;
  343. m_window->close_tab(i);
  344. }
  345. });
  346. m_context_menu = new QMenu("Context menu", this);
  347. m_context_menu->addAction(reload_tab_action);
  348. m_context_menu->addAction(duplicate_tab_action);
  349. m_context_menu->addSeparator();
  350. auto* move_tab_menu = m_context_menu->addMenu("Mo&ve Tab");
  351. move_tab_menu->addAction(move_to_start_action);
  352. move_tab_menu->addAction(move_to_end_action);
  353. m_context_menu->addSeparator();
  354. m_context_menu->addAction(close_tab_action);
  355. auto* close_multiple_tabs_menu = m_context_menu->addMenu("Close &Multiple Tabs");
  356. close_multiple_tabs_menu->addAction(close_tabs_to_left_action);
  357. close_multiple_tabs_menu->addAction(close_tabs_to_right_action);
  358. close_multiple_tabs_menu->addAction(close_other_tabs_action);
  359. auto* search_selected_text_action = new QAction("&Search for <query>", this);
  360. search_selected_text_action->setIcon(load_icon_from_uri("resource://icons/16x16/find.png"sv));
  361. QObject::connect(search_selected_text_action, &QAction::triggered, this, [this]() {
  362. auto url = MUST(String::formatted(Settings::the()->search_engine().query_url, URL::percent_encode(*m_page_context_menu_search_text)));
  363. m_window->new_tab_from_url(URL::URL(url), Web::HTML::ActivateTab::Yes);
  364. });
  365. auto take_screenshot = [this](auto type) {
  366. auto& view = this->view();
  367. view.take_screenshot(type)
  368. ->when_resolved([this](auto const& path) {
  369. auto message = MUST(String::formatted("Screenshot saved to: {}", path));
  370. QMessageBox dialog(this);
  371. dialog.setWindowTitle("Ladybird");
  372. dialog.setIcon(QMessageBox::Information);
  373. dialog.setText(qstring_from_ak_string(message));
  374. dialog.addButton(QMessageBox::Ok);
  375. dialog.addButton(QMessageBox::Open)->setText("Open folder");
  376. if (dialog.exec() == QMessageBox::Open) {
  377. auto path_url = QUrl::fromLocalFile(qstring_from_ak_string(path.dirname()));
  378. QDesktopServices::openUrl(path_url);
  379. }
  380. })
  381. .when_rejected([this](auto const& error) {
  382. if (error.is_errno() && error.code() == ECANCELED)
  383. return;
  384. auto error_message = MUST(String::formatted("{}", error));
  385. QMessageBox::warning(this, "Ladybird", qstring_from_ak_string(error_message));
  386. });
  387. };
  388. auto* take_visible_screenshot_action = new QAction("Take &Visible Screenshot", this);
  389. take_visible_screenshot_action->setIcon(load_icon_from_uri("resource://icons/16x16/filetype-image.png"sv));
  390. QObject::connect(take_visible_screenshot_action, &QAction::triggered, this, [take_screenshot]() {
  391. take_screenshot(WebView::ViewImplementation::ScreenshotType::Visible);
  392. });
  393. auto* take_full_screenshot_action = new QAction("Take &Full Screenshot", this);
  394. take_full_screenshot_action->setIcon(load_icon_from_uri("resource://icons/16x16/filetype-image.png"sv));
  395. QObject::connect(take_full_screenshot_action, &QAction::triggered, this, [take_screenshot]() {
  396. take_screenshot(WebView::ViewImplementation::ScreenshotType::Full);
  397. });
  398. m_page_context_menu = new QMenu("Context menu", this);
  399. m_page_context_menu->addAction(&m_window->go_back_action());
  400. m_page_context_menu->addAction(&m_window->go_forward_action());
  401. m_page_context_menu->addAction(&m_window->reload_action());
  402. m_page_context_menu->addSeparator();
  403. m_page_context_menu->addAction(&m_window->copy_selection_action());
  404. m_page_context_menu->addAction(&m_window->paste_action());
  405. m_page_context_menu->addAction(&m_window->select_all_action());
  406. m_page_context_menu->addSeparator();
  407. m_page_context_menu->addAction(search_selected_text_action);
  408. m_page_context_menu->addSeparator();
  409. m_page_context_menu->addAction(take_visible_screenshot_action);
  410. m_page_context_menu->addAction(take_full_screenshot_action);
  411. m_page_context_menu->addSeparator();
  412. m_page_context_menu->addAction(&m_window->view_source_action());
  413. m_page_context_menu->addAction(&m_window->inspect_dom_node_action());
  414. view().on_context_menu_request = [this, search_selected_text_action](Gfx::IntPoint content_position) {
  415. auto selected_text = Settings::the()->enable_search()
  416. ? view().selected_text_with_whitespace_collapsed()
  417. : OptionalNone {};
  418. TemporaryChange change_url { m_page_context_menu_search_text, std::move(selected_text) };
  419. if (m_page_context_menu_search_text.has_value()) {
  420. auto action_text = WebView::format_search_query_for_display(Settings::the()->search_engine().query_url, *m_page_context_menu_search_text);
  421. search_selected_text_action->setText(qstring_from_ak_string(action_text));
  422. search_selected_text_action->setVisible(true);
  423. } else {
  424. search_selected_text_action->setVisible(false);
  425. }
  426. m_page_context_menu->exec(view().map_point_to_global_position(content_position));
  427. };
  428. auto* open_link_in_new_tab_action = new QAction("Open Link in New &Tab", this);
  429. open_link_in_new_tab_action->setIcon(load_icon_from_uri("resource://icons/16x16/new-tab.png"sv));
  430. QObject::connect(open_link_in_new_tab_action, &QAction::triggered, this, [this]() {
  431. open_link_in_new_tab(m_link_context_menu_url);
  432. });
  433. m_link_context_menu_copy_url_action = new QAction("Copy &Link Address", this);
  434. m_link_context_menu_copy_url_action->setIcon(load_icon_from_uri("resource://icons/16x16/edit-copy.png"sv));
  435. QObject::connect(m_link_context_menu_copy_url_action, &QAction::triggered, this, [this]() {
  436. copy_link_url(m_link_context_menu_url);
  437. });
  438. m_link_context_menu = new QMenu("Link context menu", this);
  439. m_link_context_menu->addAction(open_link_in_new_tab_action);
  440. m_link_context_menu->addAction(m_link_context_menu_copy_url_action);
  441. m_link_context_menu->addSeparator();
  442. m_link_context_menu->addAction(&m_window->inspect_dom_node_action());
  443. view().on_link_context_menu_request = [this](auto const& url, Gfx::IntPoint content_position) {
  444. m_link_context_menu_url = url;
  445. switch (WebView::url_type(url)) {
  446. case WebView::URLType::Email:
  447. m_link_context_menu_copy_url_action->setText("Copy &Email Address");
  448. break;
  449. case WebView::URLType::Telephone:
  450. m_link_context_menu_copy_url_action->setText("Copy &Phone Number");
  451. break;
  452. case WebView::URLType::Other:
  453. m_link_context_menu_copy_url_action->setText("Copy &Link Address");
  454. break;
  455. }
  456. m_link_context_menu->exec(view().map_point_to_global_position(content_position));
  457. };
  458. auto* open_image_action = new QAction("&Open Image", this);
  459. open_image_action->setIcon(load_icon_from_uri("resource://icons/16x16/filetype-image.png"sv));
  460. QObject::connect(open_image_action, &QAction::triggered, this, [this]() {
  461. open_link(m_image_context_menu_url);
  462. });
  463. auto* open_image_in_new_tab_action = new QAction("&Open Image in New &Tab", this);
  464. open_image_in_new_tab_action->setIcon(load_icon_from_uri("resource://icons/16x16/new-tab.png"sv));
  465. QObject::connect(open_image_in_new_tab_action, &QAction::triggered, this, [this]() {
  466. open_link_in_new_tab(m_image_context_menu_url);
  467. });
  468. auto* copy_image_action = new QAction("&Copy Image", this);
  469. copy_image_action->setIcon(load_icon_from_uri("resource://icons/16x16/edit-copy.png"sv));
  470. QObject::connect(copy_image_action, &QAction::triggered, this, [this]() {
  471. auto* bitmap = m_image_context_menu_bitmap.bitmap();
  472. if (bitmap == nullptr)
  473. return;
  474. auto data = Gfx::BMPWriter::encode(*bitmap);
  475. if (data.is_error())
  476. return;
  477. auto image = QImage::fromData(data.value().data(), data.value().size(), "BMP");
  478. if (image.isNull())
  479. return;
  480. auto* clipboard = QGuiApplication::clipboard();
  481. clipboard->setImage(image);
  482. });
  483. auto* copy_image_url_action = new QAction("Copy Image &URL", this);
  484. copy_image_url_action->setIcon(load_icon_from_uri("resource://icons/16x16/edit-copy.png"sv));
  485. QObject::connect(copy_image_url_action, &QAction::triggered, this, [this]() {
  486. copy_link_url(m_image_context_menu_url);
  487. });
  488. m_image_context_menu = new QMenu("Image context menu", this);
  489. m_image_context_menu->addAction(open_image_action);
  490. m_image_context_menu->addAction(open_image_in_new_tab_action);
  491. m_image_context_menu->addSeparator();
  492. m_image_context_menu->addAction(copy_image_action);
  493. m_image_context_menu->addAction(copy_image_url_action);
  494. m_image_context_menu->addSeparator();
  495. m_image_context_menu->addAction(&m_window->inspect_dom_node_action());
  496. view().on_image_context_menu_request = [this](auto& image_url, Gfx::IntPoint content_position, Gfx::ShareableBitmap const& shareable_bitmap) {
  497. m_image_context_menu_url = image_url;
  498. m_image_context_menu_bitmap = shareable_bitmap;
  499. m_image_context_menu->exec(view().map_point_to_global_position(content_position));
  500. };
  501. m_media_context_menu_play_icon = load_icon_from_uri("resource://icons/16x16/play.png"sv);
  502. m_media_context_menu_pause_icon = load_icon_from_uri("resource://icons/16x16/pause.png"sv);
  503. m_media_context_menu_mute_icon = load_icon_from_uri("resource://icons/16x16/audio-volume-muted.png"sv);
  504. m_media_context_menu_unmute_icon = load_icon_from_uri("resource://icons/16x16/audio-volume-high.png"sv);
  505. m_media_context_menu_play_pause_action = new QAction("&Play", this);
  506. m_media_context_menu_play_pause_action->setIcon(m_media_context_menu_play_icon);
  507. QObject::connect(m_media_context_menu_play_pause_action, &QAction::triggered, this, [this]() {
  508. view().toggle_media_play_state();
  509. });
  510. m_media_context_menu_mute_unmute_action = new QAction("&Mute", this);
  511. m_media_context_menu_mute_unmute_action->setIcon(m_media_context_menu_mute_icon);
  512. QObject::connect(m_media_context_menu_mute_unmute_action, &QAction::triggered, this, [this]() {
  513. view().toggle_media_mute_state();
  514. });
  515. m_media_context_menu_controls_action = new QAction("Show &Controls", this);
  516. m_media_context_menu_controls_action->setCheckable(true);
  517. QObject::connect(m_media_context_menu_controls_action, &QAction::triggered, this, [this]() {
  518. view().toggle_media_controls_state();
  519. });
  520. m_media_context_menu_loop_action = new QAction("&Loop", this);
  521. m_media_context_menu_loop_action->setCheckable(true);
  522. QObject::connect(m_media_context_menu_loop_action, &QAction::triggered, this, [this]() {
  523. view().toggle_media_loop_state();
  524. });
  525. auto* open_audio_action = new QAction("&Open Audio", this);
  526. open_audio_action->setIcon(load_icon_from_uri("resource://icons/16x16/filetype-sound.png"sv));
  527. QObject::connect(open_audio_action, &QAction::triggered, this, [this]() {
  528. open_link(m_media_context_menu_url);
  529. });
  530. auto* open_audio_in_new_tab_action = new QAction("Open Audio in New &Tab", this);
  531. open_audio_in_new_tab_action->setIcon(load_icon_from_uri("resource://icons/16x16/new-tab.png"sv));
  532. QObject::connect(open_audio_in_new_tab_action, &QAction::triggered, this, [this]() {
  533. open_link_in_new_tab(m_media_context_menu_url);
  534. });
  535. auto* copy_audio_url_action = new QAction("Copy Audio &URL", this);
  536. copy_audio_url_action->setIcon(load_icon_from_uri("resource://icons/16x16/edit-copy.png"sv));
  537. QObject::connect(copy_audio_url_action, &QAction::triggered, this, [this]() {
  538. copy_link_url(m_media_context_menu_url);
  539. });
  540. m_audio_context_menu = new QMenu("Audio context menu", this);
  541. m_audio_context_menu->addAction(m_media_context_menu_play_pause_action);
  542. m_audio_context_menu->addAction(m_media_context_menu_mute_unmute_action);
  543. m_audio_context_menu->addAction(m_media_context_menu_controls_action);
  544. m_audio_context_menu->addAction(m_media_context_menu_loop_action);
  545. m_audio_context_menu->addSeparator();
  546. m_audio_context_menu->addAction(open_audio_action);
  547. m_audio_context_menu->addAction(open_audio_in_new_tab_action);
  548. m_audio_context_menu->addSeparator();
  549. m_audio_context_menu->addAction(copy_audio_url_action);
  550. m_audio_context_menu->addSeparator();
  551. m_audio_context_menu->addAction(&m_window->inspect_dom_node_action());
  552. auto* open_video_action = new QAction("&Open Video", this);
  553. open_video_action->setIcon(load_icon_from_uri("resource://icons/16x16/filetype-video.png"sv));
  554. QObject::connect(open_video_action, &QAction::triggered, this, [this]() {
  555. open_link(m_media_context_menu_url);
  556. });
  557. auto* open_video_in_new_tab_action = new QAction("Open Video in New &Tab", this);
  558. open_video_in_new_tab_action->setIcon(load_icon_from_uri("resource://icons/16x16/new-tab.png"sv));
  559. QObject::connect(open_video_in_new_tab_action, &QAction::triggered, this, [this]() {
  560. open_link_in_new_tab(m_media_context_menu_url);
  561. });
  562. auto* copy_video_url_action = new QAction("Copy Video &URL", this);
  563. copy_video_url_action->setIcon(load_icon_from_uri("resource://icons/16x16/edit-copy.png"sv));
  564. QObject::connect(copy_video_url_action, &QAction::triggered, this, [this]() {
  565. copy_link_url(m_media_context_menu_url);
  566. });
  567. m_video_context_menu = new QMenu("Video context menu", this);
  568. m_video_context_menu->addAction(m_media_context_menu_play_pause_action);
  569. m_video_context_menu->addAction(m_media_context_menu_mute_unmute_action);
  570. m_video_context_menu->addAction(m_media_context_menu_controls_action);
  571. m_video_context_menu->addAction(m_media_context_menu_loop_action);
  572. m_video_context_menu->addSeparator();
  573. m_video_context_menu->addAction(open_video_action);
  574. m_video_context_menu->addAction(open_video_in_new_tab_action);
  575. m_video_context_menu->addSeparator();
  576. m_video_context_menu->addAction(copy_video_url_action);
  577. m_video_context_menu->addSeparator();
  578. m_video_context_menu->addAction(&m_window->inspect_dom_node_action());
  579. view().on_media_context_menu_request = [this](Gfx::IntPoint content_position, Web::Page::MediaContextMenu const& menu) {
  580. m_media_context_menu_url = menu.media_url;
  581. if (menu.is_playing) {
  582. m_media_context_menu_play_pause_action->setIcon(m_media_context_menu_pause_icon);
  583. m_media_context_menu_play_pause_action->setText("&Pause");
  584. } else {
  585. m_media_context_menu_play_pause_action->setIcon(m_media_context_menu_play_icon);
  586. m_media_context_menu_play_pause_action->setText("&Play");
  587. }
  588. if (menu.is_muted) {
  589. m_media_context_menu_mute_unmute_action->setIcon(m_media_context_menu_unmute_icon);
  590. m_media_context_menu_mute_unmute_action->setText("Un&mute");
  591. } else {
  592. m_media_context_menu_mute_unmute_action->setIcon(m_media_context_menu_mute_icon);
  593. m_media_context_menu_mute_unmute_action->setText("&Mute");
  594. }
  595. m_media_context_menu_controls_action->setChecked(menu.has_user_agent_controls);
  596. m_media_context_menu_loop_action->setChecked(menu.is_looping);
  597. auto screen_position = view().map_point_to_global_position(content_position);
  598. if (menu.is_video)
  599. m_video_context_menu->exec(screen_position);
  600. else
  601. m_audio_context_menu->exec(screen_position);
  602. };
  603. }
  604. Tab::~Tab()
  605. {
  606. close_sub_widgets();
  607. // Delete the InspectorWidget explicitly to ensure it is deleted before the WebContentView. Otherwise, Qt
  608. // can destroy these objects in any order, which may cause use-after-free in InspectorWidget's destructor.
  609. delete m_inspector_widget;
  610. }
  611. void Tab::update_reset_zoom_button()
  612. {
  613. auto zoom_level = view().zoom_level();
  614. if (zoom_level != 1.0f) {
  615. auto zoom_level_text = MUST(String::formatted("{}%", round_to<int>(zoom_level * 100)));
  616. m_reset_zoom_button->setText(qstring_from_ak_string(zoom_level_text));
  617. m_reset_zoom_button_action->setVisible(true);
  618. } else {
  619. m_reset_zoom_button_action->setVisible(false);
  620. }
  621. }
  622. void Tab::focus_location_editor()
  623. {
  624. m_location_edit->setFocus();
  625. m_location_edit->selectAll();
  626. }
  627. void Tab::navigate(URL::URL const& url)
  628. {
  629. view().load(url);
  630. }
  631. void Tab::load_html(StringView html)
  632. {
  633. view().load_html(html);
  634. }
  635. void Tab::back()
  636. {
  637. view().traverse_the_history_by_delta(-1);
  638. }
  639. void Tab::forward()
  640. {
  641. view().traverse_the_history_by_delta(1);
  642. }
  643. void Tab::reload()
  644. {
  645. view().reload();
  646. }
  647. void Tab::open_link(URL::URL const& url)
  648. {
  649. view().on_link_click(url, "", 0);
  650. }
  651. void Tab::open_link_in_new_tab(URL::URL const& url)
  652. {
  653. view().on_link_click(url, "_blank", Web::UIEvents::Mod_Ctrl);
  654. }
  655. void Tab::copy_link_url(URL::URL const& url)
  656. {
  657. auto* clipboard = QGuiApplication::clipboard();
  658. clipboard->setText(qstring_from_ak_string(WebView::url_text_to_copy(url)));
  659. }
  660. void Tab::location_edit_return_pressed()
  661. {
  662. if (m_location_edit->text().isEmpty())
  663. return;
  664. navigate(m_location_edit->url());
  665. }
  666. void Tab::open_file()
  667. {
  668. auto filename = QFileDialog::getOpenFileUrl(this, "Open file", QDir::homePath(), "All Files (*.*)");
  669. if (filename.isValid()) {
  670. navigate(ak_url_from_qurl(filename));
  671. }
  672. }
  673. int Tab::tab_index()
  674. {
  675. return m_window->tab_index(this);
  676. }
  677. void Tab::debug_request(ByteString const& request, ByteString const& argument)
  678. {
  679. m_view->debug_request(request, argument);
  680. }
  681. void Tab::resizeEvent(QResizeEvent* event)
  682. {
  683. QWidget::resizeEvent(event);
  684. if (m_hover_label->isVisible())
  685. update_hover_label();
  686. }
  687. void Tab::update_hover_label()
  688. {
  689. m_hover_label->resize(QFontMetrics(m_hover_label->font()).boundingRect(m_hover_label->text()).adjusted(-4, -2, 4, 2).size());
  690. auto hover_label_height = height() - m_hover_label->height() - 8;
  691. if (m_find_in_page->isVisible())
  692. hover_label_height -= m_find_in_page->height() - 4;
  693. m_hover_label->move(6, hover_label_height);
  694. m_hover_label->raise();
  695. }
  696. void Tab::update_navigation_buttons_state()
  697. {
  698. if (m_window->current_tab() != this)
  699. return;
  700. m_window->go_back_action().setEnabled(m_can_navigate_back);
  701. m_window->go_forward_action().setEnabled(m_can_navigate_forward);
  702. }
  703. bool Tab::event(QEvent* event)
  704. {
  705. if (event->type() == QEvent::PaletteChange) {
  706. recreate_toolbar_icons();
  707. return QWidget::event(event);
  708. }
  709. return QWidget::event(event);
  710. }
  711. void Tab::recreate_toolbar_icons()
  712. {
  713. m_window->go_back_action().setIcon(create_tvg_icon_with_theme_colors("back", palette()));
  714. m_window->go_forward_action().setIcon(create_tvg_icon_with_theme_colors("forward", palette()));
  715. m_window->reload_action().setIcon(create_tvg_icon_with_theme_colors("reload", palette()));
  716. m_window->new_tab_action().setIcon(create_tvg_icon_with_theme_colors("new_tab", palette()));
  717. m_hamburger_button->setIcon(create_tvg_icon_with_theme_colors("hamburger", palette()));
  718. }
  719. void Tab::show_inspector_window(InspectorTarget inspector_target)
  720. {
  721. if (!m_inspector_widget)
  722. m_inspector_widget = new InspectorWidget(this, view());
  723. else
  724. m_inspector_widget->inspect();
  725. m_inspector_widget->show();
  726. m_inspector_widget->activateWindow();
  727. m_inspector_widget->raise();
  728. if (inspector_target == InspectorTarget::HoveredElement)
  729. m_inspector_widget->select_hovered_node();
  730. else
  731. m_inspector_widget->select_default_node();
  732. }
  733. void Tab::show_find_in_page()
  734. {
  735. m_find_in_page->setVisible(true);
  736. m_find_in_page->setFocus();
  737. }
  738. void Tab::find_previous()
  739. {
  740. m_find_in_page->find_previous();
  741. }
  742. void Tab::find_next()
  743. {
  744. m_find_in_page->find_next();
  745. }
  746. void Tab::close_sub_widgets()
  747. {
  748. auto close_widget_window = [](auto* widget) {
  749. if (widget)
  750. widget->close();
  751. };
  752. close_widget_window(m_inspector_widget);
  753. }
  754. void Tab::set_block_popups(bool enabled)
  755. {
  756. debug_request("block-pop-ups", enabled ? "on" : "off");
  757. }
  758. void Tab::set_line_box_borders(bool enabled)
  759. {
  760. debug_request("set-line-box-borders", enabled ? "on" : "off");
  761. }
  762. void Tab::set_same_origin_policy(bool enabled)
  763. {
  764. debug_request("same-origin-policy", enabled ? "on" : "off");
  765. }
  766. void Tab::set_scripting(bool enabled)
  767. {
  768. debug_request("scripting", enabled ? "on" : "off");
  769. }
  770. void Tab::set_user_agent_string(ByteString const& user_agent)
  771. {
  772. debug_request("spoof-user-agent", user_agent);
  773. // Clear the cache to ensure requests are re-done with the new user agent.
  774. debug_request("clear-cache");
  775. }
  776. void Tab::set_navigator_compatibility_mode(ByteString const& compatibility_mode)
  777. {
  778. debug_request("navigator-compatibility-mode", compatibility_mode);
  779. }
  780. void Tab::set_preferred_languages(Vector<String> const& preferred_languages)
  781. {
  782. m_view->set_preferred_languages(preferred_languages);
  783. }
  784. void Tab::set_enable_do_not_track(bool enable)
  785. {
  786. m_view->set_enable_do_not_track(enable);
  787. }
  788. void Tab::set_enable_autoplay(bool enable)
  789. {
  790. m_view->set_enable_autoplay(enable);
  791. }
  792. }