Ladybird/Qt: Show an informational dialog after taking a screenshot

The dialog displays where the screenshot is saved and presents an option
to open its containing folder.
This commit is contained in:
Timothy Flynn 2024-01-06 10:00:03 -05:00 committed by Sam Atkins
parent 3a06d5a9a1
commit 8801fed54e
Notes: sideshowbarker 2024-07-17 02:38:39 +09:00

View file

@ -21,6 +21,7 @@
#include <QColorDialog> #include <QColorDialog>
#include <QCoreApplication> #include <QCoreApplication>
#include <QCursor> #include <QCursor>
#include <QDesktopServices>
#include <QFileDialog> #include <QFileDialog>
#include <QFont> #include <QFont>
#include <QFontMetrics> #include <QFontMetrics>
@ -32,6 +33,7 @@
#include <QMimeData> #include <QMimeData>
#include <QPainter> #include <QPainter>
#include <QPoint> #include <QPoint>
#include <QPushButton>
#include <QResizeEvent> #include <QResizeEvent>
namespace Ladybird { namespace Ladybird {
@ -310,22 +312,41 @@ Tab::Tab(BrowserWindow* window, WebContentOptions const& web_content_options, St
m_window->new_tab(qstring_from_ak_string(url), Web::HTML::ActivateTab::Yes); m_window->new_tab(qstring_from_ak_string(url), Web::HTML::ActivateTab::Yes);
}); });
auto take_screenshot = [this](auto type) {
auto& view = this->view();
view.take_screenshot(type)
->when_resolved([this](auto const& path) {
auto message = MUST(String::formatted("Screenshot saved to: {}", path));
QMessageBox dialog(this);
dialog.setWindowTitle("Ladybird");
dialog.setIcon(QMessageBox::Information);
dialog.setText(qstring_from_ak_string(message));
dialog.addButton(QMessageBox::Ok);
dialog.addButton(QMessageBox::Open)->setText("Open folder");
if (dialog.exec() == QMessageBox::Open) {
auto path_url = QUrl::fromLocalFile(qstring_from_ak_string(path.dirname()));
QDesktopServices::openUrl(path_url);
}
})
.when_rejected([this](auto const& error) {
auto error_message = MUST(String::formatted("{}", error));
QMessageBox::warning(this, "Ladybird", qstring_from_ak_string(error_message));
});
};
auto* take_visible_screenshot_action = new QAction("Take &Visible Screenshot", this); auto* take_visible_screenshot_action = new QAction("Take &Visible Screenshot", this);
take_visible_screenshot_action->setIcon(load_icon_from_uri("resource://icons/16x16/filetype-image.png"sv)); take_visible_screenshot_action->setIcon(load_icon_from_uri("resource://icons/16x16/filetype-image.png"sv));
QObject::connect(take_visible_screenshot_action, &QAction::triggered, this, [this]() { QObject::connect(take_visible_screenshot_action, &QAction::triggered, this, [take_screenshot]() {
view().take_screenshot(WebView::ViewImplementation::ScreenshotType::Visible)->when_rejected([this](auto const& error) { take_screenshot(WebView::ViewImplementation::ScreenshotType::Visible);
auto error_message = MUST(String::formatted("{}", error));
QMessageBox::warning(this, "Ladybird", qstring_from_ak_string(error_message));
});
}); });
auto* take_full_screenshot_action = new QAction("Take &Full Screenshot", this); auto* take_full_screenshot_action = new QAction("Take &Full Screenshot", this);
take_full_screenshot_action->setIcon(load_icon_from_uri("resource://icons/16x16/filetype-image.png"sv)); take_full_screenshot_action->setIcon(load_icon_from_uri("resource://icons/16x16/filetype-image.png"sv));
QObject::connect(take_full_screenshot_action, &QAction::triggered, this, [this]() { QObject::connect(take_full_screenshot_action, &QAction::triggered, this, [take_screenshot]() {
view().take_screenshot(WebView::ViewImplementation::ScreenshotType::Full)->when_rejected([this](auto const& error) { take_screenshot(WebView::ViewImplementation::ScreenshotType::Full);
auto error_message = MUST(String::formatted("{}", error));
QMessageBox::warning(this, "Ladybird", qstring_from_ak_string(error_message));
});
}); });
m_page_context_menu = new QMenu("Context menu", this); m_page_context_menu = new QMenu("Context menu", this);