Skip to content

Truly independent web browser

UI/Qt: Let callers handle only the screenshot selection's outcome

Jelle Raaijmakerse3077487060d
diff --git a/Tests/UI/Qt/TestScreenshotSelectionOverlay.cpp b/Tests/UI/Qt/TestScreenshotSelectionOverlay.cpp
index b59d5244563..f16cacc3be6 100644
--- a/Tests/UI/Qt/TestScreenshotSelectionOverlay.cpp
+++ b/Tests/UI/Qt/TestScreenshotSelectionOverlay.cpp
@@ -42,14 +42,14 @@ static void send_key(QWidget& widget, Qt::Key key)
struct Result {
Optional<QImage> image;
- bool canceled { false };
+ bool closed { 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; };
+ overlay.on_close = [&result] { result.closed = true; };
}
TEST_CASE(a_selection_crops_the_frame_in_its_own_pixels)
@@ -124,7 +124,7 @@ TEST_CASE(the_keyboard_takes_the_whole_frame_or_cancels)
track(overlay, result);
send_key(overlay, Qt::Key_Escape);
EXPECT(!result.image.has_value());
- EXPECT(result.canceled);
+ EXPECT(result.closed);
EXPECT(!overlay.isVisible());
}
}
@@ -213,5 +213,5 @@ TEST_CASE(the_instructions_offer_the_same_choices)
track(third_overlay, result);
third_overlay.findChild<QPushButton*>("ScreenshotSelectionCancelButton")->click();
EXPECT(!result.image.has_value());
- EXPECT(result.canceled);
+ EXPECT(result.closed);
}
diff --git a/UI/Qt/ScreenshotSelectionOverlay.cpp b/UI/Qt/ScreenshotSelectionOverlay.cpp
index 06973f9d004..2206c0c2110 100644
--- a/UI/Qt/ScreenshotSelectionOverlay.cpp
+++ b/UI/Qt/ScreenshotSelectionOverlay.cpp
@@ -89,7 +89,7 @@ ScreenshotSelectionOverlay::ScreenshotSelectionOverlay(QImage frame, QWidget* pa
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(); });
+ QObject::connect(cancel_button, &QPushButton::clicked, this, [this] { dismiss(); });
layout->addWidget(cancel_button);
m_buttons = { whole_frame_button, skip_button, cancel_button };
@@ -159,16 +159,17 @@ void ScreenshotSelectionOverlay::skip()
finish({});
}
-void ScreenshotSelectionOverlay::cancel()
+void ScreenshotSelectionOverlay::dismiss()
{
hide();
- if (on_canceled)
- on_canceled();
+ if (on_close)
+ on_close();
}
+// The overlay closes before the screenshot is handed on, which may open something that takes the focus.
void ScreenshotSelectionOverlay::finish(QImage image)
{
- hide();
+ dismiss();
if (on_finished)
on_finished(AK::move(image));
}
@@ -269,7 +270,7 @@ void ScreenshotSelectionOverlay::keyPressEvent(QKeyEvent* event)
use_whole_frame();
return;
case Qt::Key_Escape:
- cancel();
+ dismiss();
return;
default:
QWidget::keyPressEvent(event);
diff --git a/UI/Qt/ScreenshotSelectionOverlay.h b/UI/Qt/ScreenshotSelectionOverlay.h
index 4d907c48d2b..e18e911d94e 100644
--- a/UI/Qt/ScreenshotSelectionOverlay.h
+++ b/UI/Qt/ScreenshotSelectionOverlay.h
@@ -31,10 +31,10 @@ public:
void select(QRect);
void use_whole_frame();
void skip();
- void cancel();
+ void dismiss();
+ Function<void()> on_close;
Function<void(QImage)> on_finished;
- Function<void()> on_canceled;
protected:
virtual void paintEvent(QPaintEvent*) override;
diff --git a/UI/Qt/WebContentView.cpp b/UI/Qt/WebContentView.cpp
index d47e39d296d..3c4bde71b6d 100644
--- a/UI/Qt/WebContentView.cpp
+++ b/UI/Qt/WebContentView.cpp
@@ -1501,11 +1501,10 @@ void WebContentView::start_web_compat_report()
return;
}
- auto frame = capture.frame;
- select_screenshot(frame, [review = AK::move(review), capture = AK::move(capture)](QImage screenshot) mutable {
+ 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));
- });
+ };
}
// The tab was opened only for the review, so no web content runs behind it, and leaving the review closes it.
@@ -1523,14 +1522,14 @@ void WebContentView::review_web_compat_report(WebCompatReportCapture capture)
column_layout->addWidget(review);
review->on_select_screenshot = [this, review = QPointer { review }, frame = AK::move(capture.frame)] {
- if (frame.isNull())
+ if (frame.isNull() || m_screenshot_selection_overlay)
return;
- select_screenshot(frame, [review](QImage screenshot) {
+ select_screenshot(frame).on_finished = [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)
@@ -1569,21 +1568,17 @@ 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.
-void WebContentView::select_screenshot(QImage const& frame, Function<void(QImage)> on_selected)
+ScreenshotSelectionOverlay& WebContentView::select_screenshot(QImage const& frame)
{
- if (m_screenshot_selection_overlay)
- return;
+ 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_finished = [this, overlay, focus_to_restore, on_selected = AK::move(on_selected)](QImage screenshot) {
- close_transient_overlay(*overlay, focus_to_restore);
- on_selected(AK::move(screenshot));
- };
- overlay->on_canceled = [this, overlay, focus_to_restore] {
+ overlay->on_close = [this, overlay, focus_to_restore] {
close_transient_overlay(*overlay, focus_to_restore);
};
+ return *overlay;
}
// An overlay shown for a moment, such as to select or view a screenshot, hands the focus back once it is done.
diff --git a/UI/Qt/WebContentView.h b/UI/Qt/WebContentView.h
index f150a5946fc..3ea1d227c06 100644
--- a/UI/Qt/WebContentView.h
+++ b/UI/Qt/WebContentView.h
@@ -237,7 +237,7 @@ private:
void show_crash_report_review(Optional<ByteString> const& hidden_crash_report_name = {});
void start_web_compat_report();
- void select_screenshot(QImage const& frame, Function<void(QImage)> on_selected);
+ ScreenshotSelectionOverlay& select_screenshot(QImage const& frame);
QPointer<QWidget> show_transient_overlay(QWidget&);
void close_transient_overlay(QWidget&, QPointer<QWidget> const& focus_to_restore);