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