瀏覽代碼

LibWeb: Move image viewport awareness from ImageBox to ImagePaintable

Images being aware of being visible inside the viewport is a painting
concern, not a layout concern.
Andreas Kling 2 年之前
父節點
當前提交
dbe961ca02

+ 0 - 15
Userland/Libraries/LibWeb/Layout/ImageBox.cpp

@@ -15,20 +15,10 @@ ImageBox::ImageBox(DOM::Document& document, DOM::Element& element, NonnullRefPtr
     : ReplacedBox(document, element, move(style))
     : ReplacedBox(document, element, move(style))
     , m_image_loader(image_loader)
     , m_image_loader(image_loader)
 {
 {
-    browsing_context().register_viewport_client(*this);
 }
 }
 
 
 ImageBox::~ImageBox() = default;
 ImageBox::~ImageBox() = default;
 
 
-void ImageBox::finalize()
-{
-    Base::finalize();
-
-    // NOTE: We unregister from the browsing context in finalize() to avoid trouble
-    //       in the scenario where our BrowsingContext has already been swept by GC.
-    browsing_context().unregister_viewport_client(*this);
-}
-
 int ImageBox::preferred_width() const
 int ImageBox::preferred_width() const
 {
 {
     return dom_node().attribute(HTML::AttributeNames::width).to_int().value_or(m_image_loader.width());
     return dom_node().attribute(HTML::AttributeNames::width).to_int().value_or(m_image_loader.width());
@@ -90,11 +80,6 @@ bool ImageBox::renders_as_alt_text() const
     return false;
     return false;
 }
 }
 
 
-void ImageBox::browsing_context_did_set_viewport_rect(CSSPixelRect const& viewport_rect)
-{
-    m_image_loader.set_visible_in_viewport(paintable_box() && viewport_rect.intersects(paintable_box()->absolute_rect()));
-}
-
 JS::GCPtr<Painting::Paintable> ImageBox::create_paintable() const
 JS::GCPtr<Painting::Paintable> ImageBox::create_paintable() const
 {
 {
     return Painting::ImagePaintable::create(*this);
     return Painting::ImagePaintable::create(*this);

+ 1 - 10
Userland/Libraries/LibWeb/Layout/ImageBox.h

@@ -6,15 +6,12 @@
 
 
 #pragma once
 #pragma once
 
 
-#include <LibWeb/HTML/BrowsingContext.h>
 #include <LibWeb/HTML/HTMLImageElement.h>
 #include <LibWeb/HTML/HTMLImageElement.h>
 #include <LibWeb/Layout/ReplacedBox.h>
 #include <LibWeb/Layout/ReplacedBox.h>
 
 
 namespace Web::Layout {
 namespace Web::Layout {
 
 
-class ImageBox final
-    : public ReplacedBox
-    , public HTML::BrowsingContext::ViewportClient {
+class ImageBox final : public ReplacedBox {
     JS_CELL(ImageBox, ReplacedBox);
     JS_CELL(ImageBox, ReplacedBox);
 
 
 public:
 public:
@@ -34,12 +31,6 @@ public:
     void dom_node_did_update_alt_text(Badge<HTML::HTMLImageElement>);
     void dom_node_did_update_alt_text(Badge<HTML::HTMLImageElement>);
 
 
 private:
 private:
-    // ^BrowsingContext::ViewportClient
-    virtual void browsing_context_did_set_viewport_rect(CSSPixelRect const&) final;
-
-    // ^JS::Cell
-    virtual void finalize() override;
-
     int preferred_width() const;
     int preferred_width() const;
     int preferred_height() const;
     int preferred_height() const;
 
 

+ 15 - 0
Userland/Libraries/LibWeb/Painting/ImagePaintable.cpp

@@ -21,6 +21,16 @@ JS::NonnullGCPtr<ImagePaintable> ImagePaintable::create(Layout::ImageBox const&
 ImagePaintable::ImagePaintable(Layout::ImageBox const& layout_box)
 ImagePaintable::ImagePaintable(Layout::ImageBox const& layout_box)
     : PaintableBox(layout_box)
     : PaintableBox(layout_box)
 {
 {
+    browsing_context().register_viewport_client(*this);
+}
+
+void ImagePaintable::finalize()
+{
+    Base::finalize();
+
+    // NOTE: We unregister from the browsing context in finalize() to avoid trouble
+    //       in the scenario where our BrowsingContext has already been swept by GC.
+    browsing_context().unregister_viewport_client(*this);
 }
 }
 
 
 Layout::ImageBox const& ImagePaintable::layout_box() const
 Layout::ImageBox const& ImagePaintable::layout_box() const
@@ -57,4 +67,9 @@ void ImagePaintable::paint(PaintContext& context, PaintPhase phase) const
     }
     }
 }
 }
 
 
+void ImagePaintable::browsing_context_did_set_viewport_rect(CSSPixelRect const& viewport_rect)
+{
+    layout_box().image_loader().set_visible_in_viewport(viewport_rect.intersects(absolute_rect()));
+}
+
 }
 }

+ 10 - 1
Userland/Libraries/LibWeb/Painting/ImagePaintable.h

@@ -6,12 +6,15 @@
 
 
 #pragma once
 #pragma once
 
 
+#include <LibWeb/HTML/BrowsingContext.h>
 #include <LibWeb/Layout/ImageBox.h>
 #include <LibWeb/Layout/ImageBox.h>
 #include <LibWeb/Painting/PaintableBox.h>
 #include <LibWeb/Painting/PaintableBox.h>
 
 
 namespace Web::Painting {
 namespace Web::Painting {
 
 
-class ImagePaintable final : public PaintableBox {
+class ImagePaintable final
+    : public PaintableBox
+    , public HTML::BrowsingContext::ViewportClient {
     JS_CELL(ImagePaintable, PaintableBox);
     JS_CELL(ImagePaintable, PaintableBox);
 
 
 public:
 public:
@@ -22,6 +25,12 @@ public:
     Layout::ImageBox const& layout_box() const;
     Layout::ImageBox const& layout_box() const;
 
 
 private:
 private:
+    // ^JS::Cell
+    virtual void finalize() override;
+
+    // ^BrowsingContext::ViewportClient
+    virtual void browsing_context_did_set_viewport_rect(CSSPixelRect const&) final;
+
     ImagePaintable(Layout::ImageBox const&);
     ImagePaintable(Layout::ImageBox const&);
 };
 };