Skip to content

Truly independent web browser

UI/Qt: Add tab pinning

sideshowbarkerc9f20020c92a
diff --git a/UI/Qt/Application.cpp b/UI/Qt/Application.cpp
index 2d719c75b0f..e4f4164a8dd 100644
--- a/UI/Qt/Application.cpp
+++ b/UI/Qt/Application.cpp
@@ -319,8 +319,18 @@ public:
m_close_current_tab_action = make_action("&Close Current Tab");
m_close_current_tab_action->setShortcuts(QKeySequence::keyBindings(QKeySequence::StandardKey::Close));
QObject::connect(m_close_current_tab_action, &QAction::triggered, this, []() {
- if (auto* tab = Application::the().active_tab())
- tab->request_close();
+ auto* tab = Application::the().active_tab();
+ if (!tab)
+ return;
+
+ // The keyboard shortcut never closes a pinned tab: It selects the first unpinned tab instead. Choosing the
+ // menu item still closes the tab.
+ auto from_keyboard = QGuiApplication::keyboardModifiers() & (Qt::ControlModifier | Qt::MetaModifier);
+ if (tab->is_pinned() && from_keyboard) {
+ Application::the().active_window_if_any()->activate_first_unpinned_tab();
+ return;
+ }
+ tab->request_close();
});
m_open_next_tab_action = make_action("Open &Next Tab");
@@ -873,7 +883,7 @@ void Application::reopen_recently_closed_tab(BrowserWindow* target_window)
if (restoring_to_source_window && unit.source_tab_index.has_value())
next_insertion_index = static_cast<int>(clamp(*unit.source_tab_index, 0, static_cast<i64>(window->tab_count())));
- Optional<int> tab_index_to_activate;
+ QPointer<Tab> tab_to_activate;
i64 restored_tab_index = 0;
for (auto& closed_tab : unit.tabs) {
auto location = next_insertion_index.has_value() ? BrowserWindow::TabLocation::at_index(*next_insertion_index) : BrowserWindow::TabLocation::end();
@@ -882,15 +892,26 @@ void Application::reopen_recently_closed_tab(BrowserWindow* target_window)
++*next_insertion_index;
if (restored_tab_index == unit.active_tab_index)
- tab_index_to_activate = window->tab_index(&tab);
+ tab_to_activate = &tab;
if (!closed_tab.history.has_value() || tab.view().restore_session_history_from_snapshot(closed_tab.history.release_value()).is_error())
tab.navigate(closed_tab.active_url);
+
+ if (closed_tab.pinned) {
+ // Back in its own window, a pinned tab goes back to its place in the pinned group.
+ Optional<int> pinned_index;
+ if (restoring_to_source_window && unit.source_tab_index.has_value())
+ pinned_index = static_cast<int>(*unit.source_tab_index);
+ window->pin_tab(tab, pinned_index);
+ }
++restored_tab_index;
}
- if (tab_index_to_activate.has_value())
- window->activate_tab(*tab_index_to_activate);
+ // Pinning a reopened tab can move it, so its index is resolved only now.
+ if (tab_to_activate) {
+ if (auto index = window->tab_index(tab_to_activate); index >= 0)
+ window->activate_tab(index);
+ }
update_reopen_recently_closed_actions();
}
diff --git a/UI/Qt/BrowserWindow.cpp b/UI/Qt/BrowserWindow.cpp
index 9413116f1e2..39d8dc858be 100644
--- a/UI/Qt/BrowserWindow.cpp
+++ b/UI/Qt/BrowserWindow.cpp
@@ -589,27 +589,25 @@ Tab& BrowserWindow::create_new_tab(Web::HTML::ActivateTab activate_tab, TabLocat
set_current_tab(tab);
}
+ auto insertion_index = m_tabs_container->count();
switch (location.kind()) {
case TabLocation::Kind::End:
- m_tabs_container->add_tab(tab, "New Tab");
break;
case TabLocation::Kind::AfterCurrentTab:
- m_tabs_container->insert_tab(m_tabs_container->current_index() + 1, tab, "New Tab");
+ insertion_index = m_tabs_container->current_index() + 1;
break;
- case TabLocation::Kind::AfterTab: {
- auto insertion_index = m_tabs_container->count();
+ case TabLocation::Kind::AfterTab:
if (auto* source_tab = location.tab(); source_tab) {
- auto source_tab_index = tab_index(source_tab);
- if (source_tab_index != -1)
+ if (auto source_tab_index = tab_index(source_tab); source_tab_index != -1)
insertion_index = source_tab_index + 1;
}
- m_tabs_container->insert_tab(insertion_index, tab, "New Tab");
break;
- }
case TabLocation::Kind::AtIndex:
- m_tabs_container->insert_tab(clamp(location.index(), 0, m_tabs_container->count()), tab, "New Tab");
+ insertion_index = location.index();
break;
}
+ // A new tab is unpinned, so it never lands inside the pinned group.
+ m_tabs_container->insert_tab(constrain_insertion_index(insertion_index, false), tab, "New Tab");
if (activate_tab == Web::HTML::ActivateTab::Yes)
m_tabs_container->set_current_tab(tab);
@@ -688,7 +686,8 @@ void BrowserWindow::adopt_tab(Tab& tab, int index)
{
cancel_window_close_preflight();
- index = clamp(index, 0, m_tabs_container->count());
+ // A tab keeps its pin in its new window, inside that window's matching group.
+ index = constrain_insertion_index(index, tab.is_pinned());
tab.set_window(*this);
m_tabs_container->insert_tab(index, &tab, "New Tab");
@@ -846,7 +845,74 @@ bool BrowserWindow::definitely_close_tab(int index)
void BrowserWindow::move_tab(int old_index, int new_index)
{
- m_tabs_container->tab_bar()->moveTab(old_index, new_index);
+ auto* tab = m_tabs_container->tab(old_index);
+ if (!tab)
+ return;
+
+ auto pinned_count = pinned_tab_count();
+ if (tab->is_pinned())
+ new_index = clamp(new_index, 0, pinned_count - 1);
+ else
+ new_index = clamp(new_index, pinned_count, m_tabs_container->count() - 1);
+ if (new_index != old_index)
+ m_tabs_container->tab_bar()->moveTab(old_index, new_index);
+}
+
+int BrowserWindow::pinned_tab_count() const
+{
+ return m_tabs_container->tab_bar()->pinned_tab_count();
+}
+
+int BrowserWindow::constrain_insertion_index(int index, bool pinned) const
+{
+ auto pinned_count = pinned_tab_count();
+ if (pinned)
+ return clamp(index, 0, pinned_count);
+ return clamp(index, pinned_count, m_tabs_container->count());
+}
+
+void BrowserWindow::pin_tab(Tab& tab, Optional<int> pinned_index)
+{
+ auto index = tab_index(&tab);
+ if (index < 0 || tab.is_pinned())
+ return;
+
+ auto pinned_count = pinned_tab_count();
+ auto destination = clamp(pinned_index.value_or(pinned_count), 0, pinned_count);
+ tab.set_pinned(true);
+ if (index != destination)
+ m_tabs_container->tab_bar()->moveTab(index, destination);
+ pinned_state_changed(tab);
+}
+
+void BrowserWindow::unpin_tab(Tab& tab)
+{
+ auto index = tab_index(&tab);
+ if (index < 0 || !tab.is_pinned())
+ return;
+
+ auto destination = pinned_tab_count() - 1;
+ tab.set_pinned(false);
+ if (index != destination)
+ m_tabs_container->tab_bar()->moveTab(index, destination);
+ pinned_state_changed(tab);
+}
+
+void BrowserWindow::pinned_state_changed(Tab& tab)
+{
+ // A move already reported the new order, through the tab bar's tabMoved signal.
+ if (auto session_tab_id = tab.view().session_tab_id(); session_tab_id.has_value()) {
+ if (auto result = m_session->session_store->tab_pinned_changed(*session_tab_id, tab.is_pinned()); result.is_error())
+ dbgln("Unable to record the tab's pin in the session store: {}", result.error());
+ }
+ update_tab_close_button(tab);
+ m_tabs_container->update_tab_layout();
+}
+
+void BrowserWindow::activate_first_unpinned_tab()
+{
+ if (auto first_unpinned = pinned_tab_count(); first_unpinned < m_tabs_container->count())
+ activate_tab(first_unpinned);
}
void BrowserWindow::register_window_with_session_store()
@@ -927,21 +993,41 @@ void BrowserWindow::update_chrome_style()
m_hamburger_menu->setStyleSheet(ChromeStyle::hamburger_style_sheet(palette()));
}
+TabBarButton* BrowserWindow::create_tab_close_button(Tab& tab)
+{
+ auto* close_button = new TabBarButton(create_chrome_icon(ChromeIcon::TabClose, palette()));
+ close_button->setToolTip("Close Tab");
+ connect(close_button, &QPushButton::clicked, this, [this, tab = &tab]() {
+ request_to_close_tab(m_tabs_container->index_of(tab));
+ });
+ return close_button;
+}
+
void BrowserWindow::initialize_tab_buttons(Tab* tab)
{
auto index = m_tabs_container->index_of(tab);
m_tabs_container->set_tab_icon(index, tab->tab_icon());
- auto* close_button = new TabBarButton(create_chrome_icon(ChromeIcon::TabClose, palette()));
- close_button->setToolTip("Close Tab");
+ m_tabs_container->tab_bar()->setTabButton(index, AUDIO_STATE_BUTTON_POSITION, nullptr);
+ // A pinned tab has no close button.
+ m_tabs_container->tab_bar()->setTabButton(index, TAB_CLOSE_BUTTON_POSITION, tab->is_pinned() ? nullptr : create_tab_close_button(*tab));
+ m_tabs_container->update_tab_button_visibility();
+}
- connect(close_button, &QPushButton::clicked, this, [this, tab]() {
- auto index = m_tabs_container->index_of(tab);
- request_to_close_tab(index);
- });
+void BrowserWindow::update_tab_close_button(Tab& tab)
+{
+ auto index = m_tabs_container->index_of(&tab);
+ if (index < 0)
+ return;
- m_tabs_container->tab_bar()->setTabButton(index, AUDIO_STATE_BUTTON_POSITION, nullptr);
- m_tabs_container->tab_bar()->setTabButton(index, TAB_CLOSE_BUTTON_POSITION, close_button);
+ auto* tab_bar = m_tabs_container->tab_bar();
+ auto* close_button = tab_bar->tabButton(index, TAB_CLOSE_BUTTON_POSITION);
+ if (tab.is_pinned() && close_button) {
+ tab_bar->setTabButton(index, TAB_CLOSE_BUTTON_POSITION, nullptr);
+ close_button->deleteLater();
+ } else if (!tab.is_pinned() && !close_button) {
+ tab_bar->setTabButton(index, TAB_CLOSE_BUTTON_POSITION, create_tab_close_button(tab));
+ }
m_tabs_container->update_tab_button_visibility();
}
diff --git a/UI/Qt/BrowserWindow.h b/UI/Qt/BrowserWindow.h
index 51e63b73e68..435531ff3c6 100644
--- a/UI/Qt/BrowserWindow.h
+++ b/UI/Qt/BrowserWindow.h
@@ -35,6 +35,7 @@ class QWindow;
namespace Ladybird {
class Tab;
+class TabBarButton;
class WebContentView;
class BrowserWindow;
class DevToolsBanner;
@@ -148,6 +149,15 @@ public:
int tab_count() { return m_tabs_container->count(); }
int tab_index(Tab*);
+ Tab* tab_at(int index) const { return m_tabs_container->tab(index); }
+
+ // Pinned tabs always fill the start of the strip: indices [0, pinned_tab_count()).
+ int pinned_tab_count() const;
+ // The tab joins the end of the pinned group, or the place in it that pinned_index gives.
+ void pin_tab(Tab&, Optional<int> pinned_index = {});
+ // The tab joins the start of the unpinned group.
+ void unpin_tab(Tab&);
+ void activate_first_unpinned_tab();
Tab& create_new_tab(Web::HTML::ActivateTab activate_tab, TabLocation);
void duplicate_tab(Tab&);
@@ -250,6 +260,11 @@ private:
void update_chrome_style();
+ int constrain_insertion_index(int index, bool pinned) const;
+ void pinned_state_changed(Tab&);
+ TabBarButton* create_tab_close_button(Tab&);
+ void update_tab_close_button(Tab&);
+
void initialize_tab_buttons(Tab*);
void create_menu_bar_window_controls();
void update_tab_button_icons();
diff --git a/UI/Qt/Tab.cpp b/UI/Qt/Tab.cpp
index 68d473613e5..d7466779cd4 100644
--- a/UI/Qt/Tab.cpp
+++ b/UI/Qt/Tab.cpp
@@ -1347,6 +1347,14 @@ Tab::Tab(BrowserWindow* window, Optional<WebView::CanonicalTraversable&> travers
m_window->duplicate_tab(*this);
});
+ m_pin_tab_action = new QAction("&Pin Tab", this);
+ QObject::connect(m_pin_tab_action, &QAction::triggered, this, [this]() {
+ if (m_is_pinned)
+ m_window->unpin_tab(*this);
+ else
+ m_window->pin_tab(*this);
+ });
+
auto* move_to_start_action = new QAction("Move to &Start", this);
QObject::connect(move_to_start_action, &QAction::triggered, this, [this]() {
m_window->move_tab(tab_index(), 0);
@@ -1365,21 +1373,23 @@ Tab::Tab(BrowserWindow* window, Optional<WebView::CanonicalTraversable&> travers
auto* close_tabs_to_left_action = new QAction("C&lose Tabs to Left", this);
QObject::connect(close_tabs_to_left_action, &QAction::triggered, this, [this]() {
for (auto i = tab_index() - 1; i >= 0; i--) {
- m_window->request_to_close_tab(i);
+ if (!m_window->tab_at(i)->is_pinned())
+ m_window->request_to_close_tab(i);
}
});
auto* close_tabs_to_right_action = new QAction("Close Tabs to R&ight", this);
QObject::connect(close_tabs_to_right_action, &QAction::triggered, this, [this]() {
for (auto i = m_window->tab_count() - 1; i > tab_index(); i--) {
- m_window->request_to_close_tab(i);
+ if (!m_window->tab_at(i)->is_pinned())
+ m_window->request_to_close_tab(i);
}
});
auto* close_other_tabs_action = new QAction("Cl&ose Other Tabs", this);
QObject::connect(close_other_tabs_action, &QAction::triggered, this, [this]() {
for (auto i = m_window->tab_count() - 1; i >= 0; i--) {
- if (i == tab_index())
+ if (i == tab_index() || m_window->tab_at(i)->is_pinned())
continue;
m_window->request_to_close_tab(i);
@@ -1389,6 +1399,7 @@ Tab::Tab(BrowserWindow* window, Optional<WebView::CanonicalTraversable&> travers
m_context_menu = new QMenu("Context menu", this);
m_context_menu->addAction(create_application_action(*this, application.reload_action(), IncludeActionIcon::No));
m_context_menu->addAction(duplicate_tab_action);
+ m_context_menu->addAction(m_pin_tab_action);
m_context_menu->addSeparator();
auto* move_tab_menu = m_context_menu->addMenu("Mo&ve Tab");
move_tab_menu->addAction(move_to_start_action);
@@ -1435,6 +1446,12 @@ void Tab::set_window(BrowserWindow& window)
m_view->set_window_size({ window.width(), window.height() });
}
+void Tab::set_pinned(bool pinned)
+{
+ m_is_pinned = pinned;
+ m_pin_tab_action->setText(pinned ? "Un&pin Tab" : "&Pin Tab");
+}
+
void Tab::set_vertical_tabs_enabled(bool enabled)
{
m_vertical_tabs_enabled = enabled;
diff --git a/UI/Qt/Tab.h b/UI/Qt/Tab.h
index 193a293ebff..ca97b85e6a2 100644
--- a/UI/Qt/Tab.h
+++ b/UI/Qt/Tab.h
@@ -133,6 +133,10 @@ public:
void enter_report_review(Tab* tab_to_activate_on_close, Optional<QString> title = {});
+ bool is_pinned() const { return m_is_pinned; }
+ // BrowserWindow pins and unpins tabs, since it keeps each window's pinned tabs at the start of its strip.
+ void set_pinned(bool);
+
bool url_is_hidden() const { return m_location_edit->url_is_hidden(); }
void set_url_is_hidden(bool url_is_hidden) { m_location_edit->set_url_is_hidden(url_is_hidden); }
@@ -259,6 +263,7 @@ private:
QAction* m_stop_action { nullptr };
ReloadButton* m_reload_button { nullptr };
QAction* m_open_downloads_page_action { nullptr };
+ QAction* m_pin_tab_action { nullptr };
Optional<ChromeIcon> m_downloads_button_icon;
QString m_downloads_button_tooltip;
@@ -278,6 +283,7 @@ private:
bool m_already_requested_close { false };
bool m_preserve_close_retry_state_on_dialog_dismissal { false };
bool m_window_is_closing { false };
+ bool m_is_pinned { false };
};
}
diff --git a/UI/Qt/TabBar.cpp b/UI/Qt/TabBar.cpp
index a0515fa089b..dd45b9915a1 100644
--- a/UI/Qt/TabBar.cpp
+++ b/UI/Qt/TabBar.cpp
@@ -77,6 +77,9 @@ static constexpr int HORIZONTAL_ACTIVE_TAB_CLOSE_BUTTON_MIN_WIDTH = 68;
// Below this content width, a horizontal tab shows its favicon alone.
static constexpr int HORIZONTAL_TAB_MIN_CONTENT_WIDTH_FOR_TITLE = 48;
static constexpr int HORIZONTAL_TAB_MAX_WIDTH = 232;
+// A pinned tab's tile: wide enough for a square card inside TAB_CARD_SHAPE_HORIZONTAL_INSET and
+// TAB_CARD_SHAPE_VERTICAL_INSET on a HORIZONTAL_TAB_HEIGHT row.
+static constexpr int PINNED_TAB_TILE_WIDTH = 40;
static constexpr int VERTICAL_TAB_HEIGHT = 38;
static constexpr int VERTICAL_TABS_COLLAPSED_WIDTH = browser_chrome_layout_policy().collapsed_sidebar_width;
static constexpr int VERTICAL_TABS_DEFAULT_EXPANDED_WIDTH = browser_chrome_layout_policy().expanded_sidebar_width;
@@ -688,6 +691,22 @@ void TabBar::refresh_tab_layout()
update();
}
+int TabBar::pinned_tab_count() const
+{
+ if (!m_tab_widget)
+ return 0;
+
+ // While a tab is being taken, its page leaves the stack before its tab-bar entry, so a missing page ends the count.
+ int pinned_count = 0;
+ while (pinned_count < count()) {
+ auto* tab = m_tab_widget->tab(pinned_count);
+ if (!tab || !tab->is_pinned())
+ break;
+ ++pinned_count;
+ }
+ return pinned_count;
+}
+
void TabBar::recreate_icons()
{
m_fallback_tab_icon = create_chrome_icon(ChromeIcon::Globe, palette());
@@ -719,7 +738,16 @@ QSize TabBar::tabSizeHint(int index) const
}
if (auto count = this->count(); count > 0) {
- auto width = (m_available_width > 0 ? m_available_width : this->width()) / count;
+ auto pinned_count = pinned_tab_count();
+ if (index < pinned_count) {
+ hint.setWidth(PINNED_TAB_TILE_WIDTH);
+ hint.setHeight(HORIZONTAL_TAB_HEIGHT);
+ return hint;
+ }
+
+ // The unpinned tabs share whatever width the pinned tiles leave.
+ auto available_width = (m_available_width > 0 ? m_available_width : this->width()) - (pinned_count * PINNED_TAB_TILE_WIDTH);
+ auto width = available_width / max(1, count - pinned_count);
width = min(HORIZONTAL_TAB_MAX_WIDTH, width);
width = max(HORIZONTAL_TAB_MIN_WIDTH, width);
@@ -793,6 +821,7 @@ void TabBar::paintEvent(QPaintEvent*)
auto is_vertical = tab_layout() != TabLayout::Horizontal;
auto is_collapsed_vertical = tab_layout() == TabLayout::VerticalCollapsed;
auto dragged_index = dragged_tab_index();
+ auto pinned_count = pinned_tab_count();
// Titles are shaped once and kept for as long as some tab shows them, since resizing the window repaints the tab
// bar on every step without changing any title.
@@ -820,6 +849,8 @@ void TabBar::paintEvent(QPaintEvent*)
bool is_selected = index == currentIndex();
auto hover_progress = index == m_hover_animation_tab_index ? m_hover_progress : (index == m_hovered_tab_index ? 1.0 : 0.0);
bool is_hovered = hover_progress > 0.0;
+ // A pinned tab is a tile that shows only its favicon.
+ auto is_pinned_tile = index < pinned_count && m_tab_layout == TabLayout::Horizontal;
QRectF shape_rect;
if (is_collapsed_vertical)
@@ -861,7 +892,11 @@ void TabBar::paintEvent(QPaintEvent*)
if (icon.isNull())
icon = m_fallback_tab_icon;
- if (is_collapsed_vertical) {
+ if (is_collapsed_vertical || is_pinned_tile) {
+ // The audio button takes the favicon's place on a pinned tile.
+ if (is_pinned_tile && tabButton(index, QTabBar::LeftSide))
+ continue;
+
QRect icon_rect {
tab_rect.center().x() - (TAB_ICON_SIZE / 2) + 1,
tab_rect.center().y() - (TAB_ICON_SIZE / 2) + 1,
@@ -1196,13 +1231,32 @@ Optional<int> TabBar::dragged_tab_index() const
// A tab dragged in from elsewhere needs a gap of its own. Vertical tabs make room by moving the tabs after the gap
// down, while horizontal tabs narrow to fit the gap within the strip's current width.
-qreal TabBar::drop_gap_extent() const
+bool TabBar::dragging_pinned_tab() const
+{
+ return s_active_tab_dragged_tab && s_active_tab_dragged_tab->is_pinned();
+}
+
+// Where the tab at a position sits once a tab dragged in from another window joins the dragged tab's own group. The
+// pinned tiles keep their width, and the unpinned tabs narrow to make room, so the strip keeps its width.
+QRect TabBar::incoming_drop_layout_rect(int position) const
{
- if (tab_layout() != TabLayout::Horizontal)
- return VERTICAL_TAB_HEIGHT;
if (count() == 0)
- return 0;
- return static_cast<qreal>(tabRect(count() - 1).right() + 1 - tabRect(0).left()) / (count() + 1);
+ return {};
+
+ if (tab_layout() != TabLayout::Horizontal) {
+ auto first_tab_rect = visual_tab_rect(0);
+ return { first_tab_rect.left(), first_tab_rect.top() + (position * VERTICAL_TAB_HEIGHT), first_tab_rect.width(), VERTICAL_TAB_HEIGHT };
+ }
+
+ auto pinned_count = pinned_tab_count() + (dragging_pinned_tab() ? 1 : 0);
+ auto unpinned_count = count() + 1 - pinned_count;
+ auto first_tab_rect = tabRect(0);
+ auto strip_width = tabRect(count() - 1).right() + 1 - first_tab_rect.left();
+ auto unpinned_width = unpinned_count > 0 ? max(0.0, static_cast<qreal>(strip_width - (pinned_count * PINNED_TAB_TILE_WIDTH)) / unpinned_count) : 0.0;
+
+ auto left = first_tab_rect.left() + (min(position, pinned_count) * PINNED_TAB_TILE_WIDTH) + (max(0, position - pinned_count) * unpinned_width);
+ auto width = position < pinned_count ? PINNED_TAB_TILE_WIDTH : unpinned_width;
+ return QRectF { left, static_cast<qreal>(first_tab_rect.top()), width, static_cast<qreal>(first_tab_rect.height()) }.toRect();
}
// Returns the index that a tab dropped at the given position ends up at.
@@ -1211,28 +1265,39 @@ int TabBar::drop_slot_at(QPoint const& position) const
if (count() == 0)
return 0;
- auto is_vertical = tab_layout() != TabLayout::Horizontal;
- auto first_tab_rect = visual_tab_rect(0);
- auto strip_start = is_vertical ? first_tab_rect.top() : first_tab_rect.left();
+ // A drag never moves a tab across the boundary between the pinned and unpinned tabs, so only the dragged tab's own
+ // group has slots for it. Of those, the slot is the one whose place is nearest.
+ auto pinned_count = pinned_tab_count();
+ auto dragging_pinned = dragging_pinned_tab();
+ auto nearest_slot = [](int first_slot, int last_slot, QPointF point, auto const& rect_for_slot) {
+ auto best_slot = first_slot;
+ auto best_distance = NumericLimits<qreal>::max();
+ for (auto slot = first_slot; slot <= last_slot; ++slot) {
+ auto offset = QRectF(rect_for_slot(slot)).center() - point;
+ if (auto distance = QPointF::dotProduct(offset, offset); distance < best_distance) {
+ best_distance = distance;
+ best_slot = slot;
+ }
+ }
+ return best_slot;
+ };
if (auto dragged_index = dragged_tab_index(); dragged_index.has_value()) {
auto dragged_tab_rect = visual_tab_rect(*dragged_index);
- auto extent = is_vertical ? dragged_tab_rect.height() : dragged_tab_rect.width();
- if (extent <= 0)
+ if (dragged_tab_rect.isEmpty())
return *dragged_index;
// Follow the center of the dragged tab rather than the cursor, so that a neighbor makes way once the dragged
// tab covers half of it.
- auto dragged_tab_position = position - m_position_in_selected_tab_while_dragging;
- auto center = (is_vertical ? dragged_tab_position.y() : dragged_tab_position.x()) + (extent / 2);
- return clamp((center - strip_start) / extent, 0, count() - 1);
+ auto center = QRectF(QPointF(position - m_position_in_selected_tab_while_dragging), QSizeF(dragged_tab_rect.size())).center();
+ auto first_slot = dragging_pinned ? 0 : pinned_count;
+ auto last_slot = dragging_pinned ? pinned_count - 1 : count() - 1;
+ return nearest_slot(first_slot, last_slot, center, [this](int slot) { return visual_tab_rect(slot); });
}
- auto extent = drop_gap_extent();
- if (extent <= 0)
- return count();
- auto position_in_strip = (is_vertical ? position.y() : position.x()) - strip_start;
- return clamp(static_cast<int>(position_in_strip / extent), 0, count());
+ auto first_slot = dragging_pinned ? 0 : pinned_count;
+ auto last_slot = dragging_pinned ? pinned_count : count();
+ return nearest_slot(first_slot, last_slot, QPointF(position), [this](int slot) { return incoming_drop_layout_rect(slot); });
}
// Returns the index to insert a tab dropped at the given position at. It counts the dragged tab in its old place, as
@@ -1286,31 +1351,30 @@ TabBar::DropPreview TabBar::drop_preview_for_slot(Optional<int> slot) const
if (!slot.has_value())
return preview;
- auto is_vertical = tab_layout() != TabLayout::Horizontal;
+ auto move_to = [&](int index, QRect const& target) {
+ auto tab_rect = visual_tab_rect(index);
+ preview.tab_shifts[index] = {
+ .x_offset = static_cast<qreal>(target.left() - tab_rect.left()),
+ .y_offset = static_cast<qreal>(target.top() - tab_rect.top()),
+ .width_delta = static_cast<qreal>(target.width() - tab_rect.width()),
+ .height_delta = static_cast<qreal>(target.height() - tab_rect.height()),
+ };
+ };
- // The tabs between the dragged tab and its drop slot move over by one to fill the dragged tab's place.
+ // The tabs between the dragged tab and its drop slot move over by one place to fill the dragged tab's place.
if (auto dragged_index = dragged_tab_index(); dragged_index.has_value()) {
- auto dragged_tab_rect = visual_tab_rect(*dragged_index);
- auto extent = is_vertical ? dragged_tab_rect.height() : dragged_tab_rect.width();
for (int index = *dragged_index + 1; index <= *slot; ++index)
- preview.tab_shifts[index].offset = -extent;
+ move_to(index, visual_tab_rect(index - 1));
for (int index = *slot; index < *dragged_index; ++index)
- preview.tab_shifts[index].offset = extent;
+ move_to(index, visual_tab_rect(index + 1));
return preview;
}
- auto gap_extent = drop_gap_extent();
- auto first_tab_rect = visual_tab_rect(0);
- auto strip_start = is_vertical ? first_tab_rect.top() : first_tab_rect.left();
- for (int index = 0; index < count(); ++index) {
- auto tab_rect = visual_tab_rect(index);
- auto slot_index = index < *slot ? index : index + 1;
- auto& shift = preview.tab_shifts[index];
- shift.offset = strip_start + (slot_index * gap_extent) - (is_vertical ? tab_rect.top() : tab_rect.left());
- shift.extent_delta = gap_extent - (is_vertical ? tab_rect.height() : tab_rect.width());
- }
- if (is_vertical)
- preview.strip_growth = gap_extent;
+ // A tab dragged in from another window opens a gap at its slot.
+ for (int index = 0; index < count(); ++index)
+ move_to(index, incoming_drop_layout_rect(index < *slot ? index : index + 1));
+ if (tab_layout() != TabLayout::Horizontal)
+ preview.strip_growth = VERTICAL_TAB_HEIGHT;
return preview;
}
@@ -1333,9 +1397,12 @@ TabBar::TabShift TabBar::current_tab_shift(int index) const
auto start = shift_in(m_drop_preview_start);
auto target = shift_in(m_drop_preview_target);
+ auto interpolate = [this](qreal from, qreal to) { return from + ((to - from) * m_drop_preview_progress); };
return {
- .offset = start.offset + ((target.offset - start.offset) * m_drop_preview_progress),
- .extent_delta = start.extent_delta + ((target.extent_delta - start.extent_delta) * m_drop_preview_progress),
+ .x_offset = interpolate(start.x_offset, target.x_offset),
+ .y_offset = interpolate(start.y_offset, target.y_offset),
+ .width_delta = interpolate(start.width_delta, target.width_delta),
+ .height_delta = interpolate(start.height_delta, target.height_delta),
};
}
@@ -1350,11 +1417,12 @@ QRect TabBar::shifted_tab_rect(int index) const
{
auto tab_rect = visual_tab_rect(index);
auto shift = current_tab_shift(index);
- auto start_delta = qRound(shift.offset);
- auto end_delta = qRound(shift.offset + shift.extent_delta);
- if (tab_layout() == TabLayout::Horizontal)
- return tab_rect.adjusted(start_delta, 0, end_delta, 0);
- return tab_rect.adjusted(0, start_delta, 0, end_delta);
+ return {
+ qRound(tab_rect.left() + shift.x_offset),
+ qRound(tab_rect.top() + shift.y_offset),
+ qRound(tab_rect.width() + shift.width_delta),
+ qRound(tab_rect.height() + shift.height_delta),
+ };
}
QPixmap TabBar::render_tab_drag_pixmap(int index) const
@@ -1743,12 +1811,28 @@ void TabBar::update_tab_button_geometry()
auto dragged_index = dragged_tab_index();
if (tab_layout() == TabLayout::Horizontal) {
+ auto pinned_count = pinned_tab_count();
for (int index = 0; index < count(); ++index) {
auto is_dragged = index == dragged_index;
auto tab_rect = shifted_tab_rect(index);
auto shape_rect = horizontal_tab_card_shape_rect(QRectF(tab_rect));
auto* left_button = tabButton(index, QTabBar::LeftSide);
prepare_button(left_button, false);
+
+ // The audio button takes the favicon's place, in the middle of a pinned tile.
+ if (index < pinned_count) {
+ if (!left_button)
+ continue;
+ auto button_size = left_button->size().isEmpty() ? left_button->sizeHint() : left_button->size();
+ auto center = tab_rect.center();
+ QRect button_geometry { center.x() - (button_size.width() / 2) + 1, center.y() - (button_size.height() / 2) + 1, button_size.width(), button_size.height() };
+ if (left_button->geometry() != button_geometry)
+ left_button->setGeometry(button_geometry);
+ if (left_button->isVisible() == is_dragged)
+ left_button->setVisible(!is_dragged);
+ continue;
+ }
+
// NB: The style puts a tab's left button 1pt inside the tab's pill, where its icon looks flush against the
// pill's edge. Give it 3pt.
if (left_button) {
@@ -2853,7 +2937,9 @@ void TabWidget::update_tab_layout()
m_tab_bar->set_available_width(available_for_tabs);
- auto tab_bar_width = min(available_for_tabs, m_tab_bar->count() * HORIZONTAL_TAB_MAX_WIDTH);
+ auto pinned_count = m_tab_bar->pinned_tab_count();
+ auto tabs_width = (pinned_count * PINNED_TAB_TILE_WIDTH) + ((m_tab_bar->count() - pinned_count) * HORIZONTAL_TAB_MAX_WIDTH);
+ auto tab_bar_width = min(available_for_tabs, tabs_width);
m_tab_bar->setFixedWidth(max(0, tab_bar_width));
}
diff --git a/UI/Qt/TabBar.h b/UI/Qt/TabBar.h
index 3891c07f162..d9c837ab9a0 100644
--- a/UI/Qt/TabBar.h
+++ b/UI/Qt/TabBar.h
@@ -67,6 +67,7 @@ public:
void set_vertical_tabs_position(WebView::VerticalTabsPosition);
void refresh_tab_layout();
void recreate_icons();
+ int pinned_tab_count() const;
void play_load_sheen(Tab&);
void update_loading_swirl_animation();
@@ -98,9 +99,12 @@ private:
virtual void mouseReleaseEvent(QMouseEvent*) override;
virtual void wheelEvent(QWheelEvent*) override;
+ // How far a tab moves, and how much it grows, while tabs make way for a dragged tab.
struct TabShift {
- qreal offset { 0 };
- qreal extent_delta { 0 };
+ qreal x_offset { 0 };
+ qreal y_offset { 0 };
+ qreal width_delta { 0 };
+ qreal height_delta { 0 };
};
struct DropPreview {
Vector<TabShift> tab_shifts;
@@ -108,7 +112,8 @@ private:
};
Optional<int> dragged_tab_index() const;
- qreal drop_gap_extent() const;
+ bool dragging_pinned_tab() const;
+ QRect incoming_drop_layout_rect(int position) const;
int drop_slot_at(QPoint const&) const;
void set_drop_slot(Optional<int>);
void clear_drop_preview();
@@ -217,6 +222,7 @@ public:
void set_vertical_tabs_position(WebView::VerticalTabsPosition);
bool can_expand_vertical_tabs_on_hover() const;
void update_tab_button_visibility();
+ void update_tab_layout();
signals:
void current_tab_changed(int index);
@@ -257,7 +263,6 @@ private:
QRect vertical_tabs_chrome_rect() const;
int vertical_tabs_tab_width() const;
void update_vertical_tabs_button_layout();
- void update_tab_layout();
void update_tab_chrome_visibility();
#ifdef AK_OS_MACOS
void update_web_view_frame(Tab&);