ViewGroup: handle scroll events

This commit is contained in:
Julian Winkler
2023-08-22 18:08:16 +02:00
parent 960930a348
commit c4b7bdc63d
13 changed files with 53 additions and 11 deletions

View File

@@ -7,6 +7,8 @@
#include "../generated_headers/android_view_View.h"
#define SOURCE_TOUCHSCREEN 4098
struct touch_callback_data { JavaVM *jvm; jobject this; jobject on_touch_listener; jclass on_touch_listener_class; };
static void call_ontouch_callback(int action, float x, float y, struct touch_callback_data *d)
@@ -14,7 +16,7 @@ static void call_ontouch_callback(int action, float x, float y, struct touch_cal
JNIEnv *env;
(*d->jvm)->GetEnv(d->jvm, (void**)&env, JNI_VERSION_1_6);
jobject motion_event = (*env)->NewObject(env, handle_cache.motion_event.class, handle_cache.motion_event.constructor, action, x, y);
jobject motion_event = (*env)->NewObject(env, handle_cache.motion_event.class, handle_cache.motion_event.constructor, SOURCE_TOUCHSCREEN, action, x, y);
(*env)->CallBooleanMethod(env, d->on_touch_listener, _METHOD(d->on_touch_listener_class, "onTouch", "(Landroid/view/View;Landroid/view/MotionEvent;)Z"), d->this, motion_event);

View File

@@ -10,6 +10,8 @@
#define MEASURE_SPEC_EXACTLY (1 << 30)
#define SOURCE_CLASS_POINTER 0x2
struct _AndroidLayout {
GtkLayoutManager parent_instance;
jobject view;
@@ -64,6 +66,18 @@ static GtkLayoutManager *android_layout_new(jobject view) {
return &layout->parent_instance;
}
static gboolean scroll_cb(GtkEventControllerScroll* self, gdouble dx, gdouble dy, jobject this)
{
JNIEnv *env = get_jni_env();
jobject motion_event = (*env)->NewObject(env, handle_cache.motion_event.class, handle_cache.motion_event.constructor, SOURCE_CLASS_POINTER, MOTION_EVENT_ACTION_SCROLL, dx, -dy);
gboolean ret = (*env)->CallBooleanMethod(env, this, handle_cache.view.onGenericMotionEvent, motion_event);
if((*env)->ExceptionCheck(env))
(*env)->ExceptionDescribe(env);
return ret;
}
/**
* Should be overwritten by ViewGroup subclasses.
* Fall back to vertical GtkBox if subclass is not implemented yet
@@ -83,6 +97,12 @@ JNIEXPORT jlong JNICALL Java_android_view_ViewGroup_native_1constructor(JNIEnv *
if (measure_method != handle_cache.view.onMeasure || layout_method != handle_cache.view.onLayout) {
gtk_widget_set_layout_manager(box, android_layout_new(_REF(this)));
}
if (_METHOD(_CLASS(this), "onGenericMotionEvent", "(Landroid/view/MotionEvent;)Z") != handle_cache.view.onGenericMotionEvent) {
GtkEventController *controller = gtk_event_controller_scroll_new(GTK_EVENT_CONTROLLER_SCROLL_VERTICAL);
g_signal_connect(controller, "scroll", G_CALLBACK(scroll_cb), _REF(this));
gtk_widget_add_controller(box, controller);
}
return _INTPTR(box);
}