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 | |
5 | /// @docImport 'package:flutter/cupertino.dart'; |
6 | /// @docImport 'package:flutter/material.dart'; |
7 | /// |
8 | /// @docImport 'text_selection_toolbar_layout_delegate.dart'; |
9 | library; |
10 | |
11 | import 'package:flutter/rendering.dart'; |
12 | |
13 | /// Positions the toolbar at [anchor] if it fits, otherwise moves it so that it |
14 | /// just fits fully on-screen. |
15 | /// |
16 | /// See also: |
17 | /// |
18 | /// * [desktopTextSelectionControls], which uses this to position |
19 | /// itself. |
20 | /// * [cupertinoDesktopTextSelectionControls], which uses this to position |
21 | /// itself. |
22 | /// * [TextSelectionToolbarLayoutDelegate], which does a similar layout for |
23 | /// the mobile text selection toolbars. |
24 | class DesktopTextSelectionToolbarLayoutDelegate extends SingleChildLayoutDelegate { |
25 | /// Creates an instance of TextSelectionToolbarLayoutDelegate. |
26 | DesktopTextSelectionToolbarLayoutDelegate({ |
27 | required this.anchor, |
28 | }); |
29 | |
30 | /// The point at which to render the menu, if possible. |
31 | /// |
32 | /// Should be provided in local coordinates. |
33 | final Offset anchor; |
34 | |
35 | @override |
36 | BoxConstraints getConstraintsForChild(BoxConstraints constraints) { |
37 | return constraints.loosen(); |
38 | } |
39 | |
40 | @override |
41 | Offset getPositionForChild(Size size, Size childSize) { |
42 | final Offset overhang = Offset( |
43 | anchor.dx + childSize.width - size.width, |
44 | anchor.dy + childSize.height - size.height, |
45 | ); |
46 | return Offset( |
47 | overhang.dx > 0.0 ? anchor.dx - overhang.dx : anchor.dx, |
48 | overhang.dy > 0.0 ? anchor.dy - overhang.dy : anchor.dy, |
49 | ); |
50 | } |
51 | |
52 | @override |
53 | bool shouldRelayout(DesktopTextSelectionToolbarLayoutDelegate oldDelegate) { |
54 | return anchor != oldDelegate.anchor; |
55 | } |
56 | } |
57 | |