Implement a basic IME-aware input element
This CL introduces a new keyboard service that understands Android IME and starts work on a Input component that interacts with this service to provide an editing control. R=eseidel@chromium.org Review URL: https://codereview.chromium.org/995613002
This commit is contained in:
12
engine/src/flutter/examples/editor/editor_app.dart
Normal file
12
engine/src/flutter/examples/editor/editor_app.dart
Normal file
@@ -0,0 +1,12 @@
|
||||
// Copyright 2015 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import '../../framework/fn.dart';
|
||||
import 'input.dart';
|
||||
|
||||
class EditorApp extends App {
|
||||
Node build() {
|
||||
return new Input();
|
||||
}
|
||||
}
|
||||
10
engine/src/flutter/examples/editor/index.sky
Normal file
10
engine/src/flutter/examples/editor/index.sky
Normal file
@@ -0,0 +1,10 @@
|
||||
#!mojo mojo:sky_viewer
|
||||
<sky>
|
||||
<import src="/sky/framework/debug/shake-to-reload.sky" />
|
||||
<script>
|
||||
import 'editor_app.dart';
|
||||
main() {
|
||||
new EditorApp();
|
||||
}
|
||||
</script>
|
||||
</sky>
|
||||
111
engine/src/flutter/examples/editor/input.dart
Normal file
111
engine/src/flutter/examples/editor/input.dart
Normal file
@@ -0,0 +1,111 @@
|
||||
// Copyright 2015 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import '../../framework/fn.dart';
|
||||
import '../../framework/shell.dart' as shell;
|
||||
import 'package:sky/services/keyboard/keyboard.mojom.dart';
|
||||
import 'dart:math';
|
||||
|
||||
class Input extends Component implements KeyboardClient{
|
||||
static Style _style = new Style('''
|
||||
display: paragraph;
|
||||
padding: 10px;
|
||||
height: 200px;
|
||||
background-color: lightblue;'''
|
||||
);
|
||||
|
||||
static Style _composingStyle = new Style('''
|
||||
display: inline;
|
||||
text-decoration: underline;'''
|
||||
);
|
||||
|
||||
KeyboardServiceProxy _service;
|
||||
KeyboardClientStub _stub;
|
||||
|
||||
String _text = "";
|
||||
int _composingStart = -1;
|
||||
int _composingEnd = -1;
|
||||
|
||||
Input({Object key}) : super(key: key, stateful: true) {
|
||||
events.listen('click', _handleClick);
|
||||
_stub = new KeyboardClientStub.unbound()..impl = this;
|
||||
}
|
||||
|
||||
bool get _hasComposingRegion => _composingStart != -1 && _composingEnd != -1;
|
||||
|
||||
void _handleClick(_) {
|
||||
if (_service != null)
|
||||
return;
|
||||
_service = new KeyboardServiceProxy.unbound();
|
||||
shell.requestService(_service);
|
||||
_service.ptr.show(_stub);
|
||||
}
|
||||
|
||||
void _replaceComposing(String text) {
|
||||
if (!_hasComposingRegion) {
|
||||
_composingStart = _text.length;
|
||||
_composingEnd = _composingStart + text.length;
|
||||
_text += text;
|
||||
return;
|
||||
}
|
||||
|
||||
_text = _text.substring(0, _composingStart)
|
||||
+ text + _text.substring(_composingEnd);
|
||||
_composingEnd = _composingStart + text.length;
|
||||
}
|
||||
|
||||
void _clearComposingRegion() {
|
||||
_composingStart = -1;
|
||||
_composingEnd = -1;
|
||||
}
|
||||
|
||||
void commitText(String text, int newCursorPosition) {
|
||||
setState(() {
|
||||
_replaceComposing(text);
|
||||
_clearComposingRegion();
|
||||
});
|
||||
}
|
||||
|
||||
void setComposingText(String text, int newCursorPosition) {
|
||||
setState(() {
|
||||
_replaceComposing(text);
|
||||
});
|
||||
}
|
||||
|
||||
void setComposingRegion(int start, int end) {
|
||||
setState(() {
|
||||
_composingStart = start;
|
||||
_composingEnd = end;
|
||||
});
|
||||
}
|
||||
|
||||
Node build() {
|
||||
List<Node> children = new List<Node>();
|
||||
|
||||
if (!_hasComposingRegion) {
|
||||
children.add(new Text(_text));
|
||||
} else {
|
||||
String run = _text.substring(0, _composingStart);
|
||||
if (!run.isEmpty)
|
||||
children.add(new Text(run));
|
||||
|
||||
run = _text.substring(_composingStart, _composingEnd);
|
||||
if (!run.isEmpty) {
|
||||
children.add(new Container(
|
||||
style: _composingStyle,
|
||||
children: [new Text(_text.substring(_composingStart, _composingEnd))]
|
||||
));
|
||||
}
|
||||
|
||||
run = _text.substring(_composingEnd);
|
||||
if (!run.isEmpty)
|
||||
children.add(new Text(_text.substring(_composingEnd)));
|
||||
}
|
||||
|
||||
return new Container(
|
||||
style: _style,
|
||||
children: children
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -89,6 +89,7 @@ android_library("java") {
|
||||
"//mojo/public/java:bindings",
|
||||
"//mojo/public/java:system",
|
||||
"//mojo/services/network/public/interfaces:interfaces_java",
|
||||
"//sky/services/keyboard",
|
||||
"//sky/services/oknet",
|
||||
"//sky/services/viewport:viewport_java",
|
||||
]
|
||||
|
||||
@@ -10,6 +10,8 @@ import android.view.Surface;
|
||||
import android.view.SurfaceHolder;
|
||||
import android.view.SurfaceView;
|
||||
import android.view.View;
|
||||
import android.view.inputmethod.EditorInfo;
|
||||
import android.view.inputmethod.InputConnection;
|
||||
|
||||
import org.chromium.base.JNINamespace;
|
||||
import org.chromium.mojo.bindings.InterfaceRequest;
|
||||
@@ -21,6 +23,7 @@ import org.chromium.mojom.sky.InputEvent;
|
||||
import org.chromium.mojom.sky.PointerData;
|
||||
import org.chromium.mojom.sky.PointerKind;
|
||||
import org.chromium.mojom.sky.ViewportObserver;
|
||||
import org.domokit.keyboard.KeyboardServiceImpl;
|
||||
|
||||
/**
|
||||
* A view containing Sky
|
||||
@@ -68,6 +71,7 @@ public class PlatformView extends SurfaceView
|
||||
getHolder().addCallback(mSurfaceCallback);
|
||||
|
||||
mGestureProvider = new GestureProvider(context, this);
|
||||
KeyboardServiceImpl.setActiveView(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -86,6 +90,16 @@ public class PlatformView extends SurfaceView
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCheckIsTextEditor() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
|
||||
return KeyboardServiceImpl.createInputConnection(outAttrs);
|
||||
}
|
||||
|
||||
private int getTypeForAction(int maskedAction) {
|
||||
// Primary pointer:
|
||||
if (maskedAction == MotionEvent.ACTION_DOWN)
|
||||
|
||||
Reference in New Issue
Block a user