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

LibWeb: Move scroll state from Layout::BlockContainer to Layout::Box

Let's allow any box to be scrollable, not just block containers.
This commit is contained in:
Andreas Kling 2023-01-23 17:04:24 +01:00
parent 8fe748bb89
commit 3dd006f719
6 changed files with 40 additions and 43 deletions

View file

@ -826,14 +826,14 @@ double Element::scroll_top() const
return window->scroll_y(); return window->scroll_y();
// 8. If the element does not have any associated box, return zero and terminate these steps. // 8. If the element does not have any associated box, return zero and terminate these steps.
if (!layout_node() || !is<Layout::BlockContainer>(layout_node())) if (!layout_node() || !is<Layout::Box>(layout_node()))
return 0.0; return 0.0;
auto const* block_container = static_cast<Layout::BlockContainer const*>(layout_node()); 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 block_container->scroll_offset().y().value(); return box->scroll_offset().y().value();
} }
double Element::scroll_left() const double Element::scroll_left() const
@ -868,14 +868,14 @@ double Element::scroll_left() const
return window->scroll_x(); return window->scroll_x();
// 8. If the element does not have any associated box, return zero and terminate these steps. // 8. If the element does not have any associated box, return zero and terminate these steps.
if (!layout_node() || !is<Layout::BlockContainer>(layout_node())) if (!layout_node() || !is<Layout::Box>(layout_node()))
return 0.0; return 0.0;
auto const* block_container = static_cast<Layout::BlockContainer const*>(layout_node()); 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 block_container->scroll_offset().x().value(); return box->scroll_offset().x().value();
} }
// https://drafts.csswg.org/cssom-view/#dom-element-scrollleft // 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. // 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::BlockContainer>(layout_node())) if (!layout_node() || !is<Layout::Box>(layout_node()))
return; return;
auto* block_container = static_cast<Layout::BlockContainer*>(layout_node()); auto* box = static_cast<Layout::Box*>(layout_node());
if (!block_container->is_scrollable()) if (!box->is_scrollable())
return; return;
// FIXME: or the element has no overflow. // FIXME: or the element has no overflow.
// 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 = block_container->scroll_offset(); auto scroll_offset = box->scroll_offset();
scroll_offset.set_x(static_cast<float>(x)); scroll_offset.set_x(static_cast<float>(x));
block_container->set_scroll_offset(scroll_offset); box->set_scroll_offset(scroll_offset);
} }
void Element::set_scroll_top(double y) 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. // 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::BlockContainer>(layout_node())) if (!layout_node() || !is<Layout::Box>(layout_node()))
return; return;
auto* block_container = static_cast<Layout::BlockContainer*>(layout_node()); auto* box = static_cast<Layout::Box*>(layout_node());
if (!block_container->is_scrollable()) if (!box->is_scrollable())
return; return;
// FIXME: or the element has no overflow. // FIXME: or the element has no overflow.
// 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 = block_container->scroll_offset(); auto scroll_offset = box->scroll_offset();
scroll_offset.set_y(static_cast<float>(y)); scroll_offset.set_y(static_cast<float>(y));
block_container->set_scroll_offset(scroll_offset); box->set_scroll_offset(scroll_offset);
} }
int Element::scroll_width() const int Element::scroll_width() const

View file

@ -21,21 +21,6 @@ BlockContainer::BlockContainer(DOM::Document& document, DOM::Node* node, CSS::Co
BlockContainer::~BlockContainer() = default; 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 Painting::PaintableWithLines const* BlockContainer::paint_box() const
{ {
return static_cast<Painting::PaintableWithLines const*>(Box::paint_box()); return static_cast<Painting::PaintableWithLines const*>(Box::paint_box());

View file

@ -20,18 +20,12 @@ public:
BlockContainer(DOM::Document&, DOM::Node*, CSS::ComputedValues); BlockContainer(DOM::Document&, DOM::Node*, CSS::ComputedValues);
virtual ~BlockContainer() override; 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; Painting::PaintableWithLines const* paint_box() const;
virtual JS::GCPtr<Painting::Paintable> create_paintable() const override; virtual JS::GCPtr<Painting::Paintable> create_paintable() const override;
private: private:
virtual bool is_block_container() const final { return true; } virtual bool is_block_container() const final { return true; }
CSSPixelPoint m_scroll_offset;
}; };
template<> template<>

View file

@ -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() void Box::set_needs_display()
{ {
if (paint_box()) if (paint_box())

View file

@ -41,12 +41,18 @@ public:
virtual JS::GCPtr<Painting::Paintable> create_paintable() const override; virtual JS::GCPtr<Painting::Paintable> create_paintable() const override;
bool is_scrollable() const;
CSSPixelPoint scroll_offset() const { return m_scroll_offset; }
void set_scroll_offset(CSSPixelPoint);
protected: protected:
Box(DOM::Document&, DOM::Node*, NonnullRefPtr<CSS::StyleProperties>); Box(DOM::Document&, DOM::Node*, NonnullRefPtr<CSS::StyleProperties>);
Box(DOM::Document&, DOM::Node*, CSS::ComputedValues); Box(DOM::Document&, DOM::Node*, CSS::ComputedValues);
private: private:
virtual bool is_box() const final { return true; } virtual bool is_box() const final { return true; }
CSSPixelPoint m_scroll_offset;
}; };
template<> template<>

View file

@ -36,16 +36,13 @@ Paintable::DispatchEventOfSameName Paintable::handle_mousemove(Badge<EventHandle
bool Paintable::handle_mousewheel(Badge<EventHandler>, CSSPixelPoint, unsigned, unsigned, int wheel_delta_x, int wheel_delta_y) bool Paintable::handle_mousewheel(Badge<EventHandler>, CSSPixelPoint, unsigned, unsigned, int wheel_delta_x, int wheel_delta_y)
{ {
if (auto* containing_block = this->containing_block()) { if (auto* containing_block = this->containing_block()) {
if (!containing_block->is_block_container()) if (!containing_block->is_scrollable())
return false; return false;
auto* scroll_container = static_cast<Layout::BlockContainer const*>(containing_block); auto new_offset = containing_block->scroll_offset();
if (!scroll_container->is_scrollable())
return false;
auto new_offset = scroll_container->scroll_offset();
new_offset.translate_by(wheel_delta_x, wheel_delta_y); new_offset.translate_by(wheel_delta_x, wheel_delta_y);
// FIXME: This const_cast is gross. // FIXME: This const_cast is gross.
// FIXME: Scroll offset shouldn't live in the layout tree. // FIXME: Scroll offset shouldn't live in the layout tree.
const_cast<Layout::BlockContainer*>(scroll_container)->set_scroll_offset(new_offset); const_cast<Layout::Box*>(containing_block)->set_scroll_offset(new_offset);
return true; return true;
} }