Skip to content

Commit c049cb7

Browse files
committed
UI/Qt: Request closure instead of always force closing tabs
1 parent b185bf5 commit c049cb7

File tree

4 files changed

+40
-14
lines changed

4 files changed

+40
-14
lines changed

UI/Qt/BrowserWindow.cpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,8 @@ BrowserWindow::BrowserWindow(Vector<URL::URL> const& initial_urls, IsPopupWindow
237237

238238
set_current_tab(tab);
239239
});
240-
QObject::connect(m_tabs_container, &QTabWidget::tabCloseRequested, this, &BrowserWindow::close_tab);
241-
QObject::connect(close_current_tab_action, &QAction::triggered, this, &BrowserWindow::close_current_tab);
240+
QObject::connect(m_tabs_container, &QTabWidget::tabCloseRequested, this, &BrowserWindow::request_to_close_tab);
241+
QObject::connect(close_current_tab_action, &QAction::triggered, this, &BrowserWindow::request_to_close_current_tab);
242242

243243
for (int i = 0; i <= 7; ++i) {
244244
new QShortcut(QKeySequence(Qt::CTRL | static_cast<Qt::Key>(Qt::Key_1 + i)), this, [this, i] {
@@ -378,7 +378,7 @@ void BrowserWindow::activate_tab(int index)
378378
m_tabs_container->setCurrentIndex(index);
379379
}
380380

381-
void BrowserWindow::close_tab(int index)
381+
void BrowserWindow::definitely_close_tab(int index)
382382
{
383383
auto* tab = m_tabs_container->widget(index);
384384
m_tabs_container->removeTab(index);
@@ -398,9 +398,15 @@ void BrowserWindow::open_file()
398398
m_current_tab->open_file();
399399
}
400400

401-
void BrowserWindow::close_current_tab()
401+
void BrowserWindow::request_to_close_tab(int index)
402402
{
403-
close_tab(m_tabs_container->currentIndex());
403+
auto* tab = as<Tab>(m_tabs_container->widget(index));
404+
tab->request_close();
405+
}
406+
407+
void BrowserWindow::request_to_close_current_tab()
408+
{
409+
request_to_close_tab(m_tabs_container->currentIndex());
404410
}
405411

406412
int BrowserWindow::tab_index(Tab* tab)
@@ -452,7 +458,7 @@ void BrowserWindow::create_close_button_for_tab(Tab* tab)
452458

453459
connect(button, &QPushButton::clicked, this, [this, tab]() {
454460
auto index = m_tabs_container->indexOf(tab);
455-
close_tab(index);
461+
request_to_close_tab(index);
456462
});
457463

458464
m_tabs_container->tabBar()->setTabButton(index, position, button);
@@ -625,7 +631,7 @@ bool BrowserWindow::eventFilter(QObject* obj, QEvent* event)
625631
if (obj == m_tabs_container) {
626632
auto const tab_index = m_tabs_container->tabBar()->tabAt(mouse_event->pos());
627633
if (tab_index != -1) {
628-
close_tab(tab_index);
634+
request_to_close_tab(tab_index);
629635
return true;
630636
}
631637
}

UI/Qt/BrowserWindow.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,10 @@ public slots:
6363
Tab& new_tab_from_url(URL::URL const&, Web::HTML::ActivateTab);
6464
Tab& new_child_tab(Web::HTML::ActivateTab, Tab& parent, Optional<u64> page_index);
6565
void activate_tab(int index);
66-
void close_tab(int index);
66+
void definitely_close_tab(int index);
6767
void move_tab(int old_index, int new_index);
68-
void close_current_tab();
68+
void request_to_close_tab(int index);
69+
void request_to_close_current_tab();
6970
void open_next_tab();
7071
void open_previous_tab();
7172
void open_file();

UI/Qt/Tab.cpp

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ Tab::Tab(BrowserWindow* window, RefPtr<WebView::WebContentClient> parent_client,
115115
};
116116

117117
view().on_close = [this] {
118-
m_window->close_tab(tab_index());
118+
m_window->definitely_close_tab(tab_index());
119119
};
120120

121121
view().on_link_hover = [this](auto const& url) {
@@ -374,20 +374,20 @@ Tab::Tab(BrowserWindow* window, RefPtr<WebView::WebContentClient> parent_client,
374374

375375
auto* close_tab_action = new QAction("&Close Tab", this);
376376
QObject::connect(close_tab_action, &QAction::triggered, this, [this]() {
377-
view().on_close();
377+
request_close();
378378
});
379379

380380
auto* close_tabs_to_left_action = new QAction("C&lose Tabs to Left", this);
381381
QObject::connect(close_tabs_to_left_action, &QAction::triggered, this, [this]() {
382382
for (auto i = tab_index() - 1; i >= 0; i--) {
383-
m_window->close_tab(i);
383+
m_window->request_to_close_tab(i);
384384
}
385385
});
386386

387387
auto* close_tabs_to_right_action = new QAction("Close Tabs to R&ight", this);
388388
QObject::connect(close_tabs_to_right_action, &QAction::triggered, this, [this]() {
389389
for (auto i = m_window->tab_count() - 1; i > tab_index(); i--) {
390-
m_window->close_tab(i);
390+
m_window->request_to_close_tab(i);
391391
}
392392
});
393393

@@ -397,7 +397,7 @@ Tab::Tab(BrowserWindow* window, RefPtr<WebView::WebContentClient> parent_client,
397397
if (i == tab_index())
398398
continue;
399399

400-
m_window->close_tab(i);
400+
m_window->request_to_close_tab(i);
401401
}
402402
});
403403

@@ -513,4 +513,19 @@ void Tab::find_next()
513513
m_find_in_page->find_next();
514514
}
515515

516+
void Tab::request_close()
517+
{
518+
// Prevent closing on first request so WebContent can cleanly shutdown (e.g. asking if the user is sure they want
519+
// to leave, closing WebSocket connections, etc.)
520+
if (!m_already_requested_close) {
521+
m_already_requested_close = true;
522+
view().request_close();
523+
return;
524+
}
525+
526+
// If the user has already requested a close, then respect the user's request and just close the tab.
527+
// For example, the WebContent process may not be responding.
528+
m_window->definitely_close_tab(tab_index());
529+
}
530+
516531
}

UI/Qt/Tab.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ class Tab final : public QWidget {
6161
void find_previous();
6262
void find_next();
6363

64+
void request_close();
65+
6466
QIcon const& favicon() const { return m_favicon; }
6567
QString const& title() const { return m_title; }
6668

@@ -113,6 +115,8 @@ public slots:
113115
QAction* m_reload_action { nullptr };
114116

115117
QPointer<QDialog> m_dialog;
118+
119+
bool m_already_requested_close { false };
116120
};
117121

118122
}

0 commit comments

Comments
 (0)