iced_core/widget/operation/
text_input.rs
1use crate::widget::operation::Operation;
3use crate::widget::Id;
4use crate::Rectangle;
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(&mut self, state: &mut dyn TextInput, id: Option<&Id>) {
27 match id {
28 Some(id) if id == &self.target => {
29 state.move_cursor_to_front();
30 }
31 _ => {}
32 }
33 }
34
35 fn container(
36 &mut self,
37 _id: Option<&Id>,
38 _bounds: Rectangle,
39 operate_on_children: &mut dyn FnMut(&mut dyn Operation<T>),
40 ) {
41 operate_on_children(self);
42 }
43 }
44
45 MoveCursor { target }
46}
47
48pub fn move_cursor_to_end<T>(target: Id) -> impl Operation<T> {
51 struct MoveCursor {
52 target: Id,
53 }
54
55 impl<T> Operation<T> for MoveCursor {
56 fn text_input(&mut self, state: &mut dyn TextInput, id: Option<&Id>) {
57 match id {
58 Some(id) if id == &self.target => {
59 state.move_cursor_to_end();
60 }
61 _ => {}
62 }
63 }
64
65 fn container(
66 &mut self,
67 _id: Option<&Id>,
68 _bounds: Rectangle,
69 operate_on_children: &mut dyn FnMut(&mut dyn Operation<T>),
70 ) {
71 operate_on_children(self);
72 }
73 }
74
75 MoveCursor { target }
76}
77
78pub fn move_cursor_to<T>(target: Id, position: usize) -> impl Operation<T> {
81 struct MoveCursor {
82 target: Id,
83 position: usize,
84 }
85
86 impl<T> Operation<T> for MoveCursor {
87 fn text_input(&mut self, state: &mut dyn TextInput, id: Option<&Id>) {
88 match id {
89 Some(id) if id == &self.target => {
90 state.move_cursor_to(self.position);
91 }
92 _ => {}
93 }
94 }
95
96 fn container(
97 &mut self,
98 _id: Option<&Id>,
99 _bounds: Rectangle,
100 operate_on_children: &mut dyn FnMut(&mut dyn Operation<T>),
101 ) {
102 operate_on_children(self);
103 }
104 }
105
106 MoveCursor { target, position }
107}
108
109pub fn select_all<T>(target: Id) -> impl Operation<T> {
111 struct MoveCursor {
112 target: Id,
113 }
114
115 impl<T> Operation<T> for MoveCursor {
116 fn text_input(&mut self, state: &mut dyn TextInput, id: Option<&Id>) {
117 match id {
118 Some(id) if id == &self.target => {
119 state.select_all();
120 }
121 _ => {}
122 }
123 }
124
125 fn container(
126 &mut self,
127 _id: Option<&Id>,
128 _bounds: Rectangle,
129 operate_on_children: &mut dyn FnMut(&mut dyn Operation<T>),
130 ) {
131 operate_on_children(self);
132 }
133 }
134
135 MoveCursor { target }
136}