Truly independent web browser
UI/Qt: Add a viewer for screenshots in their actual size
Jelle Raaijmakers173be0494295
diff --git a/Tests/UI/Qt/CMakeLists.txt b/Tests/UI/Qt/CMakeLists.txtindex cb02511088f..f39fcfa32e3 100644--- a/Tests/UI/Qt/CMakeLists.txt+++ b/Tests/UI/Qt/CMakeLists.txt@@ -87,6 +87,17 @@ target_sources(TestScreenshotSelectionOverlay PRIVATE)deploy_qt_offscreen_platform_plugin(TestScreenshotSelectionOverlay)+ladybird_test("TestScreenshotViewer.cpp" UI/Qt+ CUSTOM_MAIN "$<TARGET_OBJECTS:QtTestMain>"+ LIBS LibURL Qt::Core Qt::Gui Qt::Widgets+)+target_sources(TestScreenshotViewer PRIVATE+ "${LADYBIRD_SOURCE_DIR}/UI/Qt/ChromeStyle.cpp"+ "${LADYBIRD_SOURCE_DIR}/UI/Qt/ScreenshotViewer.cpp"+ "${LADYBIRD_SOURCE_DIR}/UI/Qt/StringUtils.cpp"+)+deploy_qt_offscreen_platform_plugin(TestScreenshotViewer)+ladybird_test("TestHiddenPageSizing.cpp" UI/QtCUSTOM_MAIN "$<TARGET_OBJECTS:QtTestMain>"LIBS Qt::Core Qt::Gui Qt::Widgetsdiff --git a/Tests/UI/Qt/TestScreenshotViewer.cpp b/Tests/UI/Qt/TestScreenshotViewer.cppnew file mode 100644index 00000000000..98a90bca72e--- /dev/null+++ b/Tests/UI/Qt/TestScreenshotViewer.cpp@@ -0,0 +1,101 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++#include <LibTest/TestCase.h>+#include <UI/Qt/ScreenshotViewer.h>++#include <QApplication>+#include <QKeyEvent>+#include <QMouseEvent>++using Ladybird::ScreenshotViewer;++static QImage create_screenshot(QSize size)+{+ QImage image(size, QImage::Format_RGB32);+ image.fill(Qt::red);+ return image;+}++static void send_mouse(QWidget& widget, QEvent::Type type, QPointF position)+{+ auto buttons = type == QEvent::MouseButtonRelease ? Qt::NoButton : Qt::LeftButton;+ auto button = type == QEvent::MouseMove ? Qt::NoButton : Qt::LeftButton;+ QMouseEvent event(type, position, widget.mapToGlobal(position), button, buttons, Qt::NoModifier);+ QApplication::sendEvent(&widget, &event);+}++static void click(QWidget& widget, QPointF position)+{+ send_mouse(widget, QEvent::MouseButtonPress, position);+ send_mouse(widget, QEvent::MouseButtonRelease, position);+}++TEST_CASE(a_screenshot_that_fits_is_shown_at_its_actual_size)+{+ QWidget tab;+ tab.resize(800, 600);+ ScreenshotViewer viewer { create_screenshot({ 200, 100 }), &tab };+ tab.show();++ EXPECT(viewer.fits_at_actual_size());+ EXPECT_EQ(viewer.image_rect().size(), QSizeF(200, 100));++ click(viewer, viewer.image_rect().center());+ EXPECT(!viewer.is_at_actual_size());+ EXPECT_EQ(viewer.image_rect().size(), QSizeF(200, 100));+}++TEST_CASE(a_larger_screenshot_is_fitted_and_shown_at_its_actual_size_on_a_click)+{+ QWidget tab;+ tab.resize(800, 600);+ ScreenshotViewer viewer { create_screenshot({ 2000, 1200 }), &tab };+ tab.show();++ EXPECT(!viewer.fits_at_actual_size());+ auto fitted = viewer.image_rect();+ EXPECT(fitted.width() < 800 && fitted.height() < 600);++ auto point = fitted.topLeft() + QPointF(fitted.width() / 4, fitted.height() / 4);+ click(viewer, point);+ EXPECT(viewer.is_at_actual_size());+ auto actual = viewer.image_rect();+ EXPECT_EQ(actual.size(), QSizeF(2000, 1200));+ EXPECT(actual.contains(point));+ EXPECT(actual.left() <= 0 && actual.right() >= 800);++ send_mouse(viewer, QEvent::MouseButtonPress, QPointF(400, 300));+ send_mouse(viewer, QEvent::MouseMove, QPointF(350, 250));+ send_mouse(viewer, QEvent::MouseButtonRelease, QPointF(350, 250));+ EXPECT(viewer.is_at_actual_size());+ EXPECT_EQ(viewer.image_rect().topLeft(), actual.topLeft() - QPointF(50, 50));++ click(viewer, QPointF(400, 300));+ EXPECT(!viewer.is_at_actual_size());+ EXPECT_EQ(viewer.image_rect(), fitted);+}++TEST_CASE(escape_or_a_click_beside_the_screenshot_closes_the_viewer)+{+ QWidget tab;+ tab.resize(800, 600);+ auto closes = 0;++ ScreenshotViewer viewer { create_screenshot({ 200, 100 }), &tab };+ viewer.on_closed = [&] { ++closes; };+ tab.show();+ QKeyEvent escape(QEvent::KeyPress, Qt::Key_Escape, Qt::NoModifier);+ QApplication::sendEvent(&viewer, &escape);+ EXPECT_EQ(closes, 1);+ EXPECT(!viewer.isVisible());++ ScreenshotViewer second_viewer { create_screenshot({ 200, 100 }), &tab };+ second_viewer.on_closed = [&] { ++closes; };+ second_viewer.show();+ click(second_viewer, QPointF(10, 300));+ EXPECT_EQ(closes, 2);+}diff --git a/UI/Qt/CMakeLists.txt b/UI/Qt/CMakeLists.txtindex b92104b64d5..dde74eef6ab 100644--- a/UI/Qt/CMakeLists.txt+++ b/UI/Qt/CMakeLists.txt@@ -51,6 +51,7 @@ target_sources(ladybird PRIVATEPopover.cppReportReview.cppScreenshotSelectionOverlay.cpp+ ScreenshotViewer.cppSelectDropdown.cppStringUtils.cppTab.cppdiff --git a/UI/Qt/ChromeStyle.cpp b/UI/Qt/ChromeStyle.cppindex 96bfb702f13..6b7975058df 100644--- a/UI/Qt/ChromeStyle.cpp+++ b/UI/Qt/ChromeStyle.cpp@@ -1541,6 +1541,36 @@ QFrame#ScreenshotSelectionInstructions QPushButton:focus {{text, surface, border, control_border, hover_surface, pressed_surface, accent);}+// The viewer darkens the whole tab whatever the theme, so its controls are light on dark like a photo viewer's.+QString screenshot_viewer_style_sheet()+{+ return QStringLiteral(R"(+QLabel#ScreenshotViewerCaption {+ color: #ffffff;+ background: rgba(0, 0, 0, 180);+ border: 1px solid rgba(255, 255, 255, 40);+ border-radius: 12px;+ padding: 4px 12px;+}++QPushButton#ScreenshotViewerCloseButton {+ color: #ffffff;+ background: rgba(0, 0, 0, 180);+ border: 1px solid rgba(255, 255, 255, 64);+ border-radius: 6px;+ padding: 5px 12px;+}++QPushButton#ScreenshotViewerCloseButton:hover {+ background: rgba(48, 48, 48, 200);+}++QPushButton#ScreenshotViewerCloseButton:pressed {+ background: rgba(0, 0, 0, 220);+}+)");+}+QString content_blocking_popover_style_sheet(QPalette const& palette){auto text = style_sheet_color(chrome_text(palette));diff --git a/UI/Qt/ChromeStyle.h b/UI/Qt/ChromeStyle.hindex 8133b9e9142..6dd03e4b0da 100644--- a/UI/Qt/ChromeStyle.h+++ b/UI/Qt/ChromeStyle.h@@ -55,6 +55,7 @@ QString downloads_popover_style_sheet(QPalette const&);QString message_popover_style_sheet(QPalette const&);QString content_blocking_popover_style_sheet(QPalette const&);QString screenshot_selection_style_sheet(QPalette const&);+QString screenshot_viewer_style_sheet();enum class CircularControlFrameStyle {InteractionOnly,diff --git a/UI/Qt/ScreenshotViewer.cpp b/UI/Qt/ScreenshotViewer.cppnew file mode 100644index 00000000000..dd9c0d44387--- /dev/null+++ b/UI/Qt/ScreenshotViewer.cpp@@ -0,0 +1,288 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++#include <AK/StdLibExtras.h>+#include <UI/Qt/ChromeStyle.h>+#include <UI/Qt/ScreenshotViewer.h>++#include <QKeyEvent>+#include <QLabel>+#include <QMouseEvent>+#include <QPainter>+#include <QPainterPath>+#include <QPushButton>+#include <QRegion>+#include <QWheelEvent>++namespace Ladybird {++static constexpr int SIDE_MARGIN = 32;+static constexpr int CHROME_MARGIN = 64;+static constexpr int CHROME_INSET = 16;+static constexpr qreal CORNER_RADIUS = 6;+static constexpr int SHADOW_SIZE = 16;+static constexpr int BACKDROP_ALPHA = 200;+static constexpr qreal DRAG_THRESHOLD = 4;++// Shows a screenshot over the whole tab: at its actual size, one pixel of the screenshot to one pixel of the screen,+// when the tab has room for it, and otherwise fitted to the tab, with a click to look at it in its actual size.+ScreenshotViewer::ScreenshotViewer(QImage screenshot, QWidget* parent)+ : QWidget(parent)+ , m_screenshot(AK::move(screenshot))+{+ setObjectName("LadybirdScreenshotViewer");+ setFocusPolicy(Qt::StrongFocus);+ setMouseTracking(true);+ setStyleSheet(ChromeStyle::screenshot_viewer_style_sheet());++ m_close_button = new QPushButton(tr("Close"), this);+ m_close_button->setObjectName("ScreenshotViewerCloseButton");+ m_close_button->setFocusPolicy(Qt::NoFocus);+ m_close_button->setCursor(Qt::ArrowCursor);+ QObject::connect(m_close_button, &QPushButton::clicked, this, [this] { close_viewer(); });++ m_caption = new QLabel(this);+ m_caption->setObjectName("ScreenshotViewerCaption");+ m_caption->setAlignment(Qt::AlignCenter);+ m_caption->setAttribute(Qt::WA_TransparentForMouseEvents);++ if (parent)+ setGeometry(parent->rect());+ update_caption();+}++// The screenshot is in device pixels, so its actual size on this screen is that many device pixels.+QSizeF ScreenshotViewer::actual_size() const+{+ return QSizeF(m_screenshot.size()) / devicePixelRatioF();+}++QRectF ScreenshotViewer::available_rect() const+{+ return QRectF(rect()).adjusted(SIDE_MARGIN, CHROME_MARGIN, -SIDE_MARGIN, -CHROME_MARGIN);+}++QRectF ScreenshotViewer::centered_rect(QSizeF size) const+{+ auto center = available_rect().center();+ return QRectF(center.x() - size.width() / 2, center.y() - size.height() / 2, size.width(), size.height());+}++bool ScreenshotViewer::fits_at_actual_size() const+{+ auto available = available_rect().size();+ auto size = actual_size();+ return size.width() <= available.width() && size.height() <= available.height();+}++QRectF ScreenshotViewer::image_rect() const+{+ if (m_screenshot.isNull())+ return {};+ if (m_is_at_actual_size || fits_at_actual_size())+ return centered_rect(actual_size()).translated(m_offset);+ return centered_rect(actual_size().scaled(available_rect().size(), Qt::KeepAspectRatio));+}++// Looking at the screenshot in its actual size keeps the point under the cursor where it is, so a click zooms into+// what the user clicked on.+void ScreenshotViewer::set_actual_size(bool actual_size, Optional<QPointF> anchor)+{+ if (actual_size && fits_at_actual_size())+ actual_size = false;+ if (actual_size == m_is_at_actual_size)+ return;++ auto fitted = image_rect();+ m_is_at_actual_size = actual_size;+ m_offset = {};++ if (actual_size && anchor.has_value() && !fitted.isEmpty()) {+ QPointF relative { (anchor->x() - fitted.left()) / fitted.width(), (anchor->y() - fitted.top()) / fitted.height() };+ auto size = this->actual_size();+ auto centered = centered_rect(size);+ QPointF top_left { anchor->x() - relative.x() * size.width(), anchor->y() - relative.y() * size.height() };+ move_image_by(top_left - centered.topLeft());+ }++ update_caption();+ update_cursor(anchor.value_or(QPointF {}));+ update();+}++// The image always covers the tab along a direction in which it is larger than the tab, and stays centered along one+// in which it is not.+void ScreenshotViewer::move_image_by(QPointF delta)+{+ m_offset += delta;+ auto centered = centered_rect(actual_size());+ auto clamp_axis = [](qreal offset, qreal start, qreal length, qreal extent) {+ if (length <= extent)+ return 0.;+ auto minimum = extent - length - start;+ auto maximum = -start;+ return clamp(offset, minimum, maximum);+ };+ m_offset.setX(clamp_axis(m_offset.x(), centered.left(), centered.width(), width()));+ m_offset.setY(clamp_axis(m_offset.y(), centered.top(), centered.height(), height()));+}++void ScreenshotViewer::close_viewer()+{+ hide();+ if (on_closed)+ on_closed();+}++QPixmap const& ScreenshotViewer::pixmap_for(QSizeF size)+{+ auto device_size = (size * devicePixelRatioF()).toSize();+ if (m_pixmap.isNull() || m_pixmap.size() != device_size) {+ auto image = device_size == m_screenshot.size() ? m_screenshot : m_screenshot.scaled(device_size, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);+ m_pixmap = QPixmap::fromImage(image);+ m_pixmap.setDevicePixelRatio(devicePixelRatioF());+ }+ return m_pixmap;+}++void ScreenshotViewer::paintEvent(QPaintEvent*)+{+ QPainter painter(this);+ painter.fillRect(rect(), QColor(0, 0, 0, BACKDROP_ALPHA));++ auto target = image_rect();+ if (target.isEmpty())+ return;++ // A soft shadow lifts the screenshot off the page it was taken from. It is only painted around the screenshot, which+ // covers the rest of it.+ painter.setRenderHint(QPainter::Antialiasing);+ painter.setPen(Qt::NoPen);+ painter.setBrush(QColor(0, 0, 0, 6));+ if (auto around = QRegion(rect()).subtracted(target.toAlignedRect()); !around.isEmpty()) {+ painter.save();+ painter.setClipRegion(around);+ for (int step = SHADOW_SIZE; step > 0; --step)+ painter.drawRoundedRect(target.adjusted(-step, -step + 4, step, step + 4), CORNER_RADIUS + step, CORNER_RADIUS + step);+ painter.restore();+ }++ QPainterPath corners;+ corners.addRoundedRect(target, CORNER_RADIUS, CORNER_RADIUS);+ painter.save();+ painter.setClipPath(corners);+ painter.drawPixmap(target.topLeft(), pixmap_for(target.size()));+ painter.restore();++ painter.setBrush(Qt::NoBrush);+ painter.setPen(QPen(QColor(255, 255, 255, 48), 1));+ painter.drawRoundedRect(target.adjusted(0.5, 0.5, -0.5, -0.5), CORNER_RADIUS, CORNER_RADIUS);+}++void ScreenshotViewer::mousePressEvent(QMouseEvent* event)+{+ if (event->button() != Qt::LeftButton)+ return;+ m_press_position = event->position();+ m_press_offset = m_offset;+ m_is_dragging = false;+ update_cursor(event->position());+}++void ScreenshotViewer::mouseMoveEvent(QMouseEvent* event)+{+ if (m_press_position.has_value() && m_is_at_actual_size) {+ auto delta = event->position() - *m_press_position;+ if (delta.manhattanLength() > DRAG_THRESHOLD)+ m_is_dragging = true;+ if (m_is_dragging) {+ m_offset = m_press_offset;+ move_image_by(delta);+ update();+ }+ }+ update_cursor(event->position());+}++// A click on the screenshot switches between seeing all of it and seeing it in its actual size, and a click beside it+// closes the viewer.+void ScreenshotViewer::mouseReleaseEvent(QMouseEvent* event)+{+ if (event->button() != Qt::LeftButton || !m_press_position.has_value())+ return;+ m_press_position.clear();+ if (exchange(m_is_dragging, false)) {+ update_cursor(event->position());+ return;+ }++ if (!image_rect().contains(event->position())) {+ close_viewer();+ return;+ }+ set_actual_size(!m_is_at_actual_size, event->position());+}++void ScreenshotViewer::wheelEvent(QWheelEvent* event)+{+ if (!m_is_at_actual_size)+ return;+ auto delta = event->pixelDelta().isNull() ? QPointF(event->angleDelta()) / 2 : QPointF(event->pixelDelta());+ move_image_by(delta);+ update();+}++void ScreenshotViewer::keyPressEvent(QKeyEvent* event)+{+ switch (event->key()) {+ case Qt::Key_Escape:+ close_viewer();+ return;+ case Qt::Key_Space:+ set_actual_size(!m_is_at_actual_size);+ return;+ default:+ QWidget::keyPressEvent(event);+ }+}++void ScreenshotViewer::resizeEvent(QResizeEvent*)+{+ if (m_is_at_actual_size && fits_at_actual_size())+ m_is_at_actual_size = false;+ move_image_by({});++ auto close_size = m_close_button->sizeHint();+ m_close_button->setGeometry(width() - CHROME_INSET - close_size.width(), CHROME_INSET, close_size.width(), close_size.height());+ update_caption();+}++void ScreenshotViewer::update_cursor(QPointF position)+{+ auto cursor = Qt::ArrowCursor;+ if (image_rect().contains(position)) {+ if (m_is_at_actual_size)+ cursor = m_is_dragging ? Qt::ClosedHandCursor : Qt::OpenHandCursor;+ else if (!fits_at_actual_size())+ cursor = Qt::PointingHandCursor;+ }+ setCursor(cursor);+}++void ScreenshotViewer::update_caption()+{+ if (m_is_at_actual_size)+ m_caption->setText(tr("Actual size. Drag to look around, click to see all of it."));+ else if (!fits_at_actual_size())+ m_caption->setText(tr("Click to see it in actual size."));+ else+ m_caption->setText(tr("Actual size"));++ auto caption_size = m_caption->sizeHint().boundedTo(QSize(width() - CHROME_INSET * 2, height()));+ m_caption->setGeometry((width() - caption_size.width()) / 2, height() - CHROME_INSET - caption_size.height(), caption_size.width(), caption_size.height());+}++}diff --git a/UI/Qt/ScreenshotViewer.h b/UI/Qt/ScreenshotViewer.hnew file mode 100644index 00000000000..e870473fb52--- /dev/null+++ b/UI/Qt/ScreenshotViewer.h@@ -0,0 +1,71 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++#pragma once++#include <AK/Function.h>+#include <AK/Optional.h>+#include <AK/kmalloc.h>++#include <QImage>+#include <QPixmap>+#include <QPointF>+#include <QRectF>+#include <QSizeF>+#include <QWidget>++class QLabel;+class QPushButton;++namespace Ladybird {++class ScreenshotViewer final : public QWidget {+public:+ AK_ALLOC_WITH_KMALLOC;++ ScreenshotViewer(QImage screenshot, QWidget* parent);++ bool fits_at_actual_size() const;+ bool is_at_actual_size() const { return m_is_at_actual_size; }+ void set_actual_size(bool, Optional<QPointF> anchor = {});+ QRectF image_rect() const;++ void close_viewer();++ Function<void()> on_closed;++protected:+ virtual void paintEvent(QPaintEvent*) override;+ virtual void mousePressEvent(QMouseEvent*) override;+ virtual void mouseMoveEvent(QMouseEvent*) override;+ virtual void mouseReleaseEvent(QMouseEvent*) override;+ virtual void wheelEvent(QWheelEvent*) override;+ virtual void keyPressEvent(QKeyEvent*) override;+ virtual void resizeEvent(QResizeEvent*) override;++private:+ QSizeF actual_size() const;+ QRectF available_rect() const;+ QRectF centered_rect(QSizeF) const;+ QPixmap const& pixmap_for(QSizeF);+ void move_image_by(QPointF);+ void update_cursor(QPointF);+ void update_caption();++ QImage m_screenshot;+ QPixmap m_pixmap;+ bool m_is_at_actual_size { false };+ QPointF m_offset;++ Optional<QPointF> m_press_position;+ QPointF m_press_offset;+ bool m_is_dragging { false };++ QPushButton* m_close_button { nullptr };+ QLabel* m_caption { nullptr };+};++}