mitsuami-widgets 2.3.0

Built-in widgets for mitsuami
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
//! Text fields: single-line, password, search and multi-line.

use mitsuami_core::{
    Element, ElementBuilder, EventValue, InputPurpose, NodeId, Prop, Suggestion, TextStyle, Tweak, Ui, UiEvent, View,
    WidgetKind,
};
use mitsuami_reactive::{IntoValue, Signal, Value};

/// Single-line text entry.
pub struct TextInput(Element);
widget!(TextInput);

impl Default for TextInput {
    fn default() -> TextInput {
        TextInput::new()
    }
}

impl TextInput {
    pub fn new() -> TextInput {
        TextInput(Element::new(WidgetKind::TextInput))
    }

    pub fn value(mut self, value: impl IntoValue<String>) -> TextInput {
        self.0.prop(value.into_value(), Prop::Value);
        self
    }

    /// Two-way binding, Vue's `v-model`.
    pub fn bind(self, signal: Signal<String>) -> TextInput {
        self.value(signal).on_input(move |text| signal.set(text))
    }

    pub fn placeholder(mut self, placeholder: impl IntoValue<String>) -> TextInput {
        self.0.prop(placeholder.into_value(), Prop::Placeholder);
        self
    }

    /// What's wrong with its text, or `None`: see [`Prop::Error`]. Show
    /// the message by it too, as the platform's apps do.
    pub fn error(mut self, error: impl IntoValue<Option<String>>) -> TextInput {
        self.0.prop(error.into_value(), Prop::Error);
        self
    }

    pub fn enabled(mut self, enabled: impl IntoValue<bool>) -> TextInput {
        self.0.prop(enabled.into_value(), Prop::Enabled);
        self
    }

    /// Shows the text, which can still be selected, copied and focused, but
    /// not edited. Unlike a disabled field, it looks and reads as usual.
    pub fn read_only(mut self, read_only: impl IntoValue<bool>) -> TextInput {
        self.0.prop(read_only.into_value(), Prop::ReadOnly);
        self
    }

    /// What it's for (an email address, a URL, a phone number), which the
    /// platform uses as it uses one: to pick an on-screen keyboard, to
    /// offer autofill. It doesn't check what's typed.
    pub fn input_purpose(mut self, purpose: impl IntoValue<InputPurpose>) -> TextInput {
        self.0.prop(purpose.into_value(), Prop::InputPurpose);
        self
    }

    /// Called on every edit with the new text.
    pub fn on_input(mut self, handler: impl Fn(String) + 'static) -> TextInput {
        self.0.on(move |event| {
            if let UiEvent::Changed(EventValue::Text(text)) = event {
                handler(text.clone());
            }
        });
        self
    }

    /// Raw platform settings, past the semantic ones: see [`Tweak`]. What
    /// the platforms offer (a borderless field on AppKit, icons on GTK, a
    /// length limit on GTK, Qt and WinUI, a header on WinUI) is each one's
    /// own.
    pub fn native(mut self, tweak: Tweak<TextInput>) -> TextInput {
        tweak.apply(&mut self.0);
        self
    }

    /// Takes `key` before the field's own use of it (the arrows, Return,
    /// Escape), and runs `handler` when it's pressed in the field: a field
    /// that moves through a list beside it.
    pub fn on_key(
        mut self,
        key: impl Into<mitsuami_core::services::Shortcut>,
        handler: impl Fn() + 'static,
    ) -> TextInput {
        self.0.on_key(key.into(), handler);
        self
    }

    /// Called when the user confirms (Return / Enter).
    pub fn on_submit(mut self, handler: impl Fn() + 'static) -> TextInput {
        self.0.on(move |event| {
            if *event == UiEvent::Submit {
                handler();
            }
        });
        self
    }
}

/// Single-line entry of a password: the platform's password field, which
/// hides the text as the platform does (bullets, and a button to show it
/// where the platform has one) and never reads it out.
pub struct PasswordInput(Element);
widget!(PasswordInput);

impl Default for PasswordInput {
    fn default() -> PasswordInput {
        PasswordInput::new()
    }
}

impl PasswordInput {
    pub fn new() -> PasswordInput {
        PasswordInput(Element::new(WidgetKind::PasswordInput))
    }

    pub fn value(mut self, value: impl IntoValue<String>) -> PasswordInput {
        self.0.prop(value.into_value(), Prop::Value);
        self
    }

    /// Two-way binding, Vue's `v-model`.
    pub fn bind(self, signal: Signal<String>) -> PasswordInput {
        self.value(signal).on_input(move |text| signal.set(text))
    }

    pub fn placeholder(mut self, placeholder: impl IntoValue<String>) -> PasswordInput {
        self.0.prop(placeholder.into_value(), Prop::Placeholder);
        self
    }

