mitsuami_widgets/
text_inputs.rs1use mitsuami_core::{
4 Element, ElementBuilder, EventValue, InputPurpose, NodeId, Prop, Tweak, Ui, UiEvent, View, WidgetKind,
5};
6use mitsuami_reactive::{IntoValue, Signal, Value};
7
8pub struct TextInput(Element);
10widget!(TextInput);
11
12impl Default for TextInput {
13 fn default() -> TextInput {
14 TextInput::new()
15 }
16}
17
18impl TextInput {
19 pub fn new() -> TextInput {
20 TextInput(Element::new(WidgetKind::TextInput))
21 }
22
23 pub fn value(mut self, value: impl IntoValue<String>) -> TextInput {
24 self.0.prop(value.into_value(), Prop::Value);
25 self
26 }
27
28 pub fn bind(self, signal: Signal<String>) -> TextInput {
30 self.value(signal).on_input(move |text| signal.set(text))
31 }
32
33 pub fn placeholder(mut self, placeholder: impl IntoValue<String>) -> TextInput {
34 self.0.prop(placeholder.into_value(), Prop::Placeholder);
35 self
36 }
37
38 pub fn enabled(mut self, enabled: impl IntoValue<bool>) -> TextInput {
39 self.0.prop(enabled.into_value(), Prop::Enabled);
40 self
41 }
42
43 pub fn read_only(mut self, read_only: impl IntoValue<bool>) -> TextInput {
46 self.0.prop(read_only.into_value(), Prop::ReadOnly);
47 self
48 }
49
50 pub fn input_purpose(mut self, purpose: impl IntoValue<InputPurpose>) -> TextInput {
54 self.0.prop(purpose.into_value(), Prop::InputPurpose);
55 self
56 }
57
58 pub fn on_input(mut self, handler: impl Fn(String) + 'static) -> TextInput {
60 self.0.on(move |event| {
61 if let UiEvent::Changed(EventValue::Text(text)) = event {
62 handler(text.clone());
63 }
64 });
65 self
66 }
67
68 pub fn native(mut self, tweak: Tweak<TextInput>) -> TextInput {
73 tweak.apply(&mut self.0);
74 self
75 }
76
77 pub fn on_submit(mut self, handler: impl Fn() + 'static) -> TextInput {
79 self.0.on(move |event| {
80 if *event == UiEvent::Submit {
81 handler();
82 }
83 });
84 self
85 }
86}
87
88pub struct PasswordInput(Element);
92widget!(PasswordInput);
93
94impl Default for PasswordInput {
95 fn default() -> PasswordInput {
96 PasswordInput::new()
97 }
98}
99
100impl PasswordInput {
101 pub fn new() -> PasswordInput {
102 PasswordInput(Element::new(WidgetKind::PasswordInput))
103 }
104
105 pub fn value(mut self, value: impl IntoValue<String>) -> PasswordInput {
106 self.0.prop(value.into_value(), Prop::Value);
107 self
108 }
109
110 pub fn bind(self, signal: Signal<String>) -> PasswordInput {
112 self.value(signal).on_input(move |text| signal.set(text))
113 }
114
115 pub fn placeholder(mut self, placeholder: impl IntoValue<String>) -> PasswordInput {
116 self.0.prop(placeholder.into_value(), Prop::Placeholder);
117 self
118 }
119
120 pub fn enabled(mut self, enabled: impl IntoValue<bool>) -> PasswordInput {
121 self.0.prop(enabled.into_value(), Prop::Enabled);
122 self
123 }
124
125 pub fn native(mut self, tweak: Tweak<PasswordInput>) -> PasswordInput {
130 tweak.apply(&mut self.0);
131 self
132 }
133
134 pub fn on_input(mut self, handler: impl Fn(String) + 'static) -> PasswordInput {
136 self.0.on(move |event| {
137 if let UiEvent::Changed(EventValue::Text(text)) = event {
138 handler(text.clone());
139 }
140 });
141 self
142 }
143
144 pub fn on_submit(mut self, handler: impl Fn() + 'static) -> PasswordInput {
146 self.0.on(move |event| {
147 if *event == UiEvent::Submit {
148 handler();
149 }
150 });
151 self
152 }
153}
154
155pub struct SearchInput(Element);
159widget!(SearchInput);
160
161impl Default for SearchInput {
162 fn default() -> SearchInput {
163 SearchInput::new()
164 }
165}
166
167impl SearchInput {
168 pub fn new() -> SearchInput {
169 SearchInput(Element::new(WidgetKind::SearchInput))
170 }
171
172 pub fn value(mut self, value: impl IntoValue<String>) -> SearchInput {
173 self.0.prop(value.into_value(), Prop::Value);
174 self
175 }
176
177 pub fn bind(self, signal: Signal<String>) -> SearchInput {
179 self.value(signal).on_input(move |text| signal.set(text))
180 }
181
182 pub fn placeholder(mut self, placeholder: impl IntoValue<String>) -> SearchInput {
186 self.0.prop(placeholder.into_value(), Prop::Placeholder);
187 self
188 }
189
190 pub fn enabled(mut self, enabled: impl IntoValue<bool>) -> SearchInput {
191 self.0.prop(enabled.into_value(), Prop::Enabled);
192 self
193 }
194
195 pub fn on_input(mut self, handler: impl Fn(String) + 'static) -> SearchInput {
197 self.0.on(move |event| {
198 if let UiEvent::Changed(EventValue::Text(text)) = event {
199 handler(text.clone());
200 }
201 });
202 self
203 }
204
205 pub fn on_search(mut self, handler: impl Fn(String) + 'static) -> SearchInput {
210 self.0.on(move |event| {
211 if let UiEvent::Search(text) = event {
212 handler(text.clone());
213 }
214 });
215 self
216 }
217
218 pub fn native(mut self, tweak: Tweak<SearchInput>) -> SearchInput {
222 tweak.apply(&mut self.0);
223 self
224 }
225}
226
227pub struct TextArea(Element);
231widget!(TextArea);
232
233impl Default for TextArea {
234 fn default() -> TextArea {
235 TextArea::new()
236 }
237}
238
239impl TextArea {
240 pub fn new() -> TextArea {
242 let mut element = Element::new(WidgetKind::TextArea);
243 element.prop(Value::Static(3), Prop::Lines);
244 TextArea(element)
245 }
246
247 pub fn value(mut self, value: impl IntoValue<String>) -> TextArea {
249 self.0.prop(value.into_value(), Prop::Value);
250 self
251 }
252
253 pub fn bind(self, signal: Signal<String>) -> TextArea {
255 self.value(signal).on_input(move |text| signal.set(text))
256 }
257
258 pub fn placeholder(mut self, placeholder: impl IntoValue<String>) -> TextArea {
262 self.0.prop(placeholder.into_value(), Prop::Placeholder);
263 self
264 }
265
266 pub fn enabled(mut self, enabled: impl IntoValue<bool>) -> TextArea {
267 self.0.prop(enabled.into_value(), Prop::Enabled);
268 self
269 }
270
271 pub fn read_only(mut self, read_only: impl IntoValue<bool>) -> TextArea {
274 self.0.prop(read_only.into_value(), Prop::ReadOnly);
275 self
276 }
277
278 pub fn lines(mut self, lines: impl IntoValue<u32>) -> TextArea {
281 self.0.prop(lines.into_value(), |n| Prop::Lines(n.max(1)));
282 self
283 }
284
285 pub fn line_wrap(mut self, wrap: impl IntoValue<bool>) -> TextArea {
289 self.0.prop(wrap.into_value(), Prop::LineWrap);
290 self
291 }
292
293 pub fn on_input(mut self, handler: impl Fn(String) + 'static) -> TextArea {
295 self.0.on(move |event| {
296 if let UiEvent::Changed(EventValue::Text(text)) = event {
297 handler(text.clone());
298 }
299 });
300 self
301 }
302
303 pub fn native(mut self, tweak: Tweak<TextArea>) -> TextArea {
308 tweak.apply(&mut self.0);
309 self
310 }
311}
312
313impl TextInput {
314 #[doc(hidden)]
315 pub fn __tag() -> TextInput {
316 TextInput::new()
317 }
318}
319
320impl PasswordInput {
321 #[doc(hidden)]
322 pub fn __tag() -> PasswordInput {
323 PasswordInput::new()
324 }
325}
326
327impl SearchInput {
328 #[doc(hidden)]
329 pub fn __tag() -> SearchInput {
330 SearchInput::new()
331 }
332}
333
334impl TextArea {
335 #[doc(hidden)]
336 pub fn __tag() -> TextArea {
337 TextArea::new()
338 }
339}