Browse Source

LibWeb: Move mouse wheel handling from Paintable to PaintableBox

It is only PaintableBox that can have scrollable overflow so it doesn't
make sense to have handle_mousewheel() implementation in Paintable.

Also new implementation of handle_mousewheel() takes in account overflow
limits from scrollable_overflow_rect().
Aliaksandr Kalenik 1 year ago
parent
commit
c7673605bb

+ 1 - 12
Userland/Libraries/LibWeb/Painting/Paintable.cpp

@@ -34,19 +34,8 @@ Paintable::DispatchEventOfSameName Paintable::handle_mousemove(Badge<EventHandle
     return DispatchEventOfSameName::Yes;
 }
 
-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, int)
 {
-    if (auto const* containing_block = this->containing_block()) {
-        if (!containing_block->is_scrollable())
-            return false;
-        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<Layout::Box*>(containing_block)->set_scroll_offset(new_offset);
-        return true;
-    }
-
     return false;
 }
 

+ 7 - 4
Userland/Libraries/LibWeb/Painting/PaintableBox.cpp

@@ -663,13 +663,16 @@ void PaintableWithLines::paint(PaintContext& context, PaintPhase phase) const
     }
 }
 
-bool PaintableWithLines::handle_mousewheel(Badge<EventHandler>, CSSPixelPoint, unsigned, unsigned, int wheel_delta_x, int wheel_delta_y)
+bool PaintableBox::handle_mousewheel(Badge<EventHandler>, CSSPixelPoint, unsigned, unsigned, int wheel_delta_x, int wheel_delta_y)
 {
     if (!layout_box().is_scrollable())
         return false;
-    auto new_offset = layout_box().scroll_offset();
-    new_offset.translate_by(wheel_delta_x, wheel_delta_y);
-    layout_box().set_scroll_offset(new_offset);
+    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 });
     return true;
 }
 

+ 2 - 1
Userland/Libraries/LibWeb/Painting/PaintableBox.h

@@ -126,6 +126,8 @@ public:
 
     virtual Optional<HitTestResult> hit_test(CSSPixelPoint, HitTestType) const override;
 
+    virtual bool handle_mousewheel(Badge<EventHandler>, CSSPixelPoint, unsigned buttons, unsigned modifiers, int wheel_delta_x, int wheel_delta_y) override;
+
     void invalidate_stacking_context();
 
     bool is_out_of_view(PaintContext&) const;
@@ -235,7 +237,6 @@ public:
 
     virtual void paint(PaintContext&, PaintPhase) const override;
     virtual bool wants_mouse_events() const override { return false; }
-    virtual bool handle_mousewheel(Badge<EventHandler>, CSSPixelPoint, unsigned buttons, unsigned modifiers, int wheel_delta_x, int wheel_delta_y) override;
 
     virtual Optional<HitTestResult> hit_test(CSSPixelPoint, HitTestType) const override;