Tab.cpp 37 KB

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