1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 17:37:37 +00:00

LibWeb: Move scroll_offset() from Layout::Box to PaintableBox

Nodes in layout tree should not be aware of scroll state.
This commit is contained in:
Aliaksandr Kalenik 2023-08-06 20:51:46 +02:00 committed by Andreas Kling
parent 23a07a8ab6
commit 5b7926fa53
6 changed files with 23 additions and 26 deletions

View file

@ -943,11 +943,9 @@ double Element::scroll_top() const
if (!layout_node() || !is<Layout::Box>(layout_node())) if (!layout_node() || !is<Layout::Box>(layout_node()))
return 0.0; return 0.0;
auto const* box = static_cast<Layout::Box const*>(layout_node());
// 9. Return the y-coordinate of the scrolling area at the alignment point with the top of the padding edge of the element. // 9. Return the y-coordinate of the scrolling area at the alignment point with the top of the padding edge of the element.
// FIXME: Is this correct? // FIXME: Is this correct?
return box->scroll_offset().y().to_double(); return paintable_box()->scroll_offset().y().to_double();
} }
double Element::scroll_left() const double Element::scroll_left() const
@ -985,11 +983,9 @@ double Element::scroll_left() const
if (!layout_node() || !is<Layout::Box>(layout_node())) if (!layout_node() || !is<Layout::Box>(layout_node()))
return 0.0; return 0.0;
auto const* box = static_cast<Layout::Box const*>(layout_node());
// 9. Return the x-coordinate of the scrolling area at the alignment point with the left of the padding edge of the element. // 9. Return the x-coordinate of the scrolling area at the alignment point with the left of the padding edge of the element.
// FIXME: Is this correct? // FIXME: Is this correct?
return box->scroll_offset().x().to_double(); return paintable_box()->scroll_offset().x().to_double();
} }
// https://drafts.csswg.org/cssom-view/#dom-element-scrollleft // https://drafts.csswg.org/cssom-view/#dom-element-scrollleft
@ -1056,7 +1052,7 @@ void Element::set_scroll_left(double x)
// 11. Scroll the element to x,scrollTop, with the scroll behavior being "auto". // 11. Scroll the element to x,scrollTop, with the scroll behavior being "auto".
// FIXME: Implement this in terms of calling "scroll the element". // FIXME: Implement this in terms of calling "scroll the element".
auto scroll_offset = box->scroll_offset(); auto scroll_offset = paintable_box()->scroll_offset();
scroll_offset.set_x(static_cast<float>(x)); scroll_offset.set_x(static_cast<float>(x));
box->set_scroll_offset(scroll_offset); box->set_scroll_offset(scroll_offset);
} }
@ -1124,7 +1120,7 @@ void Element::set_scroll_top(double y)
// 11. Scroll the element to scrollLeft,y, with the scroll behavior being "auto". // 11. Scroll the element to scrollLeft,y, with the scroll behavior being "auto".
// FIXME: Implement this in terms of calling "scroll the element". // FIXME: Implement this in terms of calling "scroll the element".
auto scroll_offset = box->scroll_offset(); auto scroll_offset = paintable_box()->scroll_offset();
scroll_offset.set_y(static_cast<float>(y)); scroll_offset.set_y(static_cast<float>(y));
box->set_scroll_offset(scroll_offset); box->set_scroll_offset(scroll_offset);
} }

View file

