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
//! Single- and multi-line text entry.
use super::*;
macro_rules! text_methods {
($t:ident) => {
impl $t {
/// Replace the text (no callback fires).
pub fn set_text(&self, t: &str) {
set_text(self.id(), t, false)
}
/// The text.
pub fn text(&self) -> String {
text_of(self.id())
}
/// Called with the new full text after each user edit.
pub fn on_change(&self, mut f: impl FnMut(&str) + 'static) {
on(self.id(), Ev::Text, move |e| {
if let Event::Text(s) = e {
f(s)
}
})
}
/// Forbid or allow editing by the user.
pub fn set_read_only(&self, v: bool) {
core::set(
self.id(),
false,
|n| {
if let Some(t) = n.text_data_mut() {
t.readonly = v
}
},
Prop::ReadOnly(v),
);
}
/// Show the text in a fixed-pitch font (code, logs, hex dumps). Default off; ignored
/// by backends that cannot change the font.
pub fn set_monospace(&self, v: bool) {
core::set(
self.id(),
true,
|n| {
if let Some(t) = n.text_data_mut() {
t.monospace = v
}
},
Prop::Monospace(v),
);
}
/// Whether the fixed-pitch font is on.
pub fn monospace(&self) -> bool {
core::read(self.id(), |n| n.text_data().is_some_and(|t| t.monospace))
.unwrap_or(false)
}
}
};
}
text_methods!(TextInput);
text_methods!(TextArea);
impl TextInput {
/// A single-line text field.
pub fn new(parent: impl Into<WidgetId>) -> TextInput {
make(TextInput::from_id, Kind::TextInput, parent, |_| {})
}
/// Single-line input that masks its contents.
pub fn password(parent: impl Into<WidgetId>) -> TextInput {
make(TextInput::from_id, Kind::PasswordInput, parent, |_| {})
}
/// Text shown while the field is empty.
pub fn set_placeholder(&self, t: &str) {
core::set(
self.id(),
false,
|n| {
if let Some(d) = n.text_data_mut() {
d.placeholder = t.to_string()
}
},
Prop::Placeholder(t),
);
}
}
impl TextArea {
/// A multi-line text area, wrapping lines by default.
pub fn new(parent: impl Into<WidgetId>) -> TextArea {
make(TextArea::from_id, Kind::TextArea, parent, |_| {})
}
/// Soft-wrap long lines (default `true`); `false` scrolls horizontally instead.
pub fn set_wrap(&self, v: bool) {
core::set(
self.id(),
false,
|n| {
if let Some(d) = n.text_data_mut() {
d.wrap = v
}
},
Prop::Wrap(v),
);
}
/// Whether long lines wrap.
pub fn wrap(&self) -> bool {
core::read(self.id(), |n| n.text_data().is_some_and(|t| t.wrap)).unwrap_or(false)
}
}