tui-lipan 0.1.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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
//! Select widget.

use std::sync::Arc;

use crate::callback::{Callback, KeyHandler};
use crate::core::element::Element;
use crate::core::event::{KeyCode, KeyEvent, MouseEvent};
use crate::style::{BorderStyle, Length, Padding, ScrollbarConfig, Style, StyleSlot};
use crate::widgets::button::ButtonVariant;
use crate::widgets::internal::scroll_action_from_key;
use crate::widgets::{
    Button, List, ListConfig, ListItem, Popover, PopoverPlacement, ScrollKeymap, ZStack,
};

/// A dropdown select widget (expands in-place).
#[derive(Clone)]
pub struct Select {
    pub(crate) options: Vec<Arc<str>>,
    pub(crate) selected: Option<usize>,
    pub(crate) placeholder: Arc<str>,
    pub(crate) expanded: bool,
    pub(crate) on_toggle: Option<Callback<bool>>,
    pub(crate) on_select: Option<Callback<usize>>,
    pub(crate) on_change: Option<Callback<usize>>,
    pub(crate) width: Length,
    pub(crate) disabled: bool,
    pub(crate) button_variant: ButtonVariant,
    pub(crate) button_style: Style,
    pub(crate) button_hover_style: StyleSlot,
    pub(crate) button_focus_style: StyleSlot,
    pub(crate) button_disabled_style: Style,
    pub(crate) button_border_style: BorderStyle,
    pub(crate) button_hover_border_style: Option<BorderStyle>,
    pub(crate) button_focus_border_style: Option<BorderStyle>,
    pub(crate) button_open_suffix: Option<Arc<str>>,
    pub(crate) button_closed_suffix: Option<Arc<str>>,
    pub(crate) button_suffix_style: Style,
    pub(crate) list_title: Option<Arc<str>>,
    pub(crate) list_title_style: Style,
    pub(crate) list_config: ListConfig,

    pub(crate) list_width: Option<Length>,
    pub(crate) list_height: Length,
    pub(crate) match_button_width: bool,
    pub(crate) list_empty_text: Option<Arc<str>>,
    pub(crate) list_disabled_style: Style,
}

impl Default for Select {
    fn default() -> Self {
        Self {
            options: Vec::new(),
            selected: None,
            placeholder: "Select...".into(),
            expanded: false,
            on_toggle: None,
            on_select: None,
            on_change: None,
            width: Length::Auto,
            disabled: false,
            button_variant: ButtonVariant::Outlined,
            button_style: Style::default(),
            button_hover_style: StyleSlot::Inherit,
            button_focus_style: StyleSlot::Inherit,
            button_disabled_style: Style::default(),
            button_border_style: BorderStyle::Plain,
            button_hover_border_style: None,
            button_focus_border_style: None,
            button_open_suffix: None,
            button_closed_suffix: None,
            button_suffix_style: Style::default(),
            list_title: None,
            list_title_style: Style::default(),
            list_config: ListConfig {
                border: true,
                border_style: BorderStyle::Plain,
                padding: Padding::default(),
                style: Style::default(),
                selection_style: StyleSlot::Inherit,
                unfocused_selection_style: StyleSlot::Inherit,
                selection_full_width: false,
                selection_symbol: Some("> ".into()),
                selection_symbol_right: None,
                selection_symbol_style: None,
                unfocused_selection_symbol_style: None,
                symbol_column: true,
                gutter_gap: 0,
                gutter_for_non_selectable: false,
                item_horizontal_padding: Padding::default(),
                header_horizontal_padding: Padding::default(),
                empty_text_style: Style::default(),
                item_hover_style: None,
                scrollbar: false,
                scrollbar_config: ScrollbarConfig::default(),
            },
            list_width: None,
            list_height: Length::Px(6),
            match_button_width: false,
            list_empty_text: None,
            list_disabled_style: Style::default(),
        }
    }
}

impl Select {
    /// Create a new select.
    pub fn new() -> Self {
        Self::default()
    }

    /// Set options.
    pub fn options(mut self, options: impl IntoIterator<Item = impl Into<Arc<str>>>) -> Self {
        self.options = options.into_iter().map(Into::into).collect();
        self
    }

    /// Set selected index.
    pub fn selected(mut self, selected: Option<usize>) -> Self {
        self.selected = selected;
        self
    }

    /// Set placeholder text.
    pub fn placeholder(mut self, placeholder: impl Into<Arc<str>>) -> Self {
        self.placeholder = placeholder.into();
        self
    }

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

    /// Set toggle callback.
    pub fn on_toggle(mut self, cb: Callback<bool>) -> Self {
        self.on_toggle = Some(cb);
        self
    }

    /// Set selection callback.
    pub fn on_select(mut self, cb: Callback<usize>) -> Self {
        self.on_select = Some(cb);
        self
    }

