Estimate size of images to avoid layout shifts (#5192)

This commit is contained in:
nerix
2024-02-25 18:19:20 +01:00
committed by GitHub
parent 0cfd25ce8e
commit f285ada36c
13 changed files with 145 additions and 70 deletions
+11 -9
View File
@@ -318,7 +318,7 @@ Image::~Image()
}
}
ImagePtr Image::fromUrl(const Url &url, qreal scale)
ImagePtr Image::fromUrl(const Url &url, qreal scale, QSize expectedSize)
{
static std::unordered_map<Url, std::weak_ptr<Image>> cache;
static std::mutex mutex;
@@ -329,7 +329,7 @@ ImagePtr Image::fromUrl(const Url &url, qreal scale)
if (!shared)
{
cache[url] = shared = ImagePtr(new Image(url, scale));
cache[url] = shared = ImagePtr(new Image(url, scale, expectedSize));
}
return shared;
@@ -382,9 +382,11 @@ Image::Image()
{
}
Image::Image(const Url &url, qreal scale)
Image::Image(const Url &url, qreal scale, QSize expectedSize)
: url_(url)
, scale_(scale)
, expectedSize_(expectedSize.isValid() ? expectedSize
: (QSize(16, 16) * scale))
, shouldLoad_(true)
, frames_(std::make_unique<detail::Frames>())
{
@@ -477,11 +479,11 @@ int Image::width() const
if (auto pixmap = this->frames_->first())
{
return int(pixmap->width() * this->scale_);
return static_cast<int>(pixmap->width() * this->scale_);
}
// No frames loaded, use our default magic width 16
return 16;
// No frames loaded, use the expected size
return static_cast<int>(this->expectedSize_.width() * this->scale_);
}
int Image::height() const
@@ -490,11 +492,11 @@ int Image::height() const
if (auto pixmap = this->frames_->first())
{
return int(pixmap->height() * this->scale_);
return static_cast<int>(pixmap->height() * this->scale_);
}
// No frames loaded, use our default magic height 16
return 16;
// No frames loaded, use the expected size
return static_cast<int>(this->expectedSize_.height() * this->scale_);
}
void Image::actuallyLoad()