Truly independent web browser
LibWeb+LibWebCommon+UI: Mark pinch steps that continue a gesture
Andreas Klingaab8dd1091fe
diff --git a/Libraries/LibWeb/Internals/Internals.cpp b/Libraries/LibWeb/Internals/Internals.cppindex 44d9edaaa03..85779ea7130 100644--- a/Libraries/LibWeb/Internals/Internals.cpp+++ b/Libraries/LibWeb/Internals/Internals.cpp@@ -1070,13 +1070,14 @@ void Internals::pinch(double x, double y, double scale_delta, WebIDL::UnsignedShpage.handle_pinch_event(position, modifiers, scale_delta);}-void Internals::pinch_through_ui_process(double x, double y, double scale_delta)+void Internals::pinch_through_ui_process(double x, double y, double scale_delta, bool continues_gesture){let_compositor_take_in_every_rendered_frame(window());Web::PinchEvent event;event.position = page().css_to_device_point(window().navigable()->to_page_position({ x, y }));event.scale_delta = scale_delta;+ event.continues_gesture = continues_gesture;page().client().page_did_request_pinch_event_for_testing(window().navigable()->local_root()->id(), event);}diff --git a/Libraries/LibWeb/Internals/Internals.h b/Libraries/LibWeb/Internals/Internals.hindex 5bdc6fd182a..ec5cfcae8f4 100644--- a/Libraries/LibWeb/Internals/Internals.h+++ b/Libraries/LibWeb/Internals/Internals.h@@ -127,7 +127,7 @@ public:void wheel(double x, double y, double delta_x, double delta_y, bool precise, Bindings::ScrollGesturePhase, Bindings::WheelTicks const&, GC::Ref<WebIDL::Promise>);GC::Ref<JS::Object> compositor_scroll_state(GC::Ptr<DOM::Element> scroller);void pinch(double x, double y, double scale_delta, WebIDL::UnsignedShort modifiers);- void pinch_through_ui_process(double x, double y, double scale_delta);+ void pinch_through_ui_process(double x, double y, double scale_delta, bool continues_gesture);void reset_zoom();Utf16String current_cursor();diff --git a/Libraries/LibWeb/Internals/Internals.idl b/Libraries/LibWeb/Internals/Internals.idlindex 555bf965c16..86bc1653dce 100644--- a/Libraries/LibWeb/Internals/Internals.idl+++ b/Libraries/LibWeb/Internals/Internals.idl@@ -107,7 +107,7 @@ interface Internals {object compositorScrollState(optional Element? scroller = null);undefined pinch(double x, double y, double scaleDelta, optional unsigned short modifiers = 0);// Sends a pinch through the UI process as it sends the user's, so that the compositor zooms for it if it can.- undefined pinchThroughUIProcess(double x, double y, double scaleDelta);+ undefined pinchThroughUIProcess(double x, double y, double scaleDelta, optional boolean continuesGesture = false);undefined resetZoom();DOMString currentCursor();diff --git a/Libraries/LibWebCommon/Page/InputEvent.h b/Libraries/LibWebCommon/Page/InputEvent.hindex 45bc3384331..cc7ec7136b4 100644--- a/Libraries/LibWebCommon/Page/InputEvent.h+++ b/Libraries/LibWebCommon/Page/InputEvent.h@@ -195,6 +195,8 @@ struct WEBCOMMON_API PinchEvent {// 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 };+ // The fingers have not left the touchpad since an earlier step of the pinch.+ bool continues_gesture { false };u64 id { 0 };};diff --git a/Libraries/LibWebCommon/Page/InputEvent.ipc b/Libraries/LibWebCommon/Page/InputEvent.ipcindex b5c8d83b5e8..f3534a02f62 100644--- a/Libraries/LibWebCommon/Page/InputEvent.ipc+++ b/Libraries/LibWebCommon/Page/InputEvent.ipc@@ -112,5 +112,6 @@ struct Web::PinchEvent {[Finite] double scale_delta;bool async_zoom_performed_default_action;bool wheel_event_was_dispatched;+ bool continues_gesture;u64 id;}diff --git a/Tests/LibWeb/Text/expected/async-scrolling/pinch-gesture-is-decided-by-its-first-step.txt b/Tests/LibWeb/Text/expected/async-scrolling/pinch-gesture-is-decided-by-its-first-step.txtnew file mode 100644index 00000000000..63e6d413955--- /dev/null+++ b/Tests/LibWeb/Text/expected/async-scrolling/pinch-gesture-is-decided-by-its-first-step.txt@@ -0,0 +1,7 @@+first step, beside the task: compositor scale=1.5+step after the listener appeared, beside the task: compositor scale=1.5+listener could cancel an event of that gesture: false+next gesture: cancelable=true+rest of the canceled gesture: cancelable=true+compositor scale: 1.5+main thread scale: 1.5diff --git a/Tests/LibWeb/Text/input/async-scrolling/pinch-gesture-is-decided-by-its-first-step.html b/Tests/LibWeb/Text/input/async-scrolling/pinch-gesture-is-decided-by-its-first-step.htmlnew file mode 100644index 00000000000..40c39ffe77c--- /dev/null+++ b/Tests/LibWeb/Text/input/async-scrolling/pinch-gesture-is-decided-by-its-first-step.html@@ -0,0 +1,77 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<style>+ body {+ margin: 0;+ height: 3000px;+ }+</style>+<script>+ // The first step of a pinch gesture decides whether the page gets to hold back the rest of it. A listener added+ // after the compositor began zooming can't stop the gesture, and sees only events it can't cancel. It gets its say+ // from the next gesture on, and a gesture it canceled stays canceled.+ promiseTest(async () => {+ const compositorScale = () => internals.compositorScrollState().visualViewportScale;+ await animationFrame();+ await animationFrame();++ internals.pinchThroughUIProcess(400, 300, 0.5);++ // NB: These only give up on a zoom that never comes, so the time limit can't fail a slow machine's run of a fixed+ // engine.+ let giveUpAt = performance.now() + 5000;+ while (compositorScale() !== 1.5 && performance.now() < giveUpAt) {+ // Stay in this task.+ }+ println(`first step, beside the task: compositor scale=${compositorScale()}`);++ const events = [];+ let eventArrived = () => {};+ window.addEventListener(+ "wheel",+ event => {+ events.push(event);+ event.preventDefault();+ eventArrived();+ },+ { passive: false }+ );+ internals.synchronizeCompositor();++ internals.pinchThroughUIProcess(400, 300, 0.5, true);+ giveUpAt = performance.now() + 5000;+ while (compositorScale() !== 2.25 && performance.now() < giveUpAt) {+ // Stay in this task.+ }+ println(`step after the listener appeared, beside the task: compositor scale=${compositorScale()}`);++ const nextEvent = () => {+ const count = events.length;+ return new Promise(resolve => {+ eventArrived = () => {+ if (events.length > count)+ resolve(events[events.length - 1]);+ };+ });+ };+ if (events.length === 0) await nextEvent();+ println(`listener could cancel an event of that gesture: ${events.some(event => event.cancelable)}`);++ while (visualViewport.scale !== compositorScale()) await animationFrame();+ await animationFrame();+ await animationFrame();++ let event = nextEvent();+ internals.pinchThroughUIProcess(400, 300, 0.5);+ println(`next gesture: cancelable=${(await event).cancelable}`);++ event = nextEvent();+ internals.pinchThroughUIProcess(400, 300, 0.5, true);+ println(`rest of the canceled gesture: cancelable=${(await event).cancelable}`);++ await animationFrame();+ await animationFrame();+ println(`compositor scale: ${compositorScale()}`);+ println(`main thread scale: ${visualViewport.scale}`);+ });+</script>diff --git a/UI/Qt/WebContentView.cpp b/UI/Qt/WebContentView.cppindex 92ba01d91b1..285602c48f4 100644--- a/UI/Qt/WebContentView.cpp+++ b/UI/Qt/WebContentView.cpp@@ -2420,18 +2420,8 @@ bool WebContentView::event(QEvent* event)keyReleaseEvent(static_cast<QKeyEvent*>(event));return true;}- if (event->type() == QEvent::NativeGesture) {- auto const& native_gesture_event = *static_cast<QNativeGestureEvent const*>(event);- if (native_gesture_event.gestureType() == Qt::ZoomNativeGesture) {- Web::PinchEvent pinch_event;- auto const local_position = mapFromGlobal(native_gesture_event.globalPosition());- pinch_event.position = { local_position.x() * m_device_pixel_ratio, local_position.y() * m_device_pixel_ratio };- pinch_event.modifiers = get_modifiers_from_qt_keyboard_modifiers(native_gesture_event.modifiers());- pinch_event.scale_delta = native_gesture_event.value();- enqueue_input_event(AK::move(pinch_event));- return true;- }- }+ if (event->type() == QEvent::NativeGesture && handle_native_gesture_event(*static_cast<QNativeGestureEvent const*>(event)))+ return true;#if defined(LADYBIRD_QT_USE_IOSURFACE_LAYER) || defined(LADYBIRD_HAS_WAYLAND_GPU_QUERY)if (event->type() == QEvent::ParentChange || event->type() == QEvent::Show)@@ -2474,6 +2464,32 @@ bool WebContentView::event(QEvent* event)return WebContentViewBase::event(event);}+bool WebContentView::handle_native_gesture_event(QNativeGestureEvent const& event)+{+ switch (event.gestureType()) {+ case Qt::BeginNativeGesture:+ m_native_gesture_state = NativeGestureState::Began;+ return true;+ case Qt::EndNativeGesture:+ m_native_gesture_state = NativeGestureState::None;+ return true;+ case Qt::ZoomNativeGesture: {+ Web::PinchEvent pinch_event;+ auto const local_position = mapFromGlobal(event.globalPosition());+ pinch_event.position = { local_position.x() * m_device_pixel_ratio, local_position.y() * m_device_pixel_ratio };+ pinch_event.modifiers = get_modifiers_from_qt_keyboard_modifiers(event.modifiers());+ pinch_event.scale_delta = event.value();+ pinch_event.continues_gesture = m_native_gesture_state == NativeGestureState::Zooming;+ if (m_native_gesture_state == NativeGestureState::Began)+ m_native_gesture_state = NativeGestureState::Zooming;+ enqueue_input_event(AK::move(pinch_event));+ return true;+ }+ default:+ return false;+ }+}+#ifdef LADYBIRD_QT_USE_VULKAN_WINDOWbool WebContentView::handle_vulkan_window_event(QEvent* event){@@ -2528,19 +2544,8 @@ bool WebContentView::handle_vulkan_window_event(QEvent* event)case QEvent::Drop:dropEvent(static_cast<QDropEvent*>(event));return true;- case QEvent::NativeGesture: {- auto const& native_gesture_event = *static_cast<QNativeGestureEvent const*>(event);- if (native_gesture_event.gestureType() == Qt::ZoomNativeGesture) {- Web::PinchEvent pinch_event;- auto const local_position = mapFromGlobal(native_gesture_event.globalPosition());- pinch_event.position = { local_position.x() * m_device_pixel_ratio, local_position.y() * m_device_pixel_ratio };- pinch_event.modifiers = get_modifiers_from_qt_keyboard_modifiers(native_gesture_event.modifiers());- pinch_event.scale_delta = native_gesture_event.value();- enqueue_input_event(AK::move(pinch_event));- return true;- }- return false;- }+ case QEvent::NativeGesture:+ return handle_native_gesture_event(*static_cast<QNativeGestureEvent const*>(event));case QEvent::ShortcutOverride: {auto* key_event = static_cast<QKeyEvent*>(event);if (is_browser_reserved_shortcut(*key_event)) {diff --git a/UI/Qt/WebContentView.h b/UI/Qt/WebContentView.hindex d3ae5592776..d3162513ee4 100644--- a/UI/Qt/WebContentView.h+++ b/UI/Qt/WebContentView.h@@ -63,6 +63,7 @@#endifclass QKeyEvent;+class QNativeGestureEvent;class QLabel;class QMenu;class QPushButton;@@ -340,6 +341,16 @@ private:Vector<QPointer<QWidget>> m_floating_surfaces;PageOverlay* m_web_compat_report_overlay { nullptr };+ bool handle_native_gesture_event(QNativeGestureEvent const&);++ // A pinch only tells where it began and ended apart from its steps.+ enum class NativeGestureState : u8 {+ None,+ Began,+ Zooming,+ };+ NativeGestureState m_native_gesture_state { NativeGestureState::None };+#ifdef AK_OS_MACOSNativeTextEditingBindings m_native_text_editing_bindings;