    /// Set callback when selection changes.
    pub fn on_change(mut self, cb: Callback<usize>) -> Self {
        self.on_change = Some(cb);
        self
    }

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

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

    /// Set button variant.
    pub fn button_variant(mut self, variant: ButtonVariant) -> Self {
        self.button_variant = variant;
        self
    }

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

    /// Set button hover style.
    pub fn button_hover_style(mut self, style: Style) -> Self {
        self.button_hover_style = StyleSlot::Replace(style);
        self
    }

    /// Extend the themed button hover style.
    pub fn extend_button_hover_style(mut self, style: Style) -> Self {
        self.button_hover_style = StyleSlot::Extend(style);
        self
    }

    /// Inherit the themed button hover style.
    pub fn inherit_button_hover_style(mut self) -> Self {
        self.button_hover_style = StyleSlot::Inherit;
        self
    }

    /// Set button hover style slot directly for composite forwarding.
    pub fn button_hover_style_slot(mut self, slot: StyleSlot) -> Self {
        self.button_hover_style = slot;
        self
    }

    /// Set button focus style.
    pub fn button_focus_style(mut self, style: Style) -> Self {
        self.button_focus_style = StyleSlot::Replace(style);
        self
    }

    /// Extend the themed button focus style.
    pub fn extend_button_focus_style(mut self, style: Style) -> Self {
        self.button_focus_style = StyleSlot::Extend(style);
        self
    }

    /// Inherit the themed button focus style.
    pub fn inherit_button_focus_style(mut self) -> Self {
        self.button_focus_style = StyleSlot::Inherit;
        self
    }

    /// Set button focus style slot directly for composite forwarding.
    pub fn button_focus_style_slot(mut self, slot: StyleSlot) -> Self {
        self.button_focus_style = slot;
        self
    }

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

    /// Set button border style (used for outlined variant).
    pub fn button_border_style(mut self, style: BorderStyle) -> Self {
        self.button_border_style = style;
        self
    }

    /// Set button border style while hovered.
    pub fn button_hover_border_style(mut self, style: BorderStyle) -> Self {
        self.button_hover_border_style = Some(style);
        self
    }

    /// Set button border style while focused.
    pub fn button_focus_border_style(mut self, style: BorderStyle) -> Self {
        self.button_focus_border_style = Some(style);
        self
    }

    /// Set suffix shown when dropdown is open.
    pub fn button_open_suffix(mut self, suffix: impl Into<Arc<str>>) -> Self {
        self.button_open_suffix = Some(suffix.into());
        self
    }

    /// Set suffix shown when dropdown is closed.
    pub fn button_closed_suffix(mut self, suffix: impl Into<Arc<str>>) -> Self {
        self.button_closed_suffix = Some(suffix.into());
        self
    }

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

    /// Set dropdown title.
    pub fn list_title(mut self, title: impl Into<Arc<str>>) -> Self {
        self.list_title = Some(title.into());
        self
    }

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

    /// Set list config.
    pub fn list_config(mut self, config: ListConfig) -> Self {
        self.list_config = config;
        self
    }

    /// Set list border.
    pub fn list_border(mut self, border: bool) -> Self {
        self.list_config.border = border;
        self
    }

    /// Set list border style.
    pub fn list_border_style(mut self, style: BorderStyle) -> Self {
        self.list_config.border_style = style;
        self
    }

    /// Set list padding.
    pub fn list_padding(mut self, padding: impl Into<Padding>) -> Self {
        self.list_config.padding = padding.into();
        self
    }

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

    /// Set list highlight style.
    pub fn list_selection_style(mut self, style: Style) -> Self {
        self.list_config.selection_style = StyleSlot::Replace(style);
        self
    }

    /// Extend the themed dropdown highlight style.
    pub fn extend_list_selection_style(mut self, style: Style) -> Self {
        self.list_config.selection_style = StyleSlot::Extend(style);
        self
    }

    /// Inherit the themed dropdown highlight style.
    pub fn inherit_list_selection_style(mut self) -> Self {
        self.list_config.selection_style = StyleSlot::Inherit;
        self
    }

    /// Set list highlight style slot directly for composite forwarding.
    pub fn list_selection_style_slot(mut self, slot: StyleSlot) -> Self {
        self.list_config.selection_style = slot;
        self
    }

    /// Set dropdown highlight style while the list is not focused.
    pub fn list_unfocused_selection_style(mut self, style: Style) -> Self {
        self.list_config.unfocused_selection_style = StyleSlot::Replace(style);
        self
    }

    /// Extend the themed dropdown highlight style while the list is not focused.
    pub fn extend_list_unfocused_selection_style(mut self, style: Style) -> Self {
        self.list_config.unfocused_selection_style = StyleSlot::Extend(style);
        self
    }

