| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 import 'dart:async'; | 5 import 'dart:async'; |
| 6 import 'dart:sky' as sky; |
| 6 | 7 |
| 7 import '../painting/text_style.dart'; | 8 import '../painting/text_style.dart'; |
| 9 import '../rendering/object.dart'; |
| 8 import '../widgets/basic.dart'; | 10 import '../widgets/basic.dart'; |
| 9 import 'editable_string.dart'; | 11 import 'editable_string.dart'; |
| 10 | 12 |
| 13 const _kCursorBlinkPeriod = 500; // milliseconds |
| 14 const _kCursorGap = 1.0; |
| 15 const _kCursorHeightOffset = 2.0; |
| 16 const _kCursorWidth = 1.0; |
| 17 |
| 11 class EditableText extends Component { | 18 class EditableText extends Component { |
| 12 | 19 |
| 13 EditableText({String key, this.value, this.focused}) | 20 EditableText({ |
| 14 : super(key: key, stateful: true); | 21 String key, |
| 15 | 22 this.value, |
| 16 // static final Style _cursorStyle = new Style(''' | 23 this.focused: false, |
| 17 // width: 2px; | 24 this.style, |
| 18 // height: 1.2em; | 25 this.cursorColor}) : super(key: key, stateful: true); |
| 19 // vertical-align: top; | |
| 20 // background-color: ${Blue[500]};''' | |
| 21 // ); | |
| 22 | 26 |
| 23 EditableString value; | 27 EditableString value; |
| 24 bool focused; | 28 bool focused; |
| 29 TextStyle style; |
| 30 Color cursorColor; |
| 25 | 31 |
| 26 void syncFields(EditableText source) { | 32 void syncFields(EditableText source) { |
| 27 value = source.value; | 33 value = source.value; |
| 28 focused = source.focused; | 34 focused = source.focused; |
| 35 style = source.style; |
| 36 cursorColor = source.cursorColor; |
| 29 } | 37 } |
| 30 | 38 |
| 31 Timer _cursorTimer; | 39 Timer _cursorTimer; |
| 32 bool _showCursor = false; | 40 bool _showCursor = false; |
| 33 | 41 |
| 34 void _cursorTick(Timer timer) { | 42 void _cursorTick(Timer timer) { |
| 35 setState(() { | 43 setState(() { |
| 36 _showCursor = !_showCursor; | 44 _showCursor = !_showCursor; |
| 37 }); | 45 }); |
| 38 } | 46 } |
| 39 | 47 |
| 40 void _startCursorTimer() { | 48 void _startCursorTimer() { |
| 41 _showCursor = true; | 49 _showCursor = true; |
| 42 _cursorTimer = new Timer.periodic( | 50 _cursorTimer = new Timer.periodic( |
| 43 new Duration(milliseconds: 500), _cursorTick); | 51 new Duration(milliseconds: _kCursorBlinkPeriod), _cursorTick); |
| 44 } | 52 } |
| 45 | 53 |
| 46 void didUnmount() { | 54 void didUnmount() { |
| 47 if (_cursorTimer != null) | 55 if (_cursorTimer != null) |
| 48 _stopCursorTimer(); | 56 _stopCursorTimer(); |
| 49 super.didUnmount(); | 57 super.didUnmount(); |
| 50 } | 58 } |
| 51 | 59 |
| 52 void _stopCursorTimer() { | 60 void _stopCursorTimer() { |
| 53 _cursorTimer.cancel(); | 61 _cursorTimer.cancel(); |
| 54 _cursorTimer = null; | 62 _cursorTimer = null; |
| 55 _showCursor = false; | 63 _showCursor = false; |
| 56 } | 64 } |
| 57 | 65 |
| 66 void _paintCursor(sky.Canvas canvas, Size size) { |
| 67 if (!_showCursor) |
| 68 return; |
| 69 |
| 70 print("Draw cursor"); |
| 71 Rect cursorRect = new Rect.fromLTWH( |
| 72 _kCursorGap, |
| 73 -_kCursorHeightOffset, |
| 74 _kCursorWidth, |
| 75 style.fontSize + 2 * _kCursorHeightOffset |
| 76 ); |
| 77 canvas.drawRect(cursorRect, new Paint()..color = cursorColor); |
| 78 } |
| 79 |
| 58 Widget build() { | 80 Widget build() { |
| 81 assert(style != null); |
| 82 assert(focused != null); |
| 83 assert(cursorColor != null); |
| 84 |
| 59 if (focused && _cursorTimer == null) | 85 if (focused && _cursorTimer == null) |
| 60 _startCursorTimer(); | 86 _startCursorTimer(); |
| 61 else if (!focused && _cursorTimer != null) | 87 else if (!focused && _cursorTimer != null) |
| 62 _stopCursorTimer(); | 88 _stopCursorTimer(); |
| 63 | 89 |
| 64 if (!value.composing.isValid) { | 90 if (!value.composing.isValid) { |
| 65 return new Text(value.text); | 91 return new Text(value.text, style: style); |
| 66 } | 92 } |
| 67 | 93 |
| 68 return new StyledText(elements: [ | 94 TextStyle composingStyle = style.merge(const TextStyle(decoration: underline
)); |
| 69 const TextStyle(), | 95 StyledText text = new StyledText(elements: [ |
| 96 style, |
| 70 value.textBefore(value.composing), | 97 value.textBefore(value.composing), |
| 71 [const TextStyle(decoration: underline), value.textInside(value.composing)
], | 98 [composingStyle, value.textInside(value.composing)], |
| 72 value.textAfter(value.composing) | 99 value.textAfter(value.composing) |
| 73 ]); | 100 ]); |
| 101 |
| 102 Widget cursor = new Container( |
| 103 height: style.fontSize, |
| 104 width: _kCursorGap + _kCursorWidth, |
| 105 child: new CustomPaint(callback: _paintCursor, token: _showCursor) |
| 106 ); |
| 107 |
| 108 return new Flex([text, cursor]); |
| 74 } | 109 } |
| 75 } | 110 } |
| OLD | NEW |