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, Tweak, Ui, UiEvent, View, WidgetKind,
5};
6use mitsuami_reactive::{IntoValue, Signal, Value};
7
8/// Single-line text entry.
9pub 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    /// Two-way binding, Vue's `v-model`.
29    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    /// Shows the text, which can still be selected, copied and focused, but
44    /// not edited. Unlike a disabled field, it looks and reads as usual.
45    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    /// What it's for (an email address, a URL, a phone number), which the
51    /// platform uses as it uses one: to pick an on-screen keyboard, to
52    /// offer autofill. It doesn't check what's typed.
53    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    /// Called on every edit with the new text.
59    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    /// Raw platform settings, past the semantic ones: see [`Tweak`]. What
69    /// the platforms offer (a borderless field on AppKit, icons on GTK, a
70    /// length limit on GTK, Qt and WinUI, a header on WinUI) is each one's
71    /// own.
72    pub fn native(mut self, tweak: Tweak<TextInput>) -> TextInput {
73        tweak.apply(&mut self.0);
74        self
75    }
76
77    /// Called when the user confirms (Return / Enter).
78    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
88/// Single-line entry of a password: the platform's password field, which
89/// hides the text as the platform does (bullets, and a button to show it
90/// where the platform has one) and never reads it out.
91pub 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    /// Two-way binding, Vue's `v-model`.
111    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    /// Raw platform settings: see [`Tweak`]. Password fields have no
126    /// semantic options past a text field's: what the platforms offer
127    /// (GTK's peek icon, WinUI's reveal mode, the bullet on Qt and WinUI)
128    /// is each one's own.
129    pub fn native(mut self, tweak: Tweak<PasswordInput>) -> PasswordInput {
130        tweak.apply(&mut self.0);
131        self
132    }
133
134    /// Called on every edit with the new text.
135    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    /// Called when the user confirms (Return / Enter).
145    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
155/// Single-line entry of search text: the platform's search field, with its
156/// search icon and clear button. [`on_search`](Self::on_search) is called
157/// when the platform asks for a search, as its own apps search.
158pub 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    /// Two-way binding, Vue's `v-model`.
178    pub fn bind(self, signal: Signal<String>) -> SearchInput {
179        self.value(signal).on_input(move |text| signal.set(text))
180    }
181
182    /// Shown while it's empty, in place of the platform's own where it has
183    /// one (AppKit, Qt). It names the field to assistive technology
184    /// without a label.
185    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    /// Called on every edit with the new text, the clear button's too.
196    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    /// Called with the text when the platform asks for a search: as the
206    /// user types, after a pause where the platform waits for one (AppKit,
207    /// GTK, Qt) and at once where it doesn't (WinUI); on Return; and when
208    /// the field is cleared. Not for text the app set.
209    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    /// Raw platform settings, past the semantic ones: see [`Tweak`]. What
219    /// the platforms offer (recent searches on AppKit, GTK's search delay,
220    /// Kirigami's longer one, a header on WinUI) is each one's own.
221    pub fn native(mut self, tweak: Tweak<SearchInput>) -> SearchInput {
222        tweak.apply(&mut self.0);
223        self
224    }
225}
226
227/// Text over many lines: the platform's text area, whose lines wrap to its
228/// width and where Return starts a new line. It's as tall as
229/// [`lines`](Self::lines) of the platform's text, and scrolls past them.
230pub struct TextArea(Element);
231widget!(TextArea);
232
233impl Default for TextArea {
234    fn default() -> TextArea {
235        TextArea::new()
236    }
237}
238
239impl TextArea {
240    /// Three lines tall.
241    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    /// Its lines end in `\n`.
248    pub fn value(mut self, value: impl IntoValue<String>) -> TextArea {
249        self.0.prop(value.into_value(), Prop::Value);
250        self
251    }
252
253    /// Two-way binding, Vue's `v-model`.
254    pub fn bind(self, signal: Signal<String>) -> TextArea {
255        self.value(signal).on_input(move |text| signal.set(text))
256    }
257
258    /// Shown while it's empty, where the platform's text areas show one
259    /// (Qt, WinUI); AppKit's and GTK's have none. It still names the area
260    /// to assistive technology without a label.
261    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    /// Shows the text, which can still be selected, copied and focused, but
272    /// not edited. Unlike a disabled area, it looks and reads as usual.
273    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    /// How many lines of text tall it is at its natural size: 3 unless
279    /// set, and at least 1. The layout can still stretch or shrink it.
280    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    /// Whether its lines wrap to its width, as they do unless it's set
286    /// false. Without, each line is as long as its text, and the area
287    /// scrolls sideways: for code, or logs.
288    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    /// Called on every edit with the new text.
294    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    /// Raw platform settings, past the semantic ones: see [`Tweak`]. What
304    /// the platforms offer (spelling and substitutions on AppKit, GTK's
305    /// wrap modes and monospace, Qt's text format, WinUI's spell check and
306    /// header) is each one's own.
307    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}