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({required this.anchor}); |
27 | |
28 | /// The point at which to render the menu, if possible. |
29 | /// |
30 | /// Should be provided in local coordinates. |
31 | final Offset anchor; |
32 | |
33 | @override |
34 | BoxConstraints getConstraintsForChild(BoxConstraints constraints) { |
35 | return constraints.loosen(); |
36 | } |
37 | |
38 | @override |
39 | Offset getPositionForChild(Size size, Size childSize) { |
40 | final Offset overhang = Offset( |
41 | anchor.dx + childSize.width - size.width, |
42 | anchor.dy + childSize.height - size.height, |
43 | ); |
44 | return Offset( |
45 | overhang.dx > 0.0 ? anchor.dx - overhang.dx : anchor.dx, |
46 | overhang.dy > 0.0 ? anchor.dy - overhang.dy : anchor.dy, |
47 | ); |
48 | } |
49 | |
50 | @override |
51 | bool shouldRelayout(DesktopTextSelectionToolbarLayoutDelegate oldDelegate) { |
52 | return anchor != oldDelegate.anchor; |
53 | } |
54 | } |
55 |