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
//! Styled multi-line text input — the multiline companion to
//! [`TextInput`](crate::TextInput).
//!
//! Wraps [`egui::TextEdit::multiline`] with the same visual treatment as
//! `TextInput`: the slate input background, a 1-px border that turns
//! sky-coloured on focus, optional label, hint, dirty bar, and submit-
//! flash feedback via [`ResponseFlashExt`](crate::ResponseFlashExt).
use egui::{
CornerRadius, FontId, FontSelection, Response, Stroke, TextEdit, Ui, Vec2, Widget, WidgetInfo,
WidgetText, WidgetType,
};
use crate::{flash, theme::Theme};
/// A styled multi-line text input.
///
/// ```no_run
/// # use elegance::TextArea;
/// # egui::__run_test_ui(|ui| {
/// let mut notes = String::new();
/// ui.add(
/// TextArea::new(&mut notes)
/// .label("Notes")
/// .hint("Jot anything down…")
/// .rows(6),
/// );
/// # });
/// ```
///
/// # Ids, focus, and flash state
///
/// As with [`TextInput`](crate::TextInput), pin a stable
/// [`id_salt`](Self::id_salt) on any TextArea you plan to flash — otherwise
/// the id is layout-dependent and in-flight flash/focus/cursor state is
/// lost if siblings above this widget appear or disappear between frames.
#[must_use = "Add with `ui.add(...)`."]
pub struct TextArea<'a> {
text: &'a mut String,
label: Option<WidgetText>,
hint: Option<&'a str>,
dirty: bool,
rows: usize,
desired_width: Option<f32>,
monospace: bool,
id_salt: Option<egui::Id>,
}
impl<'a> std::fmt::Debug for TextArea<'a> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("TextArea")
.field("dirty", &self.dirty)
.field("rows", &self.rows)
.field("monospace", &self.monospace)
.field("desired_width", &self.desired_width)
.finish()
}
}
impl<'a> TextArea<'a> {
/// Create a multi-line text input bound to `text`.
pub fn new(text: &'a mut String) -> Self {
Self {
text,
label: None,
hint: None,
dirty: false,
rows: 5,
desired_width: None,
monospace: false,
id_salt: None,
}
}
/// Show a label above the text area.
pub fn label(mut self, text: impl Into<WidgetText>) -> Self {
self.label = Some(text.into());
self
}
/// Show placeholder-style hint text when the field is empty.
pub fn hint(mut self, text: &'a str) -> Self {
self.hint = Some(text);
self
}
/// Mark the input as having unsaved changes. Shows a sky-coloured
/// accent bar down the left side.
pub fn dirty(mut self, dirty: bool) -> Self {
self.dirty = dirty;
self
}
/// Minimum visible row count. Default: `5`.
pub fn rows(mut self, rows: usize) -> Self {
self.rows = rows.max(1);
self
}
/// Desired width (points) for the editor portion of the widget.
pub fn desired_width(mut self, width: f32) -> Self {
self.desired_width = Some(width);
self
}
/// Render the text in the theme's monospace font. Useful for code,
/// JSON blobs, PEM keys, and similar fixed-width content.
pub fn monospace(mut self, monospace: bool) -> Self {
self.monospace = monospace;
self
}
/// Supply a stable id salt. Required if you plan to flash this widget
/// via [`ResponseFlashExt`](crate::ResponseFlashExt).
pub fn id_salt(mut self, id: impl std::hash::Hash) -> Self {
self.id_salt = Some(egui::Id::new(id));
self
}
}
impl<'a> Widget for TextArea<'a> {
fn ui(self, ui: &mut Ui) -> Response {
let theme = Theme::current(ui.ctx());
let p = &theme.palette;
let t = &theme.typography;
ui.vertical(|ui| {
if let Some(label) = &self.label {
ui.add_space(2.0);
let rich = egui::RichText::new(label.text())
.color(p.text_muted)
.size(t.label);
ui.add(egui::Label::new(rich).wrap_mode(egui::TextWrapMode::Extend));
ui.add_space(2.0);
}
let id_salt = self.id_salt.unwrap_or_else(|| ui.next_auto_id());
let widget_id = ui.make_persistent_id(egui::Id::new(id_salt));
let flash = flash::active_flash(ui.ctx(), widget_id);
let bg_fill = flash::background_fill(&theme, p.input_bg, flash);
let desired_width = self.desired_width.unwrap_or_else(|| ui.available_width());
let margin = Vec2::new(theme.control_padding_x * 0.5, theme.control_padding_y);
let font_id = if self.monospace {
FontId::monospace(t.monospace)
} else {
FontId::proportional(t.body)
};
let response = crate::theme::with_themed_visuals(ui, |ui| {
let v = ui.visuals_mut();
crate::theme::themed_input_visuals(v, &theme, bg_fill);
v.extreme_bg_color = bg_fill;
v.selection.bg_fill = crate::theme::with_alpha(p.sky, 90);
v.selection.stroke = Stroke::new(1.0, p.sky);
let mut edit = TextEdit::multiline(self.text)
.id_salt(id_salt)
.font(FontSelection::FontId(font_id))
.text_color(p.text)
.margin(margin)
.desired_width(desired_width)
.desired_rows(self.rows);
if let Some(hint) = self.hint {
edit = edit.hint_text(egui::RichText::new(hint).color(p.text_faint));
}
ui.add(edit)
});
if self.dirty && ui.is_rect_visible(response.rect) {
// Inset by the frame stroke so the bar sits *inside* the border,
// and use the frame's inner radius so the bar's rounded left edge
// follows the same arc as the inside of the input's rounded corner.
let stroke_w = 1.0;
let bar_w = 3.0;
let r = ((theme.control_radius - stroke_w).max(0.0)) as u8;
let bar = egui::Rect::from_min_max(
egui::pos2(
response.rect.min.x + stroke_w,
response.rect.min.y + stroke_w,
),
egui::pos2(
response.rect.min.x + stroke_w + bar_w,
response.rect.max.y - stroke_w,
),
);
let corner = CornerRadius {
nw: r,
sw: r,
ne: 0,
se: 0,
};
ui.painter().rect_filled(bar, corner, p.sky);
}
if let Some(label) = &self.label {
let label = label.text().to_string();
response.widget_info(|| WidgetInfo::labeled(WidgetType::TextEdit, true, &label));
}
response
})
.inner
}
}