Skip to content

Truly independent web browser

LibWeb+LibWebView: Let the compositor zoom for an uncanceled pinch

Andreas Klingafbdba994651
diff --git a/Libraries/LibWeb/Page/EventHandler.cpp b/Libraries/LibWeb/Page/EventHandler.cpp
index b7d3bc0c2fc..373b68353a2 100644
--- a/Libraries/LibWeb/Page/EventHandler.cpp
+++ b/Libraries/LibWeb/Page/EventHandler.cpp
@@ -2451,8 +2451,9 @@ EventResult EventHandler::handle_pinch_event(CSSPixelPoint point, PinchEvent con
return EventResult::Handled;
// The pinch zooms the visual viewport of the top-level traversable. A frame hosted apart from it leaves the zoom to
- // the page displaying the tab, which the UI asks once the event is left uncanceled.
- if (!m_navigable->is_top_level_traversable())
+ // the page displaying the tab, which the UI asks once the event is left uncanceled. The page leaves it to the
+ // compositor likewise, whose scale may be newer than ours, and zooms only where the compositor could not.
+ if (!m_navigable->is_top_level_traversable() || !event.wheel_event_was_dispatched)
return EventResult::Accepted;
document = m_navigable->active_document();
diff --git a/Libraries/LibWeb/Page/Page.cpp b/Libraries/LibWeb/Page/Page.cpp
index a713f82b021..41a33c4c3c4 100644
--- a/Libraries/LibWeb/Page/Page.cpp
+++ b/Libraries/LibWeb/Page/Page.cpp
@@ -624,7 +624,8 @@ EventResult Page::handle_drag_and_drop_event(DragEvent::Type type, DevicePixelPo
EventResult Page::handle_pinch_event(HTML::LocalNavigable& root, PinchEvent const& event, Optional<RemoteInputEventTarget>* remote_target)
{
- if (event.async_zoom_performed_default_action && root.is_top_level_traversable())
+ // The compositor forwards its scroll updates ahead of the event. Listeners must see the zoom already presented.
+ if (root.is_top_level_traversable())
root.adopt_pending_async_scroll_offsets(Compositing::AsyncScrollUpdateFreshness::Pushed);
return root.event_handler().handle_pinch_event(device_to_css_point(event.position), event, remote_target);
}
@@ -636,6 +637,12 @@ EventResult Page::handle_pinch_event(DevicePixelPoint position, unsigned modifie
.modifiers = static_cast<UIEvents::KeyModifier>(modifiers),
.scale_delta = scale,
};
+ auto result = handle_pinch_event(local_traversable(), event, nullptr);
+ if (result != EventResult::Accepted)
+ return result;
+
+ // NB: No UI process is there to ask for the zoom of an event left uncanceled.
+ event.wheel_event_was_dispatched = true;
return handle_pinch_event(local_traversable(), event, nullptr);
}
diff --git a/Libraries/LibWebCommon/Page/InputEvent.h b/Libraries/LibWebCommon/Page/InputEvent.h
index fe015a96f12..45bc3384331 100644
--- a/Libraries/LibWebCommon/Page/InputEvent.h
+++ b/Libraries/LibWebCommon/Page/InputEvent.h
@@ -192,8 +192,8 @@ struct WEBCOMMON_API PinchEvent {
Web::UIEvents::KeyModifier modifiers { Web::UIEvents::KeyModifier::Mod_None };
double scale_delta;
bool async_zoom_performed_default_action { false };
- // A frame another process hosts dispatched the pinch's ctrl+wheel event and did not cancel it, which leaves only the
- // zoom of the page's visual viewport to do.
+ // The page dispatched the pinch's ctrl+wheel event and did not cancel it, which leaves only the zoom of the page's
+ // visual viewport to do.
bool wheel_event_was_dispatched { false };
u64 id { 0 };
};
diff --git a/Libraries/LibWebView/ViewImplementation.cpp b/Libraries/LibWebView/ViewImplementation.cpp
index c2ed7f15ee1..bc53f362d43 100644
--- a/Libraries/LibWebView/ViewImplementation.cpp
+++ b/Libraries/LibWebView/ViewImplementation.cpp
@@ -1299,12 +1299,16 @@ void ViewImplementation::did_finish_handling_input_event(Badge<WebContentPage>,
// Keep the returned default action in its original position. Removing it and scheduling the queue first would
// let a later pinch overtake it, which changes the zoom when focal points differ or a scale limit is reached.
auto& pending_input = m_pending_input_events[*index];
+ auto pinch_default_action_returned = event_result == Web::EventResult::Accepted
+ || (pending_input.forwarded_to_child_frame && event_result != Web::EventResult::Handled && event_result != Web::EventResult::Cancelled);
if (auto* pinch_event = pending_input.event.get_pointer<Web::PinchEvent>();
- pinch_event && pending_input.forwarded_to_child_frame && !pinch_event->wheel_event_was_dispatched && !pinch_event->async_zoom_performed_default_action
- && event_result != Web::EventResult::Handled && event_result != Web::EventResult::Cancelled) {
+ pinch_event && pinch_default_action_returned && !pinch_event->wheel_event_was_dispatched && !pinch_event->async_zoom_performed_default_action) {
pinch_event->wheel_event_was_dispatched = true;
pending_input.endpoint = page();
pending_input.sent = false;
+ // NB: WebContent's answer travels on another connection than the compositor's and may overtake it. What the
+ // compositor still answers for this trip must not be taken for an answer to the next one.
+ pending_input.expects_stale_compositor_answer = pending_input.routed_through_compositor;
pending_input.routed_through_compositor = false;
send_queued_input_events();
return;
@@ -1486,6 +1490,8 @@ void ViewImplementation::did_dispatch_input_event_through_compositor(Badge<WebCo
return;
auto& pending = m_pending_input_events[*index];
+ if (exchange(pending.expects_stale_compositor_answer, false))
+ return;
if (!pending.routed_through_compositor)
return;
if (auto* key_event = pending.event.get_pointer<Web::KeyEvent>())
diff --git a/Libraries/LibWebView/ViewImplementation.h b/Libraries/LibWebView/ViewImplementation.h
index 521a26b0bb3..70bc8e2ff4a 100644
--- a/Libraries/LibWebView/ViewImplementation.h
+++ b/Libraries/LibWebView/ViewImplementation.h
@@ -948,6 +948,9 @@ protected:
// that dies forwards none of the events still marked this way.
bool routed_through_compositor { false };
+ // Set while an answer of the compositor is still to come for a trip through it that WebContent already finished.
+ bool expects_stale_compositor_answer { false };
+
// A key event stays routed through the compositor until it forwards the event or hands it back. Later input
// waits until then.
bool awaits_compositor_dispatch() const { return routed_through_compositor && event.has<Web::KeyEvent>(); }
diff --git a/Tests/LibWeb/Text/expected/async-scrolling/pinch-let-through-by-listener-keeps-the-compositor-zoom.txt b/Tests/LibWeb/Text/expected/async-scrolling/pinch-let-through-by-listener-keeps-the-compositor-zoom.txt
index fd16404727b..8b6a3fa4b79 100644
--- a/Tests/LibWeb/Text/expected/async-scrolling/pinch-let-through-by-listener-keeps-the-compositor-zoom.txt
+++ b/Tests/LibWeb/Text/expected/async-scrolling/pinch-let-through-by-listener-keeps-the-compositor-zoom.txt
@@ -1,5 +1,5 @@
before the listener's step: compositor scale=1.5
listener's step: cancelable=true
-after the listener's step, beside the task: compositor scale=2.25
-compositor scale after the task: 2.25
-main thread scale after the task: 2.25
+after the listener's step, beside the task: compositor scale=3.375
+compositor scale after the task: 3.375
+main thread scale after the task: 3.375
diff --git a/Tests/LibWeb/Text/input/UIEvents/pinch-in-local-iframe-keeps-wheel-in-child-document.html b/Tests/LibWeb/Text/input/UIEvents/pinch-in-local-iframe-keeps-wheel-in-child-document.html
index 69a8cf3c748..15c82e25d92 100644
--- a/Tests/LibWeb/Text/input/UIEvents/pinch-in-local-iframe-keeps-wheel-in-child-document.html
+++ b/Tests/LibWeb/Text/input/UIEvents/pinch-in-local-iframe-keeps-wheel-in-child-document.html
@@ -57,6 +57,8 @@
internals.pinchThroughUIProcess(200, 150, 1);
let event = await wheel;
println(`child accepted: ctrlKey=${event.ctrlKey} defaultPrevented=${event.defaultPrevented}`);
+ // The compositor zooms for the pinch once the page has let it through, and the page hears of that after this.
+ while (visualViewport.scale === 1) await animationFrame();
println(`parent wheel events: ${parentWheels}, scale: ${visualViewport.scale}`);
const changed = nextMessage("ready");