Skip to content

Commit 9d37dbf

Browse files
committed
fix(editor): resolve tab bar jitter when adding new tabs
Batch layout operations with setUpdatesEnabled and double layout()->activate() to prevent scroll button cascade flicker. 修复新建标签页时标签栏抖动问题,通过批量布局操作和 双重layout激活消除滚动按钮级联闪烁。 Log: 修复新建标签页时标签栏抖动 PMS: BUG-353507 Influence: 新建标签页(2个及以上)时不再出现标签栏抖动现象,提升用户体验。
1 parent be1805a commit 9d37dbf

2 files changed

Lines changed: 36 additions & 16 deletions

File tree

src/controls/tabbar.cpp

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,17 @@ void Tabbar::addTabWithIndex(int index, const QString &filePath, const QString &
127127
// 除去空白符 梁卫东 2020-08-26 14:49:15 ;适配助记符
128128
QString trimmedName = replaceMnemonic(tabName.simplified());
129129
qDebug() << "trimmedName:" << trimmedName;
130+
131+
// Batch all layout-triggering operations to avoid jitter:
132+
// 1. Disable visual updates
133+
// 2. Insert tab (triggers QTabBar layout with old width → may overflow)
134+
// 3. Force DTabBar layout to resize inner QTabBar to correct width
135+
// 4. Force QTabBar to recalculate tab positions with correct width
136+
// 5. Re-enable visual updates → single paint with final state
137+
setUpdatesEnabled(false);
130138
DTabBar::insertTab(index, trimmedName);
131139
DTabBar::setCurrentIndex(index);
140+
132141
qDebug() << "filePath.contains(Utils::localDataPath())";
133142
if (filePath.contains(Utils::localDataPath())) {
134143
if (Utils::isBackupFile(filePath) && !tipPath.isNull() && tipPath.length() > 0) {
@@ -159,6 +168,16 @@ void Tabbar::addTabWithIndex(int index, const QString &filePath, const QString &
159168
qDebug() << "path:" << path;
160169
setTabToolTip(index, path);
161170
}
171+
172+
// Force DTabBar's layout to resize inner QTabBar to its new sizeHint
173+
layout()->activate();
174+
// Force QTabBar internal layout recalculation with correct width
175+
setIconSize(iconSize());
176+
// activate() may trigger scroll button show/hide cascade which
177+
// invalidates layout again; run a second time to settle completely
178+
layout()->activate();
179+
180+
setUpdatesEnabled(true);
162181
}
163182

164183
void Tabbar::resizeEvent(QResizeEvent *event)
@@ -826,23 +845,20 @@ QSize Tabbar::tabSizeHint(int index) const
826845
qDebug() << "Enter tabSizeHint, index:" << index;
827846
if (index >= 0) {
828847
int total = this->width();
829-
qDebug() << "total:" << total;
830-
//计算每个tab平均宽度 返回 100到160
831-
int aveargeWidth = 160;
832-
aveargeWidth = total / DTabBar::count();
833-
qDebug() << "aveargeWidth:" << aveargeWidth;
834-
if (aveargeWidth >= 160) {
835-
qDebug() << "aveargeWidth >= 160";
836-
aveargeWidth = 160;
837-
} else if (aveargeWidth <= 110) {
838-
qDebug() << "aveargeWidth <= 110";
839-
aveargeWidth = 110;
848+
// 计算每个tab宽度:标签数未溢出时使用固定最大宽度,溢出时等分
849+
const int maxTabWidth = 160;
850+
const int minTabWidth = 110;
851+
int tabCount = DTabBar::count();
852+
int tabWidth = maxTabWidth;
853+
854+
if (tabCount * maxTabWidth > total) {
855+
tabWidth = total / tabCount;
856+
tabWidth = qBound(minTabWidth, tabWidth, maxTabWidth);
840857
}
841-
qDebug() << "aveargeWidth:" << aveargeWidth;
842858
#ifdef DTKWIDGET_CLASS_DSizeMode
843-
return QSize(aveargeWidth, DGuiApplicationHelper::isCompactMode() ? s_TabbarHeightCompact : s_TabbarHeight);
859+
return QSize(tabWidth, DGuiApplicationHelper::isCompactMode() ? s_TabbarHeightCompact : s_TabbarHeight);
844860
#else
845-
return QSize(aveargeWidth, 40);
861+
return QSize(tabWidth, 40);
846862
#endif
847863
}
848864
qDebug() << "Exit tabSizeHint";

src/widgets/window.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3920,12 +3920,16 @@ void Window::checkTabbarForReload()
39203920
if (fi.exists() && !fi.isWritable()) {
39213921
qDebug() << "check tabbar for reload backup-files not writable";
39223922
tabName.append(readOnlyStr);
3923-
m_tabbar->setTabText(m_tabbar->currentIndex(), tabName);
3923+
if (m_tabbar->currentName() != tabName) {
3924+
m_tabbar->setTabText(m_tabbar->currentIndex(), tabName);
3925+
}
39243926
wrapper->textEditor()->setReadOnlyPermission(true);
39253927
} else {
39263928
qDebug() << "check tabbar for reload backup-files writable";
39273929
tabName.remove(readOnlyStr);
3928-
m_tabbar->setTabText(m_tabbar->currentIndex(), tabName);
3930+
if (m_tabbar->currentName() != tabName) {
3931+
m_tabbar->setTabText(m_tabbar->currentIndex(), tabName);
3932+
}
39293933
wrapper->textEditor()->setReadOnlyPermission(false);
39303934
}
39313935

0 commit comments

Comments
 (0)