editable_text.dart 2.5 KB
Newer Older
E
Eric Seidel 已提交
1 2 3 4 5
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:async';
6 7

import '../widgets/wrappers.dart';
E
Eric Seidel 已提交
8 9 10 11
import 'editable_string.dart';

class EditableText extends Component {

12 13 14
  EditableText({Object key, this.value, this.focused})
      : super(key: key, stateful: true);

E
Eric Seidel 已提交
15 16 17 18 19 20 21 22 23 24 25 26 27 28
  // static final Style _cursorStyle = new Style('''
  //   width: 2px;
  //   height: 1.2em;
  //   vertical-align: top;
  //   background-color: ${Blue[500]};'''
  // );

  // static final Style _composingStyle = new Style('''
  //   text-decoration: underline;'''
  // );

  EditableString value;
  bool focused;

29 30 31
  void syncFields(EditableText source) {
    value = source.value;
    focused = source.focused;
E
Eric Seidel 已提交
32 33
  }

34 35 36
  Timer _cursorTimer;
  bool _showCursor = false;

E
Eric Seidel 已提交
37 38 39 40 41 42 43 44 45 46 47 48
  void _cursorTick(Timer timer) {
    setState(() {
      _showCursor = !_showCursor;
    });
  }

  void _startCursorTimer() {
    _showCursor = true;
    _cursorTimer = new Timer.periodic(
        new Duration(milliseconds: 500), _cursorTick);
  }

49 50 51 52 53 54
  void didUnmount() {
    if (_cursorTimer != null)
      _stopCursorTimer();
    super.didUnmount();
  }

E
Eric Seidel 已提交
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
  void _stopCursorTimer() {
    _cursorTimer.cancel();
    _cursorTimer = null;
    _showCursor = false;
  }

  UINode build() {
    if (focused && _cursorTimer == null)
      _startCursorTimer();
    else if (!focused && _cursorTimer != null)
      _stopCursorTimer();

    //List<UINode> children = new List<UINode>();
    String hack = "";

    if (!value.composing.isValid) {
      // children.add(new TextFragment(value.text));
      hack += value.text;
    } else {
      String beforeComposing = value.textBefore(value.composing);
      if (!beforeComposing.isEmpty) {
        // children.add(new TextFragment(beforeComposing));
        hack += value.beforeComposing;
      }

      String composing = value.textInside(value.composing);
      hack += value.composing;
      hack += value.afterComposing;
      // if (!composing.isEmpty) {
      //   children.add(new TextFragment(
      //     composing,
      //     key: 'composing',
      //     style: _composingStyle
      //   ));
      // }

      // String afterComposing = value.textAfter(value.composing);
      // if (!afterComposing.isEmpty)
      //   children.add(new TextFragment(afterComposing));
    }

    // if (_showCursor)
    //   children.add(new Container(
    //     key: 'cursor',
    //     // style: _cursorStyle
    //     ));

    return new Paragraph(
      text: hack
    );
  }
}