text_selection.dart 29 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:math' as math;

7
import 'package:flutter/rendering.dart';
8
import 'package:flutter/scheduler.dart';
9
import 'package:flutter/services.dart';
10
import 'package:flutter/widgets.dart';
11

12 13
import 'colors.dart';
import 'constants.dart';
14
import 'debug.dart';
15 16
import 'icon_button.dart';
import 'icons.dart';
17
import 'material.dart';
18
import 'material_localizations.dart';
19
import 'text_button.dart';
20
import 'text_selection_theme.dart';
21 22
import 'theme.dart';

xster's avatar
xster committed
23
const double _kHandleSize = 22.0;
24

xster's avatar
xster committed
25 26 27
// Minimal padding from all edges of the selection toolbar to all edges of the
// viewport.
const double _kToolbarScreenPadding = 8.0;
28
const double _kToolbarHeight = 44.0;
29
// Padding when positioning toolbar below selection.
30
const double _kToolbarContentDistanceBelow = _kHandleSize - 2.0;
31
const double _kToolbarContentDistance = 8.0;
32 33

/// Manages a copy/paste text selection toolbar.
34
class _TextSelectionToolbar extends StatefulWidget {
xster's avatar
xster committed
35
  const _TextSelectionToolbar({
36 37 38 39 40 41 42
    Key? key,
    required this.clipboardStatus,
    required this.handleCut,
    required this.handleCopy,
    required this.handlePaste,
    required this.handleSelectAll,
    required this.isAbove,
xster's avatar
xster committed
43
  }) : super(key: key);
44

45 46 47 48 49
  final ClipboardStatusNotifier? clipboardStatus;
  final VoidCallback? handleCut;
  final VoidCallback? handleCopy;
  final VoidCallback? handlePaste;
  final VoidCallback? handleSelectAll;
xster's avatar
xster committed
50

51 52 53 54 55 56 57
  // When true, the toolbar fits above its anchor and will be positioned there.
  final bool isAbove;

  @override
  _TextSelectionToolbarState createState() => _TextSelectionToolbarState();
}

58 59 60 61 62 63 64 65 66 67 68 69
// Intermediate data used for building menu items with the _getItems method.
class _ItemData {
  const _ItemData(
    this.onPressed,
    this.label,
  ) : assert(onPressed != null),
      assert(label != null);

  final VoidCallback onPressed;
  final String label;
}

70
class _TextSelectionToolbarState extends State<_TextSelectionToolbar> with TickerProviderStateMixin {
71
  late ClipboardStatusNotifier _clipboardStatus;
72

73 74 75 76 77 78 79 80
  // Whether or not the overflow menu is open. When it is closed, the menu
  // items that don't overflow are shown. When it is open, only the overflowing
  // menu items are shown.
  bool _overflowOpen = false;

