From fee5b4deb65de1c4331b76b3ab837a130589a89d Mon Sep 17 00:00:00 2001 From: Aliaksandr Kalenik Date: Sun, 6 Aug 2023 21:10:15 +0200 Subject: [PATCH] LibWeb: Move set_scroll_offset() from Layout::Box to PaintableBox Nodes in layout tree should not be aware of scroll state. --- Userland/Libraries/LibWeb/DOM/Element.cpp | 4 ++-- Userland/Libraries/LibWeb/Layout/Box.cpp | 19 ---------------- Userland/Libraries/LibWeb/Layout/Box.h | 1 - .../LibWeb/Painting/PaintableBox.cpp | 22 ++++++++++++++++++- .../Libraries/LibWeb/Painting/PaintableBox.h | 1 + 5 files changed, 24 insertions(+), 23 deletions(-) diff --git a/Userland/Libraries/LibWeb/DOM/Element.cpp b/Userland/Libraries/LibWeb/DOM/Element.cpp index 616ba89cbe..762a50a06d 100644 --- a/Userland/Libraries/LibWeb/DOM/Element.cpp +++ b/Userland/Libraries/LibWeb/DOM/Element.cpp @@ -1054,7 +1054,7 @@ void Element::set_scroll_left(double x) // FIXME: Implement this in terms of calling "scroll the element". auto scroll_offset = paintable_box()->scroll_offset(); scroll_offset.set_x(static_cast(x)); - box->set_scroll_offset(scroll_offset); + const_cast(paintable_box())->set_scroll_offset(scroll_offset); } void Element::set_scroll_top(double y) @@ -1122,7 +1122,7 @@ void Element::set_scroll_top(double y) // FIXME: Implement this in terms of calling "scroll the element". auto scroll_offset = paintable_box()->scroll_offset(); scroll_offset.set_y(static_cast(y)); - box->set_scroll_offset(scroll_offset); + const_cast(paintable_box())->set_scroll_offset(scroll_offset); } // https://drafts.csswg.org/cssom-view/#dom-element-scrollwidth diff --git a/Userland/Libraries/LibWeb/Layout/Box.cpp b/Userland/Libraries/LibWeb/Layout/Box.cpp index ef556dfe1e..f469f99b15 100644 --- a/Userland/Libraries/LibWeb/Layout/Box.cpp +++ b/Userland/Libraries/LibWeb/Layout/Box.cpp @@ -61,25 +61,6 @@ bool Box::is_scrollable() const return computed_values().overflow_y() == CSS::Overflow::Scroll; } -void Box::set_scroll_offset(CSSPixelPoint offset) -{ - // FIXME: If there is horizontal and vertical scroll ignore only part of the new offset - if (offset.y() < 0 || paintable_box()->scroll_offset() == offset) - return; - - if (is_generated_for_before_pseudo_element()) { - pseudo_element_generator()->set_scroll_offset(DOM::Element::ScrollOffsetFor::PseudoBefore, offset); - } else if (is_generated_for_after_pseudo_element()) { - pseudo_element_generator()->set_scroll_offset(DOM::Element::ScrollOffsetFor::PseudoAfter, offset); - } else if (is(*dom_node())) { - static_cast(dom_node())->set_scroll_offset(DOM::Element::ScrollOffsetFor::Self, offset); - } else { - return; - } - - set_needs_display(); -} - void Box::set_needs_display() { if (paintable_box()) diff --git a/Userland/Libraries/LibWeb/Layout/Box.h b/Userland/Libraries/LibWeb/Layout/Box.h index c46e1dc57c..b75001d170 100644 --- a/Userland/Libraries/LibWeb/Layout/Box.h +++ b/Userland/Libraries/LibWeb/Layout/Box.h @@ -54,7 +54,6 @@ public: bool is_scroll_container() const; bool is_scrollable() const; - void set_scroll_offset(CSSPixelPoint); protected: Box(DOM::Document&, DOM::Node*, NonnullRefPtr); diff --git a/Userland/Libraries/LibWeb/Painting/PaintableBox.cpp b/Userland/Libraries/LibWeb/Painting/PaintableBox.cpp index 5241589ef8..d38af91073 100644 --- a/Userland/Libraries/LibWeb/Painting/PaintableBox.cpp +++ b/Userland/Libraries/LibWeb/Painting/PaintableBox.cpp @@ -71,6 +71,26 @@ CSSPixelPoint PaintableBox::scroll_offset() const return static_cast(dom_node())->scroll_offset(DOM::Element::ScrollOffsetFor::Self); } +void PaintableBox::set_scroll_offset(CSSPixelPoint offset) +{ + // FIXME: If there is horizontal and vertical scroll ignore only part of the new offset + if (offset.y() < 0 || scroll_offset() == offset) + return; + + auto& node = layout_node(); + if (node.is_generated_for_before_pseudo_element()) { + node.pseudo_element_generator()->set_scroll_offset(DOM::Element::ScrollOffsetFor::PseudoBefore, offset); + } else if (node.is_generated_for_after_pseudo_element()) { + node.pseudo_element_generator()->set_scroll_offset(DOM::Element::ScrollOffsetFor::PseudoAfter, offset); + } else if (is(*dom_node())) { + static_cast(dom_node())->set_scroll_offset(DOM::Element::ScrollOffsetFor::Self, offset); + } else { + return; + } + + node.set_needs_display(); +} + void PaintableBox::scroll_by(int delta_x, int delta_y) { auto scrollable_overflow_rect = this->scrollable_overflow_rect(); @@ -81,7 +101,7 @@ void PaintableBox::scroll_by(int delta_x, int delta_y) auto current_offset = scroll_offset(); auto new_offset_x = clamp(current_offset.x() + delta_x, 0, max_x_offset); auto new_offset_y = clamp(current_offset.y() + delta_y, 0, max_y_offset); - layout_box().set_scroll_offset({ new_offset_x, new_offset_y }); + set_scroll_offset({ new_offset_x, new_offset_y }); } void PaintableBox::set_offset(CSSPixelPoint offset) diff --git a/Userland/Libraries/LibWeb/Painting/PaintableBox.h b/Userland/Libraries/LibWeb/Painting/PaintableBox.h index 065359be10..bf1e6899ff 100644 --- a/Userland/Libraries/LibWeb/Painting/PaintableBox.h +++ b/Userland/Libraries/LibWeb/Painting/PaintableBox.h @@ -39,6 +39,7 @@ public: CSSPixelPoint effective_offset() const; CSSPixelPoint scroll_offset() const; + void set_scroll_offset(CSSPixelPoint); void scroll_by(int delta_x, int delta_y); void set_offset(CSSPixelPoint);