1// Copyright 2014 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5import 'package:flutter/rendering.dart';
6
7/// Positions the toolbar at [anchor] if it fits, otherwise moves it so that it
8/// just fits fully on-screen.
9///
10/// See also:
11///
12/// * [desktopTextSelectionControls], which uses this to position
13/// itself.
14/// * [cupertinoDesktopTextSelectionControls], which uses this to position
15/// itself.
16/// * [TextSelectionToolbarLayoutDelegate], which does a similar layout for
17/// the mobile text selection toolbars.
18class DesktopTextSelectionToolbarLayoutDelegate extends SingleChildLayoutDelegate {
19 /// Creates an instance of TextSelectionToolbarLayoutDelegate.
20 DesktopTextSelectionToolbarLayoutDelegate({
21 required this.anchor,
22 });
23
24 /// The point at which to render the menu, if possible.
25 ///
26 /// Should be provided in local coordinates.
27 final Offset anchor;
28
29 @override
30 BoxConstraints getConstraintsForChild(BoxConstraints constraints) {
31 return constraints.loosen();
32 }
33
34 @override
35 Offset getPositionForChild(Size size, Size childSize) {
36 final Offset overhang = Offset(
37 anchor.dx + childSize.width - size.width,
38 anchor.dy + childSize.height - size.height,
39 );
40 return Offset(
41 overhang.dx > 0.0 ? anchor.dx - overhang.dx : anchor.dx,
42 overhang.dy > 0.0 ? anchor.dy - overhang.dy : anchor.dy,
43 );
44 }
45
46 @override
47 bool shouldRelayout(DesktopTextSelectionToolbarLayoutDelegate oldDelegate) {
48 return anchor != oldDelegate.anchor;
49 }
50}
51