  // The key for _TextSelectionToolbarContainer.
  UniqueKey _containerKey = UniqueKey();

81 82 83
  Widget _getItem(_ItemData itemData, bool isFirst, bool isLast) {
    assert(isFirst != null);
    assert(isLast != null);
84 85

    // TODO(hansmuller): Should be colorScheme.onSurface
86
    final ThemeData theme = Theme.of(context)!;
87 88 89 90 91 92 93 94
    final bool isDark = theme.colorScheme.brightness == Brightness.dark;
    final Color primary = isDark ? Colors.white : Colors.black87;

    return TextButton(
      style: TextButton.styleFrom(
        primary: primary,
        shape: const RoundedRectangleBorder(),
        minimumSize: const Size(kMinInteractiveDimension, kMinInteractiveDimension),
95 96 97 98 99 100 101
        padding: EdgeInsets.only(
          // These values were eyeballed to match the native text selection menu
          // on a Pixel 2 running Android 10.
          left: 9.5 + (isFirst ? 5.0 : 0.0),
          right: 9.5 + (isLast ? 5.0 : 0.0),
        ),
      ),
102 103
      onPressed: itemData.onPressed,
      child: Text(itemData.label),
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
  // Close the menu and reset layout calculations, as in when the menu has
  // changed and saved values are no longer relevant. This should be called in
  // setState or another context where a rebuild is happening.
  void _reset() {
    // Change _TextSelectionToolbarContainer's key when the menu changes in
    // order to cause it to rebuild. This lets it recalculate its
    // saved width for the new set of children, and it prevents AnimatedSize
    // from animating the size change.
    _containerKey = UniqueKey();
    // If the menu items change, make sure the overflow menu is closed. This
    // prevents an empty overflow menu.
    _overflowOpen = false;
  }

  void _onChangedClipboardStatus() {
    setState(() {
      // Inform the widget that the value of clipboardStatus has changed.
    });
  }

  @override
  void initState() {
    super.initState();
    _clipboardStatus = widget.clipboardStatus ?? ClipboardStatusNotifier();
    _clipboardStatus.addListener(_onChangedClipboardStatus);
    _clipboardStatus.update();
  }

135 136
  @override
  void didUpdateWidget(_TextSelectionToolbar oldWidget) {
137 138
    super.didUpdateWidget(oldWidget);
    // If the children are changing, the current page should be reset.
139 140 141 142
    if (((widget.handleCut == null) != (oldWidget.handleCut == null))
      || ((widget.handleCopy == null) != (oldWidget.handleCopy == null))
      || ((widget.handlePaste == null) != (oldWidget.handlePaste == null))
      || ((widget.handleSelectAll == null) != (oldWidget.handleSelectAll == null))) {
143 144 145 146 147
      _reset();
    }
    if (oldWidget.clipboardStatus == null && widget.clipboardStatus != null) {
      _clipboardStatus.removeListener(_onChangedClipboardStatus);
      _clipboardStatus.dispose();
148
      _clipboardStatus = widget.clipboardStatus!;
149 150 151 152
    } else if (oldWidget.clipboardStatus != null) {
      if (widget.clipboardStatus == null) {
        _clipboardStatus = ClipboardStatusNotifier();
        _clipboardStatus.addListener(_onChangedClipboardStatus);
153
        oldWidget.clipboardStatus!.removeListener(_onChangedClipboardStatus);
154
      } else if (widget.clipboardStatus != oldWidget.clipboardStatus) {
155
        _clipboardStatus = widget.clipboardStatus!;
156
        _clipboardStatus.addListener(_onChangedClipboardStatus);
157
        oldWidget.clipboardStatus!.removeListener(_onChangedClipboardStatus);
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
      }
    }
    if (widget.handlePaste != null) {
      _clipboardStatus.update();
    }
  }

  @override
  void dispose() {
    super.dispose();
    // When used in an Overlay, this can be disposed after its creator has
    // already disposed _clipboardStatus.
    if (!_clipboardStatus.disposed) {
      _clipboardStatus.removeListener(_onChangedClipboardStatus);
      if (widget.clipboardStatus == null) {
        _clipboardStatus.dispose();
      }
175 176 177
    }
  }

178 179
  @override
  Widget build(BuildContext context) {
180 181 182 183 184 185
    // Don't render the menu until the state of the clipboard is known.
    if (widget.handlePaste != null
        && _clipboardStatus.value == ClipboardStatus.unknown) {
      return const SizedBox(width: 0.0, height: 0.0);
    }

186
    final MaterialLocalizations localizations = MaterialLocalizations.of(context)!;
187
    final List<_ItemData> itemDatas = <_ItemData>[
188
      if (widget.handleCut != null)
189
        _ItemData(widget.handleCut!, localizations.cutButtonLabel),
190
      if (widget.handleCopy != null)
191
        _ItemData(widget.handleCopy!, localizations.copyButtonLabel),
192 193
      if (widget.handlePaste != null
          && _clipboardStatus.value == ClipboardStatus.pasteable)
194
        _ItemData(widget.handlePaste!, localizations.pasteButtonLabel),
195
      if (widget.handleSelectAll != null)
196
        _ItemData(widget.handleSelectAll!, localizations.selectAllButtonLabel),
197
    ];
198

199
    // If there is no option available, build an empty widget.
200
    if (itemDatas.isEmpty) {
201
      return const SizedBox(width: 0.0, height: 0.0);
202 203
    }

204 205 206 207 208 209 210 211 212
    return _TextSelectionToolbarContainer(
      key: _containerKey,
      overflowOpen: _overflowOpen,
      child: AnimatedSize(
        vsync: this,
        // This duration was eyeballed on a Pixel 2 emulator running Android
        // API 28.
        duration: const Duration(milliseconds: 140),
        child: Material(
213 214 215 216
          // This value was eyeballed to match the native text selection menu on
          // a Pixel 2 running Android 10.
          borderRadius: const BorderRadius.all(Radius.circular(7.0)),
          clipBehavior: Clip.antiAlias,
217
          elevation: 1.0,
218
          type: MaterialType.card,
219 220 221 222 223 224 225
          child: _TextSelectionToolbarItems(
            isAbove: widget.isAbove,
            overflowOpen: _overflowOpen,
            children: <Widget>[
              // The navButton that shows and hides the overflow menu is the
              // first child.
              Material(
226
                type: MaterialType.card,
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
                child: IconButton(
                  // TODO(justinmc): This should be an AnimatedIcon, but
                  // AnimatedIcons doesn't yet support arrow_back to more_vert.
                  // https://github.com/flutter/flutter/issues/51209
                  icon: Icon(_overflowOpen ? Icons.arrow_back : Icons.more_vert),
                  onPressed: () {
                    setState(() {
                      _overflowOpen = !_overflowOpen;
                    });
                  },
                  tooltip: _overflowOpen
                      ? localizations.backButtonTooltip
                      : localizations.moreButtonTooltip,
                ),
              ),
242 243
              for (int i = 0; i < itemDatas.length; i++)
                _getItem(itemDatas[i], i == 0, i == itemDatas.length - 1)
244 245 246
            ],
          ),
        ),
247
      ),
248 249 250 251
    );
  }
}

252 253 254 255 256
// When the overflow menu is open, it tries to align its right edge to the right
// edge of the closed menu. This widget handles this effect by measuring and
// maintaining the width of the closed menu and aligning the child to the right.
class _TextSelectionToolbarContainer extends SingleChildRenderObjectWidget {
  const _TextSelectionToolbarContainer({
257 258 259
    Key? key,
    required Widget child,
    required this.overflowOpen,
260 261 262
  }) : assert(child != null),
       assert(overflowOpen != null),
       super(key: key, child: child);
263

264
  final bool overflowOpen;
265

266 267 268 269 270 271 272 273 274 275
  @override
  _TextSelectionToolbarContainerRenderBox createRenderObject(BuildContext context) {
    return _TextSelectionToolbarContainerRenderBox(overflowOpen: overflowOpen);
  }

  @override
  void updateRenderObject(BuildContext context, _TextSelectionToolbarContainerRenderBox renderObject) {
    renderObject.overflowOpen = overflowOpen;
  }
}
276

277 278
class _TextSelectionToolbarContainerRenderBox extends RenderProxyBox {
  _TextSelectionToolbarContainerRenderBox({
279
    required bool overflowOpen,
280 281 282 283 284 285 286
  }) : assert(overflowOpen != null),
       _overflowOpen = overflowOpen,
       super();

  // The width of the menu when it was closed. This is used to achieve the
  // behavior where the open menu aligns its right edge to the closed menu's
  // right edge.
287
  double? _closedWidth;
288 289 290 291 292 293 294 295 296 297

  bool _overflowOpen;
  bool get overflowOpen => _overflowOpen;
  set overflowOpen(bool value) {
    if (value == overflowOpen) {
      return;
    }
    _overflowOpen = value;
    markNeedsLayout();
  }
298 299

  @override
300
  void performLayout() {
301
    child!.layout(constraints.loosen(), parentUsesSize: true);
302 303 304 305 306 307

    // Save the width when the menu is closed. If the menu changes, this width
    // is invalid, so it's important that this RenderBox be recreated in that
    // case. Currently, this is achieved by providing a new key to
    // _TextSelectionToolbarContainer.
    if (!overflowOpen && _closedWidth == null) {
308
      _closedWidth = child!.size.width;
309 310 311 312 313 314 315
    }

    size = constraints.constrain(Size(
      // If the open menu is wider than the closed menu, just use its own width
      // and don't worry about aligning the right edges.
      // _closedWidth is used even when the menu is closed to allow it to
      // animate its size while keeping the same right alignment.
316 317
      _closedWidth == null || child!.size.width > _closedWidth! ? child!.size.width : _closedWidth!,
      child!.size.height,
318 319
    ));

320
    final ToolbarItemsParentData childParentData = child!.parentData! as ToolbarItemsParentData;
321
    childParentData.offset = Offset(
322
      size.width - child!.size.width,
323 324
      0.0,
    );
325 326
  }

327
  // Paint at the offset set in the parent data.
328
  @override
329
  void paint(PaintingContext context, Offset offset) {
330 331
    final ToolbarItemsParentData childParentData = child!.parentData! as ToolbarItemsParentData;
    context.paintChild(child!, childParentData.offset + offset);
332 333 334 335
  }

  // Include the parent data offset in the hit test.
  @override
336
  bool hitTestChildren(BoxHitTestResult result, { required Offset position }) {
337
    // The x, y parameters have the top left of the node's box as the origin.
338
    final ToolbarItemsParentData childParentData = child!.parentData! as ToolbarItemsParentData;
339 340 341 342 343
    return result.addWithPaintOffset(
      offset: childParentData.offset,
      position: position,
      hitTest: (BoxHitTestResult result, Offset transformed) {
        assert(transformed == position - childParentData.offset);
344
        return child!.hitTest(result, position: transformed);
345 346 347 348 349 350
      },
    );
  }

  @override
  void setupParentData(RenderBox child) {
351 352
    if (child.parentData is! ToolbarItemsParentData) {
      child.parentData = ToolbarItemsParentData();
353 354 355 356 357
    }
  }

  @override
  void applyPaintTransform(RenderObject child, Matrix4 transform) {
358
    final ToolbarItemsParentData childParentData = child.parentData! as ToolbarItemsParentData;
359 360 361 362 363 364 365 366 367
    transform.translate(childParentData.offset.dx, childParentData.offset.dy);
    super.applyPaintTransform(child, transform);
  }
}

// Renders the menu items in the correct positions in the menu and its overflow
// submenu based on calculating which item would first overflow.
class _TextSelectionToolbarItems extends MultiChildRenderObjectWidget {
  _TextSelectionToolbarItems({
368 369 370 371
    Key? key,
    required this.isAbove,
    required this.overflowOpen,
    required List<Widget> children,
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
  }) : assert(children != null),
       assert(isAbove != null),
       assert(overflowOpen != null),
       super(key: key, children: children);

  final bool isAbove;
  final bool overflowOpen;

  @override
  _TextSelectionToolbarItemsRenderBox createRenderObject(BuildContext context) {
    return _TextSelectionToolbarItemsRenderBox(
      isAbove: isAbove,
      overflowOpen: overflowOpen,
    );
  }

  @override
  void updateRenderObject(BuildContext context, _TextSelectionToolbarItemsRenderBox renderObject) {
    renderObject
      ..isAbove = isAbove
      ..overflowOpen = overflowOpen;
  }

  @override
  _TextSelectionToolbarItemsElement createElement() => _TextSelectionToolbarItemsElement(this);
}

class _TextSelectionToolbarItemsElement extends MultiChildRenderObjectElement {
  _TextSelectionToolbarItemsElement(
    MultiChildRenderObjectWidget widget,
  ) : super(widget);
403

404
  static bool _shouldPaint(Element child) {
405
    return (child.renderObject!.parentData! as ToolbarItemsParentData).shouldPaint;
406 407 408 409 410 411 412 413
  }

  @override
  void debugVisitOnstageChildren(ElementVisitor visitor) {
    children.where(_shouldPaint).forEach(visitor);
  }
}

414
class _TextSelectionToolbarItemsRenderBox extends RenderBox with ContainerRenderObjectMixin<RenderBox, ToolbarItemsParentData> {
415
  _TextSelectionToolbarItemsRenderBox({
416 417
    required bool isAbove,
    required bool overflowOpen,
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
  }) : assert(overflowOpen != null),
       assert(isAbove != null),
       _isAbove = isAbove,
       _overflowOpen = overflowOpen,
       super();

  // The index of the last item that doesn't overflow.
  int _lastIndexThatFits = -1;

  bool _isAbove;
  bool get isAbove => _isAbove;
  set isAbove(bool value) {
    if (value == isAbove) {
      return;
    }
    _isAbove = value;
    markNeedsLayout();
  }

  bool _overflowOpen;
  bool get overflowOpen => _overflowOpen;
  set overflowOpen(bool value) {
    if (value == overflowOpen) {
      return;
    }
    _overflowOpen = value;
    markNeedsLayout();
  }

  // Layout the necessary children, and figure out where the children first
  // overflow, if at all.
  void _layoutChildren() {
    // When overflow is not open, the toolbar is always a specific height.
    final BoxConstraints sizedConstraints = _overflowOpen
      ? constraints
      : BoxConstraints.loose(Size(
          constraints.maxWidth,
          _kToolbarHeight,
        ));

    int i = -1;
    double width = 0.0;
    visitChildren((RenderObject renderObjectChild) {
      i++;

      // No need to layout children inside the overflow menu when it's closed.
      // The opposite is not true. It is necessary to layout the children that
      // don't overflow when the overflow menu is open in order to calculate
      // _lastIndexThatFits.
      if (_lastIndexThatFits != -1 && !overflowOpen) {
        return;
      }

      final RenderBox child = renderObjectChild as RenderBox;
      child.layout(sizedConstraints.loosen(), parentUsesSize: true);
      width += child.size.width;

      if (width > sizedConstraints.maxWidth && _lastIndexThatFits == -1) {
        _lastIndexThatFits = i - 1;
      }
    });

    // If the last child overflows, but only because of the width of the
    // overflow button, then just show it and hide the overflow button.
482
    final RenderBox navButton = firstChild!;
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514
    if (_lastIndexThatFits != -1
        && _lastIndexThatFits == childCount - 2
        && width - navButton.size.width <= sizedConstraints.maxWidth) {
      _lastIndexThatFits = -1;
    }
  }

  // Returns true when the child should be painted, false otherwise.
  bool _shouldPaintChild(RenderObject renderObjectChild, int index) {
    // Paint the navButton when there is overflow.
    if (renderObjectChild == firstChild) {
      return _lastIndexThatFits != -1;
    }

    // If there is no overflow, all children besides the navButton are painted.
    if (_lastIndexThatFits == -1) {
      return true;
    }

    // When there is overflow, paint if the child is in the part of the menu
    // that is currently open. Overflowing children are painted when the
    // overflow menu is open, and the children that fit are painted when the
    // overflow menu is closed.
    return (index > _lastIndexThatFits) == overflowOpen;
  }

  // Decide which children will be pained and set their shouldPaint, and set the
  // offset that painted children will be placed at.
  void _placeChildren() {
    int i = -1;
    Size nextSize = const Size(0.0, 0.0);
    double fitWidth = 0.0;
515
    final RenderBox navButton = firstChild!;
516 517 518 519 520
    double overflowHeight = overflowOpen && !isAbove ? navButton.size.height : 0.0;
    visitChildren((RenderObject renderObjectChild) {
      i++;

      final RenderBox child = renderObjectChild as RenderBox;
521
      final ToolbarItemsParentData childParentData = child.parentData! as ToolbarItemsParentData;
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552

      // Handle placing the navigation button after iterating all children.
      if (renderObjectChild == navButton) {
        return;
      }

      // There is no need to place children that won't be painted.
      if (!_shouldPaintChild(renderObjectChild, i)) {
        childParentData.shouldPaint = false;
        return;
      }
      childParentData.shouldPaint = true;

      if (!overflowOpen) {
        childParentData.offset = Offset(fitWidth, 0.0);
        fitWidth += child.size.width;
        nextSize = Size(
          fitWidth,
          math.max(child.size.height, nextSize.height),
        );
      } else {
        childParentData.offset = Offset(0.0, overflowHeight);
        overflowHeight += child.size.height;
        nextSize = Size(
          math.max(child.size.width, nextSize.width),
          overflowHeight,
        );
      }
    });

    // Place the navigation button if needed.
553
    final ToolbarItemsParentData navButtonParentData = navButton.parentData! as ToolbarItemsParentData;
554
    if (_shouldPaintChild(firstChild!, 0)) {
555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
      navButtonParentData.shouldPaint = true;
      if (overflowOpen) {
        navButtonParentData.offset = isAbove
          ? Offset(0.0, overflowHeight)
          : Offset.zero;
        nextSize = Size(
          nextSize.width,
          isAbove ? nextSize.height + navButton.size.height : nextSize.height,
        );
      } else {
        navButtonParentData.offset = Offset(fitWidth, 0.0);
        nextSize = Size(nextSize.width + navButton.size.width, nextSize.height);
      }
    } else {
      navButtonParentData.shouldPaint = false;
    }

    size = nextSize;
  }

  @override
  void performLayout() {
    _lastIndexThatFits = -1;
    if (firstChild == null) {
      performResize();
      return;
    }

    _layoutChildren();
    _placeChildren();
  }
586

587 588 589 590
  @override
  void paint(PaintingContext context, Offset offset) {
    visitChildren((RenderObject renderObjectChild) {
      final RenderBox child = renderObjectChild as RenderBox;
591
      final ToolbarItemsParentData childParentData = child.parentData! as ToolbarItemsParentData;
592 593 594 595 596 597 598 599 600 601
      if (!childParentData.shouldPaint) {
        return;
      }

      context.paintChild(child, childParentData.offset + offset);
    });
  }

  @override
  void setupParentData(RenderBox child) {
602 603
    if (child.parentData is! ToolbarItemsParentData) {
      child.parentData = ToolbarItemsParentData();
604 605 606 607
    }
  }

  @override
608
  bool hitTestChildren(BoxHitTestResult result, { required Offset position }) {
609
    // The x, y parameters have the top left of the node's box as the origin.
610
    RenderBox? child = lastChild;
611
    while (child != null) {
612
      final ToolbarItemsParentData childParentData = child.parentData! as ToolbarItemsParentData;
613 614 615 616 617 618 619 620 621 622 623 624

      // Don't hit test children aren't shown.
      if (!childParentData.shouldPaint) {
        child = childParentData.previousSibling;
        continue;
      }

      final bool isHit = result.addWithPaintOffset(
        offset: childParentData.offset,
        position: position,
        hitTest: (BoxHitTestResult result, Offset transformed) {
          assert(transformed == position - childParentData.offset);
625
          return child!.hitTest(result, position: transformed);
626 627 628 629 630 631 632 633
        },
      );
      if (isHit)
        return true;
      child = childParentData.previousSibling;
    }
    return false;
  }
634 635 636 637 638 639

  // Visit only the children that should be painted.
  @override
  void visitChildrenForSemantics(RenderObjectVisitor visitor) {
    visitChildren((RenderObject renderObjectChild) {
      final RenderBox child = renderObjectChild as RenderBox;
640
      final ToolbarItemsParentData childParentData = child.parentData! as ToolbarItemsParentData;
641 642 643 644 645
      if (childParentData.shouldPaint) {
        visitor(renderObjectChild);
      }
    });
  }
646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690
}

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

  /// Anchor position of the toolbar in global coordinates.
  final Offset anchor;

  /// The upper-most valid y value for the anchor.
  final double upperBounds;

  /// Whether the closed toolbar fits above the anchor position.
  ///
  /// If the closed toolbar doesn't fit, then the menu is rendered below the
  /// anchor position. It should never happen that the toolbar extends below the
  /// padded bottom of the screen.
  ///
  /// If the closed toolbar does fit but it doesn't fit when the overflow menu
  /// is open, then the toolbar is still rendered above the anchor position. It
  /// then grows downward, overlapping the selection.
  final bool fitsAbove;

  // Return the value that centers width as closely as possible to position
  // while fitting inside of min and max.
  static double _centerOn(double position, double width, double min, double max) {
    // If it overflows on the left, put it as far left as possible.
    if (position - width / 2.0 < min) {
      return min;
    }

    // If it overflows on the right, put it as far right as possible.
    if (position + width / 2.0 > max) {
      return max - width;
    }

    // Otherwise it fits while perfectly centered.
    return position - width / 2.0;
  }

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

692 693 694 695 696 697 698 699 700 701 702 703 704
  @override
  Offset getPositionForChild(Size size, Size childSize) {
    return Offset(
      _centerOn(
        anchor.dx,
        childSize.width,
        _kToolbarScreenPadding,
        size.width - _kToolbarScreenPadding,
      ),
      fitsAbove
        ? math.max(upperBounds, anchor.dy - childSize.height)
        : anchor.dy,
    );
705 706 707 708
  }

  @override
  bool shouldRelayout(_TextSelectionToolbarLayout oldDelegate) {
709
    return anchor != oldDelegate.anchor;
710 711 712
  }
}

713
/// Draws a single text selection handle which points up and to the left.
714
class _TextSelectionHandlePainter extends CustomPainter {
715
  _TextSelectionHandlePainter({ required this.color });
716 717 718 719 720

