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 'dart:async'; |
| 8 import 'dart:math'; |
| 9 import 'editable_string.dart'; |
| 10 import 'package:sky/services/keyboard/keyboard.mojom.dart'; |
| 11 |
| 12 class EditableText extends Component { |
| 13 static Style _style = new Style(''' |
| 14 display: paragraph; |
| 15 padding: 10px; |
| 16 height: 200px; |
| 17 background-color: lightblue;''' |
| 18 ); |
| 19 |
| 20 static Style _cusorStyle = new Style(''' |
| 21 display: inline-block; |
| 22 width: 2px; |
| 23 height: 1.2em; |
| 24 vertical-align: top; |
| 25 background-color: blue;''' |
| 26 ); |
| 27 |
| 28 static Style _composingStyle = new Style(''' |
| 29 display: inline; |
| 30 text-decoration: underline;''' |
| 31 ); |
| 32 |
| 33 KeyboardServiceProxy _service; |
| 34 |
| 35 EditableString _string; |
| 36 Timer _cursorTimer; |
| 37 bool _showCursor = true; |
| 38 |
| 39 EditableText({Object key}) : super(key: key, stateful: true) { |
| 40 _string = new EditableString(text: '', onChanged: _handleTextChanged); |
| 41 events.listen('click', _handleClick); |
| 42 events.listen('focus', _handleFocus); |
| 43 events.listen('blur', _handleBlur); |
| 44 } |
| 45 |
| 46 void _handleTextChanged(EditableString string) { |
| 47 setState(() { |
| 48 _string = string; |
| 49 }); |
| 50 } |
| 51 |
| 52 void _handleClick(_) { |
| 53 if (_service != null) |
| 54 return; |
| 55 _service = new KeyboardServiceProxy.unbound(); |
| 56 shell.requestService(_service); |
| 57 _service.ptr.show(_string.stub); |
| 58 } |
| 59 |
| 60 void _handleFocus(_) { |
| 61 print("_handleFocus"); |
| 62 } |
| 63 |
| 64 void _handleBlur(_) { |
| 65 print("_handleBlur"); |
| 66 } |
| 67 |
| 68 void _cursorTick(Timer timer) { |
| 69 setState(() { |
| 70 _showCursor = !_showCursor; |
| 71 }); |
| 72 } |
| 73 |
| 74 void didMount() { |
| 75 _cursorTimer = new Timer.periodic( |
| 76 new Duration(milliseconds: 500), _cursorTick); |
| 77 } |
| 78 |
| 79 void didUnmount() { |
| 80 _cursorTimer.stop(); |
| 81 } |
| 82 |
| 83 Node build() { |
| 84 List<Node> children = new List<Node>(); |
| 85 |
| 86 if (!_string.composing.isValid) { |
| 87 children.add(new Text(_string.text)); |
| 88 } else { |
| 89 String beforeComposing = _string.textBefore(_string.composing); |
| 90 if (!beforeComposing.isEmpty) |
| 91 children.add(new Text(beforeComposing)); |
| 92 |
| 93 String composing = _string.textInside(_string.composing); |
| 94 if (!composing.isEmpty) { |
| 95 children.add(new Container( |
| 96 key: 'composing', |
| 97 style: _composingStyle, |
| 98 children: [new Text(composing)] |
| 99 )); |
| 100 } |
| 101 |
| 102 String afterComposing = _string.textAfter(_string.composing); |
| 103 if (!afterComposing.isEmpty) |
| 104 children.add(new Text(afterComposing)); |
| 105 } |
| 106 |
| 107 if (_showCursor) |
| 108 children.add(new Container(key: 'cursor', style: _cusorStyle)); |
| 109 |
| 110 return new Container( |
| 111 style: _style, |
| 112 children: children |
| 113 ); |
| 114 } |
| 115 } |
OLD | NEW |