text_selection.dart 6.44 KB
Newer Older
1 2 3 4
// Copyright 2016 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.

5
import 'dart:async';
6 7 8
import 'dart:math' as math;

import 'package:flutter/widgets.dart';
9
import 'package:flutter/services.dart';
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34

import 'flat_button.dart';
import 'material.dart';
import 'theme.dart';

const double _kHandleSize = 22.0; // pixels
const double _kToolbarScreenPadding = 8.0; // pixels

/// Manages a copy/paste text selection toolbar.
class _TextSelectionToolbar extends StatelessWidget {
  _TextSelectionToolbar(this.delegate, {Key key}) : super(key: key);

  final TextSelectionDelegate delegate;
  InputValue get value => delegate.inputValue;

  @override
  Widget build(BuildContext context) {
    List<Widget> items = <Widget>[];

    if (!value.selection.isCollapsed) {
      items.add(new FlatButton(child: new Text('CUT'), onPressed: _handleCut));
      items.add(new FlatButton(child: new Text('COPY'), onPressed: _handleCopy));
    }
    items.add(new FlatButton(
      child: new Text('PASTE'),
35 36 37
      // TODO(mpcomplete): This should probably be grayed-out if there is nothing to paste.
      onPressed: _handlePaste
    ));
38 39 40
    if (value.text.isNotEmpty) {
      if (value.selection.isCollapsed)
        items.add(new FlatButton(child: new Text('SELECT ALL'), onPressed: _handleSelectAll));
41 42 43 44 45 46
    }

    return new Material(
      elevation: 1,
      child: new Container(
        height: 44.0,
47
        child: new Row(mainAxisSize: MainAxisSize.min, children: items)
48 49 50 51 52
      )
    );
  }

  void _handleCut() {
53
    Clipboard.setData(new ClipboardData(text: value.selection.textInside(value.text)));
54 55 56 57 58 59 60 61
    delegate.inputValue = new InputValue(
      text: value.selection.textBefore(value.text) + value.selection.textAfter(value.text),
      selection: new TextSelection.collapsed(offset: value.selection.start)
    );
    delegate.hideToolbar();
  }

  void _handleCopy() {
62
    Clipboard.setData(new ClipboardData(text: value.selection.textInside(value.text)));
63 64 65 66 67 68 69
    delegate.inputValue = new InputValue(
      text: value.text,
      selection: new TextSelection.collapsed(offset: value.selection.end)
    );
    delegate.hideToolbar();
  }

70 71
  Future<Null> _handlePaste() async {
    InputValue value = this.value;  // Snapshot the input before using `await`.
72 73
    ClipboardData data = await Clipboard.getData(Clipboard.kTextPlain);
    if (data != null) {
74
      delegate.inputValue = new InputValue(
75 76
        text: value.selection.textBefore(value.text) + data.text + value.selection.textAfter(value.text),
        selection: new TextSelection.collapsed(offset: value.selection.start + data.text.length)
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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
    delegate.hideToolbar();
  }

  void _handleSelectAll() {
    delegate.inputValue = new InputValue(
      text: value.text,
      selection: new TextSelection(baseOffset: 0, extentOffset: value.text.length)
    );
  }
}

/// Centers the toolbar around the given position, ensuring that it remains on
/// screen.
class _TextSelectionToolbarLayout extends SingleChildLayoutDelegate {
  _TextSelectionToolbarLayout(this.position);

  final Point position;

  @override
  BoxConstraints getConstraintsForChild(BoxConstraints constraints) {
    return constraints.loosen();
  }

  @override
  Offset getPositionForChild(Size size, Size childSize) {
    double x = position.x - childSize.width/2.0;
    double y = position.y - childSize.height;

    if (x < _kToolbarScreenPadding)
      x = _kToolbarScreenPadding;
    else if (x + childSize.width > size.width - 2 * _kToolbarScreenPadding)
      x = size.width - childSize.width - _kToolbarScreenPadding;
    if (y < _kToolbarScreenPadding)
      y = _kToolbarScreenPadding;
    else if (y + childSize.height > size.height - 2 * _kToolbarScreenPadding)
      y = size.height - childSize.height - _kToolbarScreenPadding;

    return new Offset(x, y);
  }

  @override
  bool shouldRelayout(_TextSelectionToolbarLayout oldDelegate) {
    return position != oldDelegate.position;
  }
}

/// Draws a single text selection handle. The [type] determines where the handle
/// points (e.g. the [left] handle points up and to the right).
class _TextSelectionHandlePainter extends CustomPainter {
  _TextSelectionHandlePainter({ this.color });

  final Color color;

  @override
  void paint(Canvas canvas, Size size) {
    Paint paint = new Paint()..color = color;
    double radius = size.width/2.0;
    canvas.drawCircle(new Point(radius, radius), radius, paint);
    canvas.drawRect(new Rect.fromLTWH(0.0, 0.0, radius, radius), paint);
  }

  @override
  bool shouldRepaint(_TextSelectionHandlePainter oldPainter) {
    return color != oldPainter.color;
  }
}

146 147 148
class _MaterialTextSelectionControls extends TextSelectionControls {
  @override
  Size handleSize = const Size(_kHandleSize, _kHandleSize);
149

150 151 152 153 154 155 156 157 158 159
  /// Builder for material-style copy/paste text selection toolbar.
  @override
  Widget buildToolbar(
      BuildContext context, Point position, TextSelectionDelegate delegate) {
    final Size screenSize = MediaQuery.of(context).size;
    return new ConstrainedBox(
      constraints: new BoxConstraints.loose(screenSize),
      child: new CustomSingleChildLayout(
        delegate: new _TextSelectionToolbarLayout(position),
        child: new _TextSelectionToolbar(delegate)
160
      )
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
    );
  }

  /// Builder for material-style text selection handles.
  @override
  Widget buildHandle(BuildContext context, TextSelectionHandleType type) {
    Widget handle = new SizedBox(
      width: _kHandleSize,
      height: _kHandleSize,
      child: new CustomPaint(
        painter: new _TextSelectionHandlePainter(
          color: Theme.of(context).textSelectionHandleColor
        )
      )
    );

    // [handle] is a circle, with a rectangle in the top left quadrant of that
    // circle (an onion pointing to 10:30). We rotate [handle] to point
    // straight up or up-right depending on the handle type.
    switch (type) {
      case TextSelectionHandleType.left:  // points up-right
        return new Transform(
          transform: new Matrix4.rotationZ(math.PI / 2.0),
          child: handle
        );
      case TextSelectionHandleType.right:  // points up-left
        return handle;
      case TextSelectionHandleType.collapsed:  // points up
        return new Transform(
          transform: new Matrix4.rotationZ(math.PI / 4.0),
          child: handle
        );
    }
    assert(type != null);
    return null;
196 197
  }
}
198 199

final _MaterialTextSelectionControls materialTextSelectionControls = new _MaterialTextSelectionControls();