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