iced_core/widget/operation/
text_input.rs1use crate::Rectangle;
3use crate::widget::Id;
4use crate::widget::operation::Operation;
5
6pub trait TextInput {
8 fn text(&self) -> &str;
12
13 fn move_cursor_to_front(&mut self);
15
16 fn move_cursor_to_end(&mut self);
18
19 fn move_cursor_to(&mut self, position: usize);
21
22 fn select_all(&mut self);
24}
25
26pub 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
56pub 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
86pub 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
117pub 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}