slt/widgets/input.rs
1/// Accumulated static output lines for [`crate::run_static`].
2///
3/// Use [`println`](Self::println) to append lines above the dynamic inline TUI.
4#[derive(Debug, Clone, Default)]
5pub struct StaticOutput {
6 lines: Vec<String>,
7 new_lines: Vec<String>,
8}
9
10impl StaticOutput {
11 /// Create an empty static output buffer.
12 pub fn new() -> Self {
13 Self::default()
14 }
15
16 /// Append one line of static output.
17 pub fn println(&mut self, line: impl Into<String>) {
18 let line = line.into();
19 self.lines.push(line.clone());
20 self.new_lines.push(line);
21 }
22
23 /// Return all accumulated static lines.
24 pub fn lines(&self) -> &[String] {
25 &self.lines
26 }
27
28 /// Drain and return only lines added since the previous drain.
29 pub fn drain_new(&mut self) -> Vec<String> {
30 std::mem::take(&mut self.new_lines)
31 }
32
33 /// Clear all accumulated lines.
34 pub fn clear(&mut self) {
35 self.lines.clear();
36 self.new_lines.clear();
37 }
38}
39
40/// State for a single-line text input widget.
41///
42/// Pass a mutable reference to `Context::text_input` each frame. The widget
43/// handles all keyboard events when focused.
44///
45/// # Example
46///
47/// ```no_run
48/// # use slt::widgets::TextInputState;
49/// # slt::run(|ui: &mut slt::Context| {
50/// let mut input = TextInputState::with_placeholder("Type here...");
51/// ui.text_input(&mut input);
52/// println!("{}", input.value);
53/// # });
54/// ```
55pub struct TextInputState {
56 /// The current input text.
57 pub value: String,
58 /// Cursor position as a character index into `value`.
59 pub cursor: usize,
60 /// Placeholder text shown when `value` is empty.
61 pub placeholder: String,
62 /// Maximum character count. Input is rejected beyond this limit.
63 pub max_length: Option<usize>,
64 /// The most recent validation error message, if any.
65 pub validation_error: Option<String>,
66 /// When `true`, input is displayed as `•` characters (for passwords).
67 pub masked: bool,
68 /// Autocomplete candidates shown below the input.
69 pub suggestions: Vec<String>,
70 /// Highlighted index within the currently shown suggestions.
71 pub suggestion_index: usize,
72 /// Whether the suggestions popup should be rendered.
73 pub show_suggestions: bool,
74 /// Multiple validators that produce their own error messages.
75 validators: Vec<TextInputValidator>,
76 /// All current validation errors from all validators.
77 validation_errors: Vec<String>,
78}
79
80impl std::fmt::Debug for TextInputState {
81 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
82 f.debug_struct("TextInputState")
83 .field("value", &self.value)
84 .field("cursor", &self.cursor)
85 .field("placeholder", &self.placeholder)
86 .field("max_length", &self.max_length)
87 .field("validation_error", &self.validation_error)
88 .field("masked", &self.masked)
89 .field("suggestions", &self.suggestions)
90 .field("suggestion_index", &self.suggestion_index)
91 .field("show_suggestions", &self.show_suggestions)
92 .field("validators_len", &self.validators.len())
93 .field("validation_errors", &self.validation_errors)
94 .finish()
95 }
96}
97
98impl Clone for TextInputState {
99 /// # Clone behavior
100 ///
101 /// `validators` registered via [`TextInputState::add_validator`] are **not**
102 /// cloned because closures are not `Clone`. `validation_errors` is preserved
103 /// in the clone, but it becomes stale — calling
104 /// [`TextInputState::run_validators`] on the clone will clear errors without
105 /// re-running any validation.
106 ///
107 /// Re-register validators on the clone before calling `run_validators()`.
108 fn clone(&self) -> Self {
109 Self {
110 value: self.value.clone(),
111 cursor: self.cursor,
112 placeholder: self.placeholder.clone(),
113 max_length: self.max_length,
114 validation_error: self.validation_error.clone(),
115 masked: self.masked,
116 suggestions: self.suggestions.clone(),
117 suggestion_index: self.suggestion_index,
118 show_suggestions: self.show_suggestions,
119 validators: Vec::new(),
120 validation_errors: self.validation_errors.clone(),
121 }
122 }
123}
124
125impl TextInputState {
126 /// Create an empty text input state.
127 pub fn new() -> Self {
128 Self {
129 value: String::new(),
130 cursor: 0,
131 placeholder: String::new(),
132 max_length: None,
133 validation_error: None,
134 masked: false,
135 suggestions: Vec::new(),
136 suggestion_index: 0,
137 show_suggestions: false,
138 validators: Vec::new(),
139 validation_errors: Vec::new(),
140 }
141 }
142
143 /// Create a text input with placeholder text shown when the value is empty.
144 pub fn with_placeholder(p: impl Into<String>) -> Self {
145 Self {
146 placeholder: p.into(),
147 ..Self::new()
148 }
149 }
150
151 /// Set the maximum allowed character count.
152 pub fn max_length(mut self, len: usize) -> Self {
153 self.max_length = Some(len);
154 self
155 }
156
157 /// Validate the current value and store the latest error message.
158 ///
159 /// Sets [`TextInputState::validation_error`] to `None` when validation
160 /// succeeds, or to `Some(error)` when validation fails.
161 ///
162 /// This is a backward-compatible shorthand that runs a single validator.
163 /// For multiple validators, use [`add_validator`](Self::add_validator) and [`run_validators`](Self::run_validators).
164 pub fn validate(&mut self, validator: impl Fn(&str) -> Result<(), String>) {
165 self.validation_error = validator(&self.value).err();
166 }
167
168 /// Add a validator function that produces its own error message.
169 ///
170 /// Multiple validators can be added. Call [`run_validators`](Self::run_validators)
171 /// to execute all validators and collect their errors.
172 ///
173 /// # Note on cloning
174 ///
175 /// Validators are **not** preserved across [`Clone`] because closures are
176 /// not `Clone`. Re-register after cloning the state.
177 pub fn add_validator(&mut self, f: impl Fn(&str) -> Result<(), String> + 'static) {
178 self.validators.push(Box::new(f));
179 }
180
181 /// Run all registered validators and collect their error messages.
182 ///
183 /// Updates `validation_errors` with all errors from all validators.
184 /// Also updates `validation_error` to the first error for backward compatibility.
185 ///
186 /// # Note on cloning
187 ///
188 /// Validators do not survive [`Clone`]. Calling this on a cloned state with
189 /// no re-registered validators clears `validation_errors` without re-running
190 /// any check. Re-register validators on the clone first.
191 pub fn run_validators(&mut self) {
192 self.validation_errors.clear();
193 for validator in &self.validators {
194 if let Err(err) = validator(&self.value) {
195 self.validation_errors.push(err);
196 }
197 }
198 self.validation_error = self.validation_errors.first().cloned();
199 }
200
201 /// Get all current validation errors from all validators.
202 pub fn errors(&self) -> &[String] {
203 &self.validation_errors
204 }
205
206 /// Set autocomplete suggestions and reset popup state.
207 pub fn set_suggestions(&mut self, suggestions: Vec<String>) {
208 self.suggestions = suggestions;
209 self.suggestion_index = 0;
210 self.show_suggestions = !self.suggestions.is_empty();
211 }
212
213 /// Return suggestions that start with the current input (case-insensitive).
214 pub fn matched_suggestions(&self) -> Vec<&str> {
215 if self.value.is_empty() {
216 return Vec::new();
217 }
218 let lower = self.value.to_lowercase();
219 self.suggestions
220 .iter()
221 .filter(|s| s.to_lowercase().starts_with(&lower))
222 .map(|s| s.as_str())
223 .collect()
224 }
225}
226
227impl Default for TextInputState {
228 fn default() -> Self {
229 Self::new()
230 }
231}
232
233/// A boxed, state-capturing field validator.
234///
235/// Unlike the deprecated [`FormValidator`] function pointer, a `Validator`
236/// wraps a closure, so it can capture surrounding state — a compiled matcher,
237/// a min/max pulled from config, or a sibling field's value. Built-in
238/// constructors live in the [`validators`] module.
239///
240/// You rarely construct one directly: [`FormField::validate`] accepts a closure
241/// and boxes it for you. Use [`Validator::new`] when you need to build a
242/// `Validator` value yourself.
243///
244/// # Example
245///
246/// ```no_run
247/// # use slt::widgets::Validator;
248/// let min = 3usize; // captured state — impossible with a fn pointer
249/// let v = Validator::new(move |s: &str| {
250/// if s.len() >= min { Ok(()) } else { Err(format!("min {min} chars")) }
251/// });
252/// assert!(v.run("hello").is_ok());
253/// assert!(v.run("hi").is_err());
254/// ```
255pub struct Validator(TextInputValidator);
256
257impl Validator {
258 /// Wrap a closure as a [`Validator`].
259 ///
260 /// The closure may capture state (it is `Box<dyn Fn>`, not a function
261 /// pointer).
262 pub fn new(f: impl Fn(&str) -> Result<(), String> + 'static) -> Self {
263 Self(Box::new(f))
264 }
265
266 /// Run the validator against `value`, returning its `Err` message on
267 /// failure.
268 pub fn run(&self, value: &str) -> Result<(), String> {
269 (self.0)(value)
270 }
271}
272
273impl std::fmt::Debug for Validator {
274 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
275 f.write_str("Validator(<fn>)")
276 }
277}
278
279/// One in-flight asynchronous field validation.
280///
281/// Created by [`FormField::validate_async`] and polled each frame by
282/// [`Context::form_field`](crate::Context::form_field) (or directly via
283/// [`FormField::poll_async`]). Gated behind the `async` feature.
284#[cfg(feature = "async")]
285#[cfg_attr(docsrs, doc(cfg(feature = "async")))]
286pub struct AsyncValidation {
287 rx: tokio::sync::oneshot::Receiver<Result<(), String>>,
288}
289
290#[cfg(feature = "async")]
291impl std::fmt::Debug for AsyncValidation {
292 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
293 f.write_str("AsyncValidation(<pending>)")
294 }
295}
296
297/// When [`Context::form_field`](crate::Context::form_field) runs a field's
298/// validators.
299///
300/// Defaults to [`OnBlur`](ValidateTrigger::OnBlur), matching the behavior of
301/// `huh` and `bubbles/textinput`.
302///
303/// # Example
304///
305/// ```no_run
306/// # use slt::widgets::{FormField, ValidateTrigger, validators};
307/// let field = FormField::new("Email")
308/// .validate(validators::email())
309/// .on_change(); // validate as the user types
310/// assert_eq!(field.trigger, ValidateTrigger::OnChange);
311/// ```
312#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
313pub enum ValidateTrigger {
314 /// Validate on every value change (each keystroke).
315 OnChange,
316 /// Validate when the field loses focus. The default.
317 #[default]
318 OnBlur,
319 /// Never auto-validate; the app calls
320 /// [`FormState::validate_all`] or [`FormField::run_validators`] manually.
321 Manual,
322}
323
324/// A single form field with a label, an input, and its own validators.
325///
326/// Attach validators with the chainable [`validate`](Self::validate) builder
327/// (multiple allowed); choose when they run with [`on_change`](Self::on_change)
328/// / [`on_blur`](Self::on_blur). [`Context::form_field`](crate::Context::form_field)
329/// runs them automatically per [`trigger`](Self::trigger).
330///
331/// # Example
332///
333/// ```no_run
334/// # use slt::widgets::{FormField, validators};
335/// let field = FormField::new("Email")
336/// .placeholder("you@example.com")
337/// .validate(validators::required("required"))
338/// .validate(validators::email());
339/// # let _ = field;
340/// ```
341#[derive(Debug, Default)]
342pub struct FormField {
343 /// Field label shown above the input.
344 pub label: String,
345 /// Text input state for this field.
346 pub input: TextInputState,
347 /// Validation error shown below the input when present.
348 pub error: Option<String>,
349 /// When the field's validators run. Defaults to
350 /// [`ValidateTrigger::OnBlur`].
351 pub trigger: ValidateTrigger,
352 /// This field's validators. Mutate via [`validate`](Self::validate); run
353 /// via [`run_validators`](Self::run_validators).
354 validators: Vec<Validator>,
355 /// Whether the field's input held keyboard focus on the previous frame.
356 ///
357 /// [`Context::form_field`](crate::Context::form_field) uses the
358 /// focused → unfocused edge to detect blur for
359 /// [`ValidateTrigger::OnBlur`]. This is tracked here (rather than read from
360 /// the input's [`Response`]) because the `text_input` Response does not yet
361 /// carry the `lost_focus` signal on its container-assembled response.
362 was_focused: bool,
363 /// One in-flight async validation, if any. Polled each frame by
364 /// [`Context::form_field`](crate::Context::form_field).
365 #[cfg(feature = "async")]
366 pending: Option<AsyncValidation>,
367}
368
369impl FormField {
370 /// Create a new form field with the given label.
371 pub fn new(label: impl Into<String>) -> Self {
372 Self {
373 label: label.into(),
374 input: TextInputState::new(),
375 error: None,
376 trigger: ValidateTrigger::default(),
377 validators: Vec::new(),
378 was_focused: false,
379 #[cfg(feature = "async")]
380 pending: None,
381 }
382 }
383
384 /// Set placeholder text for this field's input.
385 pub fn placeholder(mut self, p: impl Into<String>) -> Self {
386 self.input.placeholder = p.into();
387 self
388 }
389
390 /// Attach a validator closure (chainable; call multiple times to stack
391 /// validators — the first failure becomes the field error).
392 ///
393 /// The closure may capture state, unlike the deprecated positional
394 /// [`FormValidator`]. Built-ins live in
395 /// [`validators`].
396 ///
397 /// # Example
398 ///
399 /// ```no_run
400 /// # use slt::widgets::{FormField, validators};
401 /// let field = FormField::new("Name")
402 /// .validate(validators::required("required"))
403 /// .validate(validators::max_len(50, "too long"));
404 /// # let _ = field;
405 /// ```
406 pub fn validate(mut self, f: impl Fn(&str) -> Result<(), String> + 'static) -> Self {
407 self.validators.push(Validator::new(f));
408 self
409 }
410
411 /// Run this field's validators on every change (each keystroke).
412 pub fn on_change(mut self) -> Self {
413 self.trigger = ValidateTrigger::OnChange;
414 self
415 }
416
417 /// Run this field's validators when it loses focus (the default).
418 pub fn on_blur(mut self) -> Self {
419 self.trigger = ValidateTrigger::OnBlur;
420 self
421 }
422
423 /// Disable automatic validation; the app must call
424 /// [`run_validators`](Self::run_validators) or
425 /// [`FormState::validate_all`] explicitly.
426 pub fn manual(mut self) -> Self {
427 self.trigger = ValidateTrigger::Manual;
428 self
429 }
430
431 /// Number of validators attached to this field.
432 pub fn validator_count(&self) -> usize {
433 self.validators.len()
434 }
435
436 /// Run this field's validators now, setting [`error`](Self::error) to the
437 /// first failure (or clearing it on success).
438 ///
439 /// Returns `true` when the field is valid.
440 ///
441 /// # Example
442 ///
443 /// ```no_run
444 /// # use slt::widgets::{FormField, validators};
445 /// let mut field = FormField::new("Name").validate(validators::required("required"));
446 /// assert!(!field.run_validators()); // empty -> error
447 /// field.input.value = "Jane".into();
448 /// assert!(field.run_validators()); // non-empty -> ok
449 /// ```
450 pub fn run_validators(&mut self) -> bool {
451 self.error = self
452 .validators
453 .iter()
454 .find_map(|v| v.run(&self.input.value).err());
455 self.error.is_none()
456 }
457
458 /// Update the tracked focus edge and report whether the field *just* lost
459 /// focus this frame (a focused → unfocused transition).
460 ///
461 /// Called by [`Context::form_field`](crate::Context::form_field) each frame
462 /// with the input's current focus state. Kept crate-internal: blur
463 /// detection is an implementation detail of the form-field trigger plumbing.
464 pub(crate) fn observe_focus(&mut self, focused: bool) -> bool {
465 let lost = self.was_focused && !focused;
466 self.was_focused = focused;
467 lost
468 }
469
470 /// Spawn an asynchronous validation of the current value, replacing any
471 /// previously pending check.
472 ///
473 /// The future runs on the ambient tokio runtime; its `Result` is surfaced
474 /// as [`error`](Self::error) once [`poll_async`](Self::poll_async) (called
475 /// each frame by [`Context::form_field`](crate::Context::form_field)) sees
476 /// it complete.
477 ///
478 /// Requires the `async` feature.
479 ///
480 /// # Example
481 ///
482 /// ```no_run
483 /// # #[cfg(feature = "async")]
484 /// # async fn ex(field: &mut slt::widgets::FormField) {
485 /// let value = field.input.value.clone();
486 /// field.validate_async(async move {
487 /// // e.g. hit a "username taken?" endpoint
488 /// if value == "taken" { Err("already taken".into()) } else { Ok(()) }
489 /// });
490 /// # }
491 /// ```
492 #[cfg(feature = "async")]
493 #[cfg_attr(docsrs, doc(cfg(feature = "async")))]
494 pub fn validate_async<F>(&mut self, future: F)
495 where
496 F: std::future::Future<Output = Result<(), String>> + Send + 'static,
497 {
498 let (tx, rx) = tokio::sync::oneshot::channel();
499 tokio::spawn(async move {
500 let result = future.await;
501 let _ = tx.send(result);
502 });
503 self.pending = Some(AsyncValidation { rx });
504 }
505
506 /// Whether an async validation is currently in flight.
507 ///
508 /// Requires the `async` feature.
509 #[cfg(feature = "async")]
510 #[cfg_attr(docsrs, doc(cfg(feature = "async")))]
511 pub fn is_validating(&self) -> bool {
512 self.pending.is_some()
513 }
514
515 /// Poll the in-flight async validation (if any) without blocking.
516 ///
517 /// When the future has resolved, its result is written to
518 /// [`error`](Self::error) and the pending slot is cleared. Returns `true`
519 /// when a result was just applied this call.
520 ///
521 /// Requires the `async` feature.
522 #[cfg(feature = "async")]
523 #[cfg_attr(docsrs, doc(cfg(feature = "async")))]
524 pub fn poll_async(&mut self) -> bool {
525 use tokio::sync::oneshot::error::TryRecvError;
526 let Some(pending) = self.pending.as_mut() else {
527 return false;
528 };
529 match pending.rx.try_recv() {
530 Ok(result) => {
531 self.error = result.err();
532 self.pending = None;
533 true
534 }
535 Err(TryRecvError::Empty) => false,
536 Err(TryRecvError::Closed) => {
537 // Sender dropped without sending — treat as resolved (no error
538 // to surface) and clear the stuck pending slot.
539 self.pending = None;
540 true
541 }
542 }
543 }
544}
545
546/// State for a form with multiple fields.
547#[derive(Debug)]
548pub struct FormState {
549 /// Ordered list of form fields.
550 pub fields: Vec<FormField>,
551 /// Whether the form has been successfully submitted.
552 pub submitted: bool,
553}
554
555impl FormState {
556 /// Create an empty form state.
557 pub fn new() -> Self {
558 Self {
559 fields: Vec::new(),
560 submitted: false,
561 }
562 }
563
564 /// Add a field and return the updated form for chaining.
565 pub fn field(mut self, field: FormField) -> Self {
566 self.fields.push(field);
567 self
568 }
569
570 /// Whether the form is currently valid — no field holds an error.
571 ///
572 /// Reflects the last run of each field's validators (auto-triggered by
573 /// [`Context::form_field`](crate::Context::form_field) or run explicitly via
574 /// [`validate_all`](Self::validate_all)). It does not re-run validation.
575 ///
576 /// # Example
577 ///
578 /// ```no_run
579 /// # use slt::widgets::{FormField, FormState, validators};
580 /// let mut form = FormState::new().field(FormField::new("Name").validate(validators::required("required")));
581 /// assert!(form.is_valid()); // no validation run yet
582 /// form.validate_all();
583 /// assert!(!form.is_valid()); // empty Name failed
584 /// ```
585 pub fn is_valid(&self) -> bool {
586 self.fields.iter().all(|f| f.error.is_none())
587 }
588
589 /// Collect every current field error as `(field_index, message)` pairs.
590 ///
591 /// # Example
592 ///
593 /// ```no_run
594 /// # use slt::widgets::{FormField, FormState, validators};
595 /// let mut form = FormState::new().field(FormField::new("Name").validate(validators::required("required")));
596 /// form.validate_all();
597 /// assert_eq!(form.errors(), vec![(0, "required")]);
598 /// ```
599 pub fn errors(&self) -> Vec<(usize, &str)> {
600 self.fields
601 .iter()
602 .enumerate()
603 .filter_map(|(i, f)| f.error.as_deref().map(|e| (i, e)))
604 .collect()
605 }
606
607 /// Run every field's own validators, returning `true` when all pass.
608 ///
609 /// This is the replacement for the deprecated positional
610 /// [`validate`](Self::validate) — validators are co-located with their
611 /// fields, so there is no index slice to misalign.
612 ///
613 /// # Example
614 ///
615 /// ```no_run
616 /// # use slt::widgets::{FormField, FormState, validators};
617 /// let mut form = FormState::new()
618 /// .field(FormField::new("Email").validate(validators::email()));
619 /// let ok = form.validate_all();
620 /// # let _ = ok;
621 /// ```
622 pub fn validate_all(&mut self) -> bool {
623 let mut ok = true;
624 for field in &mut self.fields {
625 ok &= field.run_validators();
626 }
627 ok
628 }
629
630 /// Apply cross-field validation rules.
631 ///
632 /// The closure receives the whole form and returns `(field_index, message)`
633 /// pairs; each pair sets that field's [`error`](FormField::error). Returns
634 /// `true` when the closure reports no errors. Useful for rules like
635 /// "confirm password must match password".
636 ///
637 /// # Example
638 ///
639 /// ```no_run
640 /// # use slt::widgets::{FormField, FormState};
641 /// let mut form = FormState::new()
642 /// .field(FormField::new("Password"))
643 /// .field(FormField::new("Confirm"));
644 /// let ok = form.validate_with(|f| {
645 /// if f.value(0) != f.value(1) {
646 /// vec![(1, "passwords must match".to_string())]
647 /// } else {
648 /// vec![]
649 /// }
650 /// });
651 /// # let _ = ok;
652 /// ```
653 pub fn validate_with(
654 &mut self,
655 f: impl Fn(&FormState) -> Vec<(usize, String)>,
656 ) -> bool {
657 let extra = f(self);
658 for (i, msg) in &extra {
659 if let Some(field) = self.fields.get_mut(*i) {
660 field.error = Some(msg.clone());
661 }
662 }
663 extra.is_empty()
664 }
665
666 /// Validate all fields with a positional slice of function-pointer
667 /// validators.
668 ///
669 /// Returns `true` when all validations pass. A field whose index has no
670 /// matching validator is silently skipped.
671 #[deprecated(
672 since = "0.21.0",
673 note = "Attach validators per-field via FormField::validate and call validate_all(); positional slices misalign silently."
674 )]
675 pub fn validate(&mut self, validators: &[FormValidator]) -> bool {
676 let mut all_valid = true;
677 for (i, field) in self.fields.iter_mut().enumerate() {
678 if let Some(validator) = validators.get(i) {
679 match validator(&field.input.value) {
680 Ok(()) => field.error = None,
681 Err(msg) => {
682 field.error = Some(msg);
683 all_valid = false;
684 }
685 }
686 }
687 }
688 all_valid
689 }
690
691 /// Get field value by index.
692 pub fn value(&self, index: usize) -> &str {
693 self.fields
694 .get(index)
695 .map(|f| f.input.value.as_str())
696 .unwrap_or("")
697 }
698}
699
700impl Default for FormState {
701 fn default() -> Self {
702 Self::new()
703 }
704}
705
706/// State for toast notification display.
707///
708/// Add messages with [`ToastState::info`], [`ToastState::success`],
709/// [`ToastState::warning`], or [`ToastState::error`], then pass the state to
710/// `Context::toast` each frame. Expired messages are removed automatically.
711#[derive(Debug, Clone)]
712pub struct ToastState {
713 /// Active toast messages, ordered oldest-first.
714 pub messages: Vec<ToastMessage>,
715}
716
717/// A single toast notification message.
718#[derive(Debug, Clone)]
719pub struct ToastMessage {
720 /// The text content of the notification.
721 pub text: String,
722 /// Severity level, used to choose the display color.
723 pub level: ToastLevel,
724 /// The tick at which this message was created.
725 pub created_tick: u64,
726 /// How many ticks the message remains visible.
727 pub duration_ticks: u64,
728}
729
730impl Default for ToastMessage {
731 fn default() -> Self {
732 Self {
733 text: String::new(),
734 level: ToastLevel::Info,
735 created_tick: 0,
736 duration_ticks: 30,
737 }
738 }
739}
740
741/// Severity level for a [`ToastMessage`].
742#[derive(Debug, Clone, Copy, PartialEq, Eq)]
743pub enum ToastLevel {
744 /// Informational message (primary color).
745 Info,
746 /// Success message (success color).
747 Success,
748 /// Warning message (warning color).
749 Warning,
750 /// Error message (error color).
751 Error,
752}
753
754/// Severity level for alert widgets.
755#[non_exhaustive]
756#[derive(Debug, Clone, Copy, PartialEq, Eq)]
757pub enum AlertLevel {
758 /// Informational alert.
759 Info,
760 /// Success alert.
761 Success,
762 /// Warning alert.
763 Warning,
764 /// Error alert.
765 Error,
766}
767
768impl ToastState {
769 /// Create an empty toast state with no messages.
770 pub fn new() -> Self {
771 Self {
772 messages: Vec::new(),
773 }
774 }
775
776 /// Push an informational toast visible for 30 ticks.
777 pub fn info(&mut self, text: impl Into<String>, tick: u64) {
778 self.push(text, ToastLevel::Info, tick, 30);
779 }
780
781 /// Push a success toast visible for 30 ticks.
782 pub fn success(&mut self, text: impl Into<String>, tick: u64) {
783 self.push(text, ToastLevel::Success, tick, 30);
784 }
785
786 /// Push a warning toast visible for 50 ticks.
787 pub fn warning(&mut self, text: impl Into<String>, tick: u64) {
788 self.push(text, ToastLevel::Warning, tick, 50);
789 }
790
791 /// Push an error toast visible for 80 ticks.
792 pub fn error(&mut self, text: impl Into<String>, tick: u64) {
793 self.push(text, ToastLevel::Error, tick, 80);
794 }
795
796 /// Push a toast with a custom level and duration.
797 pub fn push(
798 &mut self,
799 text: impl Into<String>,
800 level: ToastLevel,
801 tick: u64,
802 duration_ticks: u64,
803 ) {
804 self.messages.push(ToastMessage {
805 text: text.into(),
806 level,
807 created_tick: tick,
808 duration_ticks,
809 });
810 }
811
812 /// Remove all messages whose display duration has elapsed.
813 ///
814 /// Called automatically by `Context::toast` before rendering.
815 pub fn cleanup(&mut self, current_tick: u64) {
816 self.messages.retain(|message| {
817 current_tick < message.created_tick.saturating_add(message.duration_ticks)
818 });
819 }
820}
821
822impl Default for ToastState {
823 fn default() -> Self {
824 Self::new()
825 }
826}
827
828/// Default maximum number of [`TextareaSnapshot`] entries kept in
829/// [`TextareaState::history`]. Used by [`TextareaState::new`] and the
830/// `Default` impl. Override per-instance via
831/// [`TextareaState::history_max`].
832pub(crate) const DEFAULT_TEXTAREA_HISTORY_MAX: usize = 100;
833
834/// Snapshot of textarea content + cursor for the undo/redo history stack.
835///
836/// One snapshot is pushed before every destructive mutation (char insert,
837/// delete, Enter, Backspace, paste). `Ctrl+Z` walks the index backward to a
838/// previous snapshot; `Ctrl+Y` walks it forward.
839///
840/// Crate-internal — the `pub(crate)` visibility keeps the history layout an
841/// implementation detail. Inspect via the public undo/redo behavior instead.
842#[derive(Debug, Clone)]
843pub(crate) struct TextareaSnapshot {
844 /// Lines of text at the time of the snapshot.
845 pub(crate) lines: Vec<String>,
846 /// Cursor row at the time of the snapshot.
847 pub(crate) cursor_row: usize,
848 /// Cursor column at the time of the snapshot.
849 pub(crate) cursor_col: usize,
850}
851
852/// State for a multi-line text area widget.
853///
854/// Pass a mutable reference to `Context::textarea` each frame along with the
855/// number of visible rows. The widget handles all keyboard events when focused.
856///
857/// # Undo / redo
858///
859/// `Ctrl+Z` undoes the most recent edit and `Ctrl+Y` redoes it. The widget
860/// pushes a snapshot before every destructive mutation (char insert, delete,
861/// Enter, Backspace, paste). Rapid character typing coalesces into a single
862/// undoable batch — only the first char of a typing burst pushes a snapshot.
863/// History is capped at [`history_max`](Self::history_max) entries (default
864/// `100`); the oldest snapshot is dropped when the cap is exceeded.
865///
866/// # Example
867///
868/// ```no_run
869/// # use slt::widgets::TextareaState;
870/// # slt::run(|ui: &mut slt::Context| {
871/// let mut state = TextareaState::new();
872/// // Type, then press Ctrl+Z to undo or Ctrl+Y to redo.
873/// ui.textarea(&mut state, 5);
874/// # });
875/// ```
876#[derive(Debug, Clone)]
877pub struct TextareaState {
878 /// The lines of text, one entry per line.
879 pub lines: Vec<String>,
880 /// Row index of the cursor (0-based, logical line).
881 pub cursor_row: usize,
882 /// Column index of the cursor within the current row (character index).
883 pub cursor_col: usize,
884 /// Maximum total character count across all lines.
885 pub max_length: Option<usize>,
886 /// When set, lines longer than this display-column width are soft-wrapped.
887 pub wrap_width: Option<u32>,
888 /// First visible visual line (managed internally by `textarea()`).
889 pub scroll_offset: usize,
890 /// Undo/redo snapshot stack. Newest entry is at the tip; the index walks
891 /// backward on `Ctrl+Z` and forward on `Ctrl+Y`.
892 pub(crate) history: Vec<TextareaSnapshot>,
893 /// Pointer into [`history`](Self::history) for the next undo target.
894 pub(crate) history_index: usize,
895 /// Maximum [`history`](Self::history) length before the oldest snapshot is
896 /// evicted. Defaults to [`DEFAULT_TEXTAREA_HISTORY_MAX`].
897 pub(crate) history_max: usize,
898 /// Whether the previous keypress was a `Char` insert. Used to coalesce
899 /// rapid typing into a single undoable burst — when true, the next `Char`
900 /// keypress does not push a snapshot.
901 pub(crate) last_was_char_insert: bool,
902}
903
904impl TextareaState {
905 /// Create an empty text area state with one blank line.
906 pub fn new() -> Self {
907 Self {
908 lines: vec![String::new()],
909 cursor_row: 0,
910 cursor_col: 0,
911 max_length: None,
912 wrap_width: None,
913 scroll_offset: 0,
914 history: Vec::new(),
915 history_index: 0,
916 history_max: DEFAULT_TEXTAREA_HISTORY_MAX,
917 last_was_char_insert: false,
918 }
919 }
920
921 /// Return all lines joined with newline characters.
922 pub fn value(&self) -> String {
923 self.lines.join("\n")
924 }
925
926 /// Replace the content with the given text, splitting on newlines.
927 ///
928 /// Resets the cursor to the beginning of the first line and clears the
929 /// undo history — programmatic replacement is treated as a fresh state,
930 /// not an undoable edit.
931 pub fn set_value(&mut self, text: impl Into<String>) {
932 let value = text.into();
933 self.lines = value.split('\n').map(str::to_string).collect();
934 if self.lines.is_empty() {
935 self.lines.push(String::new());
936 }
937 self.cursor_row = 0;
938 self.cursor_col = 0;
939 self.scroll_offset = 0;
940 self.history.clear();
941 self.history_index = 0;
942 self.last_was_char_insert = false;
943 }
944
945 /// Set the maximum allowed total character count.
946 pub fn max_length(mut self, len: usize) -> Self {
947 self.max_length = Some(len);
948 self
949 }
950
951 /// Enable soft word-wrap at the given display-column width.
952 pub fn word_wrap(mut self, width: u32) -> Self {
953 self.wrap_width = Some(width);
954 self
955 }
956
957 /// Override the maximum number of undo snapshots kept (default `100`).
958 ///
959 /// When the history exceeds this cap the oldest snapshot is dropped.
960 /// Setting `0` disables undo recording — the field is read every keypress.
961 pub fn history_max(mut self, cap: usize) -> Self {
962 self.history_max = cap;
963 self
964 }
965
966 /// Number of undo snapshots currently retained.
967 ///
968 /// Read-only — useful for tests and debugging the history cap. The cap
969 /// itself is set via [`history_max`](Self::history_max).
970 pub fn history_len(&self) -> usize {
971 self.history.len()
972 }
973
974 /// Maximum number of undo snapshots retained.
975 ///
976 /// Mirrors [`history_max`](Self::history_max) (the builder setter) but as
977 /// a getter — useful for tests asserting the cap stays bounded.
978 pub fn history_cap(&self) -> usize {
979 self.history_max
980 }
981
982 /// Push a snapshot of the current content + cursor onto the undo stack.
983 ///
984 /// Truncates any redo tail beyond `history_index`, appends the snapshot,
985 /// and caps the stack at [`history_max`](Self::history_max) by dropping the
986 /// oldest entry. `history_index` is left pointing one past the newest
987 /// snapshot so the next `Ctrl+Z` returns to the just-pushed state.
988 pub(crate) fn push_history(&mut self) {
989 if self.history_max == 0 {
990 return;
991 }
992 // Drop any redo tail — a fresh edit invalidates the redo branch.
993 if self.history_index < self.history.len() {
994 self.history.truncate(self.history_index);
995 }
996 self.history.push(TextareaSnapshot {
997 lines: self.lines.clone(),
998 cursor_row: self.cursor_row,
999 cursor_col: self.cursor_col,
1000 });
1001 // Evict oldest when over the cap. `Vec::remove(0)` is O(n) but the
1002 // history cap is small (default 100) and this only runs at the cap
1003 // boundary, so the cost is bounded.
1004 while self.history.len() > self.history_max {
1005 self.history.remove(0);
1006 }
1007 self.history_index = self.history.len();
1008 }
1009
1010 /// Walk the undo index back one step and apply the snapshot.
1011 ///
1012 /// No-op when the history is empty or already at the start. Returns `true`
1013 /// when a snapshot was applied.
1014 pub(crate) fn undo(&mut self) -> bool {
1015 if self.history.is_empty() || self.history_index == 0 {
1016 return false;
1017 }
1018 // First Ctrl+Z after edits captures the current (unsaved) tip so the
1019 // user can redo back to it; subsequent presses walk down the stack.
1020 if self.history_index == self.history.len() {
1021 self.history.push(TextareaSnapshot {
1022 lines: self.lines.clone(),
1023 cursor_row: self.cursor_row,
1024 cursor_col: self.cursor_col,
1025 });
1026 }
1027 self.history_index -= 1;
1028 let snap = &self.history[self.history_index];
1029 self.lines = snap.lines.clone();
1030 self.cursor_row = snap.cursor_row;
1031 self.cursor_col = snap.cursor_col;
1032 true
1033 }
1034
1035 /// Walk the undo index forward one step and apply the snapshot.
1036 ///
1037 /// No-op when already at the redo tip. Returns `true` when a snapshot was
1038 /// applied.
1039 pub(crate) fn redo(&mut self) -> bool {
1040 if self.history_index + 1 >= self.history.len() {
1041 return false;
1042 }
1043 self.history_index += 1;
1044 let snap = &self.history[self.history_index];
1045 self.lines = snap.lines.clone();
1046 self.cursor_row = snap.cursor_row;
1047 self.cursor_col = snap.cursor_col;
1048 true
1049 }
1050}
1051
1052impl Default for TextareaState {
1053 fn default() -> Self {
1054 Self::new()
1055 }
1056}
1057
1058/// Named throbber preset for [`SpinnerState`].
1059///
1060/// Each variant maps to a fixed frame sequence (parity with the common
1061/// `cli-spinners` / `ratatui-throbber` sets). Construct a spinner from a preset
1062/// with [`SpinnerState::preset`], or use the matching named constructor such as
1063/// [`SpinnerState::moon`].
1064///
1065/// # Example
1066///
1067/// ```
1068/// # use slt::widgets::{SpinnerState, SpinnerPreset};
1069/// let s = SpinnerState::preset(SpinnerPreset::Arrow);
1070/// assert_eq!(s, SpinnerState::arrow());
1071/// ```
1072///
1073/// Available since `0.21.1`.
1074#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
1075pub enum SpinnerPreset {
1076 /// Braille dots: `⠋ ⠙ ⠹ ⠸ ⠼ ⠴ ⠦ ⠧ ⠇ ⠏`.
1077 Dots,
1078 /// ASCII line: `| / - \`.
1079 Line,
1080 /// Moon phases: `🌑 🌒 🌓 🌔 🌕 🌖 🌗 🌘`.
1081 Moon,
1082 /// Bouncing bar between brackets: `(● )` … `( ●)` and back.
1083 Bounce,
1084 /// Quarter-circle arc: `◜ ◠ ◝ ◞ ◡ ◟`.
1085 Circle,
1086 /// Travelling braille dot: `⠁ ⠂ ⠄ ⡀ ⢀ ⠠ ⠐ ⠈`.
1087 Points,
1088 /// Half-circle arc: `◜ ◠ ◝ ◞ ◡ ◟`.
1089 Arc,
1090 /// Toggle pulse: `⊶ ⊷`.
1091 Toggle,
1092 /// Clockwise arrow: `← ↖ ↑ ↗ → ↘ ↓ ↙`.
1093 Arrow,
1094}
1095
1096/// State for an animated spinner widget.
1097///
1098/// Create with a named constructor such as [`SpinnerState::dots`] or
1099/// [`SpinnerState::line`] (or from a [`SpinnerPreset`] via
1100/// [`SpinnerState::preset`]), then pass to `Context::spinner` each frame. The
1101/// frame advances automatically with the tick counter.
1102#[derive(Debug, Clone, PartialEq, Eq)]
1103pub struct SpinnerState {
1104 chars: &'static [char],
1105}
1106
1107static DOTS_CHARS: &[char] = &['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'];
1108static LINE_CHARS: &[char] = &['|', '/', '-', '\\'];
1109static MOON_CHARS: &[char] = &['🌑', '🌒', '🌓', '🌔', '🌕', '🌖', '🌗', '🌘'];
1110static BOUNCE_CHARS: &[char] = &['⠁', '⠂', '⠄', '⠂'];
1111static CIRCLE_CHARS: &[char] = &['◜', '◠', '◝', '◞', '◡', '◟'];
1112static POINTS_CHARS: &[char] = &['⠁', '⠂', '⠄', '⡀', '⢀', '⠠', '⠐', '⠈'];
1113static ARC_CHARS: &[char] = &['◜', '◠', '◝', '◞', '◡', '◟'];
1114static TOGGLE_CHARS: &[char] = &['⊶', '⊷'];
1115static ARROW_CHARS: &[char] = &['←', '↖', '↑', '↗', '→', '↘', '↓', '↙'];
1116
1117impl SpinnerState {
1118 /// Create a dots-style spinner using braille characters.
1119 ///
1120 /// Cycles through: `⠋ ⠙ ⠹ ⠸ ⠼ ⠴ ⠦ ⠧ ⠇ ⠏`
1121 pub fn dots() -> Self {
1122 Self { chars: DOTS_CHARS }
1123 }
1124
1125 /// Create a line-style spinner using ASCII characters.
1126 ///
1127 /// Cycles through: `| / - \`
1128 pub fn line() -> Self {
1129 Self { chars: LINE_CHARS }
1130 }
1131
1132 /// Create a moon-phase spinner.
1133 ///
1134 /// Cycles through: `🌑 🌒 🌓 🌔 🌕 🌖 🌗 🌘`
1135 ///
1136 /// Available since `0.21.1`.
1137 pub fn moon() -> Self {
1138 Self { chars: MOON_CHARS }
1139 }
1140
1141 /// Create a bouncing single-dot spinner.
1142 ///
1143 /// Cycles through `⠁ ⠂ ⠄ ⠂`, giving a dot that rises and falls in place.
1144 ///
1145 /// Available since `0.21.1`.
1146 pub fn bounce() -> Self {
1147 Self {
1148 chars: BOUNCE_CHARS,
1149 }
1150 }
1151
1152 /// Create a quarter-circle arc spinner.
1153 ///
1154 /// Cycles through: `◜ ◠ ◝ ◞ ◡ ◟`
1155 ///
1156 /// Available since `0.21.1`.
1157 pub fn circle() -> Self {
1158 Self {
1159 chars: CIRCLE_CHARS,
1160 }
1161 }
1162
1163 /// Create a travelling braille-dot ("points") spinner.
1164 ///
1165 /// Cycles through: `⠁ ⠂ ⠄ ⡀ ⢀ ⠠ ⠐ ⠈`
1166 ///
1167 /// Available since `0.21.1`.
1168 pub fn points() -> Self {
1169 Self {
1170 chars: POINTS_CHARS,
1171 }
1172 }
1173
1174 /// Create a half-circle arc spinner.
1175 ///
1176 /// Cycles through: `◜ ◠ ◝ ◞ ◡ ◟`
1177 ///
1178 /// Available since `0.21.1`.
1179 pub fn arc() -> Self {
1180 Self { chars: ARC_CHARS }
1181 }
1182
1183 /// Create a two-frame toggle/pulse spinner.
1184 ///
1185 /// Cycles through: `⊶ ⊷`
1186 ///
1187 /// Available since `0.21.1`.
1188 pub fn toggle() -> Self {
1189 Self {
1190 chars: TOGGLE_CHARS,
1191 }
1192 }
1193
1194 /// Create a rotating-arrow spinner.
1195 ///
1196 /// Cycles clockwise through: `← ↖ ↑ ↗ → ↘ ↓ ↙`
1197 ///
1198 /// Available since `0.21.1`.
1199 pub fn arrow() -> Self {
1200 Self { chars: ARROW_CHARS }
1201 }
1202
1203 /// Create a spinner from a named [`SpinnerPreset`].
1204 ///
1205 /// Equivalent to calling the matching named constructor.
1206 ///
1207 /// # Example
1208 ///
1209 /// ```
1210 /// # use slt::widgets::{SpinnerState, SpinnerPreset};
1211 /// let s = SpinnerState::preset(SpinnerPreset::Moon);
1212 /// assert_eq!(s, SpinnerState::moon());
1213 /// ```
1214 ///
1215 /// Available since `0.21.1`.
1216 pub fn preset(preset: SpinnerPreset) -> Self {
1217 match preset {
1218 SpinnerPreset::Dots => Self::dots(),
1219 SpinnerPreset::Line => Self::line(),
1220 SpinnerPreset::Moon => Self::moon(),
1221 SpinnerPreset::Bounce => Self::bounce(),
1222 SpinnerPreset::Circle => Self::circle(),
1223 SpinnerPreset::Points => Self::points(),
1224 SpinnerPreset::Arc => Self::arc(),
1225 SpinnerPreset::Toggle => Self::toggle(),
1226 SpinnerPreset::Arrow => Self::arrow(),
1227 }
1228 }
1229
1230 /// Number of distinct frames in this spinner's cycle.
1231 ///
1232 /// Useful for tests and for detecting wrap-around.
1233 ///
1234 /// # Example
1235 ///
1236 /// ```
1237 /// # use slt::widgets::SpinnerState;
1238 /// assert_eq!(SpinnerState::line().frame_count(), 4);
1239 /// ```
1240 ///
1241 /// Available since `0.21.1`.
1242 pub fn frame_count(&self) -> usize {
1243 self.chars.len()
1244 }
1245
1246 /// Return the spinner character for the given tick.
1247 pub fn frame(&self, tick: u64) -> char {
1248 if self.chars.is_empty() {
1249 return ' ';
1250 }
1251 self.chars[tick as usize % self.chars.len()]
1252 }
1253}
1254
1255impl Default for SpinnerState {
1256 fn default() -> Self {
1257 Self::dots()
1258 }
1259}
1260
1261/// State for a numeric stepper field (clamp + step, integer or float).
1262///
1263/// A numeric stepper renders the value as an editable field with `▾`/`▴`
1264/// affordances. When focused it adjusts via Up/Down (or `k`/`j`) and the scroll
1265/// wheel, or the user can type a value directly and press `Enter` to commit it.
1266/// The committed [`value`](NumberInputState::value) is always clamped into
1267/// `[min, max]` (and rounded to a whole number in integer mode).
1268///
1269/// Create with [`NumberInputState::new`] (float) or
1270/// [`NumberInputState::integer`], then pass to
1271/// [`Context::number_input`](crate::Context::number_input) each frame.
1272///
1273/// # Example
1274///
1275/// ```no_run
1276/// # use slt::widgets::NumberInputState;
1277/// # slt::run(|ui: &mut slt::Context| {
1278/// let mut qty = NumberInputState::integer(3, 0, 10).step(1.0);
1279/// let r = ui.number_input(&mut qty);
1280/// if r.changed {
1281/// // qty.value was adjusted this frame
1282/// }
1283/// # });
1284/// ```
1285///
1286/// Available since `0.21.0`.
1287#[derive(Debug, Clone)]
1288pub struct NumberInputState {
1289 /// Committed numeric value, always within `[min, max]`.
1290 pub value: f64,
1291 /// Inclusive lower bound.
1292 pub min: f64,
1293 /// Inclusive upper bound.
1294 pub max: f64,
1295 /// Increment applied per Up/Down/scroll tick.
1296 pub step: f64,
1297 /// When true, the value is whole-number only and rendered without a decimal point.
1298 pub integer: bool,
1299 /// In-progress typed text; `Some` while the user is editing the field.
1300 pub editing: Option<String>,
1301 /// Last parse failure from `Enter` on an invalid buffer, if any.
1302 pub parse_error: Option<String>,
1303}
1304
1305impl NumberInputState {
1306 /// Float stepper with the given starting value and inclusive range.
1307 ///
1308 /// `value` is clamped into `[min, max]` immediately. If `min > max` the two
1309 /// bounds are swapped so the range is always well-formed.
1310 ///
1311 /// # Example
1312 ///
1313 /// ```
1314 /// # use slt::widgets::NumberInputState;
1315 /// let s = NumberInputState::new(1.5, 0.0, 10.0);
1316 /// assert_eq!(s.value, 1.5);
1317 /// assert!(!s.integer);
1318 /// ```
1319 pub fn new(value: f64, min: f64, max: f64) -> Self {
1320 let (min, max) = if min <= max { (min, max) } else { (max, min) };
1321 Self {
1322 value: value.clamp(min, max),
1323 min,
1324 max,
1325 step: 1.0,
1326 integer: false,
1327 editing: None,
1328 parse_error: None,
1329 }
1330 }
1331
1332 /// Integer stepper (rounds value, renders without a decimal point).
1333 ///
1334 /// Convenience constructor that sets `integer = true` and a default step of
1335 /// `1.0`. `value` is clamped into `[min, max]`.
1336 ///
1337 /// # Example
1338 ///
1339 /// ```
1340 /// # use slt::widgets::NumberInputState;
1341 /// let s = NumberInputState::integer(42, 0, 100);
1342 /// assert_eq!(s.value, 42.0);
1343 /// assert!(s.integer);
1344 /// ```
1345 pub fn integer(value: i64, min: i64, max: i64) -> Self {
1346 let mut s = Self::new(value as f64, min as f64, max as f64);
1347 s.integer = true;
1348 s
1349 }
1350
1351 /// Set the per-tick increment (consumes self, builder style).
1352 ///
1353 /// Negative or zero steps are coerced to `0.0` (no adjustment).
1354 ///
1355 /// # Example
1356 ///
1357 /// ```
1358 /// # use slt::widgets::NumberInputState;
1359 /// let s = NumberInputState::new(0.0, 0.0, 1.0).step(0.1);
1360 /// assert!((s.step - 0.1).abs() < f64::EPSILON);
1361 /// ```
1362 pub fn step(mut self, step: f64) -> Self {
1363 self.step = step.max(0.0);
1364 self
1365 }
1366
1367 /// Clamp `value` into `[min, max]` (and round if `integer`).
1368 ///
1369 /// Used internally after every adjustment and typed commit, and exposed so
1370 /// callers that mutate [`value`](NumberInputState::value) directly can
1371 /// re-normalize it.
1372 ///
1373 /// # Example
1374 ///
1375 /// ```
1376 /// # use slt::widgets::NumberInputState;
1377 /// let mut s = NumberInputState::integer(0, 0, 10);
1378 /// s.value = 99.0;
1379 /// assert_eq!(s.clamped(), 10.0);
1380 /// s.value = 3.7;
1381 /// assert_eq!(s.clamped(), 4.0);
1382 /// ```
1383 pub fn clamped(&self) -> f64 {
1384 let v = self.value.clamp(self.min, self.max);
1385 if self.integer {
1386 v.round()
1387 } else {
1388 v
1389 }
1390 }
1391}
1392
1393impl Default for NumberInputState {
1394 fn default() -> Self {
1395 Self::new(0.0, 0.0, 100.0)
1396 }
1397}
1398
1399#[cfg(test)]
1400mod spinner_tests {
1401 use super::{SpinnerPreset, SpinnerState};
1402
1403 /// Collect one full cycle of frames for a spinner.
1404 fn cycle(s: &SpinnerState) -> Vec<char> {
1405 (0..s.frame_count() as u64).map(|t| s.frame(t)).collect()
1406 }
1407
1408 #[test]
1409 fn existing_presets_unchanged() {
1410 // dots() and line() must keep their historic sequences.
1411 assert_eq!(
1412 cycle(&SpinnerState::dots()),
1413 vec!['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏']
1414 );
1415 assert_eq!(cycle(&SpinnerState::line()), vec!['|', '/', '-', '\\']);
1416 // Default stays dots().
1417 assert_eq!(SpinnerState::default(), SpinnerState::dots());
1418 }
1419
1420 #[test]
1421 fn new_presets_have_expected_lengths() {
1422 assert_eq!(SpinnerState::dots().frame_count(), 10);
1423 assert_eq!(SpinnerState::line().frame_count(), 4);
1424 assert_eq!(SpinnerState::moon().frame_count(), 8);
1425 assert_eq!(SpinnerState::bounce().frame_count(), 4);
1426 assert_eq!(SpinnerState::circle().frame_count(), 6);
1427 assert_eq!(SpinnerState::points().frame_count(), 8);
1428 assert_eq!(SpinnerState::arc().frame_count(), 6);
1429 assert_eq!(SpinnerState::toggle().frame_count(), 2);
1430 assert_eq!(SpinnerState::arrow().frame_count(), 8);
1431 }
1432
1433 #[test]
1434 fn new_presets_yield_expected_sequences() {
1435 assert_eq!(
1436 cycle(&SpinnerState::moon()),
1437 vec!['🌑', '🌒', '🌓', '🌔', '🌕', '🌖', '🌗', '🌘']
1438 );
1439 assert_eq!(cycle(&SpinnerState::bounce()), vec!['⠁', '⠂', '⠄', '⠂']);
1440 assert_eq!(
1441 cycle(&SpinnerState::circle()),
1442 vec!['◜', '◠', '◝', '◞', '◡', '◟']
1443 );
1444 assert_eq!(
1445 cycle(&SpinnerState::points()),
1446 vec!['⠁', '⠂', '⠄', '⡀', '⢀', '⠠', '⠐', '⠈']
1447 );
1448 assert_eq!(
1449 cycle(&SpinnerState::arc()),
1450 vec!['◜', '◠', '◝', '◞', '◡', '◟']
1451 );
1452 assert_eq!(cycle(&SpinnerState::toggle()), vec!['⊶', '⊷']);
1453 assert_eq!(
1454 cycle(&SpinnerState::arrow()),
1455 vec!['←', '↖', '↑', '↗', '→', '↘', '↓', '↙']
1456 );
1457 }
1458
1459 #[test]
1460 fn frame_cycles_modulo_length() {
1461 let s = SpinnerState::arrow();
1462 let n = s.frame_count() as u64;
1463 // Tick 0 and one full revolution later yield the same frame.
1464 assert_eq!(s.frame(0), s.frame(n));
1465 assert_eq!(s.frame(1), s.frame(n + 1));
1466 // Wrap-around at the boundary.
1467 assert_eq!(s.frame(n - 1), '↙');
1468 assert_eq!(s.frame(n), '←');
1469 }
1470
1471 #[test]
1472 fn frame_advances_through_sequence() {
1473 let s = SpinnerState::toggle();
1474 assert_eq!(s.frame(0), '⊶');
1475 assert_eq!(s.frame(1), '⊷');
1476 assert_eq!(s.frame(2), '⊶');
1477 assert_eq!(s.frame(3), '⊷');
1478 }
1479
1480 #[test]
1481 fn preset_matches_named_constructor() {
1482 let cases = [
1483 (SpinnerPreset::Dots, SpinnerState::dots()),
1484 (SpinnerPreset::Line, SpinnerState::line()),
1485 (SpinnerPreset::Moon, SpinnerState::moon()),
1486 (SpinnerPreset::Bounce, SpinnerState::bounce()),
1487 (SpinnerPreset::Circle, SpinnerState::circle()),
1488 (SpinnerPreset::Points, SpinnerState::points()),
1489 (SpinnerPreset::Arc, SpinnerState::arc()),
1490 (SpinnerPreset::Toggle, SpinnerState::toggle()),
1491 (SpinnerPreset::Arrow, SpinnerState::arrow()),
1492 ];
1493 for (preset, expected) in cases {
1494 assert_eq!(SpinnerState::preset(preset), expected);
1495 }
1496 }
1497
1498 #[test]
1499 fn frame_handles_large_tick_without_panicking() {
1500 // Edge case: very large tick must wrap, not overflow/panic.
1501 let s = SpinnerState::moon();
1502 let n = s.frame_count() as u64;
1503 assert_eq!(s.frame(u64::MAX), s.frame(u64::MAX % n));
1504 }
1505}