1use mitsuami_core::{
4 Element, ElementBuilder, EventValue, InputPurpose, NodeId, Prop, Suggestion, TextStyle, Tweak, Ui, UiEvent, View,
5 WidgetKind,
6};
7use mitsuami_reactive::{IntoValue, Signal, Value};
8
9pub struct TextInput(Element);
11widget!(TextInput);
12
13impl Default for TextInput {
14 fn default() -> TextInput {
15 TextInput::new()
16 }
17}
18
19impl TextInput {
20 pub fn new() -> TextInput {
21 TextInput(Element::new(WidgetKind::TextInput))
22 }
23
24 pub fn value(mut self, value: impl IntoValue<String>) -> TextInput {
25 self.0.prop(value.into_value(), Prop::Value);
26 self
27 }
28
29 pub fn bind(self, signal: Signal<String>) -> TextInput {
31 self.value(signal).on_input(move |text| signal.set(text))
32 }
33
34 pub fn placeholder(mut self, placeholder: impl IntoValue<String>) -> TextInput {
35 self.0.prop(placeholder.into_value(), Prop::Placeholder);
36 self
37 }
38
39 pub fn error(mut self, error: impl IntoValue<Option<String>>) -> TextInput {
42 self.0.prop(error.into_value(), Prop::Error);
43 self
44 }
45
46 pub fn enabled(mut self, enabled: impl IntoValue<bool>) -> TextInput {
47 self.0.prop(enabled.into_value(), Prop::Enabled);
48 self
49 }
50
51 pub fn read_only(mut self, read_only: impl IntoValue<bool>) -> TextInput {
54 self.0.prop(read_only.into_value(), Prop::ReadOnly);
55 self
56 }
57
58 pub fn input_purpose(mut self, purpose: impl IntoValue<InputPurpose>) -> TextInput {
62 self.0.prop(purpose.into_value(), Prop::InputPurpose);
63 self
64 }
65
66 pub fn on_input(mut self, handler: impl Fn(String) + 'static) -> TextInput {
68 self.0.on(move |event| {
69 if let UiEvent::Changed(EventValue::Text(text)) = event {
70 handler(text.clone());
71 }
72 });
73 self
74 }
75
76 pub fn native(mut self, tweak: Tweak<TextInput>) -> TextInput {
81 tweak.apply(&mut self.0);
82 self
83 }
84
85 pub fn on_key(
89 mut self,
90 key: impl Into<mitsuami_core::services::Shortcut>,
91 handler: impl Fn() + 'static,
92 ) -> TextInput {
93 self.0.on_key(key.into(), handler);
94 self
95 }
96
97 pub fn on_submit(mut self, handler: impl Fn() + 'static) -> TextInput {
99 self.0.on(move |event| {
100 if *event == UiEvent::Submit {
101 handler();
102 }
103 });
104 self
105 }
106}
107
108pub struct PasswordInput(Element);
112widget!(PasswordInput);
113
114impl Default for PasswordInput {
115 fn default() -> PasswordInput {
116 PasswordInput::new()
117 }
118}
119
120impl PasswordInput {
121 pub fn new() -> PasswordInput {
122 PasswordInput(Element::new(WidgetKind::PasswordInput))
123 }
124
125 pub fn value(mut self, value: impl IntoValue<String>) -> PasswordInput {
126 self.0.prop(value.into_value(), Prop::Value);
127 self
128 }
129
130 pub fn bind(self, signal: Signal<String>) -> PasswordInput {
132 self.value(signal).on_input(move |text| signal.set(text))
133 }
134
135 pub fn placeholder(mut self, placeholder: impl IntoValue<String>) -> PasswordInput {
136 self.0.prop(placeholder.into_value(), Prop::Placeholder);
137 self
138 }
139
140 pub fn error(mut self, error: impl IntoValue<Option<String>>) -> PasswordInput {
143 self.0.prop(error.into_value(), Prop::Error);
144 self
145 }
146
147 pub fn enabled(mut self, enabled: impl IntoValue<bool>) -> PasswordInput {
148 self.0.prop(enabled.into_value(), Prop::Enabled);
149 self
150 }
151
152 pub fn native(mut self, tweak: Tweak<PasswordInput>) -> PasswordInput {
157 tweak.apply(&mut self.0);
158 self
159 }
160
161 pub fn on_input(mut self, handler: impl Fn(String) + 'static) -> PasswordInput {
163 self.0.on(move |event| {
164 if let UiEvent::Changed(EventValue::Text(text)) = event {
165 handler(text.clone());
166 }
167 });
168 self
169 }
170
171 pub fn on_submit(mut self, handler: impl Fn() + 'static) -> PasswordInput {
173 self.0.on(move |event| {
174 if *event == UiEvent::Submit {
175 handler();
176 }
177 });
178 self
179 }
180}
181
182pub struct SearchInput(Element);
186widget!(SearchInput);
187
188impl Default for SearchInput {
189 fn default() -> SearchInput {
190 SearchInput::new()
191 }
192}
193
194impl SearchInput {
195 pub fn new() -> SearchInput {
196 SearchInput(Element::new(WidgetKind::SearchInput))
197 }
198
199 pub fn value(mut self, value: impl IntoValue<String>) -> SearchInput {
200 self.0.prop(value.into_value(), Prop::Value);
201 self
202 }
203
204 pub fn bind(self, signal: Signal<String>) -> SearchInput {
206 self.value(signal).on_input(move |text| signal.set(text))
207 }
208
209 pub fn placeholder(mut self, placeholder: impl IntoValue<String>) -> SearchInput {
213 self.0.prop(placeholder.into_value(), Prop::Placeholder);
214 self
215 }
216
217 pub fn enabled(mut self, enabled: impl IntoValue<bool>) -> SearchInput {
218 self.0.prop(enabled.into_value(), Prop::Enabled);
219 self
220 }
221
222 pub fn on_input(mut self, handler: impl Fn(String) + 'static) -> SearchInput {
224 self.0.on(move |event| {
225 if let UiEvent::Changed(EventValue::Text(text)) = event {
226 handler(text.clone());
227 }
228 });
229 self
230 }
231
232 pub fn on_key(
240 mut self,
241 key: impl Into<mitsuami_core::services::Shortcut>,
242 handler: impl Fn() + 'static,
243 ) -> SearchInput {
244 self.0.on_key(key.into(), handler);
245 self
246 }
247
248 pub fn on_search(mut self, handler: impl Fn(String) + 'static) -> SearchInput {
249 self.0.on(move |event| {
250 if let UiEvent::Search(text) = event {
251 handler(text.clone());
252 }
253 });
254 self
255 }
256
257 pub fn search_on_return(mut self, on: impl IntoValue<bool>) -> SearchInput {
260 self.0.prop(on.into_value(), Prop::SearchOnReturn);
261 self
262 }
263
264 pub fn suggestions(mut self, suggestions: impl IntoValue<Vec<Suggestion>>) -> SearchInput {
269 self.0.prop(suggestions.into_value(), Prop::Suggestions);
270 self
271 }
272
273 pub fn on_suggestion(mut self, handler: impl Fn(usize) + 'static) -> SearchInput {
276 self.0.on(move |event| {
277 if let UiEvent::SuggestionChosen(index) = event {
278 handler(*index);
279 }
280 });
281 self
282 }
283
284 pub fn native(mut self, tweak: Tweak<SearchInput>) -> SearchInput {
288 tweak.apply(&mut self.0);
289 self
290 }
291}
292
293pub struct TextArea(Element);
297widget!(TextArea);
298
299impl Default for TextArea {
300 fn default() -> TextArea {
301 TextArea::new()
302 }
303}
304
305impl TextArea {
306 pub fn new() -> TextArea {
308 let mut element = Element::new(WidgetKind::TextArea);
309 element.prop(Value::Static(3), Prop::Lines);
310 TextArea(element)
311 }
312
313 pub fn value(mut self, value: impl IntoValue<String>) -> TextArea {
315 self.0.prop(value.into_value(), Prop::Value);
316 self
317 }
318
319 pub fn bind(self, signal: Signal<String>) -> TextArea {
321 self.value(signal).on_input(move |text| signal.set(text))
322 }
323
324 pub fn placeholder(mut self, placeholder: impl IntoValue<String>) -> TextArea {
328 self.0.prop(placeholder.into_value(), Prop::Placeholder);
329 self
330 }
331
332 pub fn error(mut self, error: impl IntoValue<Option<String>>) -> TextArea {
335 self.0.prop(error.into_value(), Prop::Error);
336 self
337 }
338
339 pub fn enabled(mut self, enabled: impl IntoValue<bool>) -> TextArea {
340 self.0.prop(enabled.into_value(), Prop::Enabled);
341 self
342 }
343
344 pub fn read_only(mut self, read_only: impl IntoValue<bool>) -> TextArea {
347 self.0.prop(read_only.into_value(), Prop::ReadOnly);
348 self
349 }
350
351 pub fn lines(mut self, lines: impl IntoValue<u32>) -> TextArea {
354 self.0.prop(lines.into_value(), |n| Prop::Lines(n.max(1)));
355 self
356 }
357
358 pub fn line_wrap(mut self, wrap: impl IntoValue<bool>) -> TextArea {
362 self.0.prop(wrap.into_value(), Prop::LineWrap);
363 self
364 }
365
366 pub fn text_style(mut self, style: impl IntoValue<TextStyle>) -> TextArea {
369 self.0.prop(style.into_value(), Prop::TextStyle);
370 self
371 }
372
373 pub fn follows_end(mut self, follows: impl IntoValue<bool>) -> TextArea {
376 self.0.prop(follows.into_value(), Prop::FollowsEnd);
377 self
378 }
379
380 pub fn on_input(mut self, handler: impl Fn(String) + 'static) -> TextArea {
382 self.0.on(move |event| {
383 if let UiEvent::Changed(EventValue::Text(text)) = event {
384 handler(text.clone());
385 }
386 });
387 self
388 }
389
390 pub fn native(mut self, tweak: Tweak<TextArea>) -> TextArea {
395 tweak.apply(&mut self.0);
396 self
397 }
398}
399
400impl TextInput {
401 #[doc(hidden)]
402 pub fn __tag() -> TextInput {
403 TextInput::new()
404 }
405}
406
407impl PasswordInput {
408 #[doc(hidden)]
409 pub fn __tag() -> PasswordInput {
410 PasswordInput::new()
411 }
412}
413
414impl SearchInput {
415 #[doc(hidden)]
416 pub fn __tag() -> SearchInput {
417 SearchInput::new()
418 }
419}
420
421impl TextArea {
422 #[doc(hidden)]
423 pub fn __tag() -> TextArea {
424 TextArea::new()
425 }
426}