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 | import 'framework.dart'; |
6 | |
7 | /// The buttons that can appear in a context menu by default. |
8 | /// |
9 | /// See also: |
10 | /// |
11 | /// * [ContextMenuButtonItem], which uses this enum to describe a button in a |
12 | /// context menu. |
13 | enum ContextMenuButtonType { |
14 | /// A button that cuts the current text selection. |
15 | cut, |
16 | |
17 | /// A button that copies the current text selection. |
18 | copy, |
19 | |
20 | /// A button that pastes the clipboard contents into the focused text field. |
21 | paste, |
22 | |
23 | /// A button that selects all the contents of the focused text field. |
24 | selectAll, |
25 | |
26 | /// A button that deletes the current text selection. |
27 | delete, |
28 | |
29 | /// A button that looks up the current text selection. |
30 | lookUp, |
31 | |
32 | /// A button that launches a web search for the current text selection. |
33 | searchWeb, |
34 | |
35 | /// A button that displays the share screen for the current text selection. |
36 | share, |
37 | |
38 | /// A button for starting Live Text input. |
39 | /// |
40 | /// See also: |
41 | /// * [LiveText], where the availability of Live Text input can be obtained. |
42 | /// * [LiveTextInputStatusNotifier], where the status of Live Text can be listened to. |
43 | liveTextInput, |
44 | |
45 | /// Anything other than the default button types. |
46 | custom, |
47 | } |
48 | |
49 | /// The type and callback for a context menu button. |
50 | /// |
51 | /// See also: |
52 | /// |
53 | /// * [AdaptiveTextSelectionToolbar], which can take a list of |
54 | /// ContextMenuButtonItems and create a platform-specific context menu with |
55 | /// the indicated buttons. |
56 | @immutable |
57 | class ContextMenuButtonItem { |
58 | /// Creates a const instance of [ContextMenuButtonItem]. |
59 | const ContextMenuButtonItem({ |
60 | required this.onPressed, |
61 | this.type = ContextMenuButtonType.custom, |
62 | this.label, |
63 | }); |
64 | |
65 | /// The callback to be called when the button is pressed. |
66 | final VoidCallback? onPressed; |
67 | |
68 | /// The type of button this represents. |
69 | final ContextMenuButtonType type; |
70 | |
71 | /// The label to display on the button. |
72 | /// |
73 | /// If a [type] other than [ContextMenuButtonType.custom] is given |
74 | /// and a label is not provided, then the default label for that type for the |
75 | /// platform will be looked up. |
76 | final String? label; |
77 | |
78 | /// Creates a new [ContextMenuButtonItem] with the provided parameters |
79 | /// overridden. |
80 | ContextMenuButtonItem copyWith({ |
81 | VoidCallback? onPressed, |
82 | ContextMenuButtonType? type, |
83 | String? label, |
84 | }) { |
85 | return ContextMenuButtonItem( |
86 | onPressed: onPressed ?? this.onPressed, |
87 | type: type ?? this.type, |
88 | label: label ?? this.label, |
89 | ); |
90 | } |
91 | |
92 | @override |
93 | bool operator ==(Object other) { |
94 | if (other.runtimeType != runtimeType) { |
95 | return false; |
96 | } |
97 | return other is ContextMenuButtonItem |
98 | && other.label == label |
99 | && other.onPressed == onPressed |
100 | && other.type == type; |
101 | } |
102 | |
103 | @override |
104 | int get hashCode => Object.hash(label, onPressed, type); |
105 | |
106 | @override |
107 | String toString() => 'ContextMenuButtonItem $type, $label' ; |
108 | } |
109 | |