Skip to content

Truly independent web browser

UI/Qt: Keep a flick on macOS one gesture until its momentum ends

Zaggy1024151b0cbad6a9
diff --git a/UI/Qt/MacWindow.h b/UI/Qt/MacWindow.h
index b0e2896e004..d3632032e14 100644
--- a/UI/Qt/MacWindow.h
+++ b/UI/Qt/MacWindow.h
@@ -16,6 +16,7 @@
class QColor;
class QDrag;
+class QWheelEvent;
class QWidget;
namespace Ladybird {
@@ -35,6 +36,13 @@ void offset_appkit_window_controls(QWidget&, int x_offset, int y_offset);
// to their offset right away, before the next frame is drawn.
void reapply_appkit_window_control_offsets(QWidget&);
void install_appkit_event_capture();
+// What AppKit says of a flick's momentum at the scroll event a wheel event came from, which Qt does not pass on.
+enum class AppKitScrollMomentumTransition {
+ // The fingers lifted, and the momentum of the flick they made is about to begin.
+ WillBegin,
+ Began,
+};
+Optional<AppKitScrollMomentumTransition> take_appkit_scroll_momentum_transition_for(QWheelEvent const&);
void make_appkit_window_first_responder(QWidget&);
// Rounds the corners of a native child widget's view, which shows what is below it through them, and outlines it.
void round_appkit_view_corners(QWidget&, qreal radius, QColor const& border_color);
diff --git a/UI/Qt/MacWindow.mm b/UI/Qt/MacWindow.mm
index 3d6eee37444..049d8ab5240 100644
--- a/UI/Qt/MacWindow.mm
+++ b/UI/Qt/MacWindow.mm
@@ -7,6 +7,7 @@
#include <UI/Qt/MacWindow.h>
#include <AK/ScopeGuard.h>
+#include <AK/Vector.h>
#include <LibGfx/Color.h>
#include <LibGfx/Point.h>
#include <LibGfx/Size.h>
@@ -16,12 +17,14 @@
#include <QGuiApplication>
#include <QMouseEvent>
#include <QPointer>
+#include <QWheelEvent>
#include <QWidget>
#include <QWindow>
#include <UI/Qt/WebContentView.h>
#import <Cocoa/Cocoa.h>
#import <UI/Qt/DictionaryLookup.h>
+#import <dlfcn.h>
#import <objc/runtime.h>
@interface LadybirdWindowControlOffsetObserver : NSObject
@@ -312,6 +315,92 @@ static void follow_mouse_while_inactive(NSEvent* event)
QCoreApplication::sendEvent(view, &move_event);
}
+// AppKit reports no more than whether a scroll event ends the fingers' part of a gesture, but the HID event behind it
+// says whether momentum follows. The functions that read it are private, so they are looked up rather than linked.
+static bool appkit_scroll_momentum_will_begin(NSEvent* event)
+{
+ using CopyIOHIDEventFunction = CFTypeRef (*)(CGEventRef);
+ using GetScrollMomentumFunction = u8 (*)(CFTypeRef);
+ static auto copy_iohid_event = reinterpret_cast<CopyIOHIDEventFunction>(dlsym(RTLD_DEFAULT, "CGEventCopyIOHIDEvent"));
+ static auto get_scroll_momentum = reinterpret_cast<GetScrollMomentumFunction>(dlsym(RTLD_DEFAULT, "IOHIDEventGetScrollMomentum"));
+ static constexpr u8 scroll_momentum_will_begin = 1 << 3;
+
+ if (!copy_iohid_event || !get_scroll_momentum || !event.CGEvent)
+ return false;
+ auto iohid_event = copy_iohid_event(event.CGEvent);
+ if (!iohid_event)
+ return false;
+ ScopeGuard release_iohid_event = [&] { CFRelease(iohid_event); };
+ return (get_scroll_momentum(iohid_event) & scroll_momentum_will_begin) != 0;
+}
+
+struct RecordedAppKitScrollMomentumTransition {
+ u64 timestamp_in_milliseconds { 0 };
+ AppKitScrollMomentumTransition transition;
+};
+static Vector<RecordedAppKitScrollMomentumTransition> s_recorded_appkit_scroll_momentum_transitions;
+
+// Qt delivers a wheel event after AppKit dispatched the scroll event it came from, and stamps it with that event's
+// time in milliseconds.
+static u64 qt_timestamp_for_appkit_event(NSEvent* event)
+{
+ return static_cast<u64>(event.timestamp * 1000);
+}
+
+static void record_appkit_scroll_momentum_transition(NSEvent* event)
+{
+ if (event.type != NSEventTypeScrollWheel)
+ return;
+
+ Optional<AppKitScrollMomentumTransition> transition;
+ if (event.momentumPhase == NSEventPhaseBegan)
+ transition = AppKitScrollMomentumTransition::Began;
+ else if (event.phase == NSEventPhaseEnded && event.momentumPhase == NSEventPhaseNone && appkit_scroll_momentum_will_begin(event))
+ transition = AppKitScrollMomentumTransition::WillBegin;
+ if (!transition.has_value())
+ return;
+
+ static constexpr size_t maximum_recorded_transitions = 8;
+ if (s_recorded_appkit_scroll_momentum_transitions.size() == maximum_recorded_transitions)
+ s_recorded_appkit_scroll_momentum_transitions.remove(0);
+ s_recorded_appkit_scroll_momentum_transitions.append({
+ .timestamp_in_milliseconds = qt_timestamp_for_appkit_event(event),
+ .transition = transition.value(),
+ });
+}
+
+// Qt makes a ScrollEnd of the lift, unless it saw the momentum coming, and a ScrollBegin of the momentum's first event,
+// unless it did. The fingers' last step can share the lift's millisecond, and takes neither.
+static bool qt_wheel_event_is_made_of(QWheelEvent const& wheel_event, AppKitScrollMomentumTransition transition)
+{
+ switch (transition) {
+ case AppKitScrollMomentumTransition::WillBegin:
+ return wheel_event.phase() == Qt::ScrollEnd;
+ case AppKitScrollMomentumTransition::Began:
+ return wheel_event.phase() == Qt::ScrollBegin;
+ }
+ VERIFY_NOT_REACHED();
+}
+
+Optional<AppKitScrollMomentumTransition> take_appkit_scroll_momentum_transition_for(QWheelEvent const& wheel_event)
+{
+ // The transitions recorded before this one went to other windows, or were dropped by Qt.
+ auto timestamp = static_cast<u64>(wheel_event.timestamp());
+ while (!s_recorded_appkit_scroll_momentum_transitions.is_empty() && s_recorded_appkit_scroll_momentum_transitions.first().timestamp_in_milliseconds < timestamp)
+ s_recorded_appkit_scroll_momentum_transitions.remove(0);
+ for (size_t index = 0; index < s_recorded_appkit_scroll_momentum_transitions.size(); index++) {
+ auto const& recorded = s_recorded_appkit_scroll_momentum_transitions[index];
+ if (recorded.timestamp_in_milliseconds > timestamp)
+ break;
+ if (!qt_wheel_event_is_made_of(wheel_event, recorded.transition))
+ continue;
+ auto transition = recorded.transition;
+ s_recorded_appkit_scroll_momentum_transitions.remove(index);
+ return transition;
+ }
+ return {};
+}
+
class LadybirdAppKitEventCaptureFilter final : public QAbstractNativeEventFilter {
public:
AK_ALLOC_WITH_KMALLOC;
@@ -323,6 +412,7 @@ public:
auto* event = static_cast<NSEvent*>(message);
follow_mouse_while_inactive(event);
+ record_appkit_scroll_momentum_transition(event);
if (perform_dictionary_lookup_for_event(event))
return true;
diff --git a/UI/Qt/WebContentView.cpp b/UI/Qt/WebContentView.cpp
index 8195cc36885..f43bbd39798 100644
--- a/UI/Qt/WebContentView.cpp
+++ b/UI/Qt/WebContentView.cpp
@@ -984,6 +984,37 @@ void WebContentView::wheelEvent(QWheelEvent* event)
auto is_momentum_running_out = first_is_one_of(event->phase(), Qt::ScrollMomentum, Qt::ScrollEnd) && event->pixelDelta().manhattanLength() <= 1;
if (m_screenshot_selection_overlay && !is_momentum_running_out)
m_screenshot_selection_overlay->page_did_scroll();
+
+#ifdef AK_OS_MACOS
+ // Qt makes a second wheel event of some scroll events, with no delta and the phase and time of the first, whose
+ // phase would read as an end of the gesture, or a beginning of another, in its own right.
+ bool repeats_last_wheel_event = event->phase() == m_last_wheel_event_phase && event->timestamp() == m_last_wheel_event_timestamp;
+ m_last_wheel_event_phase = event->phase();
+ m_last_wheel_event_timestamp = event->timestamp();
+ if (repeats_last_wheel_event && event->pixelDelta().isNull() && event->angleDelta().isNull()) {
+ event->accept();
+ return;
+ }
+
+ // Qt ends a scroll if momentum hasn't already begun, and begins another with the momentum it did not see coming.
+ // Check what the underlying scroll event says: hold Qt's ScrollEnd back while momentum is about to follow it, and
+ // drop it for that momentum, labelled as such, or send it ahead of whatever else comes.
+ auto momentum_transition = take_appkit_scroll_momentum_transition_for(*event);
+ if (auto wheel_end = AK::exchange(m_wheel_end_awaiting_momentum, nullptr)) {
+ if (momentum_transition != AppKitScrollMomentumTransition::Began)
+ enqueue_native_event(Web::MouseEvent::Type::MouseWheel, *wheel_end);
+ }
+ if (momentum_transition == AppKitScrollMomentumTransition::WillBegin) {
+ m_wheel_end_awaiting_momentum.reset(event->clone());
+ event->accept();
+ return;
+ }
+ if (momentum_transition == AppKitScrollMomentumTransition::Began) {
+ enqueue_native_event(Web::MouseEvent::Type::MouseWheel, *event, Web::ScrollGesturePhase::Momentum);
+ return;
+ }
+#endif
+
enqueue_native_event(Web::MouseEvent::Type::MouseWheel, *event);
}
@@ -1909,6 +1940,12 @@ void WebContentView::hideEvent(QHideEvent* event)
// page is visible again). So, the tab hides the link-preview label for a hidden view itself (Tab::hideEvent).
handle_pointer_leave();
+#ifdef AK_OS_MACOS
+ // Qt begins the momentum after a held end as a scroll of its own, which goes to the view under the pointer.
+ if (auto wheel_end = AK::exchange(m_wheel_end_awaiting_momentum, nullptr))
+ enqueue_native_event(Web::MouseEvent::Type::MouseWheel, *wheel_end);
+#endif
+
#if !defined(Q_OS_MACOS) && !defined(Q_OS_WIN)
// When this tab becomes inactive, deregister all its accessibility interfaces from Qt's global registry.
deregister_accessibility_interfaces();
@@ -2398,7 +2435,7 @@ bool WebContentView::handle_vulkan_window_event(QEvent* event)
}
#endif
-void WebContentView::enqueue_native_event(Web::MouseEvent::Type type, QSinglePointEvent const& event)
+void WebContentView::enqueue_native_event(Web::MouseEvent::Type type, QSinglePointEvent const& event, Optional<Web::ScrollGesturePhase> scroll_gesture_phase_the_platform_reports)
{
Web::DevicePixelPoint position = { event.position().x() * m_device_pixel_ratio, event.position().y() * m_device_pixel_ratio };
auto screen_position = Gfx::IntPoint { event.globalPosition().x() * m_device_pixel_ratio, event.globalPosition().y() * m_device_pixel_ratio };
@@ -2428,7 +2465,7 @@ void WebContentView::enqueue_native_event(Web::MouseEvent::Type type, QSinglePoi
wheel_tick_120ths_x = wheel_delta.tick_120ths.x();
wheel_tick_120ths_y = wheel_delta.tick_120ths.y();
wheel_delta_precision = wheel_delta.precision;
- scroll_gesture_phase = scroll_gesture_phase_from_qt_event(wheel_event);
+ scroll_gesture_phase = scroll_gesture_phase_the_platform_reports.value_or(scroll_gesture_phase_from_qt_event(wheel_event));
}
enqueue_input_event(Web::MouseEvent { type, position, screen_position.to_type<Web::DevicePixels>(), button, buttons, modifiers, wheel_delta_x, wheel_delta_y, wheel_tick_120ths_x, wheel_tick_120ths_y, wheel_delta_precision, scroll_gesture_phase, m_click_count, nullptr });
diff --git a/UI/Qt/WebContentView.h b/UI/Qt/WebContentView.h
index ef0a3700725..ee3c13d919b 100644
--- a/UI/Qt/WebContentView.h
+++ b/UI/Qt/WebContentView.h
@@ -36,6 +36,7 @@
#include <QTimer>
#include <QUrl>
#include <QVariant>
+#include <memory>
#ifdef AK_OS_MACOS
# define LADYBIRD_QT_USE_IOSURFACE_LAYER 1
@@ -227,7 +228,8 @@ private:
#endif
Web::DevicePixelPoint node_picker_position_for(QSinglePointEvent const&) const;
- void enqueue_native_event(Web::MouseEvent::Type, QSinglePointEvent const& event);
+ // The platform may report a phase of a wheel event's gesture that Qt does not pass on.
+ void enqueue_native_event(Web::MouseEvent::Type, QSinglePointEvent const& event, Optional<Web::ScrollGesturePhase> scroll_gesture_phase_the_platform_reports = {});
void handle_pointer_leave();
void enqueue_native_event(Web::DragEvent::Type, QDropEvent const& event);
@@ -309,6 +311,11 @@ private:
#ifdef AK_OS_MACOS
NativeTextEditingBindings m_native_text_editing_bindings;
+
+ // The end of a flick's fingers, held back while the momentum AppKit said would follow it has yet to begin.
+ std::unique_ptr<QWheelEvent> m_wheel_end_awaiting_momentum;
+ Qt::ScrollPhase m_last_wheel_event_phase { Qt::NoScrollPhase };
+ quint64 m_last_wheel_event_timestamp { 0 };
#endif
#ifdef LADYBIRD_QT_USE_IOSURFACE_LAYER