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

@@ -39,6 +39,7 @@
#define MOTION_EVENT_ACTION_DOWN 0 #define MOTION_EVENT_ACTION_DOWN 0
#define MOTION_EVENT_ACTION_UP 1 #define MOTION_EVENT_ACTION_UP 1
#define MOTION_EVENT_ACTION_MOVE 2 #define MOTION_EVENT_ACTION_MOVE 2
#define MOTION_EVENT_ACTION_SCROLL 8
#endif #endif

View File

@@ -77,7 +77,7 @@ void set_up_handle_cache(JNIEnv *env)
handle_cache.paint.getColor = _METHOD(handle_cache.paint.class, "getColor", "()I"); handle_cache.paint.getColor = _METHOD(handle_cache.paint.class, "getColor", "()I");
handle_cache.motion_event.class = _REF((*env)->FindClass(env, "android/view/MotionEvent")); handle_cache.motion_event.class = _REF((*env)->FindClass(env, "android/view/MotionEvent"));
handle_cache.motion_event.constructor = _METHOD(handle_cache.motion_event.class, "<init>", "(IFF)V"); handle_cache.motion_event.constructor = _METHOD(handle_cache.motion_event.class, "<init>", "(IIFF)V");
handle_cache.canvas.class = _REF((*env)->FindClass(env, "android/graphics/Canvas")); handle_cache.canvas.class = _REF((*env)->FindClass(env, "android/graphics/Canvas"));
handle_cache.canvas.constructor = _METHOD(handle_cache.canvas.class, "<init>", "(JJ)V"); handle_cache.canvas.constructor = _METHOD(handle_cache.canvas.class, "<init>", "(JJ)V");
@@ -115,6 +115,7 @@ void set_up_handle_cache(JNIEnv *env)
handle_cache.view.getSuggestedMinimumWidth = _METHOD(handle_cache.view.class, "getSuggestedMinimumWidth", "()I"); handle_cache.view.getSuggestedMinimumWidth = _METHOD(handle_cache.view.class, "getSuggestedMinimumWidth", "()I");
handle_cache.view.getSuggestedMinimumHeight = _METHOD(handle_cache.view.class, "getSuggestedMinimumHeight", "()I"); handle_cache.view.getSuggestedMinimumHeight = _METHOD(handle_cache.view.class, "getSuggestedMinimumHeight", "()I");
handle_cache.view.setMeasuredDimension = _METHOD(handle_cache.view.class, "setMeasuredDimension", "(II)V"); handle_cache.view.setMeasuredDimension = _METHOD(handle_cache.view.class, "setMeasuredDimension", "(II)V");
handle_cache.view.onGenericMotionEvent = _METHOD(handle_cache.view.class, "onGenericMotionEvent", "(Landroid/view/MotionEvent;)Z");
handle_cache.asset_manager.class = _REF((*env)->FindClass(env, "android/content/res/AssetManager")); handle_cache.asset_manager.class = _REF((*env)->FindClass(env, "android/content/res/AssetManager"));
handle_cache.asset_manager.extractFromAPK = _STATIC_METHOD(handle_cache.asset_manager.class, "extractFromAPK", "(Ljava/lang/String;Ljava/lang/String;)V"); handle_cache.asset_manager.extractFromAPK = _STATIC_METHOD(handle_cache.asset_manager.class, "extractFromAPK", "(Ljava/lang/String;Ljava/lang/String;)V");

View File

