app, io: [wasm, android] add support for numeric/email keyboard mode

Previously, the on-screen keyboard always displays the text keyboard,
(QWERTY or equivalent).

For optimal user experience, it's possible to specify the keyboard type
using `InputHint`. The on-screen keyboard will provide shortcuts or
restrict what the user can input.

Due to some limitations (gio#116), only numeric and text keyboards are
supported on Android.

Signed-off-by: Inkeliz <inkeliz@inkeliz.com>
This commit is contained in:
Inkeliz
2021-04-30 13:03:17 +01:00
committed by Elias Naur
parent e68ee35c86
commit 9b4b91fec0
14 changed files with 128 additions and 5 deletions
+24
View File
@@ -152,6 +152,7 @@ var gioView struct {
getFontScale C.jmethodID
showTextInput C.jmethodID
hideTextInput C.jmethodID
setInputHint C.jmethodID
postFrameCallback C.jmethodID
setCursor C.jmethodID
setOrientation C.jmethodID
@@ -281,6 +282,7 @@ func Java_org_gioui_GioView_onCreateView(env *C.JNIEnv, class C.jclass, view C.j
m.getFontScale = getMethodID(env, class, "getFontScale", "()F")
m.showTextInput = getMethodID(env, class, "showTextInput", "()V")
m.hideTextInput = getMethodID(env, class, "hideTextInput", "()V")
m.setInputHint = getMethodID(env, class, "setInputHint", "(I)V")
m.postFrameCallback = getMethodID(env, class, "postFrameCallback", "()V")
m.setCursor = getMethodID(env, class, "setCursor", "(I)V")
m.setOrientation = getMethodID(env, class, "setOrientation", "(II)V")
@@ -609,6 +611,28 @@ func (w *window) ShowTextInput(show bool) {
})
}
func (w *window) SetInputHint(mode key.InputHint) {
// Constants defined at https://developer.android.com/reference/android/text/InputType.
const (
TYPE_NULL = 0
TYPE_CLASS_NUMBER = 2
TYPE_NUMBER_FLAG_DECIMAL = 8192
TYPE_NUMBER_FLAG_SIGNED = 4096
)
runInJVM(javaVM(), func(env *C.JNIEnv) {
var m jvalue
switch mode {
case key.HintNumeric:
m = TYPE_CLASS_NUMBER | TYPE_NUMBER_FLAG_DECIMAL | TYPE_NUMBER_FLAG_SIGNED
default:
// TYPE_NULL, since TYPE_CLASS_TEXT isn't currently supported (gio#116), so TYPE_NULL is used instead.
m = TYPE_NULL
}
callVoidMethod(env, w.view, gioView.setInputHint, m)
})
}
func javaString(env *C.JNIEnv, str string) C.jstring {
if str == "" {
return 0