    /// Inherit the themed dropdown highlight style while the list is not focused.
    pub fn inherit_list_unfocused_selection_style(mut self) -> Self {
        self.list_config.unfocused_selection_style = StyleSlot::Inherit;
        self
    }

    /// Set list unfocused highlight style slot directly for composite forwarding.
    pub fn list_unfocused_selection_style_slot(mut self, slot: StyleSlot) -> Self {
        self.list_config.unfocused_selection_style = slot;
        self
    }

    /// Set whether active_index style spans full list row width.
    pub fn list_selection_full_width(mut self, full_width: bool) -> Self {
        self.list_config.selection_full_width = full_width;
        self
    }

    /// Set list highlight symbol.
    pub fn list_selection_symbol(mut self, symbol: Option<impl Into<Arc<str>>>) -> Self {
        self.list_config.selection_symbol = symbol.map(Into::into);
        self
    }

    /// Set the trailing list selection symbol (right "pill" cap). Pairs with
    /// [`Self::list_selection_symbol`] and shares the selection symbol style.
    pub fn list_selection_symbol_right(mut self, symbol: Option<impl Into<Arc<str>>>) -> Self {
        self.list_config.selection_symbol_right = symbol.map(Into::into);
        self
    }

    /// Set list highlight symbol style.
    pub fn list_selection_symbol_style(mut self, style: Style) -> Self {
        self.list_config.selection_symbol_style = Some(style);
        self
    }

    /// Set list highlight symbol style while the list is not focused.
    pub fn list_unfocused_selection_symbol_style(mut self, style: Style) -> Self {
        self.list_config.unfocused_selection_symbol_style = Some(style);
        self
    }

    /// Set list hover style.
    pub fn list_hover_style(mut self, style: Style) -> Self {
        self.list_config.item_hover_style = Some(StyleSlot::Replace(style));
        self
    }

    /// Extend the themed list hover style.
    pub fn extend_list_hover_style(mut self, style: Style) -> Self {
        self.list_config.item_hover_style = Some(StyleSlot::Extend(style));
        self
    }

    /// Inherit the themed list hover style.
    pub fn inherit_list_hover_style(mut self) -> Self {
        self.list_config.item_hover_style = Some(StyleSlot::Inherit);
        self
    }

    /// Set list hover style slot directly for composite forwarding.
    pub fn list_hover_style_slot(mut self, slot: StyleSlot) -> Self {
        self.list_config.item_hover_style = Some(slot);
        self
    }

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

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

    /// Force dropdown width to exactly match trigger button width.
    pub fn match_button_width(mut self, match_button_width: bool) -> Self {
        self.match_button_width = match_button_width;
        self
    }

    /// Enable list scrollbar.
    pub fn list_scrollbar(mut self, scrollbar: bool) -> Self {
        self.list_config.scrollbar = scrollbar;
        self
    }

    /// Set list scrollbar configuration.
    pub fn list_scrollbar_config(mut self, config: ScrollbarConfig) -> Self {
        self.list_config.scrollbar_config = config;
        self
    }

    /// Set dropdown empty text.
    pub fn list_empty_text(mut self, text: impl Into<Arc<str>>) -> Self {
        self.list_empty_text = Some(text.into());
        self
    }

