Skip to content

Truly independent web browser

UI/Qt: Prepare tabs for window close preflights

Sam Atkins84b487e6debf
diff --git a/Tests/UI/Qt/TestJavaScriptDialog.cpp b/Tests/UI/Qt/TestJavaScriptDialog.cpp
index ed077f74a20..a33e90f229b 100644
--- a/Tests/UI/Qt/TestJavaScriptDialog.cpp
+++ b/Tests/UI/Qt/TestJavaScriptDialog.cpp
@@ -195,6 +195,7 @@ TEST_CASE(beforeunload_uses_fixed_copy_and_navigation_button_roles)
record_completions(dialog, completion);
dialog.show_before_unload("https://example.com");
+ EXPECT(dialog.type() == Ladybird::JavaScriptDialog::Type::BeforeUnload);
QApplication::processEvents();
auto* title_label = dialog.findChild<QLabel*>("LadybirdJavaScriptDialogTitle");
auto* message_label = dialog.findChild<QLabel*>("LadybirdJavaScriptDialogMessage");
@@ -218,11 +219,13 @@ TEST_CASE(beforeunload_uses_fixed_copy_and_navigation_button_roles)
EXPECT(message_area->viewport()->height() >= message_label->heightForWidth(message_area->viewport()->width()));
send_key(*leave_button, Qt::Key_Return);
+ EXPECT(!dialog.type().has_value());
EXPECT(completion.type == Ladybird::JavaScriptDialog::Type::BeforeUnload);
EXPECT(completion.accepted);
completion = {};
dialog.show_before_unload("https://example.com");
+ EXPECT(dialog.type() == Ladybird::JavaScriptDialog::Type::BeforeUnload);
trigger_shortcut(dialog, Qt::Key_Escape);
EXPECT(completion.type == Ladybird::JavaScriptDialog::Type::BeforeUnload);
EXPECT(!completion.accepted);
diff --git a/UI/Qt/JavaScriptDialog.h b/UI/Qt/JavaScriptDialog.h
index d5a46bd3927..9714e48ff1f 100644
--- a/UI/Qt/JavaScriptDialog.h
+++ b/UI/Qt/JavaScriptDialog.h
@@ -35,6 +35,7 @@ public:
explicit JavaScriptDialog(QWidget* parent);
bool is_open() const { return m_type.has_value(); }
+ Optional<Type> type() const { return m_type; }
void show_alert(QString const& title, QString const& message);
void show_before_unload(QString const& title);
diff --git a/UI/Qt/Tab.cpp b/UI/Qt/Tab.cpp
index e56de8bc553..bc8f7ca8b76 100644
--- a/UI/Qt/Tab.cpp
+++ b/UI/Qt/Tab.cpp
@@ -1788,4 +1788,39 @@ void Tab::request_close()
m_window->definitely_close_tab(tab_index());
}
+void Tab::request_window_close_preflight(Function<void(bool)> on_complete)
+{
+ m_suppress_javascript_dialogs_until_navigation = true;
+
+ if (m_javascript_dialog->type() != JavaScriptDialog::Type::BeforeUnload)
+ m_javascript_dialog->dismiss();
+
+ if (!view().needs_beforeunload_check()) {
+ on_complete(true);
+ return;
+ }
+
+ auto weak_this = QPointer<Tab>(this);
+ view().request_close_preflight([weak_this, on_complete = AK::move(on_complete)](bool approved) mutable {
+ if (!weak_this) {
+ on_complete(true);
+ return;
+ }
+ on_complete(approved);
+ });
+}
+
+void Tab::cancel_window_close_preflight()
+{
+ m_already_requested_close = false;
+ m_suppress_javascript_dialogs_until_navigation = false;
+}
+
+Function<void()> Tab::prepare_for_window_close()
+{
+ m_suppress_javascript_dialogs_until_navigation = true;
+ m_javascript_dialog->dismiss();
+ return view().prepare_for_close_without_prompting();
+}
+
}
diff --git a/UI/Qt/Tab.h b/UI/Qt/Tab.h
index 2219572748a..2eefa631b4d 100644
--- a/UI/Qt/Tab.h
+++ b/UI/Qt/Tab.h
@@ -96,6 +96,9 @@ public:
BookmarksBar& bookmarks_bar() { return *m_bookmarks_bar; }
void request_close();
+ void request_window_close_preflight(Function<void(bool)> on_complete);
+ void cancel_window_close_preflight();
+ Function<void()> prepare_for_window_close();
QIcon const& favicon() const { return m_favicon; }
QIcon tab_icon() const;