waft_widgets/widgets/context_menu_button_item.rs
1//! Flutter counterpart: `widgets/context_menu_button_item.dart`.
2
3use std::fmt::{self, Debug};
4
5use waft_foundation::Listener;
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#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
14pub enum ContextMenuButtonType {
15 /// A button that cuts the current text selection.
16 Cut,
17 /// A button that copies the current text selection.
18 Copy,
19 /// A button that pastes the clipboard contents into the focused text field.
20 Paste,
21 /// A button that selects all the contents of the focused text field.
22 SelectAll,
23 /// A button that deletes the current text selection.
24 Delete,
25 /// A button that looks up the current text selection.
26 LookUp,
27 /// A button that launches a web search for the current text selection.
28 SearchWeb,
29 /// A button that displays the share screen for the current text selection.
30 Share,
31 /// A button for starting Live Text input.
32 LiveTextInput,
33 /// Anything other than the default button types.
34 Custom,
35}
36
37/// The type and callback for a context menu button.
38///
39/// See also:
40///
41/// * [`crate::SystemContextMenu`], which can take system items instead.
42#[derive(Clone)]
43pub struct ContextMenuButtonItem {
44 /// The callback to be called when the button is pressed.
45 pub on_pressed: Option<Listener>,
46 /// The type of button this represents.
47 pub r#type: ContextMenuButtonType,
48 /// The label to display on the button.
49 ///
50 /// If a [`r#type`](Self::r#type) other than [`ContextMenuButtonType::Custom`] is given
51 /// and a label is not provided, then the default label for that type for the
52 /// platform will be looked up.
53 pub label: Option<String>,
54}
55
56impl ContextMenuButtonItem {
57 /// Creates a [`ContextMenuButtonItem`].
58 pub fn new(on_pressed: Option<Listener>) -> ContextMenuButtonItem {
59 ContextMenuButtonItem {
60 on_pressed,
61 r#type: ContextMenuButtonType::Custom,
62 label: None,
63 }
64 }
65
66 /// Dart `ContextMenuButtonItem(type:)`.
67 pub fn r#type(mut self, r#type: ContextMenuButtonType) -> ContextMenuButtonItem {
68 self.r#type = r#type;
69 self
70 }
71
72 /// Dart `ContextMenuButtonItem(label:)`.
73 pub fn label(mut self, label: impl Into<String>) -> ContextMenuButtonItem {
74 self.label = Some(label.into());
75 self
76 }
77
78 /// Creates a new [`ContextMenuButtonItem`] with the provided parameters
79 /// overridden.
80 pub fn copy_with(&self) -> ContextMenuButtonItem {
81 self.clone()
82 }
83}
84
85impl PartialEq for ContextMenuButtonItem {
86 fn eq(&self, other: &ContextMenuButtonItem) -> bool {
87 self.label == other.label
88 && self.on_pressed == other.on_pressed
89 && self.r#type == other.r#type
90 }
91}
92
93impl Debug for ContextMenuButtonItem {
94 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
95 write!(
96 f,
97 "ContextMenuButtonItem {:?}, {:?}",
98 self.r#type, self.label
99 )
100 }
101}