Cache TitleBar icons

Minor performance improvement. Detected by Milian when profiling.
This commit is contained in:
Sergio Martins
2023-01-27 14:36:05 +00:00
parent 1fc57b02b3
commit 2326874be7
2 changed files with 23 additions and 0 deletions

View File

@@ -21,6 +21,8 @@
#include "private/Utils_p.h" #include "private/Utils_p.h"
#include "private/TabWidget_p.h" #include "private/TabWidget_p.h"
#include <QScopeGuard>
#ifdef KDDOCKWIDGETS_QTWIDGETS #ifdef KDDOCKWIDGETS_QTWIDGETS
#include "private/widgets/FrameWidget_p.h" #include "private/widgets/FrameWidget_p.h"
#include "private/widgets/TitleBarWidget_p.h" #include "private/widgets/TitleBarWidget_p.h"
@@ -239,6 +241,11 @@ QUrl DefaultWidgetFactory::floatingWindowFilename() const
// iconForButtonType impl is the same for QtQuick and QtWidgets // iconForButtonType impl is the same for QtQuick and QtWidgets
QIcon DefaultWidgetFactory::iconForButtonType(TitleBarButtonType type, qreal dpr) const QIcon DefaultWidgetFactory::iconForButtonType(TitleBarButtonType type, qreal dpr) const
{ {
auto key = std::make_pair(type, dpr);
auto it = m_cachedIcons.find(key);
if (it != m_cachedIcons.end())
return *it;
QString iconName; QString iconName;
switch (type) { switch (type) {
case TitleBarButtonType::AutoHide: case TitleBarButtonType::AutoHide:
@@ -269,6 +276,11 @@ QIcon DefaultWidgetFactory::iconForButtonType(TitleBarButtonType type, qreal dpr
return {}; return {};
QIcon icon(QStringLiteral(":/img/%1.png").arg(iconName)); QIcon icon(QStringLiteral(":/img/%1.png").arg(iconName));
auto atScopeEnd = qScopeGuard([&icon, this, key] {
m_cachedIcons.insert(key, icon);
});
if (!scalingFactorIsSupported(dpr)) if (!scalingFactorIsSupported(dpr))
return icon; return icon;
@@ -282,3 +294,8 @@ QIcon DefaultWidgetFactory::iconForButtonType(TitleBarButtonType type, qreal dpr
return icon; return icon;
} }
void DefaultWidgetFactory::clearIconCache()
{
m_cachedIcons.clear();
}

View File

@@ -16,6 +16,10 @@
#include "KDDockWidgets.h" #include "KDDockWidgets.h"
#include "QWidgetAdapter.h" #include "QWidgetAdapter.h"
#include <QMap>
#include <utility>
// clazy:excludeall=ctor-missing-parent-argument // clazy:excludeall=ctor-missing-parent-argument
/** /**
@@ -187,11 +191,13 @@ public:
#endif #endif
QIcon iconForButtonType(TitleBarButtonType type, qreal dpr) const override; QIcon iconForButtonType(TitleBarButtonType type, qreal dpr) const override;
void clearIconCache();
static DropIndicatorType s_dropIndicatorType; static DropIndicatorType s_dropIndicatorType;
private: private:
Q_DISABLE_COPY(DefaultWidgetFactory) Q_DISABLE_COPY(DefaultWidgetFactory)
mutable QMap<std::pair<TitleBarButtonType, qreal>, QIcon> m_cachedIcons;
}; };
} }