Chromium Code Reviews| 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, |
| 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 Rect cursorRect = new Rect.fromLTWH( | |
| 68 _kCursorGap, | |
| 69 -_kCursorHeightOffset, | |
| 70 _kCursorWidth, | |
| 71 style.fontSize + 2 * _kCursorHeightOffset | |
| 72 ); | |
| 73 canvas.drawRect(cursorRect, new Paint()..color = cursorColor); | |
|
abarth-chromium
2015/06/29 23:33:08
Interesting that you're drawing outside the bounds
hansmuller
2015/06/29 23:56:34
The cursor extends _kCursorHeightOffset above and
| |
| 74 } | |
| 75 | |
| 76 Widget _buildCursor() { | |
| 77 return new Container( | |
| 78 height: style.fontSize, | |
| 79 width: _kCursorGap + _kCursorWidth, | |
| 80 child: new CustomPaint(callback: _paintCursor) | |
| 81 ); | |
| 82 } | |
| 83 | |
| 58 Widget build() { | 84 Widget build() { |
| 59 if (focused && _cursorTimer == null) | 85 assert(style != null); |
| 86 | |
| 87 if (focused == true && _cursorTimer == null) | |
|
abarth-chromium
2015/06/29 23:33:08
Why compare against true? That shouldn't be neede
hansmuller
2015/06/29 23:56:34
I've asserted that the focused: must be set and re
| |
| 60 _startCursorTimer(); | 88 _startCursorTimer(); |
| 61 else if (!focused && _cursorTimer != null) | 89 else if (focused != true && _cursorTimer != null) |
|
abarth-chromium
2015/06/29 23:33:08
ditto
hansmuller
2015/06/29 23:56:34
Done.
| |
| 62 _stopCursorTimer(); | 90 _stopCursorTimer(); |
| 63 | 91 |
| 64 if (!value.composing.isValid) { | 92 if (!value.composing.isValid) { |
| 65 return new Text(value.text); | 93 return new Text(value.text, style: style); |
| 66 } | 94 } |
| 67 | 95 |
| 68 return new StyledText(elements: [ | 96 TextStyle composingStyle = style.merge(const TextStyle(decoration: underline )); |
| 69 const TextStyle(), | 97 StyledText text = new StyledText(elements: [ |
| 98 style, | |
| 70 value.textBefore(value.composing), | 99 value.textBefore(value.composing), |
| 71 [const TextStyle(decoration: underline), value.textInside(value.composing) ], | 100 [composingStyle, value.textInside(value.composing)], |
| 72 value.textAfter(value.composing) | 101 value.textAfter(value.composing) |
| 73 ]); | 102 ]); |
| 103 | |
| 104 return _showCursor ? new Flex([text, _buildCursor()]) : text; | |
|
abarth-chromium
2015/06/29 23:33:08
It would be more efficient to always build the ren
hansmuller
2015/06/29 23:56:34
Done.
| |
| 74 } | 105 } |
| 75 } | 106 } |
| OLD | NEW |