diff --git a/Userland/Libraries/LibWeb/DOM/Element.cpp b/Userland/Libraries/LibWeb/DOM/Element.cpp index 14b746d5f5..60ee3c634e 100644 --- a/Userland/Libraries/LibWeb/DOM/Element.cpp +++ b/Userland/Libraries/LibWeb/DOM/Element.cpp @@ -826,14 +826,14 @@ double Element::scroll_top() const return window->scroll_y(); // 8. If the element does not have any associated box, return zero and terminate these steps. - if (!layout_node() || !is(layout_node())) + if (!layout_node() || !is(layout_node())) return 0.0; - auto const* block_container = static_cast(layout_node()); + auto const* box = static_cast(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. // FIXME: Is this correct? - return block_container->scroll_offset().y().value(); + return box->scroll_offset().y().value(); } double Element::scroll_left() const @@ -868,14 +868,14 @@ double Element::scroll_left() const return window->scroll_x(); // 8. If the element does not have any associated box, return zero and terminate these steps. - if (!layout_node() || !is(layout_node())) + if (!layout_node() || !is(layout_node())) return 0.0; - auto const* block_container = static_cast(layout_node()); + auto const* box = static_cast(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. // FIXME: Is this correct? - return block_container->scroll_offset().x().value(); + return box->scroll_offset().x().value(); } // https://drafts.csswg.org/cssom-view/#dom-element-scrollleft @@ -927,20 +927,20 @@ void Element::set_scroll_left(double x) } // 10. If the element does not have any associated box, the element has no associated scrolling box, or the element has no overflow, terminate these steps. - if (!layout_node() || !is(layout_node())) + if (!layout_node() || !is(layout_node())) return; - auto* block_container = static_cast(layout_node()); - if (!block_container->is_scrollable()) + auto* box = static_cast(layout_node()); + if (!box->is_scrollable()) return; // FIXME: or the element has no overflow. // 11. Scroll the element to x,scrollTop, with the scroll behavior being "auto". // FIXME: Implement this in terms of calling "scroll the element". - auto scroll_offset = block_container->scroll_offset(); + auto scroll_offset = box->scroll_offset(); scroll_offset.set_x(static_cast(x)); - block_container->set_scroll_offset(scroll_offset); + box->set_scroll_offset(scroll_offset); } void Element::set_scroll_top(double y) @@ -991,20 +991,20 @@ void Element::set_scroll_top(double y) } // 10. If the element does not have any associated box, the element has no associated scrolling box, or the element has no overflow, terminate these steps. - if (!layout_node() || !is(layout_node())) + if (!layout_node() || !is(layout_node())) return; - auto* block_container = static_cast(layout_node()); - if (!block_container->is_scrollable()) + auto* box = static_cast(layout_node()); + if (!box->is_scrollable()) return; // FIXME: or the element has no overflow. // 11. Scroll the element to scrollLeft,y, with the scroll behavior being "auto". // FIXME: Implement this in terms of calling "scroll the element". - auto scroll_offset = block_container->scroll_offset(); + auto scroll_offset = box->scroll_offset(); scroll_offset.set_y(static_cast(y)); - block_container->set_scroll_offset(scroll_offset); + box->set_scroll_offset(scroll_offset); } int Element::scroll_width() const diff --git a/Userland/Libraries/LibWeb/Layout/BlockContainer.cpp b/Userland/Libraries/LibWeb/Layout/BlockContainer.cpp index 98e2f28350..e82ce9cf3b 100644 --- a/Userland/Libraries/LibWeb/Layout/BlockContainer.cpp +++ b/Userland/Libraries/LibWeb/Layout/BlockContainer.cpp @@ -21,21 +21,6 @@ BlockContainer::BlockContainer(DOM::Document& document, DOM::Node* node, CSS::Co BlockContainer::~BlockContainer() = default; -bool BlockContainer::is_scrollable() const -{ - // FIXME: Support horizontal scroll as well (overflow-x) - return computed_values().overflow_y() == CSS::Overflow::Scroll; -} - -void BlockContainer::set_scroll_offset(CSSPixelPoint offset) -{ - // FIXME: If there is horizontal and vertical scroll ignore only part of the new offset - if (offset.y() < 0 || m_scroll_offset == offset) - return; - m_scroll_offset = offset; - set_needs_display(); -} - Painting::PaintableWithLines const* BlockContainer::paint_box() const { return static_cast(Box::paint_box()); diff --git a/Userland/Libraries/LibWeb/Layout/BlockContainer.h b/Userland/Libraries/LibWeb/Layout/BlockContainer.h index b98403ac39..3a9cd45d0d 100644 --- a/Userland/Libraries/LibWeb/Layout/BlockContainer.h +++ b/Userland/Libraries/LibWeb/Layout/BlockContainer.h @@ -20,18 +20,12 @@ public: BlockContainer(DOM::Document&, DOM::Node*, CSS::ComputedValues); virtual ~BlockContainer() override; - bool is_scrollable() const; - CSSPixelPoint scroll_offset() const { return m_scroll_offset; } - void set_scroll_offset(CSSPixelPoint); - Painting::PaintableWithLines const* paint_box() const; virtual JS::GCPtr create_paintable() const override; private: virtual bool is_block_container() const final { return true; } - - CSSPixelPoint m_scroll_offset; }; template<> diff --git a/Userland/Libraries/LibWeb/Layout/Box.cpp b/Userland/Libraries/LibWeb/Layout/Box.cpp index 927c804351..0ed952b402 100644 --- a/Userland/Libraries/LibWeb/Layout/Box.cpp +++ b/Userland/Libraries/LibWeb/Layout/Box.cpp @@ -29,6 +29,21 @@ Box::~Box() { } +bool Box::is_scrollable() const +{ + // FIXME: Support horizontal scroll as well (overflow-x) + 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 || m_scroll_offset == offset) + return; + m_scroll_offset = offset; + set_needs_display(); +} + void Box::set_needs_display() { if (paint_box()) diff --git a/Userland/Libraries/LibWeb/Layout/Box.h b/Userland/Libraries/LibWeb/Layout/Box.h index 21a492c661..a02a4043ce 100644 --- a/Userland/Libraries/LibWeb/Layout/Box.h +++ b/Userland/Libraries/LibWeb/Layout/Box.h @@ -41,12 +41,18 @@ public: virtual JS::GCPtr create_paintable() const override; + bool is_scrollable() const; + CSSPixelPoint scroll_offset() const { return m_scroll_offset; } + void set_scroll_offset(CSSPixelPoint); + protected: Box(DOM::Document&, DOM::Node*, NonnullRefPtr); Box(DOM::Document&, DOM::Node*, CSS::ComputedValues); private: virtual bool is_box() const final { return true; } + + CSSPixelPoint m_scroll_offset; }; template<> diff --git a/Userland/Libraries/LibWeb/Painting/Paintable.cpp b/Userland/Libraries/LibWeb/Painting/Paintable.cpp index 930114303f..e3cfde2461 100644 --- a/Userland/Libraries/LibWeb/Painting/Paintable.cpp +++ b/Userland/Libraries/LibWeb/Painting/Paintable.cpp @@ -36,16 +36,13 @@ Paintable::DispatchEventOfSameName Paintable::handle_mousemove(Badge, CSSPixelPoint, unsigned, unsigned, int wheel_delta_x, int wheel_delta_y) { if (auto* containing_block = this->containing_block()) { - if (!containing_block->is_block_container()) + if (!containing_block->is_scrollable()) return false; - auto* scroll_container = static_cast(containing_block); - if (!scroll_container->is_scrollable()) - return false; - auto new_offset = scroll_container->scroll_offset(); + auto new_offset = containing_block->scroll_offset(); new_offset.translate_by(wheel_delta_x, wheel_delta_y); // FIXME: This const_cast is gross. // FIXME: Scroll offset shouldn't live in the layout tree. - const_cast(scroll_container)->set_scroll_offset(new_offset); + const_cast(containing_block)->set_scroll_offset(new_offset); return true; }