Skip to content
This repository has been archived by the owner on Feb 12, 2023. It is now read-only.

Commit

Permalink
perf(ui): cache stylesheets to reduce memory usage
Browse files Browse the repository at this point in the history
Issue #5078
  • Loading branch information
anthonybilinski committed Apr 18, 2018
1 parent 55d8922 commit 6d9d26d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 13 deletions.
32 changes: 23 additions & 9 deletions src/widget/style.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,23 @@ QStringList Style::getThemeColorNames()
QList<QColor> Style::themeColorColors = {QColor(), QColor("#004aa4"), QColor("#97ba00"),
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
std::map<std::pair<const QString, const QFont>, const QString> Style::stylesheetsCache;

const QString Style::getStylesheet(const QString& filename, const QFont& baseFont)
{
QFile file(filename);
if (!file.open(QFile::ReadOnly | QFile::Text)) {
qWarning() << "Stylesheet " << filename << " not found";
return QString();
const std::pair<const QString, const QFont> cacheKey(filename, baseFont);
auto it = stylesheetsCache.find(cacheKey);
if (it != stylesheetsCache.end())
{
// cache hit
return it->second;
}

return resolve(file.readAll(), baseFont);
// 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)
Expand Down Expand Up @@ -128,8 +136,15 @@ QFont Style::getFont(Style::Font 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()) {
dict = {// colors
{"@green", Style::getColor(Style::Green).name()},
Expand Down Expand Up @@ -162,7 +177,6 @@ QString Style::resolve(QString qss, const QFont& baseFont)
for (const QString& key : dict.keys()) {
qss.replace(QRegularExpression(QString("%1\\b").arg(key)), dict[key]);
}

return qss;
}

Expand Down
5 changes: 3 additions & 2 deletions src/widget/style.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,18 @@ class Style
};

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 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 setThemeColor(int color);
static void setThemeColor(const QColor& color);
static void applyTheme();
static QPixmap scaleSvgImage(const QString& path, uint32_t width, uint32_t height);

static QList<QColor> themeColorColors;
static std::map<std::pair<const QString, const QFont>, const QString> stylesheetsCache;

signals:
void themeChanged();
Expand Down
3 changes: 1 addition & 2 deletions src/widget/widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,7 @@ void Widget::init()
connect(actionQuit, &QAction::triggered, qApp, &QApplication::quit);

layout()->setContentsMargins(0, 0, 0, 0);
ui->friendList->setStyleSheet(
Style::resolve(Style::getStylesheet(":/ui/friendList/friendList.css")));
ui->friendList->setStyleSheet(Style::getStylesheet(":/ui/friendList/friendList.css"));

profilePicture = new MaskablePixmapWidget(this, QSize(40, 40), ":/img/avatar_mask.svg");
profilePicture->setPixmap(QPixmap(":/img/contact_dark.svg"));
Expand Down

0 comments on commit 6d9d26d

Please sign in to comment.