    /// Set dropdown empty text style.
    pub fn list_empty_text_style(mut self, style: Style) -> Self {
        self.list_config.empty_text_style = style;
        self
    }

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

impl From<Select> for Element {
    fn from(select: Select) -> Self {
        let label = if let Some(idx) = select.selected {
            select
                .options
                .get(idx)
                .cloned()
                .unwrap_or(select.placeholder.clone())
        } else {
            select.placeholder.clone()
        };

        let mut button = Button::new(label)
            .variant(select.button_variant)
            .width(select.width)
            .style(select.button_style)
            .hover_style_slot(select.button_hover_style)
            .focus_style_slot(select.button_focus_style)
            .disabled_style(select.button_disabled_style)
            .disabled(select.disabled);

        if matches!(select.button_variant, ButtonVariant::Outlined) {
            button = button.border_style(select.button_border_style);
        }
        if let Some(style) = select.button_hover_border_style {
            button = button.hover_border_style(Some(style));
        }
        if let Some(style) = select.button_focus_border_style {
            button = button.focus_border_style(Some(style));
        }

        let suffix = if select.expanded {
            select.button_open_suffix.clone()
        } else {
            select.button_closed_suffix.clone()
        };
        if let Some(suffix) = suffix {
            button = button
                .shortcut(suffix)
                .shortcut_style(select.button_suffix_style);
        }

        if let Some(cb) = select.on_toggle.clone()
            && !select.disabled
        {
            let expanded = select.expanded;
            button = button.on_click(Callback::new(move |_: MouseEvent| cb.emit(!expanded)));
        }

        if select.expanded && !select.disabled {
            let options_len = select.options.len();
            let selected = select
                .selected
                .unwrap_or(0)
                .min(options_len.saturating_sub(1));
            let change_cb = select.on_change.clone().or(select.on_select.clone());
            let on_select = select.on_select.clone();
            let on_toggle = select.on_toggle.clone();
            button = button.on_key(KeyHandler::new(move |key: KeyEvent| {
                if key.code == KeyCode::Esc
                    && let Some(toggle) = &on_toggle
                {
                    toggle.emit(false);
                    return true;
                }

                if key.code == KeyCode::Enter {
                    let mut handled = false;
                    if let Some(cb) = &on_select {
                        cb.emit(selected);
                        handled = true;
                    }
                    if let Some(toggle) = &on_toggle {
                        toggle.emit(false);
                        handled = true;
                    }
                    return handled;
                }

                let Some(action) = scroll_action_from_key(&key, ScrollKeymap::default()) else {
                    return false;
                };

                if options_len == 0 {
                    return true;
                }

                if let Some(next) = List::selection_for_action_in_len(selected, options_len, action)
                    && next != selected
                    && let Some(cb) = &change_cb
                {
                    cb.emit(next);
                }

                true
            }));
        }

        let list_hover_slot = select
            .list_config
            .item_hover_style
            .unwrap_or(select.list_config.selection_style);

        let mut list = List::new()
            .items(select.options.iter().map(|s| ListItem::new(s.clone())))
            .selected(select.selected.unwrap_or(0))
            .title_style(select.list_title_style)
            .border(select.list_config.border)
            .border_style(select.list_config.border_style)
            .padding(select.list_config.padding)
            .style(select.list_config.style)
            .selection_full_width(select.list_config.selection_full_width)
            .selection_symbol_style(
                select.list_config.selection_symbol_style.unwrap_or(
                    select
                        .list_config
                        .selection_style
                        .explicit_style()
                        .unwrap_or_default(),
                ),
            )
            .unfocused_selection_symbol_style(
                select
                    .list_config
                    .unfocused_selection_symbol_style
                    .or_else(|| {
                        select
                            .list_config
                            .unfocused_selection_style
                            .explicit_style()
                    })
                    .unwrap_or(
                        select
                            .list_config
                            .selection_style
                            .explicit_style()
                            .unwrap_or_default(),
                    ),
            )
            .selection_symbol(select.list_config.selection_symbol)
            .selection_symbol_right(select.list_config.selection_symbol_right)
            .symbol_column(select.list_config.symbol_column)
            .gutter_gap(select.list_config.gutter_gap)
            .gutter_for_non_selectable(select.list_config.gutter_for_non_selectable)
            .scrollbar(select.list_config.scrollbar)
            .scrollbar_config(select.list_config.scrollbar_config)
            .width(select.list_width.unwrap_or(select.width))
            .height(select.list_height)
            .item_horizontal_padding(select.list_config.item_horizontal_padding)
            .header_horizontal_padding(select.list_config.header_horizontal_padding)
            .empty_text_style(select.list_config.empty_text_style)
            .disabled_style(select.list_disabled_style)
            .disabled(select.disabled);
        list = list
            .selection_style_slot(select.list_config.selection_style)
            .unfocused_selection_style_slot(select.list_config.unfocused_selection_style)
            .item_hover_style_slot(list_hover_slot);

        if let Some(title) = select.list_title {
            list = list.title(title);
        }
        if let Some(empty_text) = select.list_empty_text {
            list = list.empty_text(empty_text);
        }

        let change_cb = select.on_change.clone().or(select.on_select.clone());
        if let Some(cb) = change_cb {
            list = list.on_select(Callback::new(move |ev: crate::widgets::ListEvent| {
                cb.emit(ev.index);
            }));
        }

        let emit_on_activate = select.on_change.is_some();
        if (emit_on_activate && select.on_select.is_some()) || select.on_toggle.is_some() {
            let on_select = select.on_select.clone();
            let on_toggle = select.on_toggle.clone();
            list = list.on_activate(Callback::new(move |ev: crate::widgets::ListEvent| {
                if emit_on_activate && let Some(cb) = &on_select {
                    cb.emit(ev.index);
                }
                if let Some(toggle) = &on_toggle {
                    toggle.emit(false);
                }
            }));
        }

        let overlay = ZStack::new().child(list);
        let mut popover = Popover::new()
            .trigger(button)
            .content(overlay)
            .open(select.expanded && !select.disabled)
            .fit_trigger_width(select.match_button_width && select.list_width.is_none())
            .placement(PopoverPlacement::BelowStart);

        if let Some(cb) = select.on_toggle.clone() {
            popover = popover.on_close(Callback::new(move |_| cb.emit(false)));
        }

        popover.into()
    }
}