Skip to content

Truly independent web browser

LibWeb: Use elapsed frame time for middle-button autoscroll

sideshowbarker83ec8b5b871d
diff --git a/Libraries/LibWeb/DOM/Document.cpp b/Libraries/LibWeb/DOM/Document.cpp
index 2adb757f0fe..279d6d7558e 100644
--- a/Libraries/LibWeb/DOM/Document.cpp
+++ b/Libraries/LibWeb/DOM/Document.cpp
@@ -5515,12 +5515,12 @@ void Document::release_video_frames_held_for_callbacks()
video_element->release_presented_video_frame_held_for_callbacks();
}
-void Document::run_the_scroll_steps()
+void Document::run_the_scroll_steps(double frame_timestamp)
{
// AD-HOC: Process auto-scroll ticks before dispatching scroll events. This is tied to the rendering update to
// ensure exactly one auto-scroll tick per frame.
if (auto navigable = this->navigable())
- navigable->event_handler().process_auto_scroll();
+ navigable->event_handler().process_auto_scroll(frame_timestamp);
// FIXME: 1. For each scrolling box box that was scrolled:
// ...
diff --git a/Libraries/LibWeb/DOM/Document.h b/Libraries/LibWeb/DOM/Document.h
index 9fb40232b0a..3c5042de1ba 100644
--- a/Libraries/LibWeb/DOM/Document.h
+++ b/Libraries/LibWeb/DOM/Document.h
@@ -1031,7 +1031,7 @@ public:
void update_the_visibility_state(HTML::VisibilityState);
void run_the_resize_steps();
- void run_the_scroll_steps();
+ void run_the_scroll_steps(double frame_timestamp);
u32 increment_video_frame_request_callback_identifier() { return ++m_video_frame_request_callback_identifier; }
void run_the_video_frame_request_callbacks(double now);
diff --git a/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp b/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp
index 6b5c5327157..2b4297a19a1 100644
--- a/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp
+++ b/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp
@@ -1069,7 +1069,7 @@ void EventLoop::update_the_rendering()
// at FormAssociatedTextControlElement::CursorRevealAfterEdit.
if (auto* text_control = as_if<FormAssociatedTextControlElement>(document->focused_area().ptr()))
text_control->reveal_cursor_if_pending();
- document->run_the_scroll_steps();
+ document->run_the_scroll_steps(frame_timestamp);
}
// 10. For each doc of docs, evaluate media queries and report changes for doc. [CSSOMVIEW]
diff --git a/Libraries/LibWeb/Page/EventHandler.cpp b/Libraries/LibWeb/Page/EventHandler.cpp
index ab1da08cacf..c5ed7006380 100644
--- a/Libraries/LibWeb/Page/EventHandler.cpp
+++ b/Libraries/LibWeb/Page/EventHandler.cpp
@@ -3071,12 +3071,12 @@ void EventHandler::handle_gamepad_disconnected(Gamepad::GamepadHandle handle)
child_navigable->event_handler().handle_gamepad_disconnected(handle);
}
-void EventHandler::process_auto_scroll()
+void EventHandler::process_auto_scroll(double frame_timestamp)
{
if (m_auto_scroll_handler)
m_auto_scroll_handler->perform_tick();
if (m_middle_button_scroll_handler)
- m_middle_button_scroll_handler->perform_tick();
+ m_middle_button_scroll_handler->perform_tick(frame_timestamp);
}
static GC::Ptr<DOM::StaticRange> collapsed_delete_target_range_for_input_event(DOM::Document const& document, DOM::Range const& range, Utf16FlyString const& input_type)
diff --git a/Libraries/LibWeb/Page/EventHandler.h b/Libraries/LibWeb/Page/EventHandler.h
index a4bab04fd42..029d71e80e5 100644
--- a/Libraries/LibWeb/Page/EventHandler.h
+++ b/Libraries/LibWeb/Page/EventHandler.h
@@ -142,7 +142,7 @@ public:
void handle_gamepad_updated(Gamepad::GamepadState const&);
void handle_gamepad_disconnected(Gamepad::GamepadHandle);
- void process_auto_scroll();
+ void process_auto_scroll(double frame_timestamp);
enum class SelectionMode : u8 {
None,
diff --git a/Libraries/LibWeb/Page/MiddleButtonScrollHandler.cpp b/Libraries/LibWeb/Page/MiddleButtonScrollHandler.cpp
index 8ff7c814cfc..24884e16d34 100644
--- a/Libraries/LibWeb/Page/MiddleButtonScrollHandler.cpp
+++ b/Libraries/LibWeb/Page/MiddleButtonScrollHandler.cpp
@@ -19,7 +19,7 @@ namespace Web {
static constexpr double DEAD_ZONE_RADIUS { 15 };
static constexpr double SPEED_FACTOR = 5.0;
static constexpr double MAX_SPEED_PER_SECOND = 5000.0;
-static constexpr double SCROLL_INTERVAL_MS = 16.0;
+static constexpr double MAX_FRAME_INTERVAL_MS = 100.0;
MiddleButtonScrollHandler::MiddleButtonScrollHandler(DOM::Element& container, CSSPixelPoint origin)
: m_container_element(container)
@@ -63,8 +63,11 @@ GC::Ptr<DOM::Element> MiddleButtonScrollHandler::find_scrollable_ancestor(DOM::D
return {};
}
-void MiddleButtonScrollHandler::perform_tick()
+void MiddleButtonScrollHandler::perform_tick(double frame_timestamp)
{
+ auto elapsed_ms = clamp(frame_timestamp - m_last_frame_time.value_or(frame_timestamp), 0.0, MAX_FRAME_INTERVAL_MS);
+ m_last_frame_time = frame_timestamp;
+
auto distance_x = (m_mouse_position.x() - m_origin.x()).to_double();
auto distance_y = (m_mouse_position.y() - m_origin.y()).to_double();
@@ -81,7 +84,7 @@ void MiddleButtonScrollHandler::perform_tick()
auto speed_x = clamp(distance_x * SPEED_FACTOR, -MAX_SPEED_PER_SECOND, MAX_SPEED_PER_SECOND);
auto speed_y = clamp(distance_y * SPEED_FACTOR, -MAX_SPEED_PER_SECOND, MAX_SPEED_PER_SECOND);
- auto elapsed_seconds = SCROLL_INTERVAL_MS / 1000.0;
+ auto elapsed_seconds = elapsed_ms / 1000.0;
m_fractional_delta += CSSPixelPoint {
CSSPixels(speed_x * elapsed_seconds),
diff --git a/Libraries/LibWeb/Page/MiddleButtonScrollHandler.h b/Libraries/LibWeb/Page/MiddleButtonScrollHandler.h
index 7fc4c02a3cc..fc9104ca583 100644
--- a/Libraries/LibWeb/Page/MiddleButtonScrollHandler.h
+++ b/Libraries/LibWeb/Page/MiddleButtonScrollHandler.h
@@ -6,6 +6,7 @@
#pragma once
+#include <AK/Optional.h>
#include <AK/OwnPtr.h>
#include <LibGC/Ptr.h>
#include <LibJS/Heap/Cell.h>
@@ -26,7 +27,7 @@ public:
static GC::Ptr<DOM::Element> find_scrollable_ancestor(DOM::Document&, Layout::Node&);
void update_mouse_position(CSSPixelPoint position) { m_mouse_position = position; }
- void perform_tick();
+ void perform_tick(double frame_timestamp);
void request_indicator_repaint();
CSSPixelPoint origin() const { return m_origin; }
@@ -36,6 +37,7 @@ private:
GC::Ref<DOM::Element> m_container_element;
CSSPixelPoint m_origin;
CSSPixelPoint m_mouse_position;
+ Optional<double> m_last_frame_time;
CSSPixelPoint m_fractional_delta;
OwnPtr<HTML::UserScrollGestureHold> m_scroll_gesture_hold;
bool m_mouse_has_moved_beyond_dead_zone { false };
diff --git a/Tests/LibWeb/Text/expected/async-scrolling/middle-button-autoscroll-frame-time.txt b/Tests/LibWeb/Text/expected/async-scrolling/middle-button-autoscroll-frame-time.txt
new file mode 100644
index 00000000000..20ce762b55e
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/async-scrolling/middle-button-autoscroll-frame-time.txt
@@ -0,0 +1,4 @@
+20ms at 500px per second: 10
+40ms at 500px per second: 20
+200ms at 500px per second: 50
+back at origin: 0
diff --git a/Tests/LibWeb/Text/input/async-scrolling/middle-button-autoscroll-frame-time.html b/Tests/LibWeb/Text/input/async-scrolling/middle-button-autoscroll-frame-time.html
new file mode 100644
index 00000000000..7c179492a15
--- /dev/null
+++ b/Tests/LibWeb/Text/input/async-scrolling/middle-button-autoscroll-frame-time.html
@@ -0,0 +1,33 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<style>body { margin: 0; height: 2000px; }</style>
+<script>
+ promiseTest(async () => {
+ await animationFrame();
+ internals.setManualRenderingOpportunities(true);
+ try {
+ const start = performance.now();
+ async function frameAt(offset) {
+ await new Promise(resolve => setTimeout(resolve, Math.max(0, start + offset - performance.now()) + 1));
+ const painted = animationFrame();
+ internals.injectRenderingOpportunity(start + offset);
+ await painted;
+ return scrollY;
+ }
+ internals.mouseDown(50, 50, 1, internals.BUTTON_MIDDLE);
+ internals.mouseUp(50, 50, internals.BUTTON_MIDDLE);
+ internals.mouseMove(50, 150);
+ const before = await frameAt(0);
+ const first = await frameAt(20);
+ const second = await frameAt(60);
+ println(`20ms at 500px per second: ${first - before}`);
+ println(`40ms at 500px per second: ${second - first}`);
+ const afterPause = await frameAt(260);
+ println(`200ms at 500px per second: ${afterPause - second}`);
+ internals.mouseMove(50, 50);
+ println(`back at origin: ${await frameAt(280) - afterPause}`);
+ } finally {
+ internals.setManualRenderingOpportunities(false);
+ }
+ });
+</script>