Truly independent web browser
UI/Qt: Hold permission prompts while their window is inactive
Luke Wildeea911b5bac7c
diff --git a/Tests/UI/Qt/TestPermissionPromptPopover.cpp b/Tests/UI/Qt/TestPermissionPromptPopover.cppindex 1da0e85d47a..79b167cb64d 100644--- a/Tests/UI/Qt/TestPermissionPromptPopover.cpp+++ b/Tests/UI/Qt/TestPermissionPromptPopover.cpp@@ -187,6 +187,25 @@ TEST_CASE(suspending_hides_without_an_answer)EXPECT(recorder.outcomes[0] == PromptOutcome::Ignored);}+TEST_CASE(a_prompt_closed_as_its_window_goes_inactive_waits)+{+ PermissionPromptPopover popover(nullptr, request_for("https://maps.example.com/"sv));+ Recorder recorder;+ recorder.attach(popover);+ popover.show();++ // Qt closes a popup as its window goes inactive, and says so afterwards, which is when the prompt is suspended.+ popover.close();+ popover.suspend();+ let_time_pass();+ EXPECT(recorder.outcomes.is_empty());++ popover.resume();+ EXPECT(popover.isVisible());+ child<QPushButton>(popover, "LadybirdPermissionPromptAllowOnce").click();+ EXPECT(recorder.outcomes == Vector { PromptOutcome::AllowOnce });+}+TEST_CASE(queued_requests_show_one_at_a_time){PermissionPromptQueue queue;diff --git a/UI/Qt/PermissionPromptPopover.cpp b/UI/Qt/PermissionPromptPopover.cppindex d2be5016f68..6d146aa207c 100644--- a/UI/Qt/PermissionPromptPopover.cpp+++ b/UI/Qt/PermissionPromptPopover.cpp@@ -16,6 +16,7 @@#include <QLabel>#include <QPushButton>#include <QShowEvent>+#include <QTimer>#include <QVBoxLayout>namespace Ladybird {@@ -192,9 +193,14 @@ void PermissionPromptPopover::hideEvent(QHideEvent* event){Popover::hideEvent(event);- // A popover hides itself when the user clicks outside it, which dismisses the prompt.- if (!m_suspended)- finish(Web::Permissions::PromptOutcome::Dismissed);+ // A popover hides itself when the user clicks outside it, which dismisses the prompt. Qt also closes a popup as its+ // window goes inactive, before it says so, so the prompt waits a turn to see whether it was suspended instead.+ if (m_suspended || m_finished)+ return;+ QTimer::singleShot(0, this, [this] {+ if (!m_suspended && !isVisible())+ finish(Web::Permissions::PromptOutcome::Dismissed);+ });}void PermissionPromptPopover::finish(Web::Permissions::PromptOutcome outcome)diff --git a/UI/Qt/PermissionPromptPopover.h b/UI/Qt/PermissionPromptPopover.hindex dc02e1ea576..74d8f57281c 100644--- a/UI/Qt/PermissionPromptPopover.h+++ b/UI/Qt/PermissionPromptPopover.h@@ -30,7 +30,7 @@ public:void update_chrome_style(QPalette const&);- // Hides the prompt while its tab isn't the selected one, without answering it.+ // Hides the prompt while its tab isn't shown in the active window, without answering it.void suspend();void resume();diff --git a/UI/Qt/Tab.cpp b/UI/Qt/Tab.cppindex 502c67ea958..47f1cfa08be 100644--- a/UI/Qt/Tab.cpp+++ b/UI/Qt/Tab.cpp@@ -560,9 +560,8 @@ void Tab::initialize(Optional<WebView::CanonicalTraversable&> traversable)};view().on_request_permission = [this](WebView::PermissionPromptRequest const& request, WebView::PermissionPromptCallback on_outcome) {- // A tab opened in the background has never been shown, so no hideEvent has suspended its prompts yet.- if (!isVisible())- m_permission_prompts.suspend();+ // A tab opened in the background, or in a window that was never active, has had nothing hold its prompts yet.+ update_permission_prompts();m_permission_prompts.enqueue(request, AK::move(on_outcome));};@@ -1241,15 +1240,23 @@ void Tab::hideEvent(QHideEvent* event)return;hide_hover_label();-- // A tab's permission prompt waits, unanswered, while another tab is shown.- m_permission_prompts.suspend();+ update_permission_prompts();}void Tab::showEvent(QShowEvent* event){QWidget::showEvent(event);- m_permission_prompts.resume();+ update_permission_prompts();+}++// A tab's permission prompt waits, unanswered, while another tab is shown or another window is active. A popup that+// comes up over an inactive window is closed by the next click or activation anywhere else.+void Tab::update_permission_prompts()+{+ if (isVisible() && isActiveWindow())+ m_permission_prompts.resume();+ else+ m_permission_prompts.suspend();}void Tab::update_hover_label()@@ -1342,9 +1349,11 @@ QImage Tab::render_hover_text(QString const& text) constbool Tab::event(QEvent* event){- // An inactive window's toolbar icons drop to the muted color.- if ((event->type() == QEvent::WindowActivate || event->type() == QEvent::WindowDeactivate) && is_loaded())+ if ((event->type() == QEvent::WindowActivate || event->type() == QEvent::WindowDeactivate) && is_loaded()) {+ // An inactive window's toolbar icons drop to the muted color.recreate_location_edit_icons();+ update_permission_prompts();+ }if (event->type() == QEvent::PaletteChange && is_loaded()) {update_chrome_style();diff --git a/UI/Qt/Tab.h b/UI/Qt/Tab.hindex 96e143d56de..e20d96d0996 100644--- a/UI/Qt/Tab.h+++ b/UI/Qt/Tab.h@@ -214,6 +214,7 @@ private:void leave_report_review();void position_private_session_popover();QWidget& site_settings_anchor() const;+ void update_permission_prompts();void hide_hover_label();QImage render_hover_text(QString const&) const;void set_loading(bool);