@ -61,23 +61,10 @@ bool Box::is_scrollable() const
return computed_values().overflow_y() == CSS::Overflow::Scroll; return computed_values().overflow_y() == CSS::Overflow::Scroll;
} }
CSSPixelPoint Box::scroll_offset() const
{
if (is_generated_for_before_pseudo_element())
return pseudo_element_generator()->scroll_offset(DOM::Element::ScrollOffsetFor::PseudoBefore);
if (is_generated_for_after_pseudo_element())
return pseudo_element_generator()->scroll_offset(DOM::Element::ScrollOffsetFor::PseudoAfter);
if (!is<DOM::Element>(*dom_node()))
return {};
return static_cast<DOM::Element const*>(dom_node())->scroll_offset(DOM::Element::ScrollOffsetFor::Self);
}
void Box::set_scroll_offset(CSSPixelPoint offset) void Box::set_scroll_offset(CSSPixelPoint offset)
{ {
// FIXME: If there is horizontal and vertical scroll ignore only part of the new offset // FIXME: If there is horizontal and vertical scroll ignore only part of the new offset
if (offset.y() < 0 || scroll_offset() == offset) if (offset.y() < 0 || paintable_box()->scroll_offset() == offset)
return; return;
if (is_generated_for_before_pseudo_element()) { if (is_generated_for_before_pseudo_element()) {

View file

@ -54,7 +54,6 @@ public:
bool is_scroll_container() const; bool is_scroll_container() const;
bool is_scrollable() const; bool is_scrollable() const;
CSSPixelPoint scroll_offset() const;
void set_scroll_offset(CSSPixelPoint); void set_scroll_offset(CSSPixelPoint);
protected: protected:

View file

@ -57,6 +57,20 @@ PaintableWithLines::~PaintableWithLines()
{ {
} }
CSSPixelPoint PaintableBox::scroll_offset() const
{
auto const& node = layout_node();
if (node.is_generated_for_before_pseudo_element())
return node.pseudo_element_generator()->scroll_offset(DOM::Element::ScrollOffsetFor::PseudoBefore);
if (node.is_generated_for_after_pseudo_element())
return node.pseudo_element_generator()->scroll_offset(DOM::Element::ScrollOffsetFor::PseudoAfter);
if (!is<DOM::Element>(*dom_node()))
return {};
return static_cast<DOM::Element const*>(dom_node())->scroll_offset(DOM::Element::ScrollOffsetFor::Self);
}
void PaintableBox::scroll_by(int delta_x, int delta_y) void PaintableBox::scroll_by(int delta_x, int delta_y)
{ {
auto scrollable_overflow_rect = this->scrollable_overflow_rect(); auto scrollable_overflow_rect = this->scrollable_overflow_rect();
@ -64,7 +78,7 @@ void PaintableBox::scroll_by(int delta_x, int delta_y)
return; return;
auto max_x_offset = scrollable_overflow_rect->width() - content_size().width(); auto max_x_offset = scrollable_overflow_rect->width() - content_size().width();
auto max_y_offset = scrollable_overflow_rect->height() - content_size().height(); auto max_y_offset = scrollable_overflow_rect->height() - content_size().height();
auto current_offset = layout_box().scroll_offset(); auto current_offset = scroll_offset();
auto new_offset_x = clamp(current_offset.x() + delta_x, 0, max_x_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); 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 }); layout_box().set_scroll_offset({ new_offset_x, new_offset_y });
@ -610,7 +624,7 @@ void PaintableWithLines::paint(PaintContext& context, PaintPhase phase) const
// FIXME: Handle overflow-x and overflow-y being different values. // FIXME: Handle overflow-x and overflow-y being different values.
auto clip_box = context.rounded_device_rect(absolute_padding_box_rect()); auto clip_box = context.rounded_device_rect(absolute_padding_box_rect());
context.painter().add_clip_rect(clip_box.to_type<int>()); context.painter().add_clip_rect(clip_box.to_type<int>());
auto scroll_offset = context.rounded_device_point(static_cast<Layout::BlockContainer const&>(layout_box()).scroll_offset()); auto scroll_offset = context.rounded_device_point(this->scroll_offset());
context.painter().translate(-scroll_offset.to_type<int>()); context.painter().translate(-scroll_offset.to_type<int>());
auto border_radii = normalized_border_radii_data(ShrinkRadiiForBorders::Yes); auto border_radii = normalized_border_radii_data(ShrinkRadiiForBorders::Yes);

View file

@ -38,6 +38,7 @@ public:
CSSPixelRect absolute_rect() const; CSSPixelRect absolute_rect() const;
CSSPixelPoint effective_offset() const; CSSPixelPoint effective_offset() const;
CSSPixelPoint scroll_offset() const;
void scroll_by(int delta_x, int delta_y); void scroll_by(int delta_x, int delta_y);
void set_offset(CSSPixelPoint); void set_offset(CSSPixelPoint);

View file

@ -29,7 +29,7 @@ static void paint_node(Layout::Node const& layout_node, PaintContext& context, P
if (auto const* paintable = layout_node.paintable()) { if (auto const* paintable = layout_node.paintable()) {
if (paintable->containing_block() && paintable->containing_block()->is_scrollable()) { if (paintable->containing_block() && paintable->containing_block()->is_scrollable()) {
Gfx::PainterStateSaver saver(context.painter()); Gfx::PainterStateSaver saver(context.painter());
auto scroll_offset = -paintable->containing_block()->scroll_offset(); auto scroll_offset = -paintable->containing_block()->paintable_box()->scroll_offset();
context.painter().translate({ context.enclosing_device_pixels(scroll_offset.x()), context.enclosing_device_pixels(scroll_offset.y()) }); context.painter().translate({ context.enclosing_device_pixels(scroll_offset.x()), context.enclosing_device_pixels(scroll_offset.y()) });
paintable->paint(context, phase); paintable->paint(context, phase);
} else { } else {