Share Plasma::Theme instances between multiple ColorScope

Especially since the Theme isn't modified in any way but just used to read some data.
While the Private part of Theme is already shared, creating a Theme instance still has some setup cost.

Differential Revision: https://phabricator.kde.org/D18149
This commit is contained in:
Kai Uwe Broulik 2019-01-18 12:24:21 +01:00
parent d755013c74
commit eac69e0469
2 changed files with 23 additions and 9 deletions

View File

@ -27,6 +27,8 @@
QHash<QObject *, ColorScope *> ColorScope::s_attachedScopes = QHash<QObject *, ColorScope *>();
QWeakPointer<Plasma::Theme> ColorScope::s_theme;
ColorScope::ColorScope(QQuickItem *parent, QObject *parentObject)
: QQuickItem(parent),
m_inherit(false),
@ -34,7 +36,15 @@ ColorScope::ColorScope(QQuickItem *parent, QObject *parentObject)
m_parent(parentObject),
m_actualGroup(Plasma::Theme::NormalColorGroup)
{
connect(&m_theme, &Plasma::Theme::themeChanged, this, &ColorScope::colorsChanged);
m_theme = s_theme.toStrongRef();
if (!m_theme) {
QSharedPointer<Plasma::Theme> themePtr(new Plasma::Theme);
s_theme = themePtr;
m_theme = s_theme.toStrongRef();
}
connect(s_theme.data(), &Plasma::Theme::themeChanged, this, &ColorScope::colorsChanged);
connect(this, &ColorScope::colorGroupChanged, this, &ColorScope::colorsChanged);
QQuickItem *parentItem = qobject_cast<QQuickItem *>(parentObject);
@ -138,37 +148,37 @@ Plasma::Theme::ColorGroup ColorScope::colorGroup() const
QColor ColorScope::textColor() const
{
return m_theme.color(Plasma::Theme::TextColor, colorGroup());
return m_theme->color(Plasma::Theme::TextColor, colorGroup());
}
QColor ColorScope::highlightColor() const
{
return m_theme.color(Plasma::Theme::HighlightColor, colorGroup());
return m_theme->color(Plasma::Theme::HighlightColor, colorGroup());
}
QColor ColorScope::highlightedTextColor() const
{
return m_theme.color(Plasma::Theme::HighlightedTextColor, colorGroup());
return m_theme->color(Plasma::Theme::HighlightedTextColor, colorGroup());
}
QColor ColorScope::backgroundColor() const
{
return m_theme.color(Plasma::Theme::BackgroundColor, colorGroup());
return m_theme->color(Plasma::Theme::BackgroundColor, colorGroup());
}
QColor ColorScope::positiveTextColor() const
{
return m_theme.color(Plasma::Theme::PositiveTextColor, colorGroup());
return m_theme->color(Plasma::Theme::PositiveTextColor, colorGroup());
}
QColor ColorScope::neutralTextColor() const
{
return m_theme.color(Plasma::Theme::NeutralTextColor, colorGroup());
return m_theme->color(Plasma::Theme::NeutralTextColor, colorGroup());
}
QColor ColorScope::negativeTextColor() const
{
return m_theme.color(Plasma::Theme::NegativeTextColor, colorGroup());
return m_theme->color(Plasma::Theme::NegativeTextColor, colorGroup());
}
bool ColorScope::inherit() const

View File

@ -22,6 +22,7 @@
#include <QQuickItem>
#include <QPointer>
#include <QSharedPointer>
#include <QVariant>
#include <Plasma/Plasma>
#include <Plasma/Theme>
@ -124,13 +125,16 @@ private:
void setParentScope(ColorScope * parentScope);
bool m_inherit;
Plasma::Theme m_theme;
Plasma::Theme::ColorGroup m_group;
QPointer<ColorScope> m_parentScope;
QObject *const m_parent;
Plasma::Theme::ColorGroup m_actualGroup;
static QHash<QObject *, ColorScope *> s_attachedScopes;
static QWeakPointer<Plasma::Theme> s_theme;
QSharedPointer<Plasma::Theme> m_theme;
};
QML_DECLARE_TYPEINFO(ColorScope, QML_HAS_ATTACHED_PROPERTIES)