Skip to content

Truly independent web browser

LibWeb: Wait for the user's scroll to stop before updating the hover

Andreas Kling62343f18d635
diff --git a/Libraries/LibWeb/HTML/LocalNavigable.cpp b/Libraries/LibWeb/HTML/LocalNavigable.cpp
index 9aec7fcd256..4b128c9ec81 100644
--- a/Libraries/LibWeb/HTML/LocalNavigable.cpp
+++ b/Libraries/LibWeb/HTML/LocalNavigable.cpp
@@ -6053,6 +6053,14 @@ void LocalNavigable::schedule_hover_update_after_async_scroll()
m_async_scroll_hover_update_timer->restart(AK::Duration::from_milliseconds(hover_update_after_async_scroll_delay_ms));
}
+bool LocalNavigable::leave_hover_update_to_end_of_user_scroll()
+{
+ if (!any_of(m_pending_async_scroll_operations, [](auto const& pending) { return pending.trigger == ScrollTrigger::UserInput; }))
+ return false;
+ schedule_hover_update_after_async_scroll();
+ return true;
+}
+
void LocalNavigable::update_hover_after_async_scroll_stops()
{
if (has_been_destroyed())
diff --git a/Libraries/LibWeb/HTML/LocalNavigable.h b/Libraries/LibWeb/HTML/LocalNavigable.h
index 04019ea49ec..0470a2d247a 100644
--- a/Libraries/LibWeb/HTML/LocalNavigable.h
+++ b/Libraries/LibWeb/HTML/LocalNavigable.h
@@ -267,6 +267,8 @@ public:
void expect_wait_for_async_scroll_operation(Compositing::AsyncScrollOperationID);
void forget_expected_wait_for_async_scroll_operation(Compositing::AsyncScrollOperationID);
void wait_for_wheel_scrolls(Vector<GC::Ref<WebIDL::Promise>>&);
+ // Answers whether a scroll of the user's is still moving, which updates the hover once it stops.
+ bool leave_hover_update_to_end_of_user_scroll();
void clamp_viewport_scroll_offset();
// https://html.spec.whatwg.org/multipage/webappapis.html#rendering-opportunity
diff --git a/Libraries/LibWeb/Page/EventHandler.cpp b/Libraries/LibWeb/Page/EventHandler.cpp
index 12faabfefbb..98845d63c83 100644
--- a/Libraries/LibWeb/Page/EventHandler.cpp
+++ b/Libraries/LibWeb/Page/EventHandler.cpp
@@ -1402,7 +1402,7 @@ void EventHandler::update_hover_at_last_known_mouse_position(Optional<i64> visua
if (m_mousedown_target)
return;
- update_hover_after_scroll(*m_last_known_mouse_visual_viewport_position, m_last_known_mouse_screen_position, UIEvents::MouseButton::None, m_last_known_mouse_buttons, m_last_known_mouse_modifiers, {}, visual_animation_sample_time_nanoseconds);
+ update_hover_after_scroll(HoverUpdateCause::PointerRests, *m_last_known_mouse_visual_viewport_position, m_last_known_mouse_screen_position, UIEvents::MouseButton::None, m_last_known_mouse_buttons, m_last_known_mouse_modifiers, {}, visual_animation_sample_time_nanoseconds);
}
// AD-HOC: `hover_target`, where given, is the element the render clock's lane hovered at the position, which the screen
@@ -1422,7 +1422,7 @@ void EventHandler::update_hover_at(CSSPixelPoint visual_viewport_position, GC::P
Optional<i64> visual_animation_sample_time_nanoseconds;
if (!hover_target)
visual_animation_sample_time_nanoseconds = m_visual_animation_sample_time_nanoseconds;
- update_hover_after_scroll(visual_viewport_position, screen_position, UIEvents::MouseButton::None, m_last_known_mouse_buttons, m_last_known_mouse_modifiers, hover_target, visual_animation_sample_time_nanoseconds);
+ update_hover_after_scroll(HoverUpdateCause::PointerMoved, visual_viewport_position, screen_position, UIEvents::MouseButton::None, m_last_known_mouse_buttons, m_last_known_mouse_modifiers, hover_target, visual_animation_sample_time_nanoseconds);
}
// AD-HOC: The hover and the cursor follow what is under the pointer while it rests. A frame may move what is there,
@@ -1508,12 +1508,18 @@ bool EventHandler::update_hover_where_the_pointer_rests(HTML::HoverUpdatePoint h
return false;
}
-void EventHandler::update_hover_after_scroll(CSSPixelPoint visual_viewport_position, CSSPixelPoint screen_position, unsigned button, unsigned buttons, unsigned modifiers, GC::Ptr<DOM::Node> hover_target, Optional<i64> visual_animation_sample_time_nanoseconds)
+void EventHandler::update_hover_after_scroll(HoverUpdateCause cause, CSSPixelPoint visual_viewport_position, CSSPixelPoint screen_position, unsigned button, unsigned buttons, unsigned modifiers, GC::Ptr<DOM::Node> hover_target, Optional<i64> visual_animation_sample_time_nanoseconds)
{
if (m_effective_legacy_mouse_pointer_type != PointerType::Mouse)
return;
if (should_ignore_device_input_event())
return;
+ // NB: A pointer at rest waits for a scroll of the user's to stop moving, here or in the frame the update reached.
+ // The update then starts from this position, of which a nested navigable hears nothing else.
+ if (cause == HoverUpdateCause::PointerRests && m_navigable->leave_hover_update_to_end_of_user_scroll()) {
+ record_last_known_mouse_position(visual_viewport_position, screen_position, buttons, modifiers);
+ return;
+ }
auto document = m_navigable->active_document();
if (!document || !document->is_fully_active())
@@ -1590,7 +1596,7 @@ void EventHandler::update_hover_after_scroll(CSSPixelPoint visual_viewport_posit
if (!hover_target) {
auto nested_navigable = local_nested_navigable_at(*target_layout_node, node);
auto dispatch_result = dispatch_event_to_nested_navigable(*this, *target_layout_node, node, visual_viewport_position, nullptr, [&](EventHandler& event_handler, CSSPixelPoint position) {
- event_handler.update_hover_after_scroll(position, screen_position, button, buttons, modifiers);
+ event_handler.update_hover_after_scroll(cause, position, screen_position, button, buttons, modifiers);
return EventResult::Handled;
});
if (dispatch_result.has_value()) {
diff --git a/Libraries/LibWeb/Page/EventHandler.h b/Libraries/LibWeb/Page/EventHandler.h
index aca23ad1e3d..5959e8855ab 100644
--- a/Libraries/LibWeb/Page/EventHandler.h
+++ b/Libraries/LibWeb/Page/EventHandler.h
@@ -292,7 +292,11 @@ private:
void clear_mousedown_tracking();
void stop_updating_selection();
- void update_hover_after_scroll(CSSPixelPoint visual_viewport_position, CSSPixelPoint screen_position, unsigned button, unsigned buttons, unsigned modifiers, GC::Ptr<DOM::Node> hover_target = {}, Optional<i64> visual_animation_sample_time_nanoseconds = {});
+ enum class HoverUpdateCause : u8 {
+ PointerMoved,
+ PointerRests,
+ };
+ void update_hover_after_scroll(HoverUpdateCause, CSSPixelPoint visual_viewport_position, CSSPixelPoint screen_position, unsigned button, unsigned buttons, unsigned modifiers, GC::Ptr<DOM::Node> hover_target = {}, Optional<i64> visual_animation_sample_time_nanoseconds = {});
EventResult dispatch_wheel_event(Layout::BegunRead const& read, Layout::Node&, CSSPixelPoint visual_viewport_position, CSSPixelPoint screen_position, unsigned button, unsigned buttons, unsigned modifiers, double wheel_delta_x, double wheel_delta_y, double wheel_tick_120ths_x, double wheel_tick_120ths_y, bool is_cancelable);
// Both drop the latch when what it refers to is gone from the document, so that the event is targeted afresh.
Layout::Node* validated_latched_wheel_event_target_layout_node(Layout::BegunRead const& read, DOM::Document&);
diff --git a/Tests/LibWeb/Text/expected/async-scrolling/hover-in-an-iframe-waits-for-its-scroll-as-the-parent-scrolls.txt b/Tests/LibWeb/Text/expected/async-scrolling/hover-in-an-iframe-waits-for-its-scroll-as-the-parent-scrolls.txt
new file mode 100644
index 00000000000..df6a688d6a6
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/async-scrolling/hover-in-an-iframe-waits-for-its-scroll-as-the-parent-scrolls.txt
@@ -0,0 +1,4 @@
+before the scroll, #first matches :hover: true
+after the long frame, #first matches :hover: true
+after the long frame, mouseout count: 0
+after the scroll, #rest matches :hover: true
diff --git a/Tests/LibWeb/Text/expected/async-scrolling/hover-waits-for-a-scroll-that-outlasts-a-long-frame.txt b/Tests/LibWeb/Text/expected/async-scrolling/hover-waits-for-a-scroll-that-outlasts-a-long-frame.txt
new file mode 100644
index 00000000000..df6a688d6a6
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/async-scrolling/hover-waits-for-a-scroll-that-outlasts-a-long-frame.txt
@@ -0,0 +1,4 @@
+before the scroll, #first matches :hover: true
+after the long frame, #first matches :hover: true
+after the long frame, mouseout count: 0
+after the scroll, #rest matches :hover: true
diff --git a/Tests/LibWeb/Text/input/async-scrolling/hover-in-an-iframe-waits-for-its-scroll-as-the-parent-scrolls.html b/Tests/LibWeb/Text/input/async-scrolling/hover-in-an-iframe-waits-for-its-scroll-as-the-parent-scrolls.html
new file mode 100644
index 00000000000..0270de26bd9
--- /dev/null
+++ b/Tests/LibWeb/Text/input/async-scrolling/hover-in-an-iframe-waits-for-its-scroll-as-the-parent-scrolls.html
@@ -0,0 +1,64 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<style>
+ body {
+ margin: 0;
+ }
+
+ #spacer {
+ height: 3000px;
+ }
+
+ iframe {
+ display: block;
+ width: 300px;
+ height: 200px;
+ border: 0;
+ }
+</style>
+<iframe srcdoc="<!DOCTYPE html><style>body { margin: 0 } #first { height: 2px } #rest { height: 3000px }</style><div id=first></div><div id=rest></div>"></iframe>
+<div id="spacer"></div>
+<script>
+ // The parent's hover update after its own scroll follows the pointer into the iframe, whose scroll goes on: a wheel
+ // step of 120px animates for 200ms, past the long frame of 120ms that lets the parent's update come due.
+ promiseTest(async () => {
+ const iframe = document.querySelector("iframe");
+ if (iframe.contentDocument?.readyState !== "complete" || iframe.contentDocument.URL !== "about:srcdoc")
+ await new Promise(resolve => iframe.addEventListener("load", resolve, { once: true }));
+ await animationFrame();
+ await animationFrame();
+
+ const inner = iframe.contentDocument;
+ const first = inner.getElementById("first");
+ const rest = inner.getElementById("rest");
+ let mouseOuts = 0;
+ inner.addEventListener("mouseout", () => ++mouseOuts);
+ internals.mouseMove(1, 1);
+ println(`before the scroll, #first matches :hover: ${first.matches(":hover")}`);
+
+ const afterLongFrame = Promise.withResolvers();
+ let parentScrolled = false;
+ inner.addEventListener("scroll", async () => {
+ if (parentScrolled || iframe.contentWindow.scrollY < 1)
+ return;
+ parentScrolled = true;
+ window.scrollTo(0, 1);
+ while (window.scrollY < 1)
+ await animationFrame();
+ const end = performance.now() + 120;
+ while (performance.now() < end);
+ setTimeout(() => {
+ println(`after the long frame, #first matches :hover: ${first.matches(":hover")}`);
+ println(`after the long frame, mouseout count: ${mouseOuts}`);
+ afterLongFrame.resolve();
+ }, 0);
+ });
+
+ await internals.wheel(1, 1, 0, 120);
+ await afterLongFrame.promise;
+
+ while (!rest.matches(":hover"))
+ await animationFrame();
+ println(`after the scroll, #rest matches :hover: ${rest.matches(":hover")}`);
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/async-scrolling/hover-waits-for-a-scroll-that-outlasts-a-long-frame.html b/Tests/LibWeb/Text/input/async-scrolling/hover-waits-for-a-scroll-that-outlasts-a-long-frame.html
new file mode 100644
index 00000000000..0dc53e8a5c1
--- /dev/null
+++ b/Tests/LibWeb/Text/input/async-scrolling/hover-waits-for-a-scroll-that-outlasts-a-long-frame.html
@@ -0,0 +1,52 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<style>
+ body {
+ margin: 0;
+ }
+
+ #first {
+ height: 2px;
+ }
+
+ #rest {
+ height: 3000px;
+ }
+</style>
+<div id="first"></div>
+<div id="rest"></div>
+<script>
+ // A frame that takes longer than the hover waits for the next scroll offset is not the end of the scroll. A wheel
+ // step of 120px animates for 200ms, past the long frame of 120ms, and the check comes in the first task after it.
+ promiseTest(async () => {
+ await animationFrame();
+ await animationFrame();
+
+ let mouseOuts = 0;
+ document.addEventListener("mouseout", () => ++mouseOuts);
+ internals.mouseMove(1, 1);
+ println(`before the scroll, #first matches :hover: ${first.matches(":hover")}`);
+
+ const afterLongFrame = Promise.withResolvers();
+ let frameWasLong = false;
+ document.addEventListener("scroll", () => {
+ if (frameWasLong || window.scrollY < 1)
+ return;
+ frameWasLong = true;
+ const end = performance.now() + 120;
+ while (performance.now() < end);
+ setTimeout(() => {
+ println(`after the long frame, #first matches :hover: ${first.matches(":hover")}`);
+ println(`after the long frame, mouseout count: ${mouseOuts}`);
+ afterLongFrame.resolve();
+ }, 0);
+ });
+
+ await internals.wheel(1, 1, 0, 120);
+ await afterLongFrame.promise;
+
+ while (!rest.matches(":hover"))
+ await animationFrame();
+ println(`after the scroll, #rest matches :hover: ${rest.matches(":hover")}`);
+ });
+</script>