Commit 5e2df994 authored by Waqar Ahmed's avatar Waqar Ahmed
Browse files

Fix tab jumping with unlimited tabs

If we set the icon every time file is modified, it causes a lot of
layouting resulting in the tab bar getting scrolled.

With this change instead of setting an icon, we make the text italic to
show that this tab has modified content. This is more like other
editors.

BUG: 440585 435780
parent d3754528
Pipeline #140281 passed with stage
in 3 minutes and 44 seconds
......@@ -17,6 +17,7 @@
#include <QMimeData>
#include <QPainter>
#include <QPixmap>
#include <QProxyStyle>
#include <QResizeEvent>
#include <QStyleOptionTab>
#include <QStylePainter>
......@@ -32,6 +33,45 @@ struct KateTabButtonData {
KTextEditor::Document *doc = nullptr;
};
class KateTabStyle : public QProxyStyle
{
public:
KateTabStyle(KateTabBar *parent)
: QProxyStyle()
, m_tabBar(parent)
{
}
void drawControl(ControlElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget = nullptr) const override
{
if (m_tabBar && element == QStyle::CE_TabBarTabLabel) {
auto opt = const_cast<QStyleOptionTabV4 *>(qstyleoption_cast<const QStyleOptionTabV4 *>(option));
if (auto doc = m_tabBar->tabDocument(opt->tabIndex)) {
// If doc is modified we paint its text italic
if (doc->isModified()) {
// Draw whatever else is there without text
auto text = opt->text;
opt->text = QString();
QProxyStyle::drawControl(element, option, painter, widget);
// Draw the text as italic
painter->save();
auto font = m_tabBar->font();
font.setItalic(true);
painter->setFont(font);
const auto rect = subElementRect(SE_TabBarTabText, opt, widget);
drawItemText(painter, rect, Qt::AlignCenter, opt->palette, true, text, QPalette::WindowText);
painter->restore();
}
}
}
QProxyStyle::drawControl(element, option, painter, widget);
}
private:
const QPointer<KateTabBar> m_tabBar;
};
Q_DECLARE_METATYPE(KateTabButtonData)
/**
......@@ -40,6 +80,11 @@ Q_DECLARE_METATYPE(KateTabButtonData)
KateTabBar::KateTabBar(QWidget *parent)
: QTabBar(parent)
{
// Set our style
auto tabBarStyle = new KateTabStyle(this);
setStyle(tabBarStyle);
tabBarStyle->setParent(this);
// we want no auto-accelerators here
KAcceleratorManager::setNoAccel(this);
......
......@@ -654,15 +654,10 @@ void KateViewSpace::updateDocumentUrl(KTextEditor::Document *doc)
void KateViewSpace::updateDocumentState(KTextEditor::Document *doc)
{
QIcon icon;
if (doc->isModified()) {
icon = QIcon::fromTheme(QStringLiteral("document-save"));
}
// update tab button if available, might not be the case for tab limit set!
const int buttonId = m_tabBar->documentIdx(doc);
if (buttonId >= 0) {
m_tabBar->setTabIcon(buttonId, icon);
m_tabBar->update(m_tabBar->tabRect(buttonId));
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment