Explorar el Código

LibWeb: Add some basic paint tree traversal helpers

Give the Paintable class some basic helpers for traversing the paint
tree. Note that they actually piggy-back on the layout tree for links
between nodes.
Andreas Kling hace 3 años
padre
commit
dcbb83a33e

+ 16 - 0
Userland/Libraries/LibWeb/Painting/Paintable.cpp

@@ -46,4 +46,20 @@ Optional<HitTestResult> Paintable::hit_test(Gfx::FloatPoint const&, HitTestType)
     return {};
 }
 
+Paintable const* Paintable::first_child() const
+{
+    auto* layout_child = m_layout_node.first_child();
+    for (; layout_child && !layout_child->paintable(); layout_child = layout_child->next_sibling())
+        ;
+    return layout_child ? layout_child->paintable() : nullptr;
+}
+
+Paintable const* Paintable::next_sibling() const
+{
+    auto* layout_node = m_layout_node.next_sibling();
+    for (; layout_node && !layout_node->paintable(); layout_node = layout_node->next_sibling())
+        ;
+    return layout_node ? layout_node->paintable() : nullptr;
+}
+
 }

+ 33 - 0
Userland/Libraries/LibWeb/Painting/Paintable.h

@@ -49,6 +49,33 @@ class Paintable : public RefCounted<Paintable> {
 public:
     virtual ~Paintable() = default;
 
+    Paintable const* first_child() const;
+    Paintable const* next_sibling() const;
+
+    template<typename U, typename Callback>
+    IterationDecision for_each_in_inclusive_subtree_of_type(Callback callback) const
+    {
+        if (is<U>(*this)) {
+            if (callback(static_cast<const U&>(*this)) == IterationDecision::Break)
+                return IterationDecision::Break;
+        }
+        for (auto* child = first_child(); child; child = child->next_sibling()) {
+            if (child->template for_each_in_inclusive_subtree_of_type<U>(callback) == IterationDecision::Break)
+                return IterationDecision::Break;
+        }
+        return IterationDecision::Continue;
+    }
+
+    template<typename U, typename Callback>
+    IterationDecision for_each_in_subtree_of_type(Callback callback) const
+    {
+        for (auto* child = first_child(); child; child = child->next_sibling()) {
+            if (child->template for_each_in_inclusive_subtree_of_type<U>(callback) == IterationDecision::Break)
+                return IterationDecision::Break;
+        }
+        return IterationDecision::Continue;
+    }
+
     virtual void paint(PaintContext&, PaintPhase) const { }
     virtual void before_children_paint(PaintContext&, PaintPhase) const { }
     virtual void after_children_paint(PaintContext&, PaintPhase) const { }
@@ -91,6 +118,9 @@ public:
         return *m_containing_block;
     }
 
+    template<typename T>
+    bool fast_is() const = delete;
+
 protected:
     explicit Paintable(Layout::Node const& layout_node)
         : m_layout_node(layout_node)
@@ -112,4 +142,7 @@ inline DOM::Node const* HitTestResult::dom_node() const
     return paintable->dom_node();
 }
 
+template<>
+inline bool Paintable::fast_is<PaintableBox>() const { return m_layout_node.is_box(); }
+
 }