tui-lipan 0.2.0

Opinionated, component-based TUI framework for Rust - declarative components, reconciliation, layout engine, focus, overlays, and rich widgets on top of ratatui.
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
//! Slider widget.

mod layout;
mod node;
mod reconcile;

pub use layout::measure_slider;
pub use node::SliderNode;
pub use reconcile::reconcile_slider;

use unicode_width::UnicodeWidthStr;

use crate::callback::{Callback, KeyHandler};
use crate::core::element::Element;
use crate::style::{Length, Padding, Style, StyleSlot};
use crate::utils::gradient::ColorGradient;

/// A slider for numeric selection.
#[derive(Clone)]
pub struct Slider {
    /// Current value.
    pub value: f64,
    /// Minimum value.
    pub min: f64,
    /// Maximum value.
    pub max: f64,
    /// Step size.
    pub step: f64,
    /// Callback when value changes.
    pub on_change: Option<Callback<f64>>,
    /// Callback when clicked.
    pub on_click: Option<Callback<f64>>,
    /// Focused key handler. Returning `true` consumes the key before default stepping.
    pub on_key: Option<KeyHandler>,
    /// Track style.
    pub style: Style,
    /// Style for the filled portion of the track.
    pub filled_track_style: Style,
    /// Optional gradient for the filled portion of the track (left -> right).
    pub filled_track_gradient: Option<ColorGradient>,
    /// Thumb style.
    pub thumb_style: Style,
    /// Optional gradient for thumb color based on current value.
    pub thumb_gradient: Option<ColorGradient>,
    /// Label text.
    pub label: Option<String>,
    /// Label style.
    pub label_style: Style,
    /// Whether to show value text.
    pub show_value: bool,
    /// Requested width.
    /// Default: `Length::Flex(1)`.
    pub width: Length,
    /// Requested height.
    /// Default: `Length::Px(1)`.
    pub height: Length,
    /// Padding.
    /// Default: `Padding::default()`.
    pub padding: Padding,
    /// Whether the slider is disabled.
    pub disabled: bool,
    /// Style when disabled.
    pub disabled_style: Style,
    /// Whether the slider is focusable.
    pub focusable: bool,
    /// Whether the slider participates in sequential focus navigation.
    pub tab_stop: bool,
    /// Callback fired when the slider receives focus.
    pub on_focus: Option<Callback<()>>,
    /// Callback fired when the slider loses focus.
    pub on_blur: Option<Callback<()>>,
    /// Hover style for the track.
    pub hover_style: StyleSlot,
    /// Style when focused.
    pub focus_style: StyleSlot,
    /// Thumb style when focused.
    pub focus_thumb_style: StyleSlot,
    /// Style for the thumb when hovered.
    pub hover_thumb_style: StyleSlot,
    /// Symbol for the thumb.
    pub thumb_symbol: String,
    /// Symbol for the unfilled track.
    pub track_symbol: String,
    /// Symbol for the filled track.
    pub filled_track_symbol: String,
    /// Symbol for the thumb when hovered.
    pub hover_thumb_symbol: Option<String>,
}

impl Slider {
    /// Create a new slider.
    pub fn new(value: f64) -> Self {
        Self {
            value,
            min: 0.0,
            max: 100.0,
            step: 1.0,
            on_change: None,
            on_click: None,
            on_key: None,
            style: Style::default(),
            filled_track_style: Style::default(),
            filled_track_gradient: None,
            thumb_style: Style::default(),
            thumb_gradient: None,
            label: None,
            label_style: Style::default(),
            show_value: true,
            width: Length::Flex(1),
            height: Length::Px(1),
            padding: Padding::default(),
            disabled: false,
            disabled_style: Style::default(),
            focusable: true,
            tab_stop: true,
            on_focus: None,
            on_blur: None,
            hover_style: StyleSlot::Inherit,
            focus_style: StyleSlot::Inherit,
            focus_thumb_style: StyleSlot::Inherit,
            hover_thumb_style: StyleSlot::Inherit,
            thumb_symbol: "".to_string(),
            track_symbol: "".to_string(),
            filled_track_symbol: "".to_string(),
            hover_thumb_symbol: None,
        }
    }

