Skip to content

Truly independent web browser

UI/Qt: Keep room for the favicon beside a tab's buttons

Andreas Kling6519313f05ee
diff --git a/Documentation/UI/QuietDesign.md b/Documentation/UI/QuietDesign.md
index 528be829f88..2f04a5f9378 100644
--- a/Documentation/UI/QuietDesign.md
+++ b/Documentation/UI/QuietDesign.md
@@ -98,7 +98,7 @@ All sizes are in logical pixels.
| End insets | Favicon 7 px from the left edge; × glyph 11 px from the right edge | Rule: each end item is as far from the tab's side as it is from the tab's top (the 16 px favicon sits 7 px down, the 8 px × glyph 11 px down). Padding: active 0 6 px 0 7 px; inactive 3 px inset + 0 3 px 0 4 px, so favicons and × never move when a tab becomes active. |
| Favicon to title | 8 px | |
| Close button | 18 px hover square, 7 px × (14 px glyph box); click area extends to the full tab height | The × is always visible on every tab except pinned tabs, in `muted`. On tabs that are neither active nor hovered it is dimmed as a whole, to 75% opacity in light and 55% in dark and private, and it is at full opacity on the active tab, on a hovered tab and with increased contrast. Its hover square appears on hover. Indicators like the speaker and the microphone sit to its left. |
-| Tab width | 20 to 232 px | Tabs shrink until only the favicon is left, giving up their padding below 32 px, then the strip scrolls. A tab too narrow for its favicon beside its × shows only the favicon; the active one shows its × in the favicon's place. |
+| Tab width | 20 to 232 px | Tabs shrink until only the favicon is left, giving up their padding below 32 px, then the strip scrolls. A tab with a speaker or a microphone stops shrinking where its favicon still fits beside them and its ×: 75 px with a speaker, 72 px with a microphone, 89 px with both. The other tabs share what is left. The active tab stops shrinking at 56 px, where its favicon and × still fit side by side. Any other tab too narrow for its favicon beside its × shows only the favicon. |
| Pinned tab | 44 px wide | Favicon only, always first. While the tab plays audio or uses the microphone, those indicators take the favicon's place. |
| New tab (+) | 26 px button, 14 px glyph | Follows the last tab directly, 8 px after it (just clear of the active tab's foot), centered on the tab row; with few tabs it does not move to the end of the strip. Click area at least 32 px. |
| Separators | 1 × 16 px | Between inactive tabs only, hidden next to hovered or active tabs |
diff --git a/UI/Qt/TabBar.cpp b/UI/Qt/TabBar.cpp
index e67ce3f422a..1e6e2ed906c 100644
--- a/UI/Qt/TabBar.cpp
+++ b/UI/Qt/TabBar.cpp
@@ -8,6 +8,7 @@
#include <AK/Array.h>
#include <AK/Function.h>
+#include <AK/QuickSort.h>
#include <AK/ScopeGuard.h>
#include <AK/StdLibExtras.h>
#include <AK/kmalloc.h>
@@ -226,6 +227,10 @@ static constexpr QSize TAB_CLOSE_BUTTON_HIT_SIZE { 20, 28 };
static constexpr int TAB_AUDIO_BUTTON_SIZE = 20;
static constexpr int TAB_BUTTON_ICON_SIZE = 14;
static constexpr qreal TAB_BUTTON_RADIUS = 4;
+// A tab's favicon or title ends this far before its speaker, microphone, or close button.
+static constexpr int TAB_CONTENT_BUTTON_GAP = 6;
+// A horizontal tab's speaker, or its microphone without one, ends this far before the close button's slot.
+static constexpr int HORIZONTAL_TAB_CLOSE_BUTTON_GAP = 2;
static constexpr int DROP_PREVIEW_DURATION_MS = 150;
static constexpr int TAB_TITLE_FADE_WIDTH = 24;
// In the sidebar's rows, the speaker is a larger button with a larger icon, so that it matches the close button's weight.
@@ -408,6 +413,37 @@ static bool horizontal_tab_has_room_for_favicon_before(qreal close_button_left,
return close_button_left - HORIZONTAL_TAB_CONTENT_GAP >= content_left + TAB_ICON_SIZE;
}
+// The narrowest a horizontal tab with a speaker or a microphone gets, so that its favicon still fits beside them and the
+// close button, and the tab can be told apart. At this width the tab keeps all of its padding and its close button's
+// slot, and update_tab_button_geometry() puts the buttons at the end of its content: the close button, then the speaker,
+// then the microphone. The microphone of a tab without a speaker sits in the middle of the speaker's slot.
+// NB: paintEvent() ends the favicon's QRect TAB_CONTENT_BUTTON_GAP before the first button, and a QRect's right edge is
+// its last pixel, so one pixel less than that gap is empty.
+static constexpr int horizontal_tab_min_width_with_media_indicators(bool has_speaker, bool has_microphone)
+{
+ auto microphone_in_speaker_slot_width = TAB_AUDIO_BUTTON_SIZE - ((TAB_AUDIO_BUTTON_SIZE - TAB_BUTTON_ICON_SIZE) / 2);
+ auto indicators_width = has_speaker ? TAB_AUDIO_BUTTON_SIZE + (has_microphone ? TAB_BUTTON_ICON_SIZE : 0) : microphone_in_speaker_slot_width;
+ return static_cast<int>(2 * HORIZONTAL_TAB_CARD_SHAPE_HORIZONTAL_INSET) + HORIZONTAL_TAB_CONTENT_LEFT_PADDING
+ + TAB_ICON_SIZE + (TAB_CONTENT_BUTTON_GAP - 1) + indicators_width + HORIZONTAL_TAB_CLOSE_BUTTON_GAP
+ + TAB_CLOSE_BUTTON_HIT_SIZE.width() + HORIZONTAL_TAB_CONTENT_RIGHT_PADDING;
+}
+static_assert(horizontal_tab_min_width_with_media_indicators(true, false) == 75);
+static_assert(horizontal_tab_min_width_with_media_indicators(false, true) == 72);
+static_assert(horizontal_tab_min_width_with_media_indicators(true, true) == 89);
+// NB: At each of those widths the tab is padded, and its close button has room beside its favicon, so it shows.
+static_assert(horizontal_tab_min_width_with_media_indicators(false, true) >= HORIZONTAL_TAB_PADDED_MIN_WIDTH);
+static_assert(horizontal_tab_min_width_with_media_indicators(false, true) - static_cast<int>(2 * HORIZONTAL_TAB_CARD_SHAPE_HORIZONTAL_INSET)
+ - HORIZONTAL_TAB_CONTENT_LEFT_PADDING - HORIZONTAL_TAB_CONTENT_RIGHT_PADDING
+ >= TAB_ICON_SIZE + HORIZONTAL_TAB_CONTENT_GAP + TAB_CLOSE_BUTTON_HIT_SIZE.width());
+
+// The narrowest the selected horizontal tab gets, so that it shows its favicon beside its close button.
+static constexpr int HORIZONTAL_SELECTED_TAB_MIN_WIDTH = static_cast<int>(2 * HORIZONTAL_TAB_CARD_SHAPE_HORIZONTAL_INSET)
+ + HORIZONTAL_TAB_CONTENT_LEFT_PADDING + TAB_ICON_SIZE + HORIZONTAL_TAB_CONTENT_GAP + TAB_CLOSE_BUTTON_HIT_SIZE.width()
+ + HORIZONTAL_TAB_CONTENT_RIGHT_PADDING;
+static_assert(HORIZONTAL_SELECTED_TAB_MIN_WIDTH == 56);
+static_assert(HORIZONTAL_SELECTED_TAB_MIN_WIDTH >= HORIZONTAL_TAB_PADDED_MIN_WIDTH);
+static_assert(HORIZONTAL_SELECTED_TAB_MIN_WIDTH <= horizontal_tab_min_width_with_media_indicators(false, true));
+
// The selected tab, once it is too narrow for its favicon beside its close button, shows the close button in its
// favicon's place, so that it can still be closed with a click. A tab playing audio or using the microphone shows that
// there instead.
@@ -983,6 +1019,9 @@ TabBar::TabBar(TabWidget* tab_widget)
connect(this, &QTabBar::currentChanged, this, [this](int index) {
hide_tab_preview();
+ // NB: The selected horizontal tab has a larger minimum width (see tabSizeHint()).
+ if (tab_layout() == TabLayout::Horizontal)
+ refresh_tab_layout();
ensure_tab_visible(index);
update_tab_button_geometry();
});
@@ -1052,9 +1091,7 @@ void TabBar::lock_tab_widths()
if (pinned_count >= count())
return;
- // NB: Only an unpinned tab between the first and the last has no room for a foot.
- auto end_room = (pinned_count == 0 ? HORIZONTAL_TAB_END_ROOM : 0) + (pinned_count == count() - 1 ? HORIZONTAL_TAB_END_ROOM : 0);
- m_locked_tab_width = tabRect(pinned_count).width() - end_room;
+ m_locked_tab_width = m_locked_tab_width.value_or(horizontal_tab_share());
m_tab_width_unlock_timer->stop();
}
@@ -1127,18 +1164,8 @@ QSize TabBar::tabSizeHint(int index) const
return hint;
}
- if (m_locked_tab_width.has_value()) {
- hint.setWidth(*m_locked_tab_width + end_room);
- hint.setHeight(HORIZONTAL_TAB_HEIGHT);
- return hint;
- }
-
- // The unpinned tabs share whatever width the pinned tiles and the room at the ends leave.
- auto available_width = (m_available_width > 0 ? m_available_width : this->width()) - (pinned_count * HORIZONTAL_PINNED_TAB_WIDTH) - (2 * HORIZONTAL_TAB_END_ROOM);
- auto width = available_width / max(1, count - pinned_count);
- width = min(HORIZONTAL_TAB_MAX_WIDTH, width);
- width = max(HORIZONTAL_TAB_MIN_WIDTH, width);
-
+ // NB: While the widths are locked, the tabs keep the share they had.
+ auto width = max(horizontal_tab_min_width(index), m_locked_tab_width.value_or(horizontal_tab_share()));
hint.setWidth(width + end_room);
}
@@ -1146,6 +1173,45 @@ QSize TabBar::tabSizeHint(int index) const
return hint;
}
+int TabBar::horizontal_tab_min_width(int index) const
+{
+ auto has_speaker = tabButton(index, QTabBar::LeftSide) != nullptr;
+ auto has_microphone = microphone_indicator(index) != nullptr;
+ if (has_speaker || has_microphone)
+ return horizontal_tab_min_width_with_media_indicators(has_speaker, has_microphone);
+ if (index == currentIndex())
+ return HORIZONTAL_SELECTED_TAB_MIN_WIDTH;
+ return HORIZONTAL_TAB_MIN_WIDTH;
+}
+
+// The width of each unpinned horizontal tab that is wider than its own minimum. The unpinned tabs share whatever width
+// the pinned tiles and the room at the ends leave, and a tab whose minimum is larger than its share keeps its minimum,
+// so that the others share what it leaves.
+int TabBar::horizontal_tab_share() const
+{
+ auto pinned_count = pinned_tab_count();
+ auto remaining_width = (m_available_width > 0 ? m_available_width : this->width()) - (pinned_count * HORIZONTAL_PINNED_TAB_WIDTH) - (2 * HORIZONTAL_TAB_END_ROOM);
+ auto sharing_count = count() - pinned_count;
+ if (sharing_count <= 0)
+ return HORIZONTAL_TAB_MIN_WIDTH;
+
+ Vector<int, 8> larger_min_widths;
+ for (int index = pinned_count; index < count(); ++index) {
+ if (auto min_width = horizontal_tab_min_width(index); min_width > HORIZONTAL_TAB_MIN_WIDTH)
+ larger_min_widths.append(min_width);
+ }
+ quick_sort(larger_min_widths, [](int a, int b) { return a > b; });
+ for (auto min_width : larger_min_widths) {
+ if (min_width <= remaining_width / sharing_count)
+ break;
+ remaining_width -= min_width;
+ --sharing_count;
+ }
+
+ auto share = sharing_count > 0 ? remaining_width / sharing_count : 0;
+ return clamp(share, HORIZONTAL_TAB_MIN_WIDTH, HORIZONTAL_TAB_MAX_WIDTH);
+}
+
void TabBar::resizeEvent(QResizeEvent* event)
{
hide_tab_preview();
@@ -1476,14 +1542,14 @@ void TabBar::paintEvent(QPaintEvent*)
// The microphone and the speaker sit after the title, with the close button.
for (auto* indicator : { static_cast<QWidget*>(microphone_indicator(index)), tabButton(index, QTabBar::LeftSide) }) {
if (indicator && indicator->isVisible())
- contents_rect.setRight(min(contents_rect.right(), indicator->geometry().left() - 6));
+ contents_rect.setRight(min(contents_rect.right(), indicator->geometry().left() - TAB_CONTENT_BUTTON_GAP));
}
// NB: A horizontal tab keeps its close button's slot whether the button shows or not, so that the title ends at
// the same place either way.
if (auto* right_button = tabButton(index, QTabBar::RightSide)) {
auto keeps_slot = !is_vertical && horizontal_tab_has_room_for_favicon_before(right_button->x(), contents_rect.left());
if (right_button->isVisible() || keeps_slot)
- contents_rect.setRight(min(contents_rect.right(), right_button->geometry().left() - 6));
+ contents_rect.setRight(min(contents_rect.right(), right_button->geometry().left() - TAB_CONTENT_BUTTON_GAP));
}
if (!is_vertical && contents_rect.width() < HORIZONTAL_TAB_MIN_CONTENT_WIDTH_FOR_TITLE) {
@@ -2634,19 +2700,28 @@ void TabBar::update_microphone_indicators()
capturing_tabs.set(tab);
}
+ auto indicators_changed = false;
m_microphone_indicators.remove_all_matching([&](Tab const* tab, TabMicrophoneIndicator* indicator) {
if (capturing_tabs.contains(tab))
return false;
indicator->hide();
indicator->deleteLater();
+ indicators_changed = true;
return true;
});
for (auto const* tab : capturing_tabs) {
- if (!m_microphone_indicators.contains(tab))
+ if (!m_microphone_indicators.contains(tab)) {
m_microphone_indicators.set(tab, new TabMicrophoneIndicator(this));
+ indicators_changed = true;
+ }
}
+ // NB: A horizontal tab with a microphone has a larger minimum width (see tabSizeHint()).
+ if (indicators_changed && tab_layout() == TabLayout::Horizontal) {
+ refresh_tab_layout();
+ return;
+ }
update_tab_button_geometry();
update();
}
@@ -3123,7 +3198,7 @@ void TabBar::update_tab_button_geometry()
close_button->setVisible(show_close_button);
}
if (has_room)
- trailing_edge = close_button_position.x() - 2;
+ trailing_edge = close_button_position.x() - HORIZONTAL_TAB_CLOSE_BUTTON_GAP;
}
// The speaker of a tab playing audio sits left of the close button's slot, and the microphone of a tab using
diff --git a/UI/Qt/TabBar.h b/UI/Qt/TabBar.h
index 3314c2ddb19..a30cf78b2e8 100644
--- a/UI/Qt/TabBar.h
+++ b/UI/Qt/TabBar.h
@@ -227,6 +227,8 @@ private:
void ensure_tab_visible(int index);
void update_tab_button_geometry();
TabMicrophoneIndicator* microphone_indicator(int index) const;
+ int horizontal_tab_min_width(int index) const;
+ int horizontal_tab_share() const;
QPointer<TabWidget> m_tab_widget;
@@ -234,6 +236,7 @@ private:
bool m_pinned_tabs_in_rows { false };
WebView::VerticalTabsPosition m_vertical_tabs_position { WebView::VerticalTabsPosition::Left };
int m_available_width { 0 };
+ // NB: While locked, the unpinned tabs keep the share of the strip that they had (see horizontal_tab_share()).
Optional<int> m_locked_tab_width;
QTimer* m_tab_width_unlock_timer { nullptr };
int m_hovered_tab_index { -1 };