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 fn select_range(&mut self, start: usize, end: usize);
26}
27
28pub fn move_cursor_to_front<T>(target: Id) -> impl Operation<T> {
31 struct MoveCursor {
32 target: Id,
33 }
34
35 impl<T> Operation<T> for MoveCursor {
36 fn text_input(&mut self, id: Option<&Id>, _bounds: Rectangle, state: &mut dyn TextInput) {
37 match id {
38 Some(id) if id == &self.target => {
39 state.move_cursor_to_front();
40 }
41 _ => {}
42 }
43 }
44
45 fn traverse(&mut self, operate: &mut dyn FnMut(&mut dyn Operation<T>)) {
46 operate(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(&mut self, id: Option<&Id>, _bounds: Rectangle, state: &mut dyn TextInput) {
62 match id {
63 Some(id) if id == &self.target => {
64 state.move_cursor_to_end();
65 }
66 _ => {}
67 }
68 }
69
70 fn traverse(&mut self, operate: &mut dyn FnMut(&mut dyn Operation<T>)) {
71 operate(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, id: Option<&Id>, _bounds: Rectangle, state: &mut dyn TextInput) {
88 match id {
89 Some(id) if id == &self.target => {
90 state.move_cursor_to(self.position);
91 }
92 _ => {}
93 }
94 }
95
96 fn traverse(&mut self, operate: &mut dyn FnMut(&mut dyn Operation<T>)) {
97 operate(self);
98 }
99 }
100
101 MoveCursor { target, position }
102}
103
104pub fn select_all<T>(target: Id) -> impl Operation<T> {
106 struct MoveCursor {
107 target: Id,
108 }
109
110 impl<T> Operation<T> for MoveCursor {
111 fn text_input(&mut self, id: Option<&Id>, _bounds: Rectangle, state: &mut dyn TextInput) {
112 match id {
113 Some(id) if id == &self.target => {
114 state.select_all();
115 }
116 _ => {}
117 }
118 }
119
120 fn traverse(&mut self, operate: &mut dyn FnMut(&mut dyn Operation<T>)) {
121 operate(self);
122 }
123 }
124
125 MoveCursor { target }
126}
127
128pub fn select_range<T>(target: Id, start: usize, end: usize) -> impl Operation<T> {
130 struct SelectRange {
131 target: Id,
132 start: usize,
133 end: usize,
134 }
135
136 impl<T> Operation<T> for SelectRange {
137 fn text_input(&mut self, id: Option<&Id>, _bounds: Rectangle, state: &mut dyn TextInput) {
138 match id {
139 Some(id) if id == &self.target => {
140 state.select_range(self.start, self.end);
141 }
142 _ => {}
143 }
144 }
145
146 fn traverse(&mut self, operate: &mut dyn FnMut(&mut dyn Operation<T>)) {
147 operate(self);
148 }
149 }
150
151 SelectRange { target, start, end }
152}