diff options
author | Andreas Kling <kling@serenityos.org> | 2022-04-08 15:19:02 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2022-04-08 20:58:42 +0200 |
commit | dcbb83a33e14370baef4836dfb09d29b39019372 (patch) | |
tree | 760b2a0eb47265a9292ae1a217696732c39e9354 /Userland | |
parent | 40f090c7e8e700ec1457ec3e892fbe902b98ad3d (diff) | |
download | serenity-dcbb83a33e14370baef4836dfb09d29b39019372.zip |
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.
Diffstat (limited to 'Userland')
-rw-r--r-- | Userland/Libraries/LibWeb/Painting/Paintable.cpp | 16 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/Painting/Paintable.h | 33 |
2 files changed, 49 insertions, 0 deletions
diff --git a/Userland/Libraries/LibWeb/Painting/Paintable.cpp b/Userland/Libraries/LibWeb/Painting/Paintable.cpp index be83683e74..d53563cc0a 100644 --- a/Userland/Libraries/LibWeb/Painting/Paintable.cpp +++ b/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; +} + } diff --git a/Userland/Libraries/LibWeb/Painting/Paintable.h b/Userland/Libraries/LibWeb/Painting/Paintable.h index 2a8b13866a..ea4cd28f26 100644 --- a/Userland/Libraries/LibWeb/Painting/Paintable.h +++ b/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(); } + } |