Skip to content

Truly independent web browser

UI/Qt: Fix the all-tabs list's highlights and keep it up to date

Andreas Kling82ef6c558aac
diff --git a/UI/Qt/AllTabsPopover.cpp b/UI/Qt/AllTabsPopover.cpp
index a0e6cff47ab..07eb29ff0bf 100644
--- a/UI/Qt/AllTabsPopover.cpp
+++ b/UI/Qt/AllTabsPopover.cpp
@@ -50,6 +50,7 @@ public:
, m_highlight(highlight)
{
setMouseTracking(true);
+ setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
}
Function<void(size_t)> on_activate;
@@ -70,18 +71,42 @@ public:
}
// The current tab starts out highlighted, and otherwise the first match.
- m_highlighted_row = m_rows.is_empty() || m_highlight == Highlight::Pointer ? -1 : 0;
- if (query.isEmpty()) {
+ m_keyboard_row = m_rows.is_empty() || m_highlight == Highlight::Pointer ? -1 : 0;
+ if (query.isEmpty() && m_highlight == Highlight::Keyboard) {
for (size_t row = 0; row < m_rows.size(); ++row) {
if (m_entries[m_rows[row]].is_current)
- m_highlighted_row = static_cast<int>(row);
+ m_keyboard_row = static_cast<int>(row);
}
}
- setFixedSize(POPOVER_WIDTH - 12, static_cast<int>(m_rows.size()) * ROW_HEIGHT);
+ m_highlighted_row = m_keyboard_row;
+ // NB: The list is as wide as the room it is given, so its rows' rounded ends both show.
+ setFixedHeight(static_cast<int>(m_rows.size()) * ROW_HEIGHT);
update();
}
int highlighted_row() const { return m_highlighted_row; }
+
+ QObject* keyboard_tab() const
+ {
+ if (m_keyboard_row < 0)
+ return nullptr;
+ return m_entries[m_rows[m_keyboard_row]].tab;
+ }
+
+ bool set_keyboard_tab(QObject* tab)
+ {
+ if (!tab)
+ return false;
+ for (size_t row = 0; row < m_rows.size(); ++row) {
+ if (m_entries[m_rows[row]].tab == tab) {
+ m_keyboard_row = static_cast<int>(row);
+ m_highlighted_row = m_keyboard_row;
+ update();
+ return true;
+ }
+ }
+ return false;
+ }
QRect row_rect(int row) const { return { 0, row * ROW_HEIGHT, width(), ROW_HEIGHT }; }
size_t row_count() const { return m_rows.size(); }
@@ -90,6 +115,7 @@ public:
if (m_rows.is_empty())
return;
m_highlighted_row = clamp(m_highlighted_row + delta, 0, static_cast<int>(m_rows.size()) - 1);
+ m_keyboard_row = m_highlighted_row;
update();
}
@@ -161,10 +187,11 @@ private:
}
}
+ // Once the pointer leaves, the highlight goes back to the row the keyboard had.
virtual void leaveEvent(QEvent*) override
{
- if (m_highlight == Highlight::Pointer && m_highlighted_row >= 0) {
- m_highlighted_row = -1;
+ if (m_highlighted_row != m_keyboard_row) {
+ m_highlighted_row = m_keyboard_row;
update();
}
}
@@ -181,6 +208,9 @@ private:
Vector<AllTabsPopover::Entry> m_entries;
Vector<size_t> m_rows;
int m_highlighted_row { -1 };
+ // The row highlighted for the keyboard, which the pointer's highlight only stands in for while the pointer is over
+ // the list.
+ int m_keyboard_row { -1 };
};
AllTabsPopover::AllTabsPopover(QWidget* parent)
@@ -218,6 +248,7 @@ AllTabsPopover::AllTabsPopover(QWidget* parent)
m_scroll_area->setFrameShape(QFrame::NoFrame);
m_scroll_area->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
m_scroll_area->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
+ m_scroll_area->setWidgetResizable(true);
m_scroll_area->setWidget(m_tab_list);
// NB: QScrollArea fills the background of the widget it scrolls, but the list sits on the card.
m_tab_list->setAutoFillBackground(false);
@@ -256,15 +287,24 @@ AllTabsPopover::AllTabsPopover(QWidget* parent)
void AllTabsPopover::set_recently_closed(Vector<Entry> entries)
{
m_recently_closed_list->set_entries(AK::move(entries));
- update_search();
+ // NB: Only this list is filtered again, so that the open tabs keep the row picked with the keyboard.
+ filter_recently_closed(m_search_field->text().trimmed());
+ card().adjustSize();
+ adjustSize();
}
void AllTabsPopover::set_tabs(Vector<Entry> entries)
{
m_search_field->setPlaceholderText(QString("Search %1 tabs").arg(entries.size()));
+ // NB: The search and the tab picked with the keyboard stay as they were while the list is filled again for tabs that
+ // open, close or change behind it. If that tab is gone, the highlight starts over.
+ QPointer<QObject> keyboard_tab = isVisible() ? m_tab_list->keyboard_tab() : nullptr;
m_tab_list->set_entries(AK::move(entries));
- m_search_field->clear();
+ if (!isVisible())
+ m_search_field->clear();
update_search();
+ if (m_tab_list->set_keyboard_tab(keyboard_tab))
+ m_scroll_area->ensureVisible(0, m_tab_list->row_rect(m_tab_list->highlighted_row()).center().y(), 0, ROW_HEIGHT);
}
void AllTabsPopover::update_search()
@@ -272,14 +312,19 @@ void AllTabsPopover::update_search()
auto query = m_search_field->text().trimmed();
m_tab_list->filter(query);
m_scroll_area->setFixedHeight(min(LIST_MAX_HEIGHT, m_tab_list->height()));
- m_recently_closed_list->filter(query);
- m_recently_closed_section->setVisible(m_recently_closed_list->row_count() > 0);
+ filter_recently_closed(query);
if (auto row = m_tab_list->highlighted_row(); row >= 0)
m_scroll_area->ensureVisible(0, m_tab_list->row_rect(row).center().y(), 0, ROW_HEIGHT);
card().adjustSize();
adjustSize();
}
+void AllTabsPopover::filter_recently_closed(QString const& query)
+{
+ m_recently_closed_list->filter(query);
+ m_recently_closed_section->setVisible(m_recently_closed_list->row_count() > 0);
+}
+
void AllTabsPopover::showEvent(QShowEvent* event)
{
Popover::showEvent(event);
diff --git a/UI/Qt/AllTabsPopover.h b/UI/Qt/AllTabsPopover.h
index 9e881e78ee7..de26d37ed62 100644
--- a/UI/Qt/AllTabsPopover.h
+++ b/UI/Qt/AllTabsPopover.h
@@ -11,6 +11,7 @@
#include <UI/Qt/Popover.h>
#include <QIcon>
+#include <QPointer>
class QLineEdit;
class QScrollArea;
@@ -31,6 +32,8 @@ public:
QString title;
QString detail;
bool is_current { false };
+ // The tab the entry stands for, so the row picked with the keyboard stays on it as tabs open and close.
+ QPointer<QObject> tab;
};
void set_tabs(Vector<Entry>);
void set_recently_closed(Vector<Entry>);
@@ -44,6 +47,7 @@ private:
virtual bool eventFilter(QObject*, QEvent*) override;
void update_search();
+ void filter_recently_closed(QString const& query);
QLineEdit* m_search_field { nullptr };
QScrollArea* m_scroll_area { nullptr };
diff --git a/UI/Qt/TabBar.cpp b/UI/Qt/TabBar.cpp
index 1f481d086a7..f0545542ebb 100644
--- a/UI/Qt/TabBar.cpp
+++ b/UI/Qt/TabBar.cpp
@@ -3326,6 +3326,11 @@ TabWidget::TabWidget(WebView::IsPrivate is_private, QWidget* parent)
m_toolbar_container->setCurrentIndex(index);
emit current_tab_changed(index);
+
+ // The all-tabs list closes as another tab is selected, such as a new tab opened in front of it. Tabs opened or
+ // closed behind it only change its rows.
+ if (m_all_tabs_popover && m_all_tabs_popover->isVisible())
+ m_all_tabs_popover->close();
place_collapsed_sidebar_buttons();
update_vertical_tabs_content_separator();
update_vertical_tabs_overlay_geometry();
@@ -3393,6 +3398,8 @@ static void size_page_like(Tab& page, Tab const& current)
void TabWidget::insert_tab(int index, Tab* widget, QString const& label)
{
+ update_all_tabs_entries_soon();
+
if (widget->is_loaded()) {
// A page inserted behind the current one stays at Qt's default geometry until it's selected, and a load can
// start into it long before that. Size its view like the current page's view first, so the page's first layout
@@ -3439,6 +3446,8 @@ Tab* TabWidget::take_tab(int index)
if (!widget)
return nullptr;
+ update_all_tabs_entries_soon();
+
// NB: Beside the collapsed sidebar, the Ladybird and all-tabs buttons and the sidebar's toggle sit in the current
// tab's toolbar, which the tab owns. They belong to this widget, so they don't go along with a tab that leaves
// it, or is deleted.
@@ -4277,6 +4286,46 @@ void TabWidget::show_all_tabs()
};
}
+ update_all_tabs_entries();
+
+ // The list hangs from the button that opens it, toward the middle of the window.
+ auto button_center = m_all_tabs_button->mapTo(window(), m_all_tabs_button->rect().center());
+ auto is_in_right_half = button_center.x() > window()->width() / 2;
+ move_popover_below(*m_all_tabs_popover, *m_all_tabs_button, is_in_right_half ? PopoverAlignment::Trailing : PopoverAlignment::Leading);
+ m_all_tabs_popover->show();
+}
+
+void TabWidget::set_tab_text(int index, QString const& text)
+{
+ m_tab_bar->setTabText(index, text);
+ update_all_tabs_entries_soon();
+}
+
+void TabWidget::set_tab_icon(int index, QIcon const& icon)
+{
+ m_tab_bar->setTabIcon(index, icon);
+ update_all_tabs_entries_soon();
+}
+
+// The open all-tabs list shows the tabs as they are, so it is filled again as tabs open, close or change behind it.
+// NB: It waits for the turn to end, so that a tab being inserted or taken out is in its new place, and several changes
+// fill it once.
+void TabWidget::update_all_tabs_entries_soon()
+{
+ if (!m_all_tabs_popover || !m_all_tabs_popover->isVisible() || m_all_tabs_entries_update_is_pending)
+ return;
+ m_all_tabs_entries_update_is_pending = true;
+ Core::deferred_invoke([self = QPointer(this)] {
+ if (!self)
+ return;
+ self->m_all_tabs_entries_update_is_pending = false;
+ if (self->m_all_tabs_popover->isVisible())
+ self->update_all_tabs_entries();
+ });
+}
+
+void TabWidget::update_all_tabs_entries()
+{
Vector<AllTabsPopover::Entry> entries;
entries.ensure_capacity(count());
for (int index = 0; index < count(); ++index) {
@@ -4287,6 +4336,7 @@ void TabWidget::show_all_tabs()
.title = tab->title(),
.detail = url.host().has_value() ? qstring_from_ak_string(url.serialized_host()) : QString {},
.is_current = index == current_index(),
+ .tab = tab,
});
}
m_all_tabs_popover->set_tabs(AK::move(entries));
@@ -4306,17 +4356,12 @@ void TabWidget::show_all_tabs()
.title = AK::move(title),
.detail = time_since_closed(now - unit.closed_at),
.is_current = false,
+ .tab = {},
});
m_recently_closed_unit_ids.append(unit.id);
}
}
m_all_tabs_popover->set_recently_closed(AK::move(closed_entries));
-
- // The list hangs from the button that opens it, toward the middle of the window.
- auto button_center = m_all_tabs_button->mapTo(window(), m_all_tabs_button->rect().center());
- auto is_in_right_half = button_center.x() > window()->width() / 2;
- move_popover_below(*m_all_tabs_popover, *m_all_tabs_button, is_in_right_half ? PopoverAlignment::Trailing : PopoverAlignment::Leading);
- m_all_tabs_popover->show();
}
void TabWidget::place_collapsed_sidebar_buttons()
diff --git a/UI/Qt/TabBar.h b/UI/Qt/TabBar.h
index 75d39a96ddc..c3d3cf8d579 100644
--- a/UI/Qt/TabBar.h
+++ b/UI/Qt/TabBar.h
@@ -311,9 +311,9 @@ public:
int index_of(Tab* widget) const;
- void set_tab_text(int index, QString const& text) { m_tab_bar->setTabText(index, text); }
+ void set_tab_text(int index, QString const& text);
void set_tab_tooltip(int index, QString const& tip) { m_tab_bar->setTabToolTip(index, tip); }
- void set_tab_icon(int index, QIcon const& icon) { m_tab_bar->setTabIcon(index, icon); }
+ void set_tab_icon(int index, QIcon const& icon);
void set_tab_bar_visible(bool visible);
void set_new_tab_action(QAction* action);
@@ -335,6 +335,8 @@ public:
void place_horizontal_new_tab_button();
int horizontal_tab_strip_drag_space() const;
void show_all_tabs();
+ void update_all_tabs_entries();
+ void update_all_tabs_entries_soon();
signals:
void current_tab_changed(int index);
@@ -409,6 +411,7 @@ private:
QToolButton* m_new_tab_button { nullptr };
QToolButton* m_all_tabs_button { nullptr };
AllTabsPopover* m_all_tabs_popover { nullptr };
+ bool m_all_tabs_entries_update_is_pending { false };
Vector<WebView::SessionWindowId> m_recently_closed_unit_ids;
LadybirdButton* m_ladybird_button { nullptr };
// The Ladybird button and the all-tabs button after it, which move together.