mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-05 09:25:38 +00:00
ui/app: implement Command for system events
And add CommandBack for the Android back button. Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
@@ -50,4 +50,9 @@ public class GioActivity extends Activity {
|
|||||||
super.onLowMemory();
|
super.onLowMemory();
|
||||||
view.lowMemory();
|
view.lowMemory();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override public void onBackPressed() {
|
||||||
|
if (!view.backPressed())
|
||||||
|
super.onBackPressed();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -163,6 +163,10 @@ public class GioView extends SurfaceView implements Choreographer.FrameCallback
|
|||||||
onLowMemory();
|
onLowMemory();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
boolean backPressed() {
|
||||||
|
return onBack(nhandle);
|
||||||
|
}
|
||||||
|
|
||||||
static private native long onCreateView(GioView view);
|
static private native long onCreateView(GioView view);
|
||||||
static private native void onDestroyView(long handle);
|
static private native void onDestroyView(long handle);
|
||||||
static private native void onStartView(long handle);
|
static private native void onStartView(long handle);
|
||||||
@@ -174,6 +178,7 @@ public class GioView extends SurfaceView implements Choreographer.FrameCallback
|
|||||||
static private native void onTouchEvent(long handle, int action, int pointerID, int tool, float x, float y, long time);
|
static private native void onTouchEvent(long handle, int action, int pointerID, int tool, float x, float y, 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);
|
||||||
|
|
||||||
private static class InputConnection extends BaseInputConnection {
|
private static class InputConnection extends BaseInputConnection {
|
||||||
private final Editable editable;
|
private final Editable editable;
|
||||||
|
|||||||
+19
-4
@@ -10,6 +10,10 @@ import (
|
|||||||
"gioui.org/ui"
|
"gioui.org/ui"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type Event interface {
|
||||||
|
ImplementsEvent()
|
||||||
|
}
|
||||||
|
|
||||||
type Draw struct {
|
type Draw struct {
|
||||||
Config *ui.Config
|
Config *ui.Config
|
||||||
Size image.Point
|
Size image.Point
|
||||||
@@ -23,12 +27,16 @@ type ChangeStage struct {
|
|||||||
Stage Stage
|
Stage Stage
|
||||||
}
|
}
|
||||||
|
|
||||||
type Stage uint8
|
// Command is a system event.
|
||||||
|
type Command struct {
|
||||||
type Event interface {
|
Type CommandType
|
||||||
ImplementsEvent()
|
// Suppress the default action of the command.
|
||||||
|
Cancel bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Stage uint8
|
||||||
|
type CommandType uint8
|
||||||
|
|
||||||
type Input interface {
|
type Input interface {
|
||||||
ImplementsInput()
|
ImplementsInput()
|
||||||
}
|
}
|
||||||
@@ -39,6 +47,12 @@ const (
|
|||||||
StageVisible
|
StageVisible
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
// CommandBack is the command for a back action
|
||||||
|
// such as the Android back button.
|
||||||
|
CommandBack CommandType = iota
|
||||||
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
inchPrDp = 1.0 / 160
|
inchPrDp = 1.0 / 160
|
||||||
mmPrDp = 25.4 / 160
|
mmPrDp = 25.4 / 160
|
||||||
@@ -98,6 +112,7 @@ func (l Stage) String() string {
|
|||||||
|
|
||||||
func (_ Draw) ImplementsEvent() {}
|
func (_ Draw) ImplementsEvent() {}
|
||||||
func (_ ChangeStage) ImplementsEvent() {}
|
func (_ ChangeStage) ImplementsEvent() {}
|
||||||
|
func (_ *Command) ImplementsEvent() {}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
args := strings.Split(extraArgs, "|")
|
args := strings.Split(extraArgs, "|")
|
||||||
|
|||||||
@@ -72,6 +72,11 @@ JNIEXPORT jint JNI_OnLoad(JavaVM *vm, void *reserver) {
|
|||||||
.name = "onFrameCallback",
|
.name = "onFrameCallback",
|
||||||
.signature = "(JJ)V",
|
.signature = "(JJ)V",
|
||||||
.fnPtr = onFrameCallback
|
.fnPtr = onFrameCallback
|
||||||
|
},
|
||||||
|
{
|
||||||
|
.name = "onBack",
|
||||||
|
.signature = "(J)Z",
|
||||||
|
.fnPtr = onBack
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
if ((*env)->RegisterNatives(env, viewClass, methods, sizeof(methods)/sizeof(methods[0])) != 0) {
|
if ((*env)->RegisterNatives(env, viewClass, methods, sizeof(methods)/sizeof(methods[0])) != 0) {
|
||||||
|
|||||||
@@ -180,6 +180,17 @@ func onFrameCallback(env *C.JNIEnv, class C.jclass, view C.jlong, nanos C.jlong)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//export onBack
|
||||||
|
func onBack(env *C.JNIEnv, class C.jclass, view C.jlong) C.jboolean {
|
||||||
|
w := views[view]
|
||||||
|
ev := &Command{Type: CommandBack}
|
||||||
|
w.event(ev)
|
||||||
|
if ev.Cancel {
|
||||||
|
return C.JNI_TRUE
|
||||||
|
}
|
||||||
|
return C.JNI_FALSE
|
||||||
|
}
|
||||||
|
|
||||||
func (w *window) setVisible() {
|
func (w *window) setVisible() {
|
||||||
win := w.aNativeWindow()
|
win := w.aNativeWindow()
|
||||||
width, height := C.ANativeWindow_getWidth(win), C.ANativeWindow_getHeight(win)
|
width, height := C.ANativeWindow_getWidth(win), C.ANativeWindow_getHeight(win)
|
||||||
|
|||||||
@@ -206,6 +206,9 @@ func (w *Window) event(e Event) {
|
|||||||
needRedraw = true
|
needRedraw = true
|
||||||
case key.Event:
|
case key.Event:
|
||||||
needRedraw = true
|
needRedraw = true
|
||||||
|
case *Command:
|
||||||
|
needAck = true
|
||||||
|
needRedraw = true
|
||||||
case ChangeStage:
|
case ChangeStage:
|
||||||
w.stage = e.Stage
|
w.stage = e.Stage
|
||||||
if w.stage > StageDead {
|
if w.stage > StageDead {
|
||||||
|
|||||||
Reference in New Issue
Block a user