1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-28 18:27:36 +00:00

LibWeb: Clamp scroll offset to valid range in set_scroll_offset()

By moving scroll offset clamp from `PaintableBox::scroll_by()` to
`PaintableBox::set_scroll_offset()`, we ensure that updates from
`Element::set_scroll_top()` and `Element::set_scroll_left()` are
constrained to a valid range.
This commit is contained in:
Aliaksandr Kalenik 2024-02-22 01:27:21 +01:00 committed by Andreas Kling
parent ee4abacde6
commit 155070cfd8
3 changed files with 48 additions and 9 deletions

View file

@ -66,6 +66,15 @@ CSSPixelPoint PaintableBox::scroll_offset() const
void PaintableBox::set_scroll_offset(CSSPixelPoint offset)
{
auto scrollable_overflow_rect = this->scrollable_overflow_rect();
if (!scrollable_overflow_rect.has_value())
return;
auto max_x_offset = scrollable_overflow_rect->width() - content_size().width();
auto max_y_offset = scrollable_overflow_rect->height() - content_size().height();
offset.set_x(clamp(offset.x(), 0, max_x_offset));
offset.set_y(clamp(offset.y(), 0, max_y_offset));
// FIXME: If there is horizontal and vertical scroll ignore only part of the new offset
if (offset.y() < 0 || scroll_offset() == offset)
return;
@ -86,15 +95,7 @@ void PaintableBox::set_scroll_offset(CSSPixelPoint offset)
void PaintableBox::scroll_by(int delta_x, int delta_y)
{
auto scrollable_overflow_rect = this->scrollable_overflow_rect();
if (!scrollable_overflow_rect.has_value())
return;
auto max_x_offset = scrollable_overflow_rect->width() - content_size().width();
auto max_y_offset = scrollable_overflow_rect->height() - content_size().height();
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);
set_scroll_offset({ new_offset_x, new_offset_y });
set_scroll_offset(scroll_offset().translated(delta_x, delta_y));
}
void PaintableBox::set_offset(CSSPixelPoint offset)