LibWeb: Allow scrolling overflowed content with the mouse wheel :^)

This is rather crude, but you can now use the mouse wheel to scroll up
and down in block-level boxes with clipped overflowing content.
There's no limit to how far you can scroll in either direction, since
we don't yet track how much overflow there is. But it's a start. :^)
This commit is contained in:
Andreas Kling 2021-02-22 19:48:24 +01:00
parent ded8c728d2
commit cd79b807dd
5 changed files with 44 additions and 0 deletions

View file

@ -68,6 +68,7 @@ void BlockBox::paint(PaintContext& context, PaintPhase phase)
context.painter().save();
// FIXME: Handle overflow-x and overflow-y being different values.
context.painter().add_clip_rect(enclosing_int_rect(padded_rect()));
context.painter().translate(-m_scroll_offset.to_type<int>());
}
for (auto& line_box : m_line_boxes) {
@ -141,4 +142,19 @@ void BlockBox::split_into_lines(InlineFormattingContext& context, LayoutMode lay
line_box->add_fragment(*this, 0, 0, border_box_width(), height());
}
void BlockBox::set_scroll_offset(const Gfx::FloatPoint& offset)
{
if (m_scroll_offset == offset)
return;
m_scroll_offset = offset;
set_needs_display();
}
void BlockBox::handle_mousewheel(Badge<EventHandler>, const Gfx::IntPoint&, unsigned int, unsigned int, int wheel_delta)
{
auto new_offset = m_scroll_offset;
new_offset.move_by(0, wheel_delta);
set_scroll_offset(new_offset);
}
}

View file

@ -53,10 +53,17 @@ public:
virtual void split_into_lines(InlineFormattingContext&, LayoutMode) override;
const Gfx::FloatPoint& scroll_offset() const { return m_scroll_offset; }
void set_scroll_offset(const Gfx::FloatPoint&);
private:
virtual bool is_block_box() const final { return true; }
virtual bool wants_mouse_events() const override { return true; }
virtual void handle_mousewheel(Badge<EventHandler>, const Gfx::IntPoint&, unsigned buttons, unsigned modifiers, int wheel_delta) override;
bool should_clip_overflow() const;
Gfx::FloatPoint m_scroll_offset;
};
template<>

View file

@ -318,6 +318,15 @@ void Node::handle_mousemove(Badge<EventHandler>, const Gfx::IntPoint&, unsigned,
{
}
void Node::handle_mousewheel(Badge<EventHandler>, const Gfx::IntPoint&, unsigned, unsigned, int wheel_delta)
{
if (auto* containing_block = this->containing_block()) {
auto new_offset = containing_block->scroll_offset();
new_offset.move_by(0, wheel_delta);
containing_block->set_scroll_offset(new_offset);
}
}
bool Node::is_root_element() const
{
if (is_anonymous())

View file

@ -109,6 +109,7 @@ public:
virtual void handle_mousedown(Badge<EventHandler>, const Gfx::IntPoint&, unsigned button, unsigned modifiers);
virtual void handle_mouseup(Badge<EventHandler>, const Gfx::IntPoint&, unsigned button, unsigned modifiers);
virtual void handle_mousemove(Badge<EventHandler>, const Gfx::IntPoint&, unsigned buttons, unsigned modifiers);
virtual void handle_mousewheel(Badge<EventHandler>, const Gfx::IntPoint&, unsigned buttons, unsigned modifiers, int wheel_delta);
virtual void before_children_paint(PaintContext&, PaintPhase) {};
virtual void paint(PaintContext&, PaintPhase);

View file

@ -75,6 +75,17 @@ Layout::InitialContainingBlockBox* EventHandler::layout_root()
bool EventHandler::handle_mousewheel(const Gfx::IntPoint& position, unsigned int buttons, unsigned int modifiers, int wheel_delta)
{
if (!layout_root())
return false;
// FIXME: Support wheel events in subframes.
auto result = layout_root()->hit_test(position, Layout::HitTestType::Exact);
if (result.layout_node) {
result.layout_node->handle_mousewheel({}, position, buttons, modifiers, wheel_delta);
return true;
}
return false;
}