iced_core/widget/operation/
text_input.rs
1use crate::Rectangle;
3use crate::widget::Id;
4use crate::widget::operation::Operation;
5
6pub trait TextInput {
8 fn move_cursor_to_front(&mut self);
10 fn move_cursor_to_end(&mut self);
12 fn move_cursor_to(&mut self, position: usize);
14 fn select_all(&mut self);
16}
17
18pub fn move_cursor_to_front<T>(target: Id) -> impl Operation<T> {
21 struct MoveCursor {
22 target: Id,
23 }
24
25 impl<T> Operation<T> for MoveCursor {
26 fn text_input(
27 &mut self,
28 id: Option<&Id>,
29 _bounds: Rectangle,
30 state: &mut dyn TextInput,
31 ) {
32 match id {
33 Some(id) if id == &self.target => {
34 state.move_cursor_to_front();
35 }
36 _ => {}
37 }
38 }
39
40 fn container(
41 &mut self,
42 _id: Option<&Id>,
43 _bounds: Rectangle,
44 operate_on_children: &mut dyn FnMut(&mut dyn Operation<T>),
45 ) {
46 operate_on_children(self);
47 }
48 }
49
50 MoveCursor { target }
51}
52
53pub fn move_cursor_to_end<T>(target: Id) -> impl Operation<T> {
56 struct MoveCursor {
57 target: Id,
58 }
59
60 impl<T> Operation<T> for MoveCursor {
61 fn text_input(
62 &mut self,
63 id: Option<&Id>,
64 _bounds: Rectangle,
65 state: &mut dyn TextInput,
66 ) {
67 match id {
68 Some(id) if id == &self.target => {
69 state.move_cursor_to_end();
70 }
71 _ => {}
72 }
73 }
74
75 fn container(
76 &mut self,
77 _id: Option<&Id>,
78 _bounds: Rectangle,
79 operate_on_children: &mut dyn FnMut(&mut dyn Operation<T>),
80 ) {
81 operate_on_children(self);
82 }
83 }
84
85 MoveCursor { target }
86}
87
88pub fn move_cursor_to<T>(target: Id, position: usize) -> impl Operation<T> {
91 struct MoveCursor {
92 target: Id,
93 position: usize,
94 }
95
96 impl<T> Operation<T> for MoveCursor {
97 fn text_input(
98 &mut self,
99 id: Option<&Id>,
100 _bounds: Rectangle,
101 state: &mut dyn TextInput,
102 ) {
103 match id {
104 Some(id) if id == &self.target => {
105 state.move_cursor_to(self.position);
106 }
107 _ => {}
108 }
109 }
110
111 fn container(
112 &mut self,
113 _id: Option<&Id>,
114 _bounds: Rectangle,
115 operate_on_children: &mut dyn FnMut(&mut dyn Operation<T>),
116 ) {
117 operate_on_children(self);
118 }
119 }
120
121 MoveCursor { target, position }
122}
123
124pub fn select_all<T>(target: Id) -> impl Operation<T> {
126 struct MoveCursor {
127 target: Id,
128 }
129
130 impl<T> Operation<T> for MoveCursor {
131 fn text_input(
132 &mut self,
133 id: Option<&Id>,
134 _bounds: Rectangle,
135 state: &mut dyn TextInput,
136 ) {
137 match id {
138 Some(id) if id == &self.target => {
139 state.select_all();
140 }
141 _ => {}
142 }
143 }
144
145 fn container(
146 &mut self,
147 _id: Option<&Id>,
148 _bounds: Rectangle,
149 operate_on_children: &mut dyn FnMut(&mut dyn Operation<T>),
150 ) {
151 operate_on_children(self);
152 }
153 }
154
155 MoveCursor { target }
156}