  final Color color;

  @override
  void paint(Canvas canvas, Size size) {
721
    final Paint paint = Paint()..color = color;
722
    final double radius = size.width/2.0;
723 724 725 726
    final Rect circle = Rect.fromCircle(center: Offset(radius, radius), radius: radius);
    final Rect point = Rect.fromLTWH(0.0, 0.0, radius, radius);
    final Path path = Path()..addOval(circle)..addRect(point);
    canvas.drawPath(path, paint);
727 728 729 730 731 732 733 734
  }

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

735
class _MaterialTextSelectionControls extends TextSelectionControls {
736
  /// Returns the size of the Material handle.
737
  @override
738
  Size getHandleSize(double textLineHeight) => const Size(_kHandleSize, _kHandleSize);
739

740 741
  /// Builder for material-style copy/paste text selection toolbar.
  @override
742 743 744
  Widget buildToolbar(
    BuildContext context,
    Rect globalEditableRegion,
745
    double textLineHeight,
746
    Offset selectionMidpoint,
747 748
    List<TextSelectionPoint> endpoints,
    TextSelectionDelegate delegate,
749
    ClipboardStatusNotifier clipboardStatus,
750
  ) {
751
    assert(debugCheckHasMediaQuery(context));
752
    assert(debugCheckHasMaterialLocalizations(context));
753

754 755
    // The toolbar should appear below the TextField when there is not enough
    // space above the TextField to show it.
756
    final TextSelectionPoint startTextSelectionPoint = endpoints[0];
757 758 759 760
    final TextSelectionPoint endTextSelectionPoint = endpoints.length > 1
      ? endpoints[1]
      : endpoints[0];
    const double closedToolbarHeightNeeded = _kToolbarScreenPadding
761 762
      + _kToolbarHeight
      + _kToolbarContentDistance;
763
    final double paddingTop = MediaQuery.of(context)!.padding.top;
764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784
    final double availableHeight = globalEditableRegion.top
      + startTextSelectionPoint.point.dy
      - textLineHeight
      - paddingTop;
    final bool fitsAbove = closedToolbarHeightNeeded <= availableHeight;
    final Offset anchor = Offset(
      globalEditableRegion.left + selectionMidpoint.dx,
      fitsAbove
        ? globalEditableRegion.top + startTextSelectionPoint.point.dy - textLineHeight - _kToolbarContentDistance
        : globalEditableRegion.top + endTextSelectionPoint.point.dy + _kToolbarContentDistanceBelow,
    );

    return Stack(
      children: <Widget>[
        CustomSingleChildLayout(
          delegate: _TextSelectionToolbarLayout(
            anchor,
            _kToolbarScreenPadding + paddingTop,
            fitsAbove,
          ),
          child: _TextSelectionToolbar(
785
            clipboardStatus: clipboardStatus,
786
            handleCut: canCut(delegate) ? () => handleCut(delegate) : null,
787
            handleCopy: canCopy(delegate) ? () => handleCopy(delegate, clipboardStatus) : null,
788 789 790 791
            handlePaste: canPaste(delegate) ? () => handlePaste(delegate) : null,
            handleSelectAll: canSelectAll(delegate) ? () => handleSelectAll(delegate) : null,
            isAbove: fitsAbove,
          ),
xster's avatar
xster committed
792
        ),
793
      ],
794 795 796 797 798
    );
  }

