Skip to content

Truly independent web browser

UI/Qt: Add an overlay to select an area of the visible page

Jelle Raaijmakers751cb5bf2186
diff --git a/Tests/UI/Qt/CMakeLists.txt b/Tests/UI/Qt/CMakeLists.txt
index eb6c004cddb..cb02511088f 100644
--- a/Tests/UI/Qt/CMakeLists.txt
+++ b/Tests/UI/Qt/CMakeLists.txt
@@ -75,6 +75,18 @@ if (APPLE OR LINUX)
deploy_qt_offscreen_platform_plugin(TestCrashReportReviewWidget)
endif()
+ladybird_test("TestScreenshotSelectionOverlay.cpp" UI/Qt
+ CUSTOM_MAIN "$<TARGET_OBJECTS:QtTestMain>"
+ LIBS LibURL Qt::Core Qt::Gui Qt::Widgets
+)
+target_sources(TestScreenshotSelectionOverlay PRIVATE
+ "${LADYBIRD_SOURCE_DIR}/UI/Qt/ChromeStyle.cpp"
+ "${LADYBIRD_SOURCE_DIR}/UI/Qt/Popover.cpp"
+ "${LADYBIRD_SOURCE_DIR}/UI/Qt/ScreenshotSelectionOverlay.cpp"
+ "${LADYBIRD_SOURCE_DIR}/UI/Qt/StringUtils.cpp"
+)
+deploy_qt_offscreen_platform_plugin(TestScreenshotSelectionOverlay)
+
ladybird_test("TestHiddenPageSizing.cpp" UI/Qt
CUSTOM_MAIN "$<TARGET_OBJECTS:QtTestMain>"
LIBS Qt::Core Qt::Gui Qt::Widgets
diff --git a/Tests/UI/Qt/TestScreenshotSelectionOverlay.cpp b/Tests/UI/Qt/TestScreenshotSelectionOverlay.cpp
new file mode 100644
index 00000000000..b59d5244563
--- /dev/null
+++ b/Tests/UI/Qt/TestScreenshotSelectionOverlay.cpp
@@ -0,0 +1,217 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <AK/Optional.h>
+#include <LibTest/TestCase.h>
+#include <UI/Qt/ScreenshotSelectionOverlay.h>
+
+#include <QApplication>
+#include <QKeyEvent>
+#include <QMouseEvent>
+#include <QPushButton>
+
+using Ladybird::ScreenshotSelectionOverlay;
+
+static QImage create_frame()
+{
+ QImage frame(400, 200, QImage::Format_RGB32);
+ frame.fill(Qt::white);
+ for (int y = 20; y < 60; ++y) {
+ for (int x = 40; x < 140; ++x)
+ frame.setPixel(x, y, qRgb(255, 0, 0));
+ }
+ return frame;
+}
+
+static void send_mouse(QWidget& widget, QEvent::Type type, QPoint 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 send_key(QWidget& widget, Qt::Key key)
+{
+ QKeyEvent event(QEvent::KeyPress, key, Qt::NoModifier);
+ QApplication::sendEvent(&widget, &event);
+}
+
+struct Result {
+ Optional<QImage> image;
+ bool canceled { false };
+};
+
+static void track(ScreenshotSelectionOverlay& overlay, Result& result)
+{
+ result = {};
+ overlay.on_finished = [&result](QImage image) { result.image = image; };
+ overlay.on_canceled = [&result] { result.canceled = true; };
+}
+
+TEST_CASE(a_selection_crops_the_frame_in_its_own_pixels)
+{
+ QWidget view;
+ view.resize(200, 100);
+ ScreenshotSelectionOverlay overlay { create_frame(), &view };
+ Result result;
+ track(overlay, result);
+ view.show();
+
+ overlay.select(QRect(20, 10, 50, 20));
+ EXPECT(result.image.has_value());
+ EXPECT_EQ(result.image->size(), QSize(100, 40));
+ EXPECT_EQ(result.image->pixel(0, 0), qRgb(255, 0, 0));
+ EXPECT_EQ(result.image->pixel(99, 39), qRgb(255, 0, 0));
+ EXPECT(!overlay.isVisible());
+}
+
+TEST_CASE(a_selection_is_bounded_by_the_frame)
+{
+ QWidget view;
+ view.resize(200, 100);
+ ScreenshotSelectionOverlay overlay { create_frame(), &view };
+ Result result;
+ track(overlay, result);
+ view.show();
+
+ overlay.select(QRect(150, 50, 100, 100));
+ EXPECT(result.image.has_value());
+ EXPECT_EQ(result.image->size(), QSize(100, 100));
+}
+
+TEST_CASE(a_click_without_a_drag_selects_nothing)
+{
+ QWidget view;
+ view.resize(200, 100);
+ ScreenshotSelectionOverlay overlay { create_frame(), &view };
+ Result result;
+ track(overlay, result);
+ view.show();
+
+ send_mouse(overlay, QEvent::MouseButtonPress, QPoint(100, 80));
+ send_mouse(overlay, QEvent::MouseButtonRelease, QPoint(100, 80));
+ EXPECT(!result.image.has_value());
+ EXPECT(overlay.isVisible());
+
+ send_mouse(overlay, QEvent::MouseButtonPress, QPoint(10, 70));
+ send_mouse(overlay, QEvent::MouseMove, QPoint(60, 90));
+ send_mouse(overlay, QEvent::MouseButtonRelease, QPoint(60, 90));
+ EXPECT(result.image.has_value());
+ EXPECT_EQ(result.image->size(), QSize(100, 40));
+}
+
+TEST_CASE(the_keyboard_takes_the_whole_frame_or_cancels)
+{
+ QWidget view;
+ view.resize(200, 100);
+ Result result;
+
+ {
+ ScreenshotSelectionOverlay overlay { create_frame(), &view };
+ track(overlay, result);
+ view.show();
+ send_key(overlay, Qt::Key_Return);
+ EXPECT(result.image.has_value());
+ EXPECT_EQ(result.image->size(), QSize(400, 200));
+ }
+
+ {
+ ScreenshotSelectionOverlay overlay { create_frame(), &view };
+ track(overlay, result);
+ send_key(overlay, Qt::Key_Escape);
+ EXPECT(!result.image.has_value());
+ EXPECT(result.canceled);
+ EXPECT(!overlay.isVisible());
+ }
+}
+
+TEST_CASE(the_instructions_keep_their_padding_when_their_buttons_grow)
+{
+ QWidget view;
+ view.resize(1000, 400);
+ ScreenshotSelectionOverlay overlay { create_frame(), &view };
+ view.show();
+
+ auto* instructions = overlay.findChild<QWidget*>("ScreenshotSelectionInstructions");
+ auto buttons = instructions->findChildren<QPushButton*>();
+ for (auto* button : buttons) {
+ auto font = button->font();
+ font.setPointSizeF(font.pointSizeF() * 2);
+ button->setFont(font);
+ }
+ QApplication::processEvents();
+
+ for (auto* button : buttons) {
+ EXPECT(button->geometry().top() >= 12);
+ EXPECT(instructions->height() - button->geometry().bottom() - 1 >= 12);
+ }
+}
+
+TEST_CASE(the_keyboard_reaches_every_choice)
+{
+ QWidget view;
+ view.resize(1000, 300);
+ Result result;
+ auto press = [](Qt::Key key) { send_key(*QApplication::focusWidget(), key); };
+
+ {
+ ScreenshotSelectionOverlay overlay { create_frame(), &view };
+ track(overlay, result);
+ view.show();
+ view.activateWindow();
+ overlay.setFocus();
+ QApplication::processEvents();
+
+ press(Qt::Key_Tab);
+ EXPECT_EQ(QApplication::focusWidget(), overlay.findChild<QPushButton*>("ScreenshotSelectionWholeFrameButton"));
+ press(Qt::Key_Tab);
+ EXPECT_EQ(QApplication::focusWidget(), overlay.findChild<QPushButton*>("ScreenshotSelectionSkipButton"));
+ press(Qt::Key_Return);
+ EXPECT(result.image.has_value());
+ EXPECT(result.image->isNull());
+ }
+
+ {
+ ScreenshotSelectionOverlay overlay { create_frame(), &view };
+ track(overlay, result);
+ overlay.show();
+ overlay.setFocus();
+ QApplication::processEvents();
+
+ // Tab stays within the overlay, going around from the last button back to the selection.
+ press(Qt::Key_Backtab);
+ EXPECT_EQ(QApplication::focusWidget(), overlay.findChild<QPushButton*>("ScreenshotSelectionCancelButton"));
+ press(Qt::Key_Tab);
+ EXPECT_EQ(QApplication::focusWidget(), &overlay);
+ press(Qt::Key_Return);
+ EXPECT_EQ(result.image->size(), QSize(400, 200));
+ }
+}
+
+TEST_CASE(the_instructions_offer_the_same_choices)
+{
+ QWidget view;
+ view.resize(400, 300);
+ Result result;
+
+ ScreenshotSelectionOverlay overlay { create_frame(), &view };
+ track(overlay, result);
+ view.show();
+ overlay.findChild<QPushButton*>("ScreenshotSelectionWholeFrameButton")->click();
+ EXPECT_EQ(result.image->size(), QSize(400, 200));
+
+ ScreenshotSelectionOverlay second_overlay { create_frame(), &view };
+ track(second_overlay, result);
+ second_overlay.findChild<QPushButton*>("ScreenshotSelectionSkipButton")->click();
+ EXPECT(result.image->isNull());
+
+ ScreenshotSelectionOverlay third_overlay { create_frame(), &view };
+ track(third_overlay, result);
+ third_overlay.findChild<QPushButton*>("ScreenshotSelectionCancelButton")->click();
+ EXPECT(!result.image.has_value());
+ EXPECT(result.canceled);
+}
diff --git a/UI/Qt/CMakeLists.txt b/UI/Qt/CMakeLists.txt
index fd48f9ce4ff..b92104b64d5 100644
--- a/UI/Qt/CMakeLists.txt
+++ b/UI/Qt/CMakeLists.txt
@@ -50,6 +50,7 @@ target_sources(ladybird PRIVATE
PictureInPictureWindow.cpp
Popover.cpp
ReportReview.cpp
+ ScreenshotSelectionOverlay.cpp
SelectDropdown.cpp
StringUtils.cpp
Tab.cpp
diff --git a/UI/Qt/ChromeStyle.cpp b/UI/Qt/ChromeStyle.cpp
index 48650a151f1..96bfb702f13 100644
--- a/UI/Qt/ChromeStyle.cpp
+++ b/UI/Qt/ChromeStyle.cpp
@@ -1492,6 +1492,55 @@ QPushButton#LadybirdMessagePopoverAcceptButton:pressed {{
text, surface, border, muted_text, control_border, hover_surface, pressed_surface, accent_color, accent_hover, accent_pressed);
}
+// The instructions over a screenshot selection look like a message popover, with every action a secondary one.
+QString screenshot_selection_style_sheet(QPalette const& palette)
+{
+ auto text = style_sheet_color(chrome_text(palette));
+ auto surface = style_sheet_color(chrome_surface(palette));
+ auto border = style_sheet_color(chrome_border(palette));
+ auto control_border = style_sheet_color(chrome_control_border(palette));
+ auto hover_surface = style_sheet_color(chrome_surface_hover(palette));
+ auto pressed_surface = style_sheet_color(chrome_surface_pressed(palette));
+ auto accent = style_sheet_color(chrome_accent());
+
+ return qformatted(R"(
+QFrame#ScreenshotSelectionInstructions {{
+ color: {0};
+ background: {1};
+ border: 1px solid {2};
+ border-radius: 8px;
+}}
+
+QFrame#ScreenshotSelectionInstructions QLabel {{
+ color: {0};
+ background: transparent;
+ border: none;
+}}
+
+QFrame#ScreenshotSelectionInstructions QPushButton {{
+ color: {0};
+ background: {1};
+ border: 1px solid {3};
+ border-radius: 6px;
+ padding: 5px 12px;
+}}
+
+QFrame#ScreenshotSelectionInstructions QPushButton:hover {{
+ background: {4};
+}}
+
+QFrame#ScreenshotSelectionInstructions QPushButton:pressed {{
+ background: {5};
+}}
+
+QFrame#ScreenshotSelectionInstructions QPushButton:focus {{
+ border: 2px solid {6};
+ padding: 4px 11px;
+}}
+)",
+ text, surface, border, control_border, hover_surface, pressed_surface, accent);
+}
+
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.h
index 07ada31b307..8133b9e9142 100644
--- a/UI/Qt/ChromeStyle.h
+++ b/UI/Qt/ChromeStyle.h
@@ -54,6 +54,7 @@ QString autocomplete_popup_style_sheet(QPalette const&);
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&);
enum class CircularControlFrameStyle {
InteractionOnly,
diff --git a/UI/Qt/Popover.cpp b/UI/Qt/Popover.cpp
index 693c4f8025b..88a4dba3b7a 100644
--- a/UI/Qt/Popover.cpp
+++ b/UI/Qt/Popover.cpp
@@ -42,12 +42,7 @@ Popover::Popover(QWidget* parent)
m_card->setFrameShape(QFrame::StyledPanel);
m_card->setFrameShadow(QFrame::Raised);
layout->addWidget(m_card);
-
- auto* shadow = new QGraphicsDropShadowEffect(m_card);
- shadow->setBlurRadius(24);
- shadow->setOffset(0, 4);
- shadow->setColor(QColor(0, 0, 0, 72));
- m_card->setGraphicsEffect(shadow);
+ cast_popover_shadow(*m_card);
}
void Popover::showEvent(QShowEvent* event)
@@ -145,4 +140,14 @@ void move_popover_below(Popover& popover, QWidget& anchor)
popover.move(card_position - QPoint(POPOVER_SHADOW_MARGIN, POPOVER_SHADOW_MARGIN));
}
+// Cards that float over other content cast the same shadow as popovers, whether they are windows of their own or not.
+void cast_popover_shadow(QWidget& card)
+{
+ auto* shadow = new QGraphicsDropShadowEffect(&card);
+ shadow->setBlurRadius(24);
+ shadow->setOffset(0, 4);
+ shadow->setColor(QColor(0, 0, 0, 72));
+ card.setGraphicsEffect(shadow);
+}
+
}
diff --git a/UI/Qt/Popover.h b/UI/Qt/Popover.h
index 62bac20b809..54191f6a957 100644
--- a/UI/Qt/Popover.h
+++ b/UI/Qt/Popover.h
@@ -53,5 +53,6 @@ private:
};
void move_popover_below(Popover&, QWidget& anchor);
+void cast_popover_shadow(QWidget&);
}
diff --git a/UI/Qt/ScreenshotSelectionOverlay.cpp b/UI/Qt/ScreenshotSelectionOverlay.cpp
new file mode 100644
index 00000000000..bc77b1b3739
--- /dev/null
+++ b/UI/Qt/ScreenshotSelectionOverlay.cpp
@@ -0,0 +1,255 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <UI/Qt/ChromeStyle.h>
+#include <UI/Qt/Popover.h>
+#include <UI/Qt/ScreenshotSelectionOverlay.h>
+
+#include <QApplication>
+#include <QEvent>
+#include <QFontMetricsF>
+#include <QFrame>
+#include <QHBoxLayout>
+#include <QKeyEvent>
+#include <QLabel>
+#include <QMouseEvent>
+#include <QPainter>
+#include <QPushButton>
+#include <QVBoxLayout>
+#include <QtMath>
+
+namespace Ladybird {
+
+static constexpr int INSTRUCTIONS_MARGIN = 16;
+static constexpr int INSTRUCTIONS_TEXT_INSET = 16;
+static constexpr int INSTRUCTIONS_BUTTON_INSET = 12;
+static constexpr int INSTRUCTIONS_BUTTON_SPACING = 8;
+static constexpr int INSTRUCTIONS_TEXT_SPACING = 24;
+static constexpr int DIMMING_ALPHA = 110;
+
+// The instructions stay on one line while the tab has room for it, and wrap rather than being cut off when it does not.
+class InstructionsLabel final : public QLabel {
+public:
+ AK_ALLOC_WITH_KMALLOC;
+
+ InstructionsLabel(QString const& text, QWidget* parent)
+ : QLabel(text, parent)
+ {
+ setTextFormat(Qt::PlainText);
+ setWordWrap(true);
+ }
+
+ virtual QSize sizeHint() const override
+ {
+ auto margins = contentsMargins();
+ auto width = qCeil(QFontMetricsF(font()).horizontalAdvance(text())) + margins.left() + margins.right() + 1;
+ return { width, heightForWidth(width) };
+ }
+};
+
+// Shows the page as it was when the report was started, so the user selects what they saw, not what the page has
+// changed to since. The rest of the frame dims around the selection.
+ScreenshotSelectionOverlay::ScreenshotSelectionOverlay(QImage frame, QWidget* parent)
+ : QWidget(parent)
+ , m_frame(AK::move(frame))
+{
+ setObjectName("LadybirdScreenshotSelection");
+ setCursor(Qt::CrossCursor);
+ setFocusPolicy(Qt::StrongFocus);
+
+ auto* instructions = new QFrame(this);
+ instructions->setObjectName("ScreenshotSelectionInstructions");
+ instructions->setAttribute(Qt::WA_StyledBackground);
+ instructions->setCursor(Qt::ArrowCursor);
+ cast_popover_shadow(*instructions);
+ // The buttons keep the same distance from the top, bottom and end of the card, and the text starts a little further
+ // in. It gives up its width before the buttons do in a narrow tab.
+ auto* layout = new QHBoxLayout(instructions);
+ layout->setContentsMargins(INSTRUCTIONS_TEXT_INSET, INSTRUCTIONS_BUTTON_INSET, INSTRUCTIONS_BUTTON_INSET, INSTRUCTIONS_BUTTON_INSET);
+ layout->setSpacing(INSTRUCTIONS_BUTTON_SPACING);
+ layout->addWidget(new InstructionsLabel(tr("Drag to select the part of the page that shows the problem."), instructions), 1);
+ layout->addSpacing(INSTRUCTIONS_TEXT_SPACING - INSTRUCTIONS_BUTTON_SPACING);
+
+ auto* whole_frame_button = new QPushButton(tr("Use Visible Page"), instructions);
+ whole_frame_button->setObjectName("ScreenshotSelectionWholeFrameButton");
+ whole_frame_button->setFocusPolicy(Qt::TabFocus);
+ QObject::connect(whole_frame_button, &QPushButton::clicked, this, [this] { use_whole_frame(); });
+ layout->addWidget(whole_frame_button);
+
+ auto* skip_button = new QPushButton(tr("No Screenshot"), instructions);
+ skip_button->setObjectName("ScreenshotSelectionSkipButton");
+ skip_button->setFocusPolicy(Qt::TabFocus);
+ QObject::connect(skip_button, &QPushButton::clicked, this, [this] { skip(); });
+ layout->addWidget(skip_button);
+
+ auto* cancel_button = new QPushButton(tr("Cancel"), instructions);
+ cancel_button->setObjectName("ScreenshotSelectionCancelButton");
+ cancel_button->setFocusPolicy(Qt::TabFocus);
+ QObject::connect(cancel_button, &QPushButton::clicked, this, [this] { cancel(); });
+ layout->addWidget(cancel_button);
+
+ m_buttons = { whole_frame_button, skip_button, cancel_button };
+
+ m_instructions = instructions;
+ update_chrome_style();
+
+ // A layout keeps the card as large as its contents, however a restyle changes them, centered at the top.
+ auto* overlay_layout = new QVBoxLayout(this);
+ overlay_layout->setContentsMargins(INSTRUCTIONS_MARGIN, INSTRUCTIONS_MARGIN, INSTRUCTIONS_MARGIN, INSTRUCTIONS_MARGIN);
+ overlay_layout->addWidget(instructions, 0, Qt::AlignHCenter);
+ overlay_layout->addStretch();
+ if (parent)
+ setGeometry(parent->rect());
+}
+
+// The selection in the coordinates of this widget, which the frame is stretched across.
+QRect ScreenshotSelectionOverlay::selection() const
+{
+ if (!m_drag_start.has_value())
+ return {};
+ QPoint top_left { qMin(m_drag_start->x(), m_drag_end.x()), qMin(m_drag_start->y(), m_drag_end.y()) };
+ QSize size { qAbs(m_drag_end.x() - m_drag_start->x()), qAbs(m_drag_end.y() - m_drag_start->y()) };
+ return QRect(top_left, size).intersected(rect());
+}
+
+void ScreenshotSelectionOverlay::select(QRect area)
+{
+ area = area.normalized().intersected(rect());
+ if (area.width() < minimum_selection_size || area.height() < minimum_selection_size || width() <= 0 || height() <= 0)
+ return;
+
+ auto scale_x = static_cast<qreal>(m_frame.width()) / width();
+ auto scale_y = static_cast<qreal>(m_frame.height()) / height();
+ QRect frame_area {
+ qRound(area.x() * scale_x),
+ qRound(area.y() * scale_y),
+ qRound(area.width() * scale_x),
+ qRound(area.height() * scale_y),
+ };
+ finish(m_frame.copy(frame_area.intersected(m_frame.rect())));
+}
+
+void ScreenshotSelectionOverlay::use_whole_frame()
+{
+ finish(m_frame);
+}
+
+void ScreenshotSelectionOverlay::skip()
+{
+ finish({});
+}
+
+void ScreenshotSelectionOverlay::cancel()
+{
+ hide();
+ if (on_canceled)
+ on_canceled();
+}
+
+void ScreenshotSelectionOverlay::finish(QImage image)
+{
+ hide();
+ if (on_finished)
+ on_finished(AK::move(image));
+}
+
+void ScreenshotSelectionOverlay::paintEvent(QPaintEvent*)
+{
+ QPainter painter(this);
+ painter.drawImage(rect(), m_frame);
+
+ auto selected = selection();
+ QRegion dimmed { rect() };
+ if (!selected.isEmpty())
+ dimmed = dimmed.subtracted(selected);
+ painter.setClipRegion(dimmed);
+ painter.fillRect(rect(), QColor(0, 0, 0, DIMMING_ALPHA));
+ painter.setClipping(false);
+
+ if (!selected.isEmpty()) {
+ painter.setPen(QPen(palette().color(QPalette::Highlight), 2));
+ painter.drawRect(selected.adjusted(1, 1, -1, -1));
+ }
+}
+
+void ScreenshotSelectionOverlay::mousePressEvent(QMouseEvent* event)
+{
+ if (event->button() != Qt::LeftButton)
+ return;
+ m_drag_start = event->position().toPoint();
+ m_drag_end = *m_drag_start;
+ update();
+}
+
+void ScreenshotSelectionOverlay::mouseMoveEvent(QMouseEvent* event)
+{
+ if (!m_drag_start.has_value())
+ return;
+
+ // Only the area the selection leaves or enters changes, along with the outline around it.
+ auto previous_selection = selection();
+ m_drag_end = event->position().toPoint();
+ update(previous_selection.united(selection()).adjusted(-2, -2, 2, 2));
+}
+
+// A click without a drag selects nothing, so the user can try again.
+void ScreenshotSelectionOverlay::mouseReleaseEvent(QMouseEvent* event)
+{
+ if (event->button() != Qt::LeftButton || !m_drag_start.has_value())
+ return;
+ m_drag_end = event->position().toPoint();
+ auto selected = selection();
+ m_drag_start.clear();
+ update();
+ select(selected);
+}
+
+void ScreenshotSelectionOverlay::keyPressEvent(QKeyEvent* event)
+{
+ switch (event->key()) {
+ case Qt::Key_Return:
+ case Qt::Key_Enter:
+ // A focused button does what it says. Otherwise, the whole frame is taken.
+ if (auto* button = qobject_cast<QPushButton*>(QApplication::focusWidget()); button && m_buttons.contains_slow(button))
+ button->click();
+ else
+ use_whole_frame();
+ return;
+ case Qt::Key_Escape:
+ cancel();
+ return;
+ default:
+ QWidget::keyPressEvent(event);
+ }
+}
+
+// Tab moves between the selection and its buttons, and stays within the overlay rather than reaching the page or the
+// review behind it.
+bool ScreenshotSelectionOverlay::focusNextPrevChild(bool next)
+{
+ Vector<QWidget*> stops { this };
+ for (auto* button : m_buttons)
+ stops.append(button);
+
+ auto index = stops.find_first_index(QApplication::focusWidget()).value_or(0);
+ index = next ? (index + 1) % stops.size() : (index + stops.size() - 1) % stops.size();
+ stops[index]->setFocus(next ? Qt::TabFocusReason : Qt::BacktabFocusReason);
+ return true;
+}
+
+void ScreenshotSelectionOverlay::changeEvent(QEvent* event)
+{
+ if (event->type() == QEvent::PaletteChange)
+ update_chrome_style();
+ QWidget::changeEvent(event);
+}
+
+void ScreenshotSelectionOverlay::update_chrome_style()
+{
+ m_instructions->setStyleSheet(ChromeStyle::screenshot_selection_style_sheet(palette()));
+}
+
+}
diff --git a/UI/Qt/ScreenshotSelectionOverlay.h b/UI/Qt/ScreenshotSelectionOverlay.h
new file mode 100644
index 00000000000..5731ee4bc76
--- /dev/null
+++ b/UI/Qt/ScreenshotSelectionOverlay.h
@@ -0,0 +1,60 @@
+/*
+ * 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/Vector.h>
+#include <AK/kmalloc.h>
+
+#include <QImage>
+#include <QPoint>
+#include <QRect>
+#include <QWidget>
+
+class QPushButton;
+
+namespace Ladybird {
+
+class ScreenshotSelectionOverlay final : public QWidget {
+public:
+ AK_ALLOC_WITH_KMALLOC;
+
+ static constexpr int minimum_selection_size = 8;
+
+ ScreenshotSelectionOverlay(QImage frame, QWidget* parent);
+
+ void select(QRect);
+ void use_whole_frame();
+ void skip();
+ void cancel();
+
+ Function<void(QImage)> on_finished;
+ Function<void()> on_canceled;
+
+protected:
+ virtual void paintEvent(QPaintEvent*) override;
+ virtual void mousePressEvent(QMouseEvent*) override;
+ virtual void mouseMoveEvent(QMouseEvent*) override;
+ virtual void mouseReleaseEvent(QMouseEvent*) override;
+ virtual void keyPressEvent(QKeyEvent*) override;
+ virtual bool focusNextPrevChild(bool next) override;
+ virtual void changeEvent(QEvent*) override;
+
+private:
+ QRect selection() const;
+ void finish(QImage);
+ void update_chrome_style();
+
+ QImage m_frame;
+ Optional<QPoint> m_drag_start;
+ QPoint m_drag_end;
+ QWidget* m_instructions { nullptr };
+ Vector<QPushButton*> m_buttons;
+};
+
+}