    /// What's wrong with its text, or `None`: see [`Prop::Error`]. Show
    /// the message by it too, as the platform's apps do.
    pub fn error(mut self, error: impl IntoValue<Option<String>>) -> PasswordInput {
        self.0.prop(error.into_value(), Prop::Error);
        self
    }

    pub fn enabled(mut self, enabled: impl IntoValue<bool>) -> PasswordInput {
        self.0.prop(enabled.into_value(), Prop::Enabled);
        self
    }

    /// Raw platform settings: see [`Tweak`]. Password fields have no
    /// semantic options past a text field's: what the platforms offer
    /// (GTK's peek icon, WinUI's reveal mode, the bullet on Qt and WinUI)
    /// is each one's own.
    pub fn native(mut self, tweak: Tweak<PasswordInput>) -> PasswordInput {
        tweak.apply(&mut self.0);
        self
    }

    /// Called on every edit with the new text.
    pub fn on_input(mut self, handler: impl Fn(String) + 'static) -> PasswordInput {
        self.0.on(move |event| {
            if let UiEvent::Changed(EventValue::Text(text)) = event {
                handler(text.clone());
            }
        });
        self
    }

    /// Called when the user confirms (Return / Enter).
    pub fn on_submit(mut self, handler: impl Fn() + 'static) -> PasswordInput {
        self.0.on(move |event| {
            if *event == UiEvent::Submit {
                handler();
            }
        });
        self
    }
}

/// Single-line entry of search text: the platform's search field, with its
/// search icon and clear button. [`on_search`](Self::on_search) is called
/// when the platform asks for a search, as its own apps search.
pub struct SearchInput(Element);
widget!(SearchInput);

impl Default for SearchInput {
    fn default() -> SearchInput {
        SearchInput::new()
    }
}

impl SearchInput {
    pub fn new() -> SearchInput {
        SearchInput(Element::new(WidgetKind::SearchInput))
    }

    pub fn value(mut self, value: impl IntoValue<String>) -> SearchInput {
        self.0.prop(value.into_value(), Prop::Value);
        self
    }

    /// Two-way binding, Vue's `v-model`.
    pub fn bind(self, signal: Signal<String>) -> SearchInput {
        self.value(signal).on_input(move |text| signal.set(text))
    }

    /// Shown while it's empty, in place of the platform's own where it has
    /// one (AppKit, Qt). It names the field to assistive technology
    /// without a label.
    pub fn placeholder(mut self, placeholder: impl IntoValue<String>) -> SearchInput {
        self.0.prop(placeholder.into_value(), Prop::Placeholder);
        self
    }

    pub fn enabled(mut self, enabled: impl IntoValue<bool>) -> SearchInput {
        self.0.prop(enabled.into_value(), Prop::Enabled);
        self
    }

    /// Called on every edit with the new text, the clear button's too.
    pub fn on_input(mut self, handler: impl Fn(String) + 'static) -> SearchInput {
        self.0.on(move |event| {
            if let UiEvent::Changed(EventValue::Text(text)) = event {
                handler(text.clone());
            }
        });
        self
    }

    /// Called with the text when the platform asks for a search: as the
    /// user types, after a pause where the platform waits for one (AppKit,
    /// GTK, Qt) and at once where it doesn't (WinUI); on Return; and when
    /// the field is cleared. Not for text the app set.
    /// Takes `key` before the field's own use of it (the arrows, Return,
    /// Escape; the suggestions' still come first), and runs `handler` when
    /// it's pressed in the field.
    pub fn on_key(
        mut self,
        key: impl Into<mitsuami_core::services::Shortcut>,
        handler: impl Fn() + 'static,
    ) -> SearchInput {
        self.0.on_key(key.into(), handler);
        self
    }

    pub fn on_search(mut self, handler: impl Fn(String) + 'static) -> SearchInput {
        self.0.on(move |event| {
            if let UiEvent::Search(text) = event {
                handler(text.clone());
            }
        });
        self
    }

    /// Searches on Return (and when cleared), not as the user types: for
    /// searches that take a while, with each platform's own setting for it.
    pub fn search_on_return(mut self, on: impl IntoValue<bool>) -> SearchInput {
        self.0.prop(on.into_value(), Prop::SearchOnReturn);
        self
    }

    /// What it suggests for its text, in a list under it as the platform
    /// shows a field's suggestions; empty: none. The field keeps the
    /// keyboard: the arrows move through the list, Return picks one, and
    /// Escape hides it. The app sets it as the text changes.
    pub fn suggestions(mut self, suggestions: impl IntoValue<Vec<Suggestion>>) -> SearchInput {
        self.0.prop(suggestions.into_value(), Prop::Suggestions);
        self
    }

    /// Called with a suggestion's index when the user picks it. The text
    /// is the app's to set.
    pub fn on_suggestion(mut self, handler: impl Fn(usize) + 'static) -> SearchInput {
        self.0.on(move |event| {
            if let UiEvent::SuggestionChosen(index) = event {
                handler(*index);
            }
        });
        self
    }

    /// Raw platform settings, past the semantic ones: see [`Tweak`]. What
    /// the platforms offer (recent searches on AppKit, GTK's search delay,
    /// Kirigami's longer one, a header on WinUI) is each one's own.
    pub fn native(mut self, tweak: Tweak<SearchInput>) -> SearchInput {
        tweak.apply(&mut self.0);
        self
    }
}

/// Text over many lines: the platform's text area, whose lines wrap to its
/// width and where Return starts a new line. It's as tall as
/// [`lines`](Self::lines) of the platform's text, and scrolls past them.
pub struct TextArea(Element);
widget!(TextArea);

impl Default for TextArea {
    fn default() -> TextArea {
        TextArea::new()
    }
}

impl TextArea {
    /// Three lines tall.
    pub fn new() -> TextArea {
        let mut element = Element::new(WidgetKind::TextArea);
        element.prop(Value::Static(3), Prop::Lines);
        TextArea(element)
    }

    /// Its lines end in `\n`.
    pub fn value(mut self, value: impl IntoValue<String>) -> TextArea {
        self.0.prop(value.into_value(), Prop::Value);
        self
    }

    /// Two-way binding, Vue's `v-model`.
    pub fn bind(self, signal: Signal<String>) -> TextArea {
        self.value(signal).on_input(move |text| signal.set(text))
    }

    /// Shown while it's empty, where the platform's text areas show one
    /// (Qt, WinUI); AppKit's and GTK's have none. It still names the area
    /// to assistive technology without a label.
    pub fn placeholder(mut self, placeholder: impl IntoValue<String>) -> TextArea {
        self.0.prop(placeholder.into_value(), Prop::Placeholder);
        self
    }

    /// What's wrong with its text, or `None`: see [`Prop::Error`]. Show
    /// the message by it too, as the platform's apps do.
    pub fn error(mut self, error: impl IntoValue<Option<String>>) -> TextArea {
        self.0.prop(error.into_value(), Prop::Error);
        self
    }

    pub fn enabled(mut self, enabled: impl IntoValue<bool>) -> TextArea {
        self.0.prop(enabled.into_value(), Prop::Enabled);
        self
    }

    /// Shows the text, which can still be selected, copied and focused, but
    /// not edited. Unlike a disabled area, it looks and reads as usual.
    pub fn read_only(mut self, read_only: impl IntoValue<bool>) -> TextArea {
        self.0.prop(read_only.into_value(), Prop::ReadOnly);
        self
    }

    /// How many lines of text tall it is at its natural size: 3 unless
    /// set, and at least 1. The layout can still stretch or shrink it.
    pub fn lines(mut self, lines: impl IntoValue<u32>) -> TextArea {
        self.0.prop(lines.into_value(), |n| Prop::Lines(n.max(1)));
        self
    }

    /// Whether its lines wrap to its width, as they do unless it's set
    /// false. Without, each line is as long as its text, and the area
    /// scrolls sideways: for code, or logs.
    pub fn line_wrap(mut self, wrap: impl IntoValue<bool>) -> TextArea {
        self.0.prop(wrap.into_value(), Prop::LineWrap);
        self
    }

    /// Its text in one of the platform's text styles, e.g. monospace for
    /// code or logs.
    pub fn text_style(mut self, style: impl IntoValue<TextStyle>) -> TextArea {
        self.0.prop(style.into_value(), Prop::TextStyle);
        self
    }

    /// Whether it keeps its end in view as its text is set, while it's
    /// there: for a log that grows. Scrolled up, it stays put.
    pub fn follows_end(mut self, follows: impl IntoValue<bool>) -> TextArea {
        self.0.prop(follows.into_value(), Prop::FollowsEnd);
        self
    }

    /// Called on every edit with the new text.
    pub fn on_input(mut self, handler: impl Fn(String) + 'static) -> TextArea {
        self.0.on(move |event| {
            if let UiEvent::Changed(EventValue::Text(text)) = event {
                handler(text.clone());
            }
        });
        self
    }

    /// Raw platform settings, past the semantic ones: see [`Tweak`]. What
    /// the platforms offer (spelling and substitutions on AppKit, GTK's
    /// wrap modes and monospace, Qt's text format, WinUI's spell check and
    /// header) is each one's own.
    pub fn native(mut self, tweak: Tweak<TextArea>) -> TextArea {
        tweak.apply(&mut self.0);
        self
    }
}

impl TextInput {
    #[doc(hidden)]
    pub fn __tag() -> TextInput {
        TextInput::new()
    }
}

impl PasswordInput {
    #[doc(hidden)]
    pub fn __tag() -> PasswordInput {
        PasswordInput::new()
    }
}

impl SearchInput {
    #[doc(hidden)]
    pub fn __tag() -> SearchInput {
        SearchInput::new()
    }
}

impl TextArea {
    #[doc(hidden)]
    pub fn __tag() -> TextArea {
        TextArea::new()
    }
}