    /// Set minimum value.
    pub fn min(mut self, min: f64) -> Self {
        self.min = min;
        self
    }

    /// Set maximum value.
    pub fn max(mut self, max: f64) -> Self {
        self.max = max;
        self
    }

    /// Set step value.
    pub fn step(mut self, step: f64) -> Self {
        self.step = step;
        self
    }

    /// Set label.
    pub fn label(mut self, label: impl Into<String>) -> Self {
        self.label = Some(label.into());
        self
    }

    /// Set on-change callback.
    pub fn on_change(mut self, cb: Callback<f64>) -> Self {
        self.on_change = Some(cb);
        self
    }

    /// Set on-click callback.
    pub fn on_click(mut self, cb: Callback<f64>) -> Self {
        self.on_click = Some(cb);
        self
    }

    /// Set focused key handler. Returning `true` consumes the key before default stepping.
    pub fn on_key(mut self, handler: KeyHandler) -> Self {
        self.on_key = Some(handler);
        self
    }

    /// Set label style.
    pub fn label_style(mut self, style: Style) -> Self {
        self.label_style = style;
        self
    }

    /// Set whether to show value text.
    pub fn show_value(mut self, show: bool) -> Self {
        self.show_value = show;
        self
    }

    /// Set style for the track.
    pub fn style(mut self, style: Style) -> Self {
        self.style = style;
        self
    }

    /// Set style for the filled portion of the track.
    pub fn filled_track_style(mut self, style: Style) -> Self {
        self.filled_track_style = style;
        self
    }

    /// Set gradient for the filled portion of the track.
    pub fn filled_track_gradient(mut self, gradient: ColorGradient) -> Self {
        self.filled_track_gradient = Some(gradient);
        self
    }

    /// Set style for the thumb.
    pub fn thumb_style(mut self, style: Style) -> Self {
        self.thumb_style = style;
        self
    }

    /// Set gradient for the thumb based on current value.
    pub fn thumb_gradient(mut self, gradient: ColorGradient) -> Self {
        self.thumb_gradient = Some(gradient);
        self
    }

    /// Set requested width.
    pub fn width(mut self, width: Length) -> Self {
        self.width = width;
        self
    }

    /// Set requested height.
    pub fn height(mut self, height: Length) -> Self {
        self.height = height;
        self
    }

    /// Set disabled state.
    pub fn disabled(mut self, disabled: bool) -> Self {
        self.disabled = disabled;
        self
    }

    /// Set disabled style.
    pub fn disabled_style(mut self, style: Style) -> Self {
        self.disabled_style = style;
        self
    }

    /// Set whether the slider is focusable.
    pub fn focusable(mut self, focusable: bool) -> Self {
        self.focusable = focusable;
        self
    }

    /// Control whether the slider participates in sequential focus navigation.
    pub fn tab_stop(mut self, tab_stop: bool) -> Self {
        self.tab_stop = tab_stop;
        self
    }

    /// Set the callback fired when the slider receives focus.
    pub fn on_focus(mut self, cb: Callback<()>) -> Self {
        self.on_focus = Some(cb);
        self
    }

    /// Set the callback fired when the slider loses focus.
    pub fn on_blur(mut self, cb: Callback<()>) -> Self {
        self.on_blur = Some(cb);
        self
    }

    /// Set hover style for the track.
    pub fn hover_style(mut self, style: Style) -> Self {
        self.hover_style = StyleSlot::Replace(style);
        self
    }

    /// Extend the themed hover style with the given style.
    pub fn extend_hover_style(mut self, style: Style) -> Self {
        self.hover_style = StyleSlot::Extend(style);
        self
    }

    /// Inherit hover style from the active theme.
    pub fn inherit_hover_style(mut self) -> Self {
        self.hover_style = StyleSlot::Inherit;
        self
    }

    /// Set the hover style slot directly.
    pub fn hover_style_slot(mut self, slot: StyleSlot) -> Self {
        self.hover_style = slot;
        self
    }

    /// Set style when focused.
    pub fn focus_style(mut self, style: Style) -> Self {
        self.focus_style = StyleSlot::Replace(style);
        self
    }

    /// Extend the themed focus style with the given style.
    pub fn extend_focus_style(mut self, style: Style) -> Self {
        self.focus_style = StyleSlot::Extend(style);
        self
    }

