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
use super::*;
impl Context {
/// Render an animated spinner.
///
/// The spinner advances one frame per tick. Use [`SpinnerState::dots`] or
/// [`SpinnerState::line`] to create the state.
///
/// Returns a [`Response`] with `hovered` populated correctly so callers
/// can attach tooltips or react to mouse interaction. Prior to v0.20.0
/// this returned `&mut Self`; existing code that ignores the return value
/// keeps compiling, though the `#[must_use]` attribute on `Response`
/// surfaces a warning that nudges callers to handle interaction state.
pub fn spinner(&mut self, state: &SpinnerState) -> Response {
let response = self.interaction();
self.styled(
state.frame(self.tick).to_string(),
Style::new().fg(self.theme.primary),
);
response
}
/// Render toast notifications. Calls `state.cleanup(tick)` automatically.
///
/// Expired messages are removed before rendering. If there are no active
/// messages, nothing is rendered and `self` is returned unchanged.
pub fn toast(&mut self, state: &mut ToastState) -> &mut Self {
state.cleanup(self.tick);
if state.messages.is_empty() {
return self;
}
self.skip_interaction_slot();
self.commands
.push(Command::BeginContainer(Box::new(BeginContainerArgs {
direction: Direction::Column,
gap: 0,
align: Align::Start,
align_self: None,
justify: Justify::Start,
border: None,
border_sides: BorderSides::all(),
border_style: Style::new().fg(self.theme.border),
bg_color: None,
padding: Padding::default(),
margin: Margin::default(),
constraints: Constraints::default(),
title: None,
grow: 0,
group_name: None,
})));
for message in state.messages.iter().rev() {
let color = match message.level {
ToastLevel::Info => self.theme.primary,
ToastLevel::Success => self.theme.success,
ToastLevel::Warning => self.theme.warning,
ToastLevel::Error => self.theme.error,
};
let mut line = String::with_capacity(4 + message.text.len());
line.push_str(" ● ");
line.push_str(&message.text);
self.styled(line, Style::new().fg(color));
}
self.commands.push(Command::EndContainer);
self.rollback.last_text_idx = None;
self
}
/// Horizontal slider for numeric values.
///
/// Step defaults to `span / 20.0`. Use [`Context::slider_with_step`] for an
/// explicit step (e.g. integer volume controls).
///
/// # Examples
/// ```
/// # use slt::*;
/// # TestBackend::new(80, 24).render(|ui| {
/// let mut volume = 75.0_f64;
/// let r = ui.slider("Volume", &mut volume, 0.0..=100.0);
/// if r.changed { /* volume was adjusted */ }
/// # });
/// ```
pub fn slider(
&mut self,
label: &str,
value: &mut f64,
range: std::ops::RangeInclusive<f64>,
) -> Response {
let span = (*range.end() - *range.start()).max(0.0);
let step = if span > 0.0 { span / 20.0 } else { 0.0 };
self.slider_inner(label, value, range, step)
}
/// Horizontal slider with an explicit step size.
///
/// Each Left/Right (or `h`/`l`) advances `value` by `step`. Use this when
/// the default step (`span / 20`) is too coarse or too fine — for example
/// integer counters need `step = 1.0`, fine controls need `step = 0.1`.
///
/// # Examples
/// ```
/// # use slt::*;
/// # TestBackend::new(80, 24).render(|ui| {
/// let mut volume = 50.0_f64;
/// ui.slider_with_step("Volume", &mut volume, 0.0..=100.0, 1.0);
/// # });
/// ```
pub fn slider_with_step(
&mut self,
label: &str,
value: &mut f64,
range: std::ops::RangeInclusive<f64>,
step: f64,
) -> Response {
self.slider_inner(label, value, range, step.max(0.0))
}
fn slider_inner(
&mut self,
label: &str,
value: &mut f64,
range: std::ops::RangeInclusive<f64>,
step: f64,
) -> Response {
let focused = self.register_focusable();
// v0.21.1: capture focus-edge flags (issue #208 gap — slider assembled
// its Response by hand and never set gained_focus/lost_focus).
let (gained_focus, lost_focus) = self.focus_transitions(focused);
let mut changed = false;
let start = *range.start();
let end = *range.end();
let span = (end - start).max(0.0);
*value = (*value).clamp(start, end);
if focused {
let mut consumed_indices = Vec::new();
for (i, key) in self.available_key_presses() {
match key.code {
KeyCode::Left | KeyCode::Char('h') => {
if step > 0.0 {
let next = (*value - step).max(start);
if (next - *value).abs() > f64::EPSILON {
*value = next;
changed = true;
}
}
consumed_indices.push(i);
}
KeyCode::Right | KeyCode::Char('l') => {
if step > 0.0 {
let next = (*value + step).min(end);
if (next - *value).abs() > f64::EPSILON {
*value = next;
changed = true;
}
}
consumed_indices.push(i);
}
_ => {}
}
}
self.consume_indices(consumed_indices);
}
let ratio = if span <= f64::EPSILON {
0.0
} else {
((*value - start) / span).clamp(0.0, 1.0)
};
let value_text = format_compact_number(*value);
let label_width = UnicodeWidthStr::width(label) as u32;
let value_width = UnicodeWidthStr::width(value_text.as_str()) as u32;
let track_width = self
.area_width
.saturating_sub(label_width + value_width + 8)
.max(10) as usize;
let thumb_idx = if track_width <= 1 {
0
} else {
(ratio * (track_width as f64 - 1.0)).round() as usize
};
let mut track = String::with_capacity(track_width);
for i in 0..track_width {
if i == thumb_idx {
track.push('○');
} else if i < thumb_idx {
track.push('█');
} else {
track.push('━');
}
}
let text_color = self.theme.text;
let border_color = self.theme.border;
let primary_color = self.theme.primary;
let dim_color = self.theme.text_dim;
let mut response = self.container().row(|ui| {
ui.text(label).fg(text_color);
ui.text("[").fg(border_color);
ui.text(track).grow(1).fg(primary_color);
ui.text("]").fg(border_color);
if focused {
ui.text(value_text.as_str()).bold().fg(primary_color);
} else {
ui.text(value_text.as_str()).fg(dim_color);
}
});
response.focused = focused;
response.changed = changed;
response.gained_focus = gained_focus;
response.lost_focus = lost_focus;
response
}
/// Numeric stepper field: Up/Down (or `k`/`j`) and scroll-wheel adjust by
/// `step`, or type a value directly and press `Enter`. The committed value
/// is always clamped to `[min, max]` (and rounded in integer mode).
///
/// Unlike [`slider`](Context::slider) — a bar-and-thumb control keyed by
/// Left/Right — this renders the raw value as a `▾ 42 ▴` field that accepts
/// direct typing. Config lives on [`NumberInputState`]. Up/`k` increments,
/// Down/`j` decrements; `Enter` commits a typed buffer, `Esc` discards it,
/// `Backspace` edits it. Left/Right are intentionally unused (reserved).
///
/// `Response.focused` reflects focus and `Response.changed` is `true` iff
/// the committed value changed this frame. All handled key and scroll
/// events are consumed so they do not leak to other widgets or the global
/// quit handler.
///
/// Available since `0.21.0`.
///
/// # Example
///
/// ```
/// # use slt::*;
/// # use slt::widgets::NumberInputState;
/// # TestBackend::new(80, 24).render(|ui| {
/// let mut qty = NumberInputState::integer(3, 0, 10).step(1.0);
/// let r = ui.number_input(&mut qty);
/// if r.changed { /* qty.value updated */ }
/// # });
/// ```
pub fn number_input(&mut self, state: &mut NumberInputState) -> Response {
let focused = self.register_focusable();
// v0.21.1: capture focus-edge flags (issue #208 gap — number_input
// assembled its Response by hand and never set gained/lost_focus).
let (gained_focus, lost_focus) = self.focus_transitions(focused);
// Normalize the committed value before processing input so the
// pre-frame baseline used for `changed` is itself in-range.
state.value = state.clamped();
let old = state.value;
let step = state.step.max(0.0);
let adjust = |state: &mut NumberInputState, delta: f64| {
if delta == 0.0 {
return;
}
// Adjusting commits any in-progress buffer (discarding it) and
// clears a prior parse error.
state.editing = None;
state.parse_error = None;
state.value = (state.value + delta).clamp(state.min, state.max);
if state.integer {
state.value = state.value.round();
}
};
if focused {
let mut consumed_indices = Vec::new();
for (i, key) in self.available_key_presses() {
match key.code {
KeyCode::Up | KeyCode::Char('k') => {
adjust(state, step);
consumed_indices.push(i);
}
KeyCode::Down | KeyCode::Char('j') => {
adjust(state, -step);
consumed_indices.push(i);
}
KeyCode::Char(ch) if is_number_char(ch, state) => {
let buf = state.editing.get_or_insert_with(String::new);
buf.push(ch);
state.parse_error = None;
consumed_indices.push(i);
}
KeyCode::Backspace => {
if let Some(buf) = state.editing.as_mut() {
buf.pop();
state.parse_error = None;
consumed_indices.push(i);
}
}
KeyCode::Enter => {
if let Some(buf) = state.editing.take() {
let trimmed = buf.trim();
match trimmed.parse::<f64>() {
Ok(parsed) if parsed.is_finite() => {
state.value = parsed.clamp(state.min, state.max);
if state.integer {
state.value = state.value.round();
}
state.parse_error = None;
}
_ => {
state.parse_error = Some(format!("invalid number: {trimmed}"));
}
}
consumed_indices.push(i);
}
}
KeyCode::Esc if state.editing.is_some() => {
state.editing = None;
state.parse_error = None;
consumed_indices.push(i);
}
_ => {}
}
}
self.consume_indices(consumed_indices);
}
// Clamp again after key handling so the rendered value is in-range.
state.value = state.clamped();
let display = if let Some(buf) = state.editing.as_ref() {
buf.clone()
} else if state.integer {
format!("{:.0}", state.value)
} else {
format_compact_number(state.value)
};
let primary_color = self.theme.primary;
let dim_color = self.theme.text_dim;
let error_color = self.theme.error;
let value_color = if focused { primary_color } else { dim_color };
let arrow_color = if focused { primary_color } else { dim_color };
let parse_error = state.parse_error.clone();
let editing = state.editing.is_some();
let mut response = self.container().row(|ui| {
ui.text("▾").fg(arrow_color);
ui.text(" ");
if focused {
ui.text(display.as_str()).bold().fg(value_color);
} else {
ui.text(display.as_str()).fg(value_color);
}
ui.text(" ");
ui.text("▴").fg(arrow_color);
if editing {
ui.text(" ✎").fg(dim_color);
}
if let Some(err) = parse_error.as_ref() {
let mut indicator = String::with_capacity(2 + err.len());
indicator.push_str(" ⚠ ");
indicator.push_str(err);
ui.text(indicator).dim().fg(error_color);
}
});
// Scroll-wheel adjustment over the rendered field's rect. The row's
// `Response.rect` comes from the previous frame's hit map (the standard
// `prev_hit_map` pattern, mirroring `rich_log`), so a scroll tick takes
// effect on the next frame. `ScrollUp` increments, `ScrollDown`
// decrements, both clamped to `[min, max]`.
if response.rect.width > 0 && response.rect.height > 0 {
let rect = response.rect;
let mut consumed = Vec::new();
for (i, mouse) in self.mouse_events_in_rect(rect) {
match mouse.kind {
MouseKind::ScrollUp => {
adjust(state, step);
consumed.push(i);
}
MouseKind::ScrollDown => {
adjust(state, -step);
consumed.push(i);
}
_ => {}
}
}
self.consume_indices(consumed);
}
// Final clamp guards against any direct mutation or scroll adjustment.
state.value = state.clamped();
response.focused = focused;
// `changed` is true iff the committed value actually moved this frame.
response.changed = (state.value - old).abs() > f64::EPSILON;
response.gained_focus = gained_focus;
response.lost_focus = lost_focus;
response
}
}
/// Whether `ch` may be appended to the in-progress edit buffer.
///
/// Always allows ASCII digits. Allows a single `.` in float mode (not when the
/// buffer already contains one). Allows a leading `-` only when negatives are
/// representable (`min < 0`) and the buffer is empty.
fn is_number_char(ch: char, state: &NumberInputState) -> bool {
if ch.is_ascii_digit() {
return true;
}
let buf = state.editing.as_deref().unwrap_or("");
match ch {
'.' => !state.integer && !buf.contains('.'),
'-' => state.min < 0.0 && buf.is_empty(),
_ => false,
}
}