@@ -76,6 +76,7 @@ struct handle_cache {
jmethodID getSuggestedMinimumWidth; jmethodID getSuggestedMinimumWidth;
jmethodID getSuggestedMinimumHeight; jmethodID getSuggestedMinimumHeight;
jmethodID setMeasuredDimension; jmethodID setMeasuredDimension;
jmethodID onGenericMotionEvent;
} view; } view;
struct { struct {
jclass class; jclass class;

View File

@@ -7,6 +7,8 @@
#include "../generated_headers/android_view_View.h" #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; }; 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) 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; JNIEnv *env;
(*d->jvm)->GetEnv(d->jvm, (void**)&env, JNI_VERSION_1_6); (*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); (*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 MEASURE_SPEC_EXACTLY (1 << 30)
#define SOURCE_CLASS_POINTER 0x2
struct _AndroidLayout { struct _AndroidLayout {
GtkLayoutManager parent_instance; GtkLayoutManager parent_instance;
jobject view; jobject view;
@@ -64,6 +66,18 @@ static GtkLayoutManager *android_layout_new(jobject view) {
return &layout->parent_instance; 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. * Should be overwritten by ViewGroup subclasses.
* Fall back to vertical GtkBox if subclass is not implemented yet * 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) { if (measure_method != handle_cache.view.onMeasure || layout_method != handle_cache.view.onLayout) {
gtk_widget_set_layout_manager(box, android_layout_new(_REF(this))); 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); return _INTPTR(box);
} }

View File

@@ -19,6 +19,8 @@
#include "../generated_headers/android_opengl_GLSurfaceView.h" #include "../generated_headers/android_opengl_GLSurfaceView.h"
#define SOURCE_TOUCHSCREEN 4098
// for whatever reason, some Mesa builds don't export the OES function (which we use in order to have GLESv1 support) // for whatever reason, some Mesa builds don't export the OES function (which we use in order to have GLESv1 support)
GL_APICALL void GL_APIENTRY _glEGLImageTargetTexture2DOES_load(GLenum target, GLeglImageOES image); GL_APICALL void GL_APIENTRY _glEGLImageTargetTexture2DOES_load(GLenum target, GLeglImageOES image);
static PFNGLEGLIMAGETARGETTEXTURE2DOESPROC _glEGLImageTargetTexture2DOES = &_glEGLImageTargetTexture2DOES_load; static PFNGLEGLIMAGETARGETTEXTURE2DOESPROC _glEGLImageTargetTexture2DOES = &_glEGLImageTargetTexture2DOES_load;
@@ -469,7 +471,7 @@ static void call_ontouch_callback(GtkEventControllerLegacy* event_controller, in
// execute the Java callback function // execute the Java callback function
jobject motion_event = (*env)->NewObject(env, handle_cache.motion_event.class, handle_cache.motion_event.constructor, action, (float)x, (float)y); jobject motion_event = (*env)->NewObject(env, handle_cache.motion_event.class, handle_cache.motion_event.constructor, SOURCE_TOUCHSCREEN, action, (float)x, (float)y);
(*env)->CallBooleanMethod(env, d->this, handle_cache.gl_surface_view.onTouchEvent, motion_event); (*env)->CallBooleanMethod(env, d->this, handle_cache.gl_surface_view.onTouchEvent, motion_event);

View File

@@ -301,9 +301,8 @@ public final class AssetManager {
return map; return map;
} }
/*package*/ final boolean getThemeValue(int theme, int ident, /*package*/ final boolean getThemeValue(Map<Integer,ValueItem> style, int ident,
TypedValue outValue, boolean resolveRefs) { TypedValue outValue, boolean resolveRefs) {
Map<Integer,ValueItem> style = loadStyle(theme);
EntryGroup entryGroup = tableBlockSearch(ident); EntryGroup entryGroup = tableBlockSearch(ident);
if (entryGroup == null) if (entryGroup == null)
return false; return false;

View File

@@ -1422,7 +1422,7 @@ public class Resources {
*/ */
public boolean resolveAttribute(int resid, TypedValue outValue, public boolean resolveAttribute(int resid, TypedValue outValue,
boolean resolveRefs) { boolean resolveRefs) {
boolean got = mAssets.getThemeValue(mTheme, resid, outValue, resolveRefs); boolean got = mAssets.getThemeValue(themeMap, resid, outValue, resolveRefs);
if (false) { if (false) {
System.out.println( System.out.println(
"resolveAttribute #" + Integer.toHexString(resid) + " got=" + got + ", type=0x" + Integer.toHexString(outValue.type) + ", data=0x" + Integer.toHexString(outValue.data)); "resolveAttribute #" + Integer.toHexString(resid) + " got=" + got + ", type=0x" + Integer.toHexString(outValue.type) + ", data=0x" + Integer.toHexString(outValue.data));

View File

@@ -202,7 +202,8 @@ public class Matrix {
* This maybe faster than testing if (getType() == 0) * This maybe faster than testing if (getType() == 0)
*/ */
public boolean isIdentity() { public boolean isIdentity() {
return native_isIdentity(native_instance); return true;
// return native_isIdentity(native_instance);
} }
/** /**
* Returns true if will map a rectangle to another rectangle. This can be * Returns true if will map a rectangle to another rectangle. This can be

View File

@@ -1363,6 +1363,7 @@ public final class MotionEvent extends InputEvent {
private static native void nativeScale(int nativePtr, float scale); private static native void nativeScale(int nativePtr, float scale);
private static native void nativeTransform(int nativePtr, Matrix matrix); private static native void nativeTransform(int nativePtr, Matrix matrix);
int source;
int action; int action;
float coord_x; float coord_x;
float coord_y; float coord_y;
@@ -1370,7 +1371,8 @@ public final class MotionEvent extends InputEvent {
private MotionEvent() { private MotionEvent() {
} }
public MotionEvent(int action, float coord_x, float coord_y) { public MotionEvent(int source, int action, float coord_x, float coord_y) {
this.source = source;
this.action = action; this.action = action;
this.coord_x = coord_x; this.coord_x = coord_x;
this.coord_y = coord_y; this.coord_y = coord_y;
@@ -1694,7 +1696,7 @@ public final class MotionEvent extends InputEvent {
*/ */
@Override @Override
public final int getSource() { public final int getSource() {
return 4098 /*SOURCE_TOUCHSCREEN*/; // TODO: reflect reality return source;
// return nativeGetSource(mNativePtr); // return nativeGetSource(mNativePtr);
} }
@@ -1931,6 +1933,10 @@ public final class MotionEvent extends InputEvent {
* @see #AXIS_Y * @see #AXIS_Y
*/ */
public final float getAxisValue(int axis) { public final float getAxisValue(int axis) {
if (axis == AXIS_HSCROLL)
return coord_x;
else if (axis == AXIS_VSCROLL)
return coord_y;
return nativeGetAxisValue(mNativePtr, axis, 0, HISTORY_CURRENT); return nativeGetAxisValue(mNativePtr, axis, 0, HISTORY_CURRENT);
} }

View File

@@ -3,6 +3,7 @@ package android.view;
import android.content.Context; import android.content.Context;
import android.content.res.Resources; import android.content.res.Resources;
import android.graphics.Canvas; import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Rect; import android.graphics.Rect;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.os.Handler; import android.os.Handler;
@@ -1195,4 +1196,9 @@ public class View extends Object {
public void setTranslationY(float translationY) {} public void setTranslationY(float translationY) {}
public void setAlpha(float alpha) {} public void setAlpha(float alpha) {}
public boolean onGenericMotionEvent(MotionEvent event) {return false;}
protected boolean awakenScrollBars() {return false;}
public Matrix getMatrix() {return new Matrix();}
} }

View File

@@ -1,6 +1,5 @@
package android.view; package android.view;
import android.graphics.Rect;
public interface ViewParent { public interface ViewParent {
public abstract ViewParent getParent();
} }

View File

@@ -14,4 +14,8 @@ public class EdgeEffect extends View {
super(context, attributeSet); super(context, attributeSet);
} }
public void setSize(int width, int height) {}
public void onPull(float deltaDistance) {}
public boolean isFinished() {return true;}
} }