    /// Inherit focus style from the active theme.
    pub fn inherit_focus_style(mut self) -> Self {
        self.focus_style = StyleSlot::Inherit;
        self
    }

    /// Set the focus style slot directly.
    pub fn focus_style_slot(mut self, slot: StyleSlot) -> Self {
        self.focus_style = slot;
        self
    }

    /// Set thumb style when focused.
    pub fn focus_thumb_style(mut self, style: Style) -> Self {
        self.focus_thumb_style = StyleSlot::Replace(style);
        self
    }

    /// Extend the themed focused thumb style with the given style.
    pub fn extend_focus_thumb_style(mut self, style: Style) -> Self {
        self.focus_thumb_style = StyleSlot::Extend(style);
        self
    }

    /// Inherit focused thumb style from the active theme.
    pub fn inherit_focus_thumb_style(mut self) -> Self {
        self.focus_thumb_style = StyleSlot::Inherit;
        self
    }

    /// Set the focused thumb style slot directly.
    pub fn focus_thumb_style_slot(mut self, slot: StyleSlot) -> Self {
        self.focus_thumb_style = slot;
        self
    }

    /// Set thumb style when hovered.
    pub fn hover_thumb_style(mut self, style: Style) -> Self {
        self.hover_thumb_style = StyleSlot::Replace(style);
        self
    }

    /// Extend the themed hovered thumb style with the given style.
    pub fn extend_hover_thumb_style(mut self, style: Style) -> Self {
        self.hover_thumb_style = StyleSlot::Extend(style);
        self
    }

    /// Inherit hovered thumb style from the active theme.
    pub fn inherit_hover_thumb_style(mut self) -> Self {
        self.hover_thumb_style = StyleSlot::Inherit;
        self
    }

    /// Set the hovered thumb style slot directly.
    pub fn hover_thumb_style_slot(mut self, slot: StyleSlot) -> Self {
        self.hover_thumb_style = slot;
        self
    }

    /// Set thumb symbol.
    pub fn thumb_symbol(mut self, symbol: impl Into<String>) -> Self {
        self.thumb_symbol = symbol.into();
        self
    }

    /// Set track symbol.
    pub fn track_symbol(mut self, symbol: impl Into<String>) -> Self {
        self.track_symbol = symbol.into();
        self
    }

    /// Set filled track symbol.
    pub fn filled_track_symbol(mut self, symbol: impl Into<String>) -> Self {
        self.filled_track_symbol = symbol.into();
        self
    }

    /// Set thumb symbol when hovered.
    pub fn hover_thumb_symbol(mut self, symbol: impl Into<String>) -> Self {
        self.hover_thumb_symbol = Some(symbol.into());
        self
    }
}

impl From<Slider> for Element {
    fn from(mut slider: Slider) -> Self {
        // Validate and fix inverted ranges to prevent NaN/Inf in layout and rendering.
        if slider.min > slider.max {
            std::mem::swap(&mut slider.min, &mut slider.max);
        }
        // Ensure min != max to avoid division by zero.
        if (slider.max - slider.min).abs() < f64::EPSILON {
            slider.max = slider.min + 1.0;
        }
        // Clamp value to valid range.
        slider.value = slider.value.clamp(slider.min, slider.max);
        Element::new(crate::core::element::ElementKind::Slider(slider))
    }
}

pub(crate) fn value_slot_width(min: f64, max: f64) -> u16 {
    let min_text = format!("{:.1}", min);
    let max_text = format!("{:.1}", max);
    let width = UnicodeWidthStr::width(min_text.as_str())
        .max(UnicodeWidthStr::width(max_text.as_str()))
        .min(u16::MAX as usize) as u16;
    width.max(1)
}

impl crate::layout::hash::LayoutHash for Slider {
    fn layout_hash(
        &self,
        hasher: &mut impl std::hash::Hasher,
        _recurse: &dyn Fn(&crate::core::element::Element) -> Option<u64>,
    ) -> Option<()> {
        use std::hash::Hash;
        self.width.hash(hasher);
        self.height.hash(hasher);
        self.label.hash(hasher);
        self.padding.hash(hasher);
        Some(())
    }
}