iced_core/widget/operation/
text_input.rs

1//! Operate on widgets that have text input.
2use crate::Rectangle;
3use crate::widget::Id;
4use crate::widget::operation::Operation;
5
6/// The internal state of a widget that has text input.
7pub trait TextInput {
8    /// Returns the current _visible_ text of the text input
9    ///
10    /// Normally, this is either its value or its placeholder.
11    fn text(&self) -> &str;
12
13    /// Moves the cursor of the text input to the front of the input text.
14    fn move_cursor_to_front(&mut self);
15
16    /// Moves the cursor of the text input to the end of the input text.
17    fn move_cursor_to_end(&mut self);
18
19    /// Moves the cursor of the text input to an arbitrary location.
20    fn move_cursor_to(&mut self, position: usize);
21
22    /// Selects all the content of the text input.
23    fn select_all(&mut self);
24}
25
26/// Produces an [`Operation`] that moves the cursor of the widget with the given [`Id`] to the
27/// front.
28pub fn move_cursor_to_front<T>(target: Id) -> impl Operation<T> {
29    struct MoveCursor {
30        target: Id,
31    }
32
33    impl<T> Operation<T> for MoveCursor {
34        fn text_input(
35            &mut self,
36            id: Option<&Id>,
37            _bounds: Rectangle,
38            state: &mut dyn TextInput,
39        ) {
40            match id {
41                Some(id) if id == &self.target => {
42                    state.move_cursor_to_front();
43                }
44                _ => {}
45            }
46        }
47
48        fn traverse(&mut self, operate: &mut dyn FnMut(&mut dyn Operation<T>)) {
49            operate(self);
50        }
51    }
52
53    MoveCursor { target }
54}
55
56/// Produces an [`Operation`] that moves the cursor of the widget with the given [`Id`] to the
57/// end.
58pub fn move_cursor_to_end<T>(target: Id) -> impl Operation<T> {
59    struct MoveCursor {
60        target: Id,
61    }
62
63    impl<T> Operation<T> for MoveCursor {
64        fn text_input(
65            &mut self,
66            id: Option<&Id>,
67            _bounds: Rectangle,
68            state: &mut dyn TextInput,
69        ) {
70            match id {
71                Some(id) if id == &self.target => {
72                    state.move_cursor_to_end();
73                }
74                _ => {}
75            }
76        }
77
78        fn traverse(&mut self, operate: &mut dyn FnMut(&mut dyn Operation<T>)) {
79            operate(self);
80        }
81    }
82
83    MoveCursor { target }
84}
85
86/// Produces an [`Operation`] that moves the cursor of the widget with the given [`Id`] to the
87/// provided position.
88pub fn move_cursor_to<T>(target: Id, position: usize) -> impl Operation<T> {
89    struct MoveCursor {
90        target: Id,
91        position: usize,
92    }
93
94    impl<T> Operation<T> for MoveCursor {
95        fn text_input(
96            &mut self,
97            id: Option<&Id>,
98            _bounds: Rectangle,
99            state: &mut dyn TextInput,
100        ) {
101            match id {
102                Some(id) if id == &self.target => {
103                    state.move_cursor_to(self.position);
104                }
105                _ => {}
106            }
107        }
108
109        fn traverse(&mut self, operate: &mut dyn FnMut(&mut dyn Operation<T>)) {
110            operate(self);
111        }
112    }
113
114    MoveCursor { target, position }
115}
116
117/// Produces an [`Operation`] that selects all the content of the widget with the given [`Id`].
118pub fn select_all<T>(target: Id) -> impl Operation<T> {
119    struct MoveCursor {
120        target: Id,
121    }
122
123    impl<T> Operation<T> for MoveCursor {
124        fn text_input(
125            &mut self,
126            id: Option<&Id>,
127            _bounds: Rectangle,
128            state: &mut dyn TextInput,
129        ) {
130            match id {
131                Some(id) if id == &self.target => {
132                    state.select_all();
133                }
134                _ => {}
135            }
136        }
137
138        fn traverse(&mut self, operate: &mut dyn FnMut(&mut dyn Operation<T>)) {
139            operate(self);
140        }
141    }
142
143    MoveCursor { target }
144}