From 23a07a8ab60f24393490dcb89ebe4628892464cd Mon Sep 17 00:00:00 2001 From: Aliaksandr Kalenik Date: Sun, 6 Aug 2023 20:29:29 +0200 Subject: [PATCH] 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. --- .../LibWeb/Painting/PaintableBox.cpp | 20 +++++++++++++------ .../Libraries/LibWeb/Painting/PaintableBox.h | 2 ++ 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/Userland/Libraries/LibWeb/Painting/PaintableBox.cpp b/Userland/Libraries/LibWeb/Painting/PaintableBox.cpp index a577d085ca..cba8554846 100644 --- a/Userland/Libraries/LibWeb/Painting/PaintableBox.cpp +++ b/Userland/Libraries/LibWeb/Painting/PaintableBox.cpp @@ -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, 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; } diff --git a/Userland/Libraries/LibWeb/Painting/PaintableBox.h b/Userland/Libraries/LibWeb/Painting/PaintableBox.h index 3727b350a1..626a55a11c 100644 --- a/Userland/Libraries/LibWeb/Painting/PaintableBox.h +++ b/Userland/Libraries/LibWeb/Painting/PaintableBox.h @@ -38,6 +38,8 @@ public: CSSPixelRect absolute_rect() const; CSSPixelPoint effective_offset() const; + void scroll_by(int delta_x, int delta_y); + void set_offset(CSSPixelPoint); void set_offset(float x, float y) { set_offset({ x, y }); }