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

LibWeb: Introduce PaintableBox::scroll_by()

Moves code responsible for calculation of new scroll offset into
scroll_by() so it could be reused for JS functions that trigger
scroll.
This commit is contained in:
Aliaksandr Kalenik 2023-08-06 20:29:29 +02:00 committed by Andreas Kling
parent fa56ce11a9
commit 23a07a8ab6
2 changed files with 16 additions and 6 deletions

View file

@ -57,6 +57,19 @@ PaintableWithLines::~PaintableWithLines()
{
}
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 = layout_box().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 });
}
void PaintableBox::set_offset(CSSPixelPoint offset)
{
m_offset = offset;
@ -667,12 +680,7 @@ bool PaintableBox::handle_mousewheel(Badge<EventHandler>, CSSPixelPoint, unsigne
{
if (!layout_box().is_scrollable())
return false;
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 = layout_box().scroll_offset();
auto new_offset_x = clamp(current_offset.x() + wheel_delta_x, 0, max_x_offset);
auto new_offset_y = clamp(current_offset.y() + wheel_delta_y, 0, max_y_offset);
layout_box().set_scroll_offset({ new_offset_x, new_offset_y });
scroll_by(wheel_delta_x, wheel_delta_y);
return true;
}