| 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 '../fn.dart'; | |
| 6 import '../theme/colors.dart'; | |
| 7 import 'dart:async'; | |
| 8 import 'editable_string.dart'; | |
| 9 | |
| 10 class EditableText extends Component { | |
| 11 | |
| 12 static final Style _cursorStyle = new Style(''' | |
| 13 width: 2px; | |
| 14 height: 1.2em; | |
| 15 vertical-align: top; | |
| 16 background-color: ${Blue[500]};''' | |
| 17 ); | |
| 18 | |
| 19 static final Style _composingStyle = new Style(''' | |
| 20 text-decoration: underline;''' | |
| 21 ); | |
| 22 | |
| 23 EditableString value; | |
| 24 bool focused; | |
| 25 Timer _cursorTimer; | |
| 26 bool _showCursor = false; | |
| 27 | |
| 28 EditableText({Object key, this.value, this.focused}) | |
| 29 : super(key: key, stateful: true) { | |
| 30 onDidUnmount(() { | |
| 31 if (_cursorTimer != null) | |
| 32 _stopCursorTimer(); | |
| 33 }); | |
| 34 } | |
| 35 | |
| 36 void _cursorTick(Timer timer) { | |
| 37 setState(() { | |
| 38 _showCursor = !_showCursor; | |
| 39 }); | |
| 40 } | |
| 41 | |
| 42 void _startCursorTimer() { | |
| 43 _showCursor = true; | |
| 44 _cursorTimer = new Timer.periodic( | |
| 45 new Duration(milliseconds: 500), _cursorTick); | |
| 46 } | |
| 47 | |
| 48 void _stopCursorTimer() { | |
| 49 _cursorTimer.cancel(); | |
| 50 _cursorTimer = null; | |
| 51 _showCursor = false; | |
| 52 } | |
| 53 | |
| 54 UINode build() { | |
| 55 if (focused && _cursorTimer == null) | |
| 56 _startCursorTimer(); | |
| 57 else if (!focused && _cursorTimer != null) | |
| 58 _stopCursorTimer(); | |
| 59 | |
| 60 List<UINode> children = new List<UINode>(); | |
| 61 | |
| 62 if (!value.composing.isValid) { | |
| 63 children.add(new TextFragment(value.text)); | |
| 64 } else { | |
| 65 String beforeComposing = value.textBefore(value.composing); | |
| 66 if (!beforeComposing.isEmpty) | |
| 67 children.add(new TextFragment(beforeComposing)); | |
| 68 | |
| 69 String composing = value.textInside(value.composing); | |
| 70 if (!composing.isEmpty) { | |
| 71 children.add(new TextFragment( | |
| 72 composing, | |
| 73 key: 'composing', | |
| 74 style: _composingStyle | |
| 75 )); | |
| 76 } | |
| 77 | |
| 78 String afterComposing = value.textAfter(value.composing); | |
| 79 if (!afterComposing.isEmpty) | |
| 80 children.add(new TextFragment(afterComposing)); | |
| 81 } | |
| 82 | |
| 83 if (_showCursor) | |
| 84 children.add(new Container(key: 'cursor', style: _cursorStyle)); | |
| 85 | |
| 86 return new Paragraph( | |
| 87 children: children | |
| 88 ); | |
| 89 } | |
| 90 } | |
| OLD | NEW |