iced_native/widget/operation/
text_input.rs

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