app/internal/window: [Android] implement mouse scrolling

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2020-06-13 18:52:29 +02:00
parent 8688ed95c2
commit feacd1e2df
2 changed files with 28 additions and 5 deletions
+24 -4
View File
@@ -21,6 +21,7 @@ import android.view.KeyCharacterMap;
import android.view.KeyEvent; import android.view.KeyEvent;
import android.view.MotionEvent; import android.view.MotionEvent;
import android.view.View; import android.view.View;
import android.view.ViewConfiguration;
import android.view.WindowInsets; import android.view.WindowInsets;
import android.view.Surface; import android.view.Surface;
import android.view.SurfaceView; import android.view.SurfaceView;
@@ -39,6 +40,9 @@ public final class GioView extends SurfaceView implements Choreographer.FrameCal
private final SurfaceHolder.Callback surfCallbacks; private final SurfaceHolder.Callback surfCallbacks;
private final View.OnFocusChangeListener focusCallback; private final View.OnFocusChangeListener focusCallback;
private final InputMethodManager imm; private final InputMethodManager imm;
private final float scrollXScale;
private final float scrollYScale;
private long nhandle; private long nhandle;
public GioView(Context context) { public GioView(Context context) {
@@ -51,6 +55,10 @@ public final class GioView extends SurfaceView implements Choreographer.FrameCal
// Late initialization of the Go runtime to wait for a valid context. // Late initialization of the Go runtime to wait for a valid context.
Gio.init(context.getApplicationContext()); Gio.init(context.getApplicationContext());
ViewConfiguration conf = ViewConfiguration.get(context);
scrollXScale = conf.getScaledHorizontalScrollFactor();
scrollYScale = conf.getScaledVerticalScrollFactor();
nhandle = onCreateView(this); nhandle = onCreateView(this);
imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE); imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
setFocusable(true); setFocusable(true);
@@ -80,6 +88,11 @@ public final class GioView extends SurfaceView implements Choreographer.FrameCal
return false; return false;
} }
@Override public boolean onGenericMotionEvent(MotionEvent event) {
dispatchMotionEvent(event);
return true;
}
@Override public boolean onTouchEvent(MotionEvent event) { @Override public boolean onTouchEvent(MotionEvent event) {
// Ask for unbuffered events. Flutter and Chrome does it // Ask for unbuffered events. Flutter and Chrome does it
// so I assume its good for us as well. // so I assume its good for us as well.
@@ -87,6 +100,11 @@ public final class GioView extends SurfaceView implements Choreographer.FrameCal
requestUnbufferedDispatch(event); requestUnbufferedDispatch(event);
} }
dispatchMotionEvent(event);
return true;
}
private void dispatchMotionEvent(MotionEvent event) {
for (int j = 0; j < event.getHistorySize(); j++) { for (int j = 0; j < event.getHistorySize(); j++) {
long time = event.getHistoricalEventTime(j); long time = event.getHistoricalEventTime(j);
for (int i = 0; i < event.getPointerCount(); i++) { for (int i = 0; i < event.getPointerCount(); i++) {
@@ -97,6 +115,8 @@ public final class GioView extends SurfaceView implements Choreographer.FrameCal
event.getToolType(i), event.getToolType(i),
event.getHistoricalX(i, j), event.getHistoricalX(i, j),
event.getHistoricalY(i, j), event.getHistoricalY(i, j),
scrollXScale*event.getHistoricalAxisValue(MotionEvent.AXIS_HSCROLL, i, j),
scrollYScale*event.getHistoricalAxisValue(MotionEvent.AXIS_VSCROLL, i, j),
event.getButtonState(), event.getButtonState(),
time); time);
} }
@@ -113,12 +133,12 @@ public final class GioView extends SurfaceView implements Choreographer.FrameCal
pact, pact,
event.getPointerId(i), event.getPointerId(i),
event.getToolType(i), event.getToolType(i),
event.getX(i), event.getX(i), event.getY(i),
event.getY(i), scrollXScale*event.getAxisValue(MotionEvent.AXIS_HSCROLL, i),
scrollYScale*event.getAxisValue(MotionEvent.AXIS_VSCROLL, i),
event.getButtonState(), event.getButtonState(),
event.getEventTime()); event.getEventTime());
} }
return true;
} }
@Override public InputConnection onCreateInputConnection(EditorInfo outAttrs) { @Override public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
@@ -200,7 +220,7 @@ public final class GioView extends SurfaceView implements Choreographer.FrameCal
static private native void onConfigurationChanged(long handle); static private native void onConfigurationChanged(long handle);
static private native void onWindowInsets(long handle, int top, int right, int bottom, int left); static private native void onWindowInsets(long handle, int top, int right, int bottom, int left);
static private native void onLowMemory(); static private native void onLowMemory();
static private native void onTouchEvent(long handle, int action, int pointerID, int tool, float x, float y, int buttons, long time); static private native void onTouchEvent(long handle, int action, int pointerID, int tool, float x, float y, float scrollX, float scrollY, int buttons, long time);
static private native void onKeyEvent(long handle, int code, int character, long time); static private native void onKeyEvent(long handle, int code, int character, long time);
static private native void onFrameCallback(long handle, long nanos); static private native void onFrameCallback(long handle, long nanos);
static private native boolean onBack(long handle); static private native boolean onBack(long handle);
+4 -1
View File
@@ -460,7 +460,7 @@ func Java_org_gioui_GioView_onKeyEvent(env *C.JNIEnv, class C.jclass, handle C.j
} }
//export Java_org_gioui_GioView_onTouchEvent //export Java_org_gioui_GioView_onTouchEvent
func Java_org_gioui_GioView_onTouchEvent(env *C.JNIEnv, class C.jclass, handle C.jlong, action, pointerID, tool C.jint, x, y C.jfloat, jbtns C.jint, t C.jlong) { func Java_org_gioui_GioView_onTouchEvent(env *C.JNIEnv, class C.jclass, handle C.jlong, action, pointerID, tool C.jint, x, y, scrollX, scrollY C.jfloat, jbtns C.jint, t C.jlong) {
w := views[handle] w := views[handle]
var typ pointer.Type var typ pointer.Type
switch action { switch action {
@@ -472,6 +472,8 @@ func Java_org_gioui_GioView_onTouchEvent(env *C.JNIEnv, class C.jclass, handle C
typ = pointer.Cancel typ = pointer.Cancel
case C.AMOTION_EVENT_ACTION_MOVE: case C.AMOTION_EVENT_ACTION_MOVE:
typ = pointer.Move typ = pointer.Move
case C.AMOTION_EVENT_ACTION_SCROLL:
typ = pointer.Scroll
default: default:
return return
} }
@@ -505,6 +507,7 @@ func Java_org_gioui_GioView_onTouchEvent(env *C.JNIEnv, class C.jclass, handle C
PointerID: pointer.ID(pointerID), PointerID: pointer.ID(pointerID),
Time: time.Duration(t) * time.Millisecond, Time: time.Duration(t) * time.Millisecond,
Position: f32.Point{X: float32(x), Y: float32(y)}, Position: f32.Point{X: float32(x), Y: float32(y)},
Scroll: f32.Pt(float32(scrollX), float32(scrollY)),
}) })
} }