mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 06:58:11 +00:00
LibWeb: Until an image has loaded or failed, don't occupy layout size
This patch makes images have an implicit zero intrinsic size before they have either loaded or failed to load. This is tracked by the ImageLoader object. This fixes a long-standing issue with images occupying empty 150x150 rectangles of space.
This commit is contained in:
parent
bd54854c64
commit
305e2ef69c
3 changed files with 33 additions and 11 deletions
|
@ -54,20 +54,27 @@ int LayoutImage::preferred_height() const
|
||||||
|
|
||||||
void LayoutImage::layout(LayoutMode layout_mode)
|
void LayoutImage::layout(LayoutMode layout_mode)
|
||||||
{
|
{
|
||||||
if (m_image_loader.width()) {
|
if (!m_image_loader.has_loaded_or_failed()) {
|
||||||
set_has_intrinsic_width(true);
|
set_has_intrinsic_width(true);
|
||||||
set_intrinsic_width(m_image_loader.width());
|
|
||||||
}
|
|
||||||
if (m_image_loader.height()) {
|
|
||||||
set_has_intrinsic_height(true);
|
set_has_intrinsic_height(true);
|
||||||
set_intrinsic_height(m_image_loader.height());
|
set_intrinsic_width(0);
|
||||||
}
|
set_intrinsic_height(0);
|
||||||
|
|
||||||
if (m_image_loader.width() && m_image_loader.height()) {
|
|
||||||
set_has_intrinsic_ratio(true);
|
|
||||||
set_intrinsic_ratio((float)m_image_loader.width() / (float)m_image_loader.height());
|
|
||||||
} else {
|
} else {
|
||||||
set_has_intrinsic_ratio(false);
|
if (m_image_loader.width()) {
|
||||||
|
set_has_intrinsic_width(true);
|
||||||
|
set_intrinsic_width(m_image_loader.width());
|
||||||
|
}
|
||||||
|
if (m_image_loader.height()) {
|
||||||
|
set_has_intrinsic_height(true);
|
||||||
|
set_intrinsic_height(m_image_loader.height());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (m_image_loader.width() && m_image_loader.height()) {
|
||||||
|
set_has_intrinsic_ratio(true);
|
||||||
|
set_intrinsic_ratio((float)m_image_loader.width() / (float)m_image_loader.height());
|
||||||
|
} else {
|
||||||
|
set_has_intrinsic_ratio(false);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (renders_as_alt_text()) {
|
if (renders_as_alt_text()) {
|
||||||
|
|
|
@ -39,6 +39,7 @@ ImageLoader::ImageLoader()
|
||||||
|
|
||||||
void ImageLoader::load(const URL& url)
|
void ImageLoader::load(const URL& url)
|
||||||
{
|
{
|
||||||
|
m_loading_state = LoadingState::Loading;
|
||||||
LoadRequest request;
|
LoadRequest request;
|
||||||
request.set_url(url);
|
request.set_url(url);
|
||||||
set_resource(ResourceLoader::the().load_resource(Resource::Type::Image, request));
|
set_resource(ResourceLoader::the().load_resource(Resource::Type::Image, request));
|
||||||
|
@ -62,11 +63,14 @@ void ImageLoader::resource_did_load()
|
||||||
ASSERT(resource());
|
ASSERT(resource());
|
||||||
|
|
||||||
if (!resource()->mime_type().starts_with("image/")) {
|
if (!resource()->mime_type().starts_with("image/")) {
|
||||||
|
m_loading_state = LoadingState::Failed;
|
||||||
if (on_fail)
|
if (on_fail)
|
||||||
on_fail();
|
on_fail();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
m_loading_state = LoadingState::Loaded;
|
||||||
|
|
||||||
#ifdef IMAGE_LOADER_DEBUG
|
#ifdef IMAGE_LOADER_DEBUG
|
||||||
if (!resource()->has_encoded_data()) {
|
if (!resource()->has_encoded_data()) {
|
||||||
dbg() << "ImageLoader: Resource did load, no encoded data. URL: " << resource()->url();
|
dbg() << "ImageLoader: Resource did load, no encoded data. URL: " << resource()->url();
|
||||||
|
@ -118,6 +122,7 @@ void ImageLoader::animate()
|
||||||
void ImageLoader::resource_did_fail()
|
void ImageLoader::resource_did_fail()
|
||||||
{
|
{
|
||||||
dbg() << "ImageLoader: Resource did fail. URL: " << resource()->url();
|
dbg() << "ImageLoader: Resource did fail. URL: " << resource()->url();
|
||||||
|
m_loading_state = LoadingState::Failed;
|
||||||
if (on_fail)
|
if (on_fail)
|
||||||
on_fail();
|
on_fail();
|
||||||
}
|
}
|
||||||
|
|
|
@ -42,6 +42,8 @@ public:
|
||||||
|
|
||||||
bool has_image() const;
|
bool has_image() const;
|
||||||
|
|
||||||
|
bool has_loaded_or_failed() const { return m_loading_state != LoadingState::Loading; }
|
||||||
|
|
||||||
void set_visible_in_viewport(bool) const;
|
void set_visible_in_viewport(bool) const;
|
||||||
|
|
||||||
unsigned width() const;
|
unsigned width() const;
|
||||||
|
@ -59,10 +61,18 @@ private:
|
||||||
|
|
||||||
void animate();
|
void animate();
|
||||||
|
|
||||||
|
enum class LoadingState {
|
||||||
|
None,
|
||||||
|
Loading,
|
||||||
|
Loaded,
|
||||||
|
Failed,
|
||||||
|
};
|
||||||
|
|
||||||
mutable bool m_visible_in_viewport { false };
|
mutable bool m_visible_in_viewport { false };
|
||||||
|
|
||||||
size_t m_current_frame_index { 0 };
|
size_t m_current_frame_index { 0 };
|
||||||
size_t m_loops_completed { 0 };
|
size_t m_loops_completed { 0 };
|
||||||
|
LoadingState m_loading_state { LoadingState::Loading };
|
||||||
NonnullRefPtr<Core::Timer> m_timer;
|
NonnullRefPtr<Core::Timer> m_timer;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue