| OLD | NEW |
| (Empty) |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 import '../../framework/fn.dart'; | |
| 6 import '../../framework/shell.dart' as shell; | |
| 7 import 'package:sky/services/keyboard/keyboard.mojom.dart'; | |
| 8 import 'dart:math'; | |
| 9 | |
| 10 class Input extends Component implements KeyboardClient{ | |
| 11 static Style _style = new Style(''' | |
| 12 display: paragraph; | |
| 13 padding: 10px; | |
| 14 height: 200px; | |
| 15 background-color: lightblue;''' | |
| 16 ); | |
| 17 | |
| 18 static Style _composingStyle = new Style(''' | |
| 19 display: inline; | |
| 20 text-decoration: underline;''' | |
| 21 ); | |
| 22 | |
| 23 KeyboardServiceProxy _service; | |
| 24 KeyboardClientStub _stub; | |
| 25 | |
| 26 String _text = ""; | |
| 27 int _composingStart = -1; | |
| 28 int _composingEnd = -1; | |
| 29 | |
| 30 Input({Object key}) : super(key: key, stateful: true) { | |
| 31 events.listen('click', _handleClick); | |
| 32 _stub = new KeyboardClientStub.unbound()..impl = this; | |
| 33 } | |
| 34 | |
| 35 bool get _hasComposingRegion => _composingStart != -1 && _composingEnd != -1; | |
| 36 | |
| 37 void _handleClick(_) { | |
| 38 if (_service != null) | |
| 39 return; | |
| 40 _service = new KeyboardServiceProxy.unbound(); | |
| 41 shell.requestService(_service); | |
| 42 _service.ptr.show(_stub); | |
| 43 } | |
| 44 | |
| 45 void _replaceComposing(String text) { | |
| 46 if (!_hasComposingRegion) { | |
| 47 _composingStart = _text.length; | |
| 48 _composingEnd = _composingStart + text.length; | |
| 49 _text += text; | |
| 50 return; | |
| 51 } | |
| 52 | |
| 53 _text = _text.substring(0, _composingStart) | |
| 54 + text + _text.substring(_composingEnd); | |
| 55 _composingEnd = _composingStart + text.length; | |
| 56 } | |
| 57 | |
| 58 void _clearComposingRegion() { | |
| 59 _composingStart = -1; | |
| 60 _composingEnd = -1; | |
| 61 } | |
| 62 | |
| 63 void commitText(String text, int newCursorPosition) { | |
| 64 setState(() { | |
| 65 _replaceComposing(text); | |
| 66 _clearComposingRegion(); | |
| 67 }); | |
| 68 } | |
| 69 | |
| 70 void setComposingText(String text, int newCursorPosition) { | |
| 71 setState(() { | |
| 72 _replaceComposing(text); | |
| 73 }); | |
| 74 } | |
| 75 | |
| 76 void setComposingRegion(int start, int end) { | |
| 77 setState(() { | |
| 78 _composingStart = start; | |
| 79 _composingEnd = end; | |
| 80 }); | |
| 81 } | |
| 82 | |
| 83 Node build() { | |
| 84 List<Node> children = new List<Node>(); | |
| 85 | |
| 86 if (!_hasComposingRegion) { | |
| 87 children.add(new Text(_text)); | |
| 88 } else { | |
| 89 String run = _text.substring(0, _composingStart); | |
| 90 if (!run.isEmpty) | |
| 91 children.add(new Text(run)); | |
| 92 | |
| 93 run = _text.substring(_composingStart, _composingEnd); | |
| 94 if (!run.isEmpty) { | |
| 95 children.add(new Container( | |
| 96 style: _composingStyle, | |
| 97 children: [new Text(_text.substring(_composingStart, _composingEnd))] | |
| 98 )); | |
| 99 } | |
| 100 | |
| 101 run = _text.substring(_composingEnd); | |
| 102 if (!run.isEmpty) | |
| 103 children.add(new Text(_text.substring(_composingEnd))); | |
| 104 } | |
| 105 | |
| 106 return new Container( | |
| 107 style: _style, | |
| 108 children: children | |
| 109 ); | |
| 110 } | |
| 111 } | |
| OLD | NEW |