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