Skip to content

Truly independent web browser

UI/Qt: Let the page scroll underneath a screenshot selection

Jelle Raaijmakers1e4ad404bb5e
diff --git a/UI/Qt/ScreenshotSelectionOverlay.cpp b/UI/Qt/ScreenshotSelectionOverlay.cpp
index 2206c0c2110..f5537b0c99d 100644
--- a/UI/Qt/ScreenshotSelectionOverlay.cpp
+++ b/UI/Qt/ScreenshotSelectionOverlay.cpp
@@ -19,6 +19,7 @@
#include <QMouseEvent>
#include <QPainter>
#include <QPushButton>
+#include <QTimer>
#include <QtMath>
namespace Ladybird {
@@ -29,6 +30,8 @@ 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;
+static constexpr int SCROLL_SETTLE_TIME_MS = 100;
+static constexpr int MAXIMUM_SCROLL_ANIMATION_TIME_MS = 300;
// 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 {
@@ -50,8 +53,8 @@ public:
}
};
-// 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.
+// Shows the page as it was when the selection started or the user last scrolled it, 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))
@@ -60,6 +63,11 @@ ScreenshotSelectionOverlay::ScreenshotSelectionOverlay(QImage frame, QWidget* pa
setCursor(Qt::CrossCursor);
setFocusPolicy(Qt::StrongFocus);
+ m_scroll_settle_timer = new QTimer(this);
+ m_scroll_settle_timer->setSingleShot(true);
+ m_scroll_settle_timer->setInterval(SCROLL_SETTLE_TIME_MS);
+ QObject::connect(m_scroll_settle_timer, &QTimer::timeout, this, [this] { cover_page(); });
+
auto* instructions = new QFrame(this);
instructions->setObjectName("ScreenshotSelectionInstructions");
instructions->setAttribute(Qt::WA_StyledBackground);
@@ -159,8 +167,53 @@ void ScreenshotSelectionOverlay::skip()
finish({});
}
+// While the page scrolls underneath, the overlay steps aside for the page to present itself as it does without one,
+// rather than copying each frame the page paints. It still takes the input, so that a press starts a selection rather
+// than reaching the page. Once the scrolling settles, or the user goes on selecting, the overlay takes the page's frame
+// and covers it again.
+void ScreenshotSelectionOverlay::page_did_scroll()
+{
+ if (m_drag_start.has_value())
+ return;
+
+ m_scroll_settle_timer->start();
+ m_follow_frames_until.setRemainingTime(MAXIMUM_SCROLL_ANIMATION_TIME_MS);
+ if (m_reveals_page)
+ return;
+
+ m_reveals_page = true;
+ m_instructions->hide();
+ update();
+ if (on_page_visibility_change)
+ on_page_visibility_change();
+}
+
+// The scrolling settles once the page stops moving, which it may go on doing for a moment after the last input, such as
+// to scroll smoothly. A page that always paints, such as to play an animation, settles soon after the input anyway.
+void ScreenshotSelectionOverlay::page_did_paint()
+{
+ if (m_reveals_page && !m_follow_frames_until.hasExpired())
+ m_scroll_settle_timer->start();
+}
+
+void ScreenshotSelectionOverlay::cover_page()
+{
+ if (!m_reveals_page)
+ return;
+
+ m_scroll_settle_timer->stop();
+ if (auto frame = on_request_page_frame(); !frame.isNull())
+ m_frame = AK::move(frame);
+ m_reveals_page = false;
+ m_instructions->show();
+ update();
+ if (on_page_visibility_change)
+ on_page_visibility_change();
+}
+
void ScreenshotSelectionOverlay::dismiss()
{
+ m_scroll_settle_timer->stop();
hide();
if (on_close)
on_close();
@@ -176,6 +229,9 @@ void ScreenshotSelectionOverlay::finish(QImage image)
void ScreenshotSelectionOverlay::paintEvent(QPaintEvent*)
{
+ if (m_reveals_page)
+ return;
+
QPainter painter(this);
painter.drawImage(rect(), m_frame);
@@ -197,6 +253,7 @@ void ScreenshotSelectionOverlay::mousePressEvent(QMouseEvent* event)
{
if (event->button() != Qt::LeftButton)
return;
+ cover_page();
m_drag_start = event->position().toPoint();
m_drag_end = *m_drag_start;
update();
@@ -263,6 +320,7 @@ void ScreenshotSelectionOverlay::keyPressEvent(QKeyEvent* event)
switch (event->key()) {
case Qt::Key_Return:
case Qt::Key_Enter:
+ cover_page();
// 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();
@@ -281,6 +339,8 @@ void ScreenshotSelectionOverlay::keyPressEvent(QKeyEvent* event)
// review behind it.
bool ScreenshotSelectionOverlay::focusNextPrevChild(bool next)
{
+ cover_page();
+
Vector<QWidget*> stops { this };
for (auto* button : m_buttons)
stops.append(button);
diff --git a/UI/Qt/ScreenshotSelectionOverlay.h b/UI/Qt/ScreenshotSelectionOverlay.h
index e18e911d94e..e57c0ba9240 100644
--- a/UI/Qt/ScreenshotSelectionOverlay.h
+++ b/UI/Qt/ScreenshotSelectionOverlay.h
@@ -11,12 +11,14 @@
#include <AK/Vector.h>
#include <AK/kmalloc.h>
+#include <QDeadlineTimer>
#include <QImage>
#include <QPoint>
#include <QRect>
#include <QWidget>
class QPushButton;
+class QTimer;
namespace Ladybird {
@@ -33,8 +35,14 @@ public:
void skip();
void dismiss();
+ bool reveals_page() const { return m_reveals_page; }
+ void page_did_scroll();
+ void page_did_paint();
+
Function<void()> on_close;
Function<void(QImage)> on_finished;
+ Function<QImage()> on_request_page_frame;
+ Function<void()> on_page_visibility_change;
protected:
virtual void paintEvent(QPaintEvent*) override;
@@ -49,11 +57,15 @@ protected:
private:
void place_instructions();
+ void cover_page();
QRect selection() const;
void finish(QImage);
void update_chrome_style();
QImage m_frame;
+ bool m_reveals_page { false };
+ QTimer* m_scroll_settle_timer { nullptr };
+ QDeadlineTimer m_follow_frames_until;
Optional<QPoint> m_drag_start;
QPoint m_drag_end;
QWidget* m_instructions { nullptr };
diff --git a/UI/Qt/Tab.cpp b/UI/Qt/Tab.cpp
index 08ec3444c69..ae521fd534c 100644
--- a/UI/Qt/Tab.cpp
+++ b/UI/Qt/Tab.cpp
@@ -972,6 +972,18 @@ void Tab::initialize(Optional<WebView::CanonicalTraversable&> traversable)
tab.enter_report_review(this, tr("Report Website Issue"));
};
+ // A review takes its screenshot on the page it reports, in the tab it was opened from, and comes back once done.
+ view().on_request_web_compat_report_screenshot = [this](Function<void(QImage)> on_selected) {
+ auto* reported_tab = m_tab_to_activate_on_close.data();
+ if (!reported_tab)
+ return;
+ m_window->activate_tab(m_window->tab_index(reported_tab));
+ reported_tab->view().select_web_compat_report_screenshot(AK::move(on_selected), [this, review_tab = QPointer { this }] {
+ if (review_tab)
+ m_window->activate_tab(m_window->tab_index(this));
+ });
+ };
+
view().on_link_hover = [this](auto const& url) {
m_hover_label->setText(qstring_from_ak_string(url.to_byte_string()));
update_hover_label();
diff --git a/UI/Qt/WebContentView.cpp b/UI/Qt/WebContentView.cpp
index 3c4bde71b6d..c0d8f132516 100644
--- a/UI/Qt/WebContentView.cpp
+++ b/UI/Qt/WebContentView.cpp
@@ -10,6 +10,7 @@
#include <AK/Assertions.h>
#include <AK/ByteBuffer.h>
#include <AK/Format.h>
+#include <AK/GenericShorthands.h>
#include <AK/LexicalPath.h>
#include <AK/NonnullOwnPtr.h>
#include <AK/Types.h>
@@ -306,6 +307,9 @@ WebContentView::WebContentView(QWidget* window, Optional<WebView::CanonicalTrave
#endif
on_ready_to_paint = [this]() {
+ if (m_screenshot_selection_overlay)
+ m_screenshot_selection_overlay->page_did_paint();
+
// Nothing of the page shows while an overlay covers it, which repaints the page once it goes away.
if (!is_page_covered_by_overlay()) {
#ifdef LADYBIRD_QT_USE_RHI_WIDGET
@@ -811,6 +815,8 @@ void WebContentView::keyPressEvent(QKeyEvent* event)
return;
}
+ if (m_screenshot_selection_overlay)
+ m_screenshot_selection_overlay->page_did_scroll();
enqueue_native_event(Web::KeyEvent::Type::KeyDown, *event);
}
@@ -938,6 +944,11 @@ void WebContentView::wheelEvent(QWheelEvent* event)
return;
}
+ // The momentum that remains of a scroll on a touchpad goes on moving the page by a pixel or so for a while after it
+ // looks to have stopped, which a screenshot selection does not wait for.
+ auto is_momentum_running_out = first_is_one_of(event->phase(), Qt::ScrollMomentum, Qt::ScrollEnd) && event->pixelDelta().manhattanLength() <= 1;
+ if (m_screenshot_selection_overlay && !is_momentum_running_out)
+ m_screenshot_selection_overlay->page_did_scroll();
enqueue_native_event(Web::MouseEvent::Type::MouseWheel, *event);
}
@@ -1478,8 +1489,8 @@ void WebContentView::leave_report_review()
schedule_repaint();
}
-// The frame and the page's dumps are taken right away, so the report shows the page as it was when the user asked. The
-// user selects what they saw on the page itself, and reviews the report in a tab of its own.
+// The page's dumps are taken right away, so the report describes the page as it was when the user asked. The user
+// selects what they saw on the page itself, and reviews the report in a tab of its own.
void WebContentView::start_web_compat_report()
{
if (m_screenshot_selection_overlay)
@@ -1487,24 +1498,32 @@ void WebContentView::start_web_compat_report()
WebCompatReportCapture capture {
.page = WebView::WebCompatReport::describe_page(*this),
- .frame = visible_snapshot(),
.screenshot = {},
.page_dumps = WebView::WebCompatReport::request_page_dumps(*this),
};
-
- auto review = [this](WebCompatReportCapture capture) {
+ select_web_compat_report_screenshot([this, capture = AK::move(capture)](QImage screenshot) mutable {
+ capture.screenshot = AK::move(screenshot);
if (on_request_web_compat_report_review)
on_request_web_compat_report_review(AK::move(capture));
- };
- if (capture.frame.isNull()) {
- review(AK::move(capture));
+ });
+}
+
+// A report's screenshot is selected on the page itself, both when the report starts and when its review takes another.
+// Without a frame to select from, the report goes without one.
+void WebContentView::select_web_compat_report_screenshot(Function<void(QImage)> on_selected, Function<void()> on_close)
+{
+ if (m_screenshot_selection_overlay)
+ return;
+
+ auto frame = visible_snapshot();
+ if (frame.isNull()) {
+ if (on_close)
+ on_close();
+ on_selected({});
return;
}
- select_screenshot(capture.frame).on_finished = [review = AK::move(review), capture = AK::move(capture)](QImage screenshot) mutable {
- capture.screenshot = AK::move(screenshot);
- review(AK::move(capture));
- };
+ select_screenshot(frame, AK::move(on_close)).on_finished = AK::move(on_selected);
}
// The tab was opened only for the review, so no web content runs behind it, and leaving the review closes it.
@@ -1521,15 +1540,15 @@ void WebContentView::review_web_compat_report(WebCompatReportCapture capture)
column_layout->setContentsMargins(0, 0, 0, 0);
column_layout->addWidget(review);
- review->on_select_screenshot = [this, review = QPointer { review }, frame = AK::move(capture.frame)] {
- if (frame.isNull() || m_screenshot_selection_overlay)
+ review->on_select_screenshot = [this, review = QPointer { review }] {
+ if (!on_request_web_compat_report_screenshot)
return;
- select_screenshot(frame).on_finished = [review](QImage screenshot) {
+ on_request_web_compat_report_screenshot([review](QImage screenshot) {
if (!review)
return;
review->set_screenshot(AK::move(screenshot));
review->setFocus(Qt::OtherFocusReason);
- };
+ });
};
review->on_view_screenshot = [this](QImage const& screenshot) {
if (screenshot.isNull() || m_screenshot_viewer)
@@ -1568,16 +1587,20 @@ void WebContentView::review_web_compat_report(WebCompatReportCapture capture)
}
// The selection shows a frame taken earlier, so the user selects what they saw, even while the page underneath changes.
-ScreenshotSelectionOverlay& WebContentView::select_screenshot(QImage const& frame)
+ScreenshotSelectionOverlay& WebContentView::select_screenshot(QImage const& frame, Function<void()> on_close)
{
VERIFY(!m_screenshot_selection_overlay);
auto* overlay = new ScreenshotSelectionOverlay(frame, this);
m_screenshot_selection_overlay = overlay;
auto focus_to_restore = show_transient_overlay(*overlay);
- overlay->on_close = [this, overlay, focus_to_restore] {
+ overlay->on_close = [this, overlay, focus_to_restore, on_close = AK::move(on_close)] {
close_transient_overlay(*overlay, focus_to_restore);
+ if (on_close)
+ on_close();
};
+ overlay->on_request_page_frame = [this] { return visible_snapshot(); };
+ overlay->on_page_visibility_change = [this] { schedule_repaint(); };
return *overlay;
}
@@ -1609,7 +1632,7 @@ Array<QWidget*, 4> WebContentView::overlays() const
bool WebContentView::is_page_covered_by_overlay() const
{
for (auto* overlay : overlays()) {
- if (overlay && !overlay->isHidden())
+ if (overlay && !overlay->isHidden() && !(overlay == m_screenshot_selection_overlay && m_screenshot_selection_overlay->reveals_page()))
return true;
}
return false;
diff --git a/UI/Qt/WebContentView.h b/UI/Qt/WebContentView.h
index 3ea1d227c06..b5ea4deb949 100644
--- a/UI/Qt/WebContentView.h
+++ b/UI/Qt/WebContentView.h
@@ -93,7 +93,6 @@ class SelectDropdown;
struct WebCompatReportCapture {
WebView::WebCompatReport::Page page;
- QImage frame;
QImage screenshot;
NonnullRefPtr<Core::Promise<String>> page_dumps;
};
@@ -157,8 +156,10 @@ public:
void review_hidden_crash_report(ByteString const& report_name);
void leave_report_review();
void review_web_compat_report(WebCompatReportCapture);
+ void select_web_compat_report_screenshot(Function<void(QImage)> on_selected, Function<void()> on_close = {});
Function<void(WebCompatReportCapture)> on_request_web_compat_report_review;
+ Function<void(Function<void(QImage)> on_selected)> on_request_web_compat_report_screenshot;
enum class PaletteMode {
Default,
@@ -237,7 +238,7 @@ private:
void show_crash_report_review(Optional<ByteString> const& hidden_crash_report_name = {});
void start_web_compat_report();
- ScreenshotSelectionOverlay& select_screenshot(QImage const& frame);
+ ScreenshotSelectionOverlay& select_screenshot(QImage const& frame, Function<void()> on_close = {});
QPointer<QWidget> show_transient_overlay(QWidget&);
void close_transient_overlay(QWidget&, QPointer<QWidget> const& focus_to_restore);