1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00

perf(ui): cache stylesheets to reduce memory usage

Issue #5078
This commit is contained in:
Anthony Bilinski 2018-04-16 17:16:04 -07:00
parent 55d8922d69
commit 6d9d26db65
No known key found for this signature in database
GPG Key ID: 2AA8E0DA1B31FB3C
3 changed files with 28 additions and 14 deletions

View File

@ -92,15 +92,23 @@ QStringList Style::getThemeColorNames()
QList<QColor> Style::themeColorColors = {QColor(), QColor("#004aa4"), QColor("#97ba00"), QList<QColor> Style::themeColorColors = {QColor(), QColor("#004aa4"), QColor("#97ba00"),
QColor("#c23716"), QColor("#4617b5")}; QColor("#c23716"), QColor("#4617b5")};
QString Style::getStylesheet(const QString& filename, const QFont& baseFont) // stylesheet filename, font -> stylesheet
{ // QString implicit sharing deduplicates stylesheets rather than constructing a new one each time
QFile file(filename); std::map<std::pair<const QString, const QFont>, const QString> Style::stylesheetsCache;
if (!file.open(QFile::ReadOnly | QFile::Text)) {
qWarning() << "Stylesheet " << filename << " not found";
return QString();
}
return resolve(file.readAll(), baseFont); const QString Style::getStylesheet(const QString& filename, const QFont& baseFont)
{
const std::pair<const QString, const QFont> cacheKey(filename, baseFont);
auto it = stylesheetsCache.find(cacheKey);
if (it != stylesheetsCache.end())
{
// cache hit
return it->second;
}
// cache miss, new styleSheet, read it from file and add to cache
const QString newStylesheet = resolve(filename, baseFont);
stylesheetsCache.insert(std::make_pair(cacheKey, newStylesheet));
return newStylesheet;
} }
QColor Style::getColor(Style::ColorPalette entry) QColor Style::getColor(Style::ColorPalette entry)
@ -128,8 +136,15 @@ QFont Style::getFont(Style::Font font)
return fonts[font]; return fonts[font];
} }
QString Style::resolve(QString qss, const QFont& baseFont) const QString Style::resolve(const QString& filename, const QFont& baseFont)
{ {
QFile file{filename};
if (!file.open(QFile::ReadOnly | QFile::Text)) {
qWarning() << "Stylesheet " << filename << " not found";
return QString("");
}
QString qss = file.readAll();
if (dict.isEmpty()) { if (dict.isEmpty()) {
dict = {// colors dict = {// colors
{"@green", Style::getColor(Style::Green).name()}, {"@green", Style::getColor(Style::Green).name()},
@ -162,7 +177,6 @@ QString Style::resolve(QString qss, const QFont& baseFont)
for (const QString& key : dict.keys()) { for (const QString& key : dict.keys()) {
qss.replace(QRegularExpression(QString("%1\\b").arg(key)), dict[key]); qss.replace(QRegularExpression(QString("%1\\b").arg(key)), dict[key]);
} }
return qss; return qss;
} }

View File

@ -59,10 +59,10 @@ public:
}; };
static QStringList getThemeColorNames(); static QStringList getThemeColorNames();
static QString getStylesheet(const QString& filename, const QFont& baseFont = QFont()); static const QString getStylesheet(const QString& filename, const QFont& baseFont = QFont());
static QColor getColor(ColorPalette entry); static QColor getColor(ColorPalette entry);
static QFont getFont(Font font); static QFont getFont(Font font);
static QString resolve(QString qss, const QFont& baseFont = QFont()); static const QString resolve(const QString& filename, const QFont& baseFont = QFont());
static void repolish(QWidget* w); static void repolish(QWidget* w);
static void setThemeColor(int color); static void setThemeColor(int color);
static void setThemeColor(const QColor& color); static void setThemeColor(const QColor& color);
@ -70,6 +70,7 @@ public:
static QPixmap scaleSvgImage(const QString& path, uint32_t width, uint32_t height); static QPixmap scaleSvgImage(const QString& path, uint32_t width, uint32_t height);
static QList<QColor> themeColorColors; static QList<QColor> themeColorColors;
static std::map<std::pair<const QString, const QFont>, const QString> stylesheetsCache;
signals: signals:
void themeChanged(); void themeChanged();

View File

@ -143,8 +143,7 @@ void Widget::init()
connect(actionQuit, &QAction::triggered, qApp, &QApplication::quit); connect(actionQuit, &QAction::triggered, qApp, &QApplication::quit);
layout()->setContentsMargins(0, 0, 0, 0); layout()->setContentsMargins(0, 0, 0, 0);
ui->friendList->setStyleSheet( ui->friendList->setStyleSheet(Style::getStylesheet(":/ui/friendList/friendList.css"));
Style::resolve(Style::getStylesheet(":/ui/friendList/friendList.css")));
profilePicture = new MaskablePixmapWidget(this, QSize(40, 40), ":/img/avatar_mask.svg"); profilePicture = new MaskablePixmapWidget(this, QSize(40, 40), ":/img/avatar_mask.svg");
profilePicture->setPixmap(QPixmap(":/img/contact_dark.svg")); profilePicture->setPixmap(QPixmap(":/img/contact_dark.svg"));