iced_native/widget/operation/
text_input.rs1use crate::widget::operation::Operation;
3use crate::widget::Id;
4
5pub trait TextInput {
7 fn move_cursor_to_front(&mut self);
9 fn move_cursor_to_end(&mut self);
11 fn move_cursor_to(&mut self, position: usize);
13 fn select_all(&mut self);
15}
16
17pub 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
46pub 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
75pub 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
105pub 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}