  /// Builder for material-style text selection handles.
  @override
xster's avatar
xster committed
799
  Widget buildHandle(BuildContext context, TextSelectionHandleType type, double textHeight) {
800
    final ThemeData theme = Theme.of(context)!;
801
    final Color handleColor = TextSelectionTheme.of(context).selectionHandleColor ?? theme.colorScheme.primary;
802 803 804 805 806
    final Widget handle = SizedBox(
      width: _kHandleSize,
      height: _kHandleSize,
      child: CustomPaint(
        painter: _TextSelectionHandlePainter(
807
          color: handleColor,
808 809
        ),
      ),
810 811 812 813 814 815
    );

    // [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) {
816
      case TextSelectionHandleType.left: // points up-right
817 818
        return Transform.rotate(
          angle: math.pi / 2.0,
819
          child: handle,
820
        );
821
      case TextSelectionHandleType.right: // points up-left
822
        return handle;
823
      case TextSelectionHandleType.collapsed: // points up
824 825
        return Transform.rotate(
          angle: math.pi / 4.0,
826
          child: handle,
827 828
        );
    }
829
  }
830

831 832 833 834 835 836 837 838 839 840 841 842 843 844 845
  /// Gets anchor for material-style text selection handles.
  ///
  /// See [TextSelectionControls.getHandleAnchor].
  @override
  Offset getHandleAnchor(TextSelectionHandleType type, double textLineHeight) {
    switch (type) {
      case TextSelectionHandleType.left:
        return const Offset(_kHandleSize, 0);
      case TextSelectionHandleType.right:
        return Offset.zero;
      default:
        return const Offset(_kHandleSize / 2, -4);
    }
  }

846 847 848 849 850
  @override
  bool canSelectAll(TextSelectionDelegate delegate) {
    // Android allows SelectAll when selection is not collapsed, unless
    // everything has already been selected.
    final TextEditingValue value = delegate.textEditingValue;
851 852 853
    return delegate.selectAllEnabled &&
           value.text.isNotEmpty &&
           !(value.selection.start == 0 && value.selection.end == value.text.length);
854
  }
855
}
856

Adam Barth's avatar
Adam Barth committed
857
/// Text selection controls that follow the Material Design specification.
858
final TextSelectionControls materialTextSelectionControls = _MaterialTextSelectionControls();