Skip to content

Commit

Permalink
Clear icon cache on theme change
Browse files Browse the repository at this point in the history
  • Loading branch information
TheOneRing committed Aug 30, 2024
1 parent 6e9eaee commit 8d0cf57
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions src/resources/resources.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "resources/resources.h"
#include "resources/qmlresources.h"
#include "resources/template.h"
#include "resources/themewatcher.h"

#include "common/asserts.h"

Expand All @@ -31,6 +32,16 @@ using namespace Resources;
Q_LOGGING_CATEGORY(lcResources, "sync.resoruces", QtInfoMsg)

namespace {
struct IconCache
{
IconCache()
{
auto *watcher = new ThemeWatcher(qApp);
QObject::connect(watcher, &ThemeWatcher::themeChanged, [this]() { _cache.clear(); });
}
QMap<QString, QIcon> _cache;
};
Q_GLOBAL_STATIC(IconCache, iconCache)

QString vanillaThemePath()
{
Expand Down Expand Up @@ -103,14 +114,11 @@ QIcon OCC::Resources::getCoreIcon(const QString &iconName)
if (iconName.isEmpty()) {
return {};
}
const QString color = isUsingDarkTheme() ? QStringLiteral("#838FA1") : QStringLiteral("#435671");
const QString key = QStringLiteral("%1,%2").arg(iconName, color);

static QMap<QString, QIcon> _iconCache;
QIcon &cached = _iconCache[key]; // Take reference, this will also "set" the cache entry
QIcon &cached = iconCache->_cache[iconName]; // Take reference, this will also "set" the cache entry
if (cached.isNull()) {
const QString iconPath = QStringLiteral(":/client/resources/core/%1.svg").arg(iconName);
Q_ASSERT(QFileInfo::exists(iconPath));
const QString color = isUsingDarkTheme() ? QStringLiteral("#838FA1") : QStringLiteral("#435671");
QByteArray data = Template::renderTemplateFromFile(iconPath, {{QStringLiteral("color"), color}}).toUtf8();
QBuffer buffer(&data);
QImageReader iconReader(&buffer, "svg");
Expand All @@ -125,12 +133,10 @@ QIcon OCC::Resources::getCoreIcon(const QString &iconName)
*/
QIcon OCC::Resources::loadIcon(const QString &flavor, const QString &name, IconType iconType)
{
static QMap<QString, QIcon> _iconCache;
// prevent recusion
const bool useCoreIcon = (iconType == IconType::VanillaIcon) || isVanillaTheme();
const QString path = QStringLiteral("%1/%2/%3").arg(useCoreIcon ? vanillaThemePath() : brandThemePath(), flavor, name);
const QString key = name + QLatin1Char(',') + flavor;
QIcon &cached = _iconCache[key]; // Take reference, this will also "set" the cache entry
QIcon &cached = iconCache->_cache[path]; // Take reference, this will also "set" the cache entry
if (cached.isNull()) {
if (isVanillaTheme() && QIcon::hasThemeIcon(name)) {
// use from theme
Expand Down

0 comments on commit 8d0cf57

Please sign in to comment.