Skip to content

Truly independent web browser

UI/Qt: Position tab buttons without registering them with QTabBar

Callum Law7100634e2906
diff --git a/UI/Qt/BrowserWindow.cpp b/UI/Qt/BrowserWindow.cpp
index c0c198d36a0..ce1c3d9d34f 100644
--- a/UI/Qt/BrowserWindow.cpp
+++ b/UI/Qt/BrowserWindow.cpp
@@ -1245,14 +1245,7 @@ void BrowserWindow::discard_tab(Tab& tab)
other_tab->did_replace_tab(tab, *discarded_tab);
}
- // NB: The tab bar only hides a tab button that another takes the place of, and the old one refers to the old tab.
uninitialize_tab(&tab);
- auto* tab_bar = m_tabs_container->tab_bar();
- if (auto* close_button = tab_bar->tab_close_button(index)) {
- tab_bar->set_tab_close_button(index, nullptr);
- close_button->deleteLater();
- }
-
m_tabs_container->replace_tab(index, discarded_tab);
initialize_tab(discarded_tab);
tab_title_changed(index, discarded_tab->title());
@@ -1434,14 +1427,7 @@ void BrowserWindow::update_tab_close_button(Tab& tab)
if (index < 0)
return;
- auto* tab_bar = m_tabs_container->tab_bar();
- auto* close_button = tab_bar->tab_close_button(index);
- if (tab.is_pinned() && close_button) {
- tab_bar->set_tab_close_button(index, nullptr);
- close_button->deleteLater();
- } else if (!tab.is_pinned() && !close_button) {
- tab_bar->set_tab_close_button(index, create_tab_close_button(tab));
- }
+ m_tabs_container->tab_bar()->set_tab_close_button(index, tab.is_pinned() ? nullptr : create_tab_close_button(tab));
m_tabs_container->update_tab_button_visibility();
}
@@ -1607,9 +1593,7 @@ void BrowserWindow::update_tab_audio_button(Tab& tab)
auto* button = tab_bar->tab_audio_button(index);
if (!tab.is_loaded() || (!shows_playing_audio && tab.view().page_mute_state() == Web::HTML::MuteState::Unmuted)) {
- // The tab bar only hides the button it lets go of.
if (button) {
- button->deleteLater();
tab_bar->set_tab_audio_button(index, nullptr);
m_tabs_container->update_tab_button_visibility();
}
diff --git a/UI/Qt/TabBar.cpp b/UI/Qt/TabBar.cpp
index 6fb0901abb6..2c4dd3eab69 100644
--- a/UI/Qt/TabBar.cpp
+++ b/UI/Qt/TabBar.cpp
@@ -2566,22 +2566,39 @@ void TabBar::update_loading_ring_animation()
TabBarButton* TabBar::tab_close_button(int index) const
{
- return static_cast<TabBarButton*>(tabButton(index, QTabBar::RightSide));
+ return tabData(index).value<TabButtons>().close;
}
void TabBar::set_tab_close_button(int index, TabBarButton* button)
{
- setTabButton(index, QTabBar::RightSide, button);
+ set_tab_button(index, &TabButtons::close, button);
}
TabBarButton* TabBar::tab_audio_button(int index) const
{
- return static_cast<TabBarButton*>(tabButton(index, QTabBar::LeftSide));
+ return tabData(index).value<TabButtons>().audio;
}
void TabBar::set_tab_audio_button(int index, TabBarButton* button)
{
- setTabButton(index, QTabBar::LeftSide, button);
+ set_tab_button(index, &TabButtons::audio, button);
+}
+
+void TabBar::set_tab_button(int index, TabBarButton* TabButtons::* member, TabBarButton* button)
+{
+ auto buttons = tabData(index).value<TabButtons>();
+ auto*& slot = buttons.*member;
+ if (slot) {
+ slot->hide();
+ slot->deleteLater();
+ }
+ if (button) {
+ button->setParent(this);
+ button->lower();
+ }
+ // NB: Qt elides each tab's title to position registered buttons. We position these buttons ourselves.
+ slot = button;
+ setTabData(index, QVariant::fromValue(buttons));
}
void TabBar::update_microphone_indicators()
@@ -3538,6 +3555,8 @@ Tab* TabWidget::take_tab(int index)
disconnect(as<Tab>(widget), &Tab::loaded, this, nullptr);
m_toolbar->release_location_edit(*as<Tab>(widget));
+ m_tab_bar->set_tab_close_button(index, nullptr);
+ m_tab_bar->set_tab_audio_button(index, nullptr);
m_tab_bar->removeTab(index);
if (m_tab_bar->count() > 0 && m_tab_bar->currentIndex() >= 0)
diff --git a/UI/Qt/TabBar.h b/UI/Qt/TabBar.h
index 2e236b48fda..e07053ad883 100644
--- a/UI/Qt/TabBar.h
+++ b/UI/Qt/TabBar.h
@@ -230,6 +230,11 @@ private:
void set_hovered_tab_index(int);
qreal loading_ring_opacity(int index) const;
void ensure_tab_visible(int index);
+ struct TabButtons {
+ TabBarButton* close { nullptr };
+ TabBarButton* audio { nullptr };
+ };
+ void set_tab_button(int index, TabBarButton* TabButtons::*, TabBarButton*);
void update_tab_button_geometry();
TabMicrophoneIndicator* microphone_indicator(int index) const;
int horizontal_tab_min_width(int index) const;
@@ -282,8 +287,6 @@ private:
int m_settled_tab_count { 0 };
QVariantAnimation* m_loading_ring_animation { nullptr };
- // NB: QTabBar has room for only two widgets on each tab, the speaker and the close button, so the tab bar keeps the
- // microphone indicators itself.
HashMap<Tab const*, TabMicrophoneIndicator*> m_microphone_indicators;
QIcon m_fallback_tab_icon;
QHash<QString, QStaticText> m_tab_title_texts;