From 864750020edb90af45957157f7e0fcf53030e25b Mon Sep 17 00:00:00 2001 From: Julian Winkler Date: Tue, 10 Dec 2024 23:41:33 +0100 Subject: [PATCH] View: always call dispatchTouchEvent() if overwritten --- src/api-impl-jni/views/android_view_View.c | 8 +++++--- src/api-impl-jni/widgets/WrapperWidget.c | 3 ++- src/api-impl-jni/widgets/WrapperWidget.h | 1 + src/api-impl/android/view/View.java | 2 -- 4 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/api-impl-jni/views/android_view_View.c b/src/api-impl-jni/views/android_view_View.c index 65cef0fb..d40be6ea 100644 --- a/src/api-impl-jni/views/android_view_View.c +++ b/src/api-impl-jni/views/android_view_View.c @@ -27,7 +27,9 @@ static bool call_ontouch_callback(WrapperWidget *wrapper, int action, double x, gdk_event_get_position(event, &raw_x, &raw_y); jobject motion_event = (*env)->NewObject(env, handle_cache.motion_event.class, handle_cache.motion_event.constructor, SOURCE_TOUCHSCREEN, action, (float)x, (float)y, (long)timestamp, (float)raw_x, (float)raw_y); - if (phase == GTK_PHASE_CAPTURE && !wrapper->intercepting_touch) { + if (wrapper->custom_dispatch_touch) { + ret = (*env)->CallBooleanMethod(env, this, handle_cache.view.dispatchTouchEvent, motion_event); + } else if (phase == GTK_PHASE_CAPTURE && !wrapper->intercepting_touch) { wrapper->intercepting_touch = (*env)->CallBooleanMethod(env, this, handle_cache.view.onInterceptTouchEvent, motion_event); if (wrapper->intercepting_touch) { // store the event that was canceled and let it propagate to the child widgets @@ -158,8 +160,9 @@ void _setOnTouchListener(JNIEnv *env, jobject this, GtkWidget *widget) gtk_widget_add_controller(widget, controller); g_object_set_data(G_OBJECT(widget), "on_touch_listener", controller); + WrapperWidget *wrapper = WRAPPER_WIDGET(widget); jmethodID onintercepttouchevent_method = _METHOD(_CLASS(this), "onInterceptTouchEvent", "(Landroid/view/MotionEvent;)Z"); - if (onintercepttouchevent_method != handle_cache.view.onInterceptTouchEvent) { + if (onintercepttouchevent_method != handle_cache.view.onInterceptTouchEvent || wrapper->custom_dispatch_touch) { GtkEventController *old_controller = g_object_get_data(G_OBJECT(widget), "on_intercept_touch_listener"); if(old_controller) gtk_widget_remove_controller(widget, old_controller); @@ -171,7 +174,6 @@ void _setOnTouchListener(JNIEnv *env, jobject this, GtkWidget *widget) gtk_widget_add_controller(widget, controller); g_object_set_data(G_OBJECT(widget), "on_intercept_touch_listener", controller); } - WrapperWidget *wrapper = WRAPPER_WIDGET(widget); if (!wrapper->needs_allocation && (wrapper->layout_width || wrapper->layout_height)) gtk_widget_size_allocate(GTK_WIDGET(wrapper), &(GtkAllocation){.x = 0, .y = 0, .width = wrapper->layout_width, .height = wrapper->layout_height}, 0); wrapper->needs_allocation = true; diff --git a/src/api-impl-jni/widgets/WrapperWidget.c b/src/api-impl-jni/widgets/WrapperWidget.c index d1263f58..e2fef601 100644 --- a/src/api-impl-jni/widgets/WrapperWidget.c +++ b/src/api-impl-jni/widgets/WrapperWidget.c @@ -347,7 +347,8 @@ void wrapper_widget_set_jobject(WrapperWidget *wrapper, JNIEnv *env, jobject job jmethodID ontouchevent_method = _METHOD(_CLASS(jobj), "onTouchEvent", "(Landroid/view/MotionEvent;)Z"); jmethodID dispatchtouchevent_method = _METHOD(_CLASS(jobj), "dispatchTouchEvent", "(Landroid/view/MotionEvent;)Z"); - if (ontouchevent_method != handle_cache.view.onTouchEvent || dispatchtouchevent_method != handle_cache.view.dispatchTouchEvent) { + wrapper->custom_dispatch_touch = dispatchtouchevent_method != handle_cache.view.dispatchTouchEvent; + if (ontouchevent_method != handle_cache.view.onTouchEvent || wrapper->custom_dispatch_touch) { _setOnTouchListener(env, jobj, GTK_WIDGET(wrapper)); } diff --git a/src/api-impl-jni/widgets/WrapperWidget.h b/src/api-impl-jni/widgets/WrapperWidget.h index d10fd41b..0dfba781 100644 --- a/src/api-impl-jni/widgets/WrapperWidget.h +++ b/src/api-impl-jni/widgets/WrapperWidget.h @@ -23,6 +23,7 @@ struct _WrapperWidget int real_height; gboolean needs_allocation; gboolean intercepting_touch; + gboolean custom_dispatch_touch; }; struct _WrapperWidgetClass diff --git a/src/api-impl/android/view/View.java b/src/api-impl/android/view/View.java index 9fc14084..4862a87d 100644 --- a/src/api-impl/android/view/View.java +++ b/src/api-impl/android/view/View.java @@ -1066,8 +1066,6 @@ public class View implements Drawable.Callback { boolean handled = false; if (on_touch_listener != null) handled = on_touch_listener.onTouch(this, event); - if (!handled) - handled = dispatchTouchEvent(event); return handled; }