Truly independent web browser
LibWeb+Compositor: Let later keys pass one with nothing to scroll
Andreas Klingc38b5e59f030
diff --git a/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp b/Libraries/LibWeb/HTML/EventLoop/EventLoop.cppindex c18512077e9..0f877510ed7 100644--- a/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp+++ b/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp@@ -475,7 +475,7 @@ void EventLoop::process_input_events() const[&](Web::KeyEvent const& key_event) {switch (key_event.type) {case Web::KeyEvent::Type::KeyDown:- return page.handle_keydown(key_event.key, key_event.modifiers, key_event.code_point, key_event.repeat, key_event.should_insert_text, key_event.async_scroll_performed_default_action, key_event.editing_commands, key_event.native_editing_binding_resolved);+ return page.handle_keydown(key_event.key, key_event.modifiers, key_event.code_point, key_event.repeat, key_event.should_insert_text, key_event.async_scroll_performed_default_action, key_event.editing_commands, key_event.native_editing_binding_resolved, key_event.user_input_scroll_count_with_nothing_to_scroll);case Web::KeyEvent::Type::KeyUp:return page.handle_keyup(key_event.key, key_event.modifiers, key_event.code_point, key_event.repeat);}diff --git a/Libraries/LibWeb/HTML/LocalNavigable.cpp b/Libraries/LibWeb/HTML/LocalNavigable.cppindex e9bcd147d1d..cea3fe9c800 100644--- a/Libraries/LibWeb/HTML/LocalNavigable.cpp+++ b/Libraries/LibWeb/HTML/LocalNavigable.cpp@@ -7363,6 +7363,14 @@ bool LocalNavigable::set_scroll_offset_for_user_input(Layout::Node& scrolling_boreturn did_scroll;}+u64 LocalNavigable::compositor_user_input_scroll_count() const+{+ u64 count = 0;+ for (auto const& entry : m_compositor_user_input_scroll_counts)+ count += entry.value;+ return count;+}+void LocalNavigable::did_change_scroll_offset(Web::AsyncScrollNodeStableID stable_node_id){note_scroll_sequence_moved(stable_node_id);diff --git a/Libraries/LibWeb/HTML/LocalNavigable.h b/Libraries/LibWeb/HTML/LocalNavigable.hindex 9896b0e1686..0d1d06c64e8 100644--- a/Libraries/LibWeb/HTML/LocalNavigable.h+++ b/Libraries/LibWeb/HTML/LocalNavigable.h@@ -254,6 +254,7 @@ public:void perform_scroll_of_viewport_scrolling_box(CSSPixelPoint position);// Every change of a scroll offset here, other than one the compositor made, goes to the compositor as a request.void did_change_scroll_offset(Web::AsyncScrollNodeStableID);+ u64 compositor_user_input_scroll_count() const;// Changes a scrolling box's offset for user input on this thread, which the compositor is asked for as the user's.bool set_scroll_offset_for_user_input(Layout::Node& scrolling_box, Web::AsyncScrollNodeStableID, CSSPixelPoint);void adopt_pending_async_scroll_offsets(Compositing::AsyncScrollUpdateFreshness = Compositing::AsyncScrollUpdateFreshness::Pushed);diff --git a/Libraries/LibWeb/Page/Page.cpp b/Libraries/LibWeb/Page/Page.cppindex 41a33c4c3c4..03610ab9cc2 100644--- a/Libraries/LibWeb/Page/Page.cpp+++ b/Libraries/LibWeb/Page/Page.cpp@@ -646,7 +646,7 @@ EventResult Page::handle_pinch_event(DevicePixelPoint position, unsigned modifiereturn handle_pinch_event(local_traversable(), event, nullptr);}-EventResult Page::handle_keydown(UIEvents::KeyCode key, unsigned modifiers, u32 code_point, bool repeat, bool should_insert_text, bool async_scroll_performed_default_action, ReadonlySpan<TextEditingCommand> editing_commands, bool native_editing_binding_resolved)+EventResult Page::handle_keydown(UIEvents::KeyCode key, unsigned modifiers, u32 code_point, bool repeat, bool should_insert_text, bool async_scroll_performed_default_action, ReadonlySpan<TextEditingCommand> editing_commands, bool native_editing_binding_resolved, Optional<u64> user_input_scroll_count_with_nothing_to_scroll){// The compositor forwards these updates ahead of keyboard events. Both DOM listeners and a main-thread// fallback default action must observe the offsets that have already been presented.@@ -654,6 +654,12 @@ EventResult Page::handle_keydown(UIEvents::KeyCode key, unsigned modifiers, u32if (!navigable)return EventResult::Dropped;navigable->local_root()->adopt_pending_async_scroll_offsets(Compositing::AsyncScrollUpdateFreshness::Pushed);+ // NB: The compositor found nothing to scroll for this key, and left it here for content the page may have added+ // past the end of a scroll range it knew. This thread may hear of the key late. Once the compositor has+ // scrolled for later input, the key has been left behind, and scrolling for it now would go against that input.+ if (user_input_scroll_count_with_nothing_to_scroll.has_value()+ && navigable->local_root()->compositor_user_input_scroll_count() > *user_input_scroll_count_with_nothing_to_scroll)+ async_scroll_performed_default_action = true;return navigable->event_handler().handle_keydown(key, modifiers, code_point, repeat, should_insert_text, async_scroll_performed_default_action, editing_commands, native_editing_binding_resolved);}diff --git a/Libraries/LibWeb/Page/Page.h b/Libraries/LibWeb/Page/Page.hindex ca39e0dfdce..dda565ce251 100644--- a/Libraries/LibWeb/Page/Page.h+++ b/Libraries/LibWeb/Page/Page.h@@ -222,7 +222,7 @@ public:EventResult handle_drag_and_drop_event(DragEvent::Type, DevicePixelPoint, DevicePixelPoint screen_position, unsigned button, unsigned buttons, unsigned modifiers, Vector<HTML::SelectedFile> files);EventResult handle_pinch_event(DevicePixelPoint point, unsigned modifiers, double scale);- EventResult handle_keydown(UIEvents::KeyCode, unsigned modifiers, u32 code_point, bool repeat, bool should_insert_text, bool async_scroll_performed_default_action = false, ReadonlySpan<TextEditingCommand> editing_commands = {}, bool native_editing_binding_resolved = false);+ EventResult handle_keydown(UIEvents::KeyCode, unsigned modifiers, u32 code_point, bool repeat, bool should_insert_text, bool async_scroll_performed_default_action = false, ReadonlySpan<TextEditingCommand> editing_commands = {}, bool native_editing_binding_resolved = false, Optional<u64> user_input_scroll_count_with_nothing_to_scroll = {});EventResult handle_keyup(UIEvents::KeyCode, unsigned modifiers, u32 code_point, bool repeat);void handle_gamepad_connected(Gamepad::GamepadDescription const&);diff --git a/Libraries/LibWebCommon/Page/InputEvent.cpp b/Libraries/LibWebCommon/Page/InputEvent.cppindex 0177ed88d7c..38313533bf2 100644--- a/Libraries/LibWebCommon/Page/InputEvent.cpp+++ b/Libraries/LibWebCommon/Page/InputEvent.cpp@@ -28,7 +28,7 @@ Optional<PointerProperties> PointerInputData::properties() constKeyEvent KeyEvent::clone_without_browser_data() const{- return { type, key, modifiers, code_point, repeat, should_insert_text, nullptr, async_scroll_performed_default_action, id, editing_commands, native_editing_binding_resolved };+ return { type, key, modifiers, code_point, repeat, should_insert_text, nullptr, async_scroll_performed_default_action, user_input_scroll_count_with_nothing_to_scroll, id, editing_commands, native_editing_binding_resolved };}MouseEvent MouseEvent::clone_without_browser_data() constdiff --git a/Libraries/LibWebCommon/Page/InputEvent.h b/Libraries/LibWebCommon/Page/InputEvent.hindex cc7ec7136b4..d0d0e4174cc 100644--- a/Libraries/LibWebCommon/Page/InputEvent.h+++ b/Libraries/LibWebCommon/Page/InputEvent.h@@ -43,6 +43,8 @@ struct WEBCOMMON_API KeyEvent {OwnPtr<BrowserInputData> browser_data {};bool async_scroll_performed_default_action { false };+ // How many scrolls for user input the compositor had made when it found nothing to scroll for this key.+ Optional<u64> user_input_scroll_count_with_nothing_to_scroll {};// The UI process numbers the events it sends, and a completion names the event it finished.u64 id { 0 };diff --git a/Libraries/LibWebCommon/Page/InputEvent.ipc b/Libraries/LibWebCommon/Page/InputEvent.ipcindex f3534a02f62..b7b09afafca 100644--- a/Libraries/LibWebCommon/Page/InputEvent.ipc+++ b/Libraries/LibWebCommon/Page/InputEvent.ipc@@ -51,6 +51,7 @@ struct Web::KeyEvent {bool repeat;bool should_insert_text;bool async_scroll_performed_default_action;+ Optional<u64> user_input_scroll_count_with_nothing_to_scroll;u64 id;Vector<Web::TextEditingCommand> editing_commands;bool native_editing_binding_resolved;diff --git a/Libraries/LibWebView/WebContentPage.cpp b/Libraries/LibWebView/WebContentPage.cppindex 857fad0ca28..1b8ced49af4 100644--- a/Libraries/LibWebView/WebContentPage.cpp+++ b/Libraries/LibWebView/WebContentPage.cpp@@ -2105,8 +2105,9 @@ void WebContentPage::did_request_key_event_for_testing(Web::KeyEvent event)client().did_misbehave("did_request_key_event_for_testing"sv, "not driven by WebDriver or a test"sv);return;}- // Only the compositor sets this, on its way to WebContent.+ // Only the compositor sets these, on its way to WebContent.event.async_scroll_performed_default_action = false;+ event.user_input_scroll_count_with_nothing_to_scroll = {};view().enqueue_input_event(move(event));}diff --git a/Services/Compositor/CompositorState.cpp b/Services/Compositor/CompositorState.cppindex c599ebcbb0d..f766873b7ac 100644--- a/Services/Compositor/CompositorState.cpp+++ b/Services/Compositor/CompositorState.cpp@@ -602,17 +602,21 @@ void CompositorState::handle_and_dispatch_key_event(Web::CompositorContextId conreturn;}+ bool later_keys_need_not_wait = false;if (allow_keyboard_scroll) {if (auto box = context->box_scrolled_for_key_event(event); box.has_value())dispatch_scroll_fling_step(context_id, *context, context->end_momentum_scrolling(box.value()), MonotonicTime::now());- auto handled = apply_context_update_result(context_id, *context, context->handle_key_event(event));- event.async_scroll_performed_default_action = handled && event.type == Web::KeyEvent::Type::KeyDown;+ auto result = context->handle_key_event(event);+ later_keys_need_not_wait = apply_context_update_result(context_id, *context, result) && event.type == Web::KeyEvent::Type::KeyDown;+ event.async_scroll_performed_default_action = later_keys_need_not_wait && !result.key_found_nothing_to_scroll;+ if (later_keys_need_not_wait && result.key_found_nothing_to_scroll)+ event.user_input_scroll_count_with_nothing_to_scroll = context->user_input_scroll_count();}// The scroll updates the event produced reach WebContent ahead of the event itself on the same connection.publish_pending_async_scroll_updates(context_id, *context);context->dispatch_key_event_to_web_content(event);- presentation->did_dispatch_input_event(context_id, event.id, event.async_scroll_performed_default_action);+ presentation->did_dispatch_input_event(context_id, event.id, later_keys_need_not_wait);}void CompositorState::handle_and_dispatch_mouse_event(Web::CompositorContextId context_id, Web::MouseEvent event)diff --git a/Services/Compositor/ContextState.cpp b/Services/Compositor/ContextState.cppindex 0929bc84349..7552b69c48e 100644--- a/Services/Compositor/ContextState.cpp+++ b/Services/Compositor/ContextState.cpp@@ -290,9 +290,7 @@ Optional<ContextState::KeyScroll> ContextState::key_scroll_for(Web::KeyEvent con// Falling back would cancel the running animation and lose the steps already accumulated in its destination.if (!target_scroll_node_index.has_value())target_scroll_node_index = m_async_scroll_tree.scroll_node_for_keyboard_scroll(key_step->keyboard_scroll_target, key_step->delta, m_scroll_state_snapshot);- if (!target_scroll_node_index.has_value())- return {};- return KeyScroll { .step = key_step.release_value(), .target_scroll_node_index = target_scroll_node_index.value() };+ return KeyScroll { .step = key_step.release_value(), .target_scroll_node_index = target_scroll_node_index };}Optional<Web::AsyncScrollNodeStableID> ContextState::box_scrolled_for_key_event(Web::KeyEvent const& event) const@@ -300,9 +298,9 @@ Optional<Web::AsyncScrollNodeStableID> ContextState::box_scrolled_for_key_event(if (event.type != Web::KeyEvent::Type::KeyDown)return {};auto key_scroll = key_scroll_for(event);- if (!key_scroll.has_value())+ if (!key_scroll.has_value() || !key_scroll->target_scroll_node_index.has_value())return {};- return m_async_scroll_tree.scroll_node_for_index(key_scroll->target_scroll_node_index)->stable_node_id;+ return m_async_scroll_tree.scroll_node_for_index(*key_scroll->target_scroll_node_index)->stable_node_id;}ContextState::ContextUpdateResult ContextState::handle_key_event(Web::KeyEvent const& event)@@ -324,11 +322,15 @@ ContextState::ContextUpdateResult ContextState::handle_key_event(Web::KeyEvent cauto key_scroll = key_scroll_for(event);if (!key_scroll.has_value())return {};+ // NB: With nothing to scroll here, the main thread must not hold back the keys after this one. It still gets to+ // scroll for this one, as content it added may have extended the scroll range since the compositor last heard.+ if (!key_scroll->target_scroll_node_index.has_value())+ return { .accepted = true, .key_found_nothing_to_scroll = true };auto const& key_step = key_scroll->step;auto delta = key_step.delta;auto scrolls_to_extent = key_step.scrolls_to_extent;auto now = MonotonicTime::now();- auto target_scroll_node_index = key_scroll->target_scroll_node_index;+ auto target_scroll_node_index = *key_scroll->target_scroll_node_index;m_keyboard_scroll_controller.hold_key(event.key);@@ -969,6 +971,14 @@ void ContextState::note_user_input_scrolled(Web::AsyncScrollNodeStableID stable_m_user_input_scroll_counts.ensure(stable_node_id)++;}+u64 ContextState::user_input_scroll_count() const+{+ u64 count = 0;+ for (auto const& entry : m_user_input_scroll_counts)+ count += entry.value;+ return count;+}+Optional<Gfx::Orientation> ContextState::axis_held_by_scrollbar_of(Compositing::SpatialNodeIndex scroll_node_index) const{// Paging steps toward the pointer like keys do, and takes a script's scroll along like them.diff --git a/Services/Compositor/ContextState.h b/Services/Compositor/ContextState.hindex 10f0821cbaa..f5e592ae1e7 100644--- a/Services/Compositor/ContextState.h+++ b/Services/Compositor/ContextState.h@@ -88,6 +88,8 @@ public:bool accepted { false };Optional<PendingFrame> frame_to_present {};bool should_request_rendering_update { false };+ // Whether a key event found nothing to scroll by the compositor's scroll range, which may lag behind the page's.+ bool key_found_nothing_to_scroll { false };Optional<Web::ScrollbarDraggedByCompositor> scrollbar_dragged_by_compositor {};// Why a wheel or mouse event was taken or turned away, and the scroller it was routed to.Optional<Web::InputRoutingDecision> routing_decision {};@@ -247,6 +249,7 @@ public:// Whether this context's page may cancel the ctrl+wheel event of a pinch at the position, or has yet to tell.bool page_may_cancel_pinch_at(Gfx::FloatPoint position);bool pinch_gesture_was_let_through() const { return m_pinch_gesture_was_let_through; }+ u64 user_input_scroll_count() const;void set_pinch_gesture_was_let_through(bool was_let_through) { m_pinch_gesture_was_let_through = was_let_through; }bool may_hand_first_wheel_steps_to_nested_contexts() const;@@ -367,7 +370,8 @@ private:void end_keyboard_scroll_gesture();struct KeyScroll {KeyboardScrollController::KeyStep step;- Compositing::SpatialNodeIndex target_scroll_node_index;+ // Empty if every box the key could scroll is at the end of its scroll range.+ Optional<Compositing::SpatialNodeIndex> target_scroll_node_index;};Optional<KeyScroll> key_scroll_for(Web::KeyEvent const&) const;Optional<Web::MouseEvent> interrupt_reported_scroll_momentum();diff --git a/Tests/LibWeb/Text/expected/async-scrolling/key-scrolls-beside-a-busy-task-after-a-key-with-nothing-to-scroll.txt b/Tests/LibWeb/Text/expected/async-scrolling/key-scrolls-beside-a-busy-task-after-a-key-with-nothing-to-scroll.txtindex 4cecbb1d1bc..e34b43d7412 100644--- a/Tests/LibWeb/Text/expected/async-scrolling/key-scrolls-beside-a-busy-task-after-a-key-with-nothing-to-scroll.txt+++ b/Tests/LibWeb/Text/expected/async-scrolling/key-scrolls-beside-a-busy-task-after-a-key-with-nothing-to-scroll.txt@@ -1,3 +1,3 @@-beside the task: compositor offset=0+beside the task: compositor offset=100keys the page heard of: ArrowUp, ArrowDown-main thread offset: 0+main thread offset: 100diff --git a/Tests/LibWeb/Text/expected/async-scrolling/key-scrolls-into-content-added-at-the-end-of-the-page.txt b/Tests/LibWeb/Text/expected/async-scrolling/key-scrolls-into-content-added-at-the-end-of-the-page.txtnew file mode 100644index 00000000000..ea030dd402e--- /dev/null+++ b/Tests/LibWeb/Text/expected/async-scrolling/key-scrolls-into-content-added-at-the-end-of-the-page.txt@@ -0,0 +1 @@+the key scrolled into the new content: truediff --git a/Tests/LibWeb/Text/expected/async-scrolling/key-with-nothing-to-scroll-leaves-later-keys-alone.txt b/Tests/LibWeb/Text/expected/async-scrolling/key-with-nothing-to-scroll-leaves-later-keys-alone.txtnew file mode 100644index 00000000000..7e85a690c97--- /dev/null+++ b/Tests/LibWeb/Text/expected/async-scrolling/key-with-nothing-to-scroll-leaves-later-keys-alone.txt@@ -0,0 +1,4 @@+beside the task, the compositor scrolled past the first Down: true+keys the page heard of: ArrowUp, ArrowDown, ArrowDown+compositor offset once settled: 200+main thread offset: 200diff --git a/Tests/LibWeb/Text/input/async-scrolling/key-scrolls-into-content-added-at-the-end-of-the-page.html b/Tests/LibWeb/Text/input/async-scrolling/key-scrolls-into-content-added-at-the-end-of-the-page.htmlnew file mode 100644index 00000000000..d1acb49a93c--- /dev/null+++ b/Tests/LibWeb/Text/input/async-scrolling/key-scrolls-into-content-added-at-the-end-of-the-page.html@@ -0,0 +1,31 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<style>+ body {+ margin: 0;+ height: 5000px;+ }+</style>+<script>+ // The compositor finds nothing to scroll for a key at the end of the scroll range it knows, which is older than the+ // content this task adds. That key must still scroll into the new content, once the main thread gets to it.+ promiseTest(async () => {+ window.scrollTo(0, document.documentElement.scrollHeight);+ await animationFrame();+ await animationFrame();+ internals.synchronizeCompositor();+ const start = window.scrollY;++ document.body.style.height = "10000px";+ internals.sendKeyThroughUIProcess("Down");+ const waitUntil = performance.now() + 100;+ while (performance.now() < waitUntil) {+ // Stay in this task, so that the compositor handles the key before it hears of the new content.+ }++ // NB: This only gives up on what never comes, so the time limit can't fail a slow machine's run of a fixed engine.+ const giveUpAt = performance.now() + 2000;+ while (window.scrollY === start && performance.now() < giveUpAt) await animationFrame();+ println(`the key scrolled into the new content: ${window.scrollY > start}`);+ });+</script>diff --git a/Tests/LibWeb/Text/input/async-scrolling/key-with-nothing-to-scroll-leaves-later-keys-alone.html b/Tests/LibWeb/Text/input/async-scrolling/key-with-nothing-to-scroll-leaves-later-keys-alone.htmlnew file mode 100644index 00000000000..b5385e9ea33--- /dev/null+++ b/Tests/LibWeb/Text/input/async-scrolling/key-with-nothing-to-scroll-leaves-later-keys-alone.html@@ -0,0 +1,46 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<style>+ body {+ margin: 0;+ height: 5000px;+ }+</style>+<script>+ // Beside a task, Up at the top of the page goes nowhere and the compositor scrolls for the Downs after it. The main+ // thread hears of Up only once the task ends, by which time the user has moved on.+ promiseTest(async () => {+ const compositorOffset = () => internals.compositorScrollState().scrollOffset.y;+ const keys = [];+ document.addEventListener("keyup", event => keys.push(event.key));+ await animationFrame();+ await animationFrame();+ internals.synchronizeCompositor();++ for (const key of ["Up", "Down", "Down"]) internals.sendKeyThroughUIProcess(key);++ // NB: These only give up on what never comes, so the time limits can't fail a slow machine's run of a fixed engine.+ let giveUpAt = performance.now() + 5000;+ while (compositorOffset() <= 100 && performance.now() < giveUpAt) {+ // Stay in this task.+ }+ println(`beside the task, the compositor scrolled past the first Down: ${compositorOffset() > 100}`);++ giveUpAt = performance.now() + 5000;+ while (keys.length < 3 && performance.now() < giveUpAt) await animationFrame();+ println(`keys the page heard of: ${keys.join(", ")}`);++ // NB: The scroll eases to its end, so an offset only counts as settled once it stays put for a while.+ let framesAtRest = 0;+ let lastOffset = compositorOffset();+ giveUpAt = performance.now() + 5000;+ while (framesAtRest < 20 && performance.now() < giveUpAt) {+ await new Promise(resolve => setTimeout(resolve, 16));+ const offset = compositorOffset();+ framesAtRest = offset === lastOffset ? framesAtRest + 1 : 0;+ lastOffset = offset;+ }+ println(`compositor offset once settled: ${lastOffset}`);+ println(`main thread offset: ${window.scrollY}`);+ });+</script>