From 9f4ac24cf8c9df723398a158c8e12a5d00d2dc4c Mon Sep 17 00:00:00 2001 From: David Rosca Date: Fri, 2 Feb 2018 08:54:45 +0100 Subject: [PATCH] TabBar: Initialize pinned tab width from real tab Calculates the padding between tab border and icon from tabRect instead of using style pixel metric which was oftentimes incorrect. Pinned tabs should now have correct size and icon be perfectly centered with all possible widget and qss styles. --- src/lib/tabwidget/tabbar.cpp | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/src/lib/tabwidget/tabbar.cpp b/src/lib/tabwidget/tabbar.cpp index dd80a59c1..2e8d6ccfb 100644 --- a/src/lib/tabwidget/tabbar.cpp +++ b/src/lib/tabwidget/tabbar.cpp @@ -51,7 +51,7 @@ class TabBarTabMetrics : public QWidget Q_PROPERTY(int pinnedWidth READ pinnedWidth WRITE setPinnedWidth) public: - void init(TabBar *t) + void init() { if (!m_metrics.isEmpty()) { return; @@ -60,7 +60,7 @@ public: m_metrics[1] = 100; m_metrics[2] = 100; m_metrics[3] = 100; - m_metrics[4] = t->iconButtonSize().width() + t->style()->pixelMetric(QStyle::PM_TabBarTabHSpace, nullptr, t); + m_metrics[4] = -1; // Will be initialized from TabBar } int normalMaxWidth() const { return m_metrics.value(0); } @@ -111,7 +111,7 @@ TabBar::TabBar(BrowserWindow* window, TabWidget* tabWidget) setCloseButtonsToolTip(BrowserWindow::tr("Close Tab")); connect(this, SIGNAL(overFlowChanged(bool)), this, SLOT(overflowChanged(bool))); - tabMetrics()->init(this); + tabMetrics()->init(); if (mApp->isPrivate()) { QLabel* privateBrowsing = new QLabel(this); @@ -292,7 +292,7 @@ int TabBar::comboTabBarPixelMetric(ComboTabBar::SizeType sizeType) const { switch (sizeType) { case ComboTabBar::PinnedTabWidth: - return tabMetrics()->pinnedWidth(); + return tabMetrics()->pinnedWidth() > 0 ? tabMetrics()->pinnedWidth() : 32; case ComboTabBar::ActiveTabMinimumWidth: return tabMetrics()->activeMinWidth(); @@ -455,6 +455,22 @@ void TabBar::tabInserted(int index) { Q_UNUSED(index) + // Initialize pinned tab metrics + if (tabMetrics()->pinnedWidth() == -1) { + QTimer::singleShot(0, this, [this]() { + if (tabMetrics()->pinnedWidth() != -1) { + return; + } + QWidget *w = tabButton(0, iconButtonPosition()); + const QRect r = tabRect(0); + if (w && r.isValid()) { + const int padding = w->geometry().x() - r.x(); + tabMetrics()->setPinnedWidth(iconButtonSize().width() + padding * 2); + setUpLayout(); + } + }); + } + setVisible(!(count() <= 1 && m_hideTabBarWithOneTab)); }