Skip to main content

mitsuami_widgets/
text_inputs.rs

1//! Text fields: single-line, password, search and multi-line.
2
3use 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
9/// Single-line text entry.
10pub 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    /// Two-way binding, Vue's `v-model`.
30    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    /// What's wrong with its text, or `None`: see [`Prop::Error`]. Show
40    /// the message by it too, as the platform's apps do.
41    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    /// Shows the text, which can still be selected, copied and focused, but
52    /// not edited. Unlike a disabled field, it looks and reads as usual.
53    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    /// What it's for (an email address, a URL, a phone number), which the
59    /// platform uses as it uses one: to pick an on-screen keyboard, to
60    /// offer autofill. It doesn't check what's typed.
61    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    /// Called on every edit with the new text.
67    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    /// Raw platform settings, past the semantic ones: see [`Tweak`]. What
77    /// the platforms offer (a borderless field on AppKit, icons on GTK, a
78    /// length limit on GTK, Qt and WinUI, a header on WinUI) is each one's
79    /// own.
80    pub fn native(mut self, tweak: Tweak<TextInput>) -> TextInput {
81        tweak.apply(&mut self.0);
82        self
83    }
84
85    /// Takes `key` before the field's own use of it (the arrows, Return,
86    /// Escape), and runs `handler` when it's pressed in the field: a field
87    /// that moves through a list beside it.
88    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    /// Called when the user confirms (Return / Enter).
98    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
108/// Single-line entry of a password: the platform's password field, which
109/// hides the text as the platform does (bullets, and a button to show it
110/// where the platform has one) and never reads it out.
111pub 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    /// Two-way binding, Vue's `v-model`.
131    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    /// What's wrong with its text, or `None`: see [`Prop::Error`]. Show
141    /// the message by it too, as the platform's apps do.
142    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    /// Raw platform settings: see [`Tweak`]. Password fields have no
153    /// semantic options past a text field's: what the platforms offer
154    /// (GTK's peek icon, WinUI's reveal mode, the bullet on Qt and WinUI)
155    /// is each one's own.
156    pub fn native(mut self, tweak: Tweak<PasswordInput>) -> PasswordInput {
157        tweak.apply(&mut self.0);
158        self
159    }
160
161    /// Called on every edit with the new text.
162    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    /// Called when the user confirms (Return / Enter).
172    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
182/// Single-line entry of search text: the platform's search field, with its
183/// search icon and clear button. [`on_search`](Self::on_search) is called
184/// when the platform asks for a search, as its own apps search.
185pub 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    /// Two-way binding, Vue's `v-model`.
205    pub fn bind(self, signal: Signal<String>) -> SearchInput {
206        self.value(signal).on_input(move |text| signal.set(text))
207    }
208
209    /// Shown while it's empty, in place of the platform's own where it has
210    /// one (AppKit, Qt). It names the field to assistive technology
211    /// without a label.
212    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    /// Called on every edit with the new text, the clear button's too.
223    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    /// Called with the text when the platform asks for a search: as the
233    /// user types, after a pause where the platform waits for one (AppKit,
234    /// GTK, Qt) and at once where it doesn't (WinUI); on Return; and when
235    /// the field is cleared. Not for text the app set.
236    /// Takes `key` before the field's own use of it (the arrows, Return,
237    /// Escape; the suggestions' still come first), and runs `handler` when
238    /// it's pressed in the field.
239    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    /// Searches on Return (and when cleared), not as the user types: for
258    /// searches that take a while, with each platform's own setting for it.
259    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    /// What it suggests for its text, in a list under it as the platform
265    /// shows a field's suggestions; empty: none. The field keeps the
266    /// keyboard: the arrows move through the list, Return picks one, and
267    /// Escape hides it. The app sets it as the text changes.
268    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    /// Called with a suggestion's index when the user picks it. The text
274    /// is the app's to set.
275    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    /// Raw platform settings, past the semantic ones: see [`Tweak`]. What
285    /// the platforms offer (recent searches on AppKit, GTK's search delay,
286    /// Kirigami's longer one, a header on WinUI) is each one's own.
287    pub fn native(mut self, tweak: Tweak<SearchInput>) -> SearchInput {
288        tweak.apply(&mut self.0);
289        self
290    }
291}
292
293/// Text over many lines: the platform's text area, whose lines wrap to its
294/// width and where Return starts a new line. It's as tall as
295/// [`lines`](Self::lines) of the platform's text, and scrolls past them.
296pub struct TextArea(Element);
297widget!(TextArea);
298
299impl Default for TextArea {
300    fn default() -> TextArea {
301        TextArea::new()
302    }
303}
304
305impl TextArea {
306    /// Three lines tall.
307    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    /// Its lines end in `\n`.
314    pub fn value(mut self, value: impl IntoValue<String>) -> TextArea {
315        self.0.prop(value.into_value(), Prop::Value);
316        self
317    }
318
319    /// Two-way binding, Vue's `v-model`.
320    pub fn bind(self, signal: Signal<String>) -> TextArea {
321        self.value(signal).on_input(move |text| signal.set(text))
322    }
323
324    /// Shown while it's empty, where the platform's text areas show one
325    /// (Qt, WinUI); AppKit's and GTK's have none. It still names the area
326    /// to assistive technology without a label.
327    pub fn placeholder(mut self, placeholder: impl IntoValue<String>) -> TextArea {
328        self.0.prop(placeholder.into_value(), Prop::Placeholder);
329        self
330    }
331
332    /// What's wrong with its text, or `None`: see [`Prop::Error`]. Show
333    /// the message by it too, as the platform's apps do.
334    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    /// Shows the text, which can still be selected, copied and focused, but
345    /// not edited. Unlike a disabled area, it looks and reads as usual.
346    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    /// How many lines of text tall it is at its natural size: 3 unless
352    /// set, and at least 1. The layout can still stretch or shrink it.
353    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    /// Whether its lines wrap to its width, as they do unless it's set
359    /// false. Without, each line is as long as its text, and the area
360    /// scrolls sideways: for code, or logs.
361    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    /// Its text in one of the platform's text styles, e.g. monospace for
367    /// code or logs.
368    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    /// Whether it keeps its end in view as its text is set, while it's
374    /// there: for a log that grows. Scrolled up, it stays put.
375    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    /// Called on every edit with the new text.
381    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    /// Raw platform settings, past the semantic ones: see [`Tweak`]. What
391    /// the platforms offer (spelling and substitutions on AppKit, GTK's
392    /// wrap modes and monospace, Qt's text format, WinUI's spell check and
393    /// header) is each one's own.
394    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}