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
//! Radio widget.

use std::hash::{Hash, Hasher};
use std::sync::Arc;

use rustc_hash::FxHasher;

use crate::callback::{Callback, KeyHandler};
use crate::core::element::{Element, IntoElement};
use crate::core::event::{KeyCode, KeyEvent, MouseEvent};
use crate::style::{Length, Padding, Style, StyleSlot};
use crate::widgets::{Checkbox, CheckboxEvent, CheckboxVariant, HStack, VStack};

/// Layout direction for radio groups.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum RadioLayout {
    /// Stack options vertically.
    #[default]
    Vertical,
    /// Stack options horizontally.
    Horizontal,
}

/// A radio button group.
///
/// Keyboard model (WAI-ARIA radio group / roving tabindex):
/// - The selected option (or the first option when none is selected) is the
///   sole tab stop and focus entry.
/// - Other options stay mouse-activatable but are not focusable.
/// - Arrow keys move the selection (and focus follows via [`Radio::focus_key`]).
#[derive(Clone)]
pub struct Radio {
    options: Vec<Arc<str>>,
    selected: Option<usize>,
    on_change: Option<Callback<usize>>,
    disabled: bool,
    gap: u16,
    layout: RadioLayout,
    variant: CheckboxVariant,
    style: Style,
    hover_style: StyleSlot,
    focus_style: StyleSlot,
    checked_style: Style,
    unchecked_style: Style,
    label_style: Style,
    padding: Padding,
    width: Length,
    height: Length,
    disabled_style: Style,
    focusable: bool,
    tab_stop: bool,
    focus_key: Option<Arc<str>>,
    on_focus: Option<Callback<usize>>,
    on_blur: Option<Callback<usize>>,
    on_key: Option<KeyHandler>,
}

fn derived_focus_key(options: &[Arc<str>]) -> Arc<str> {
    let mut hasher = FxHasher::default();
    options.len().hash(&mut hasher);
    for option in options {
        option.hash(&mut hasher);
    }
    Arc::from(format!("__tui_lipan_radio:{:016x}", hasher.finish()))
}

impl Radio {
    /// Create a new radio group.
    pub fn new(options: impl IntoIterator<Item = impl Into<Arc<str>>>) -> Self {
        Self {
            options: options.into_iter().map(Into::into).collect(),
            selected: None,
            on_change: None,
            disabled: false,
            gap: 0,
            layout: RadioLayout::Vertical,
            variant: CheckboxVariant::Circle,
            style: Style::default(),
            hover_style: StyleSlot::Inherit,
            focus_style: StyleSlot::Inherit,
            checked_style: Style::default(),
            unchecked_style: Style::default(),
            label_style: Style::default(),
            padding: Padding::default(),
            width: Length::Auto,
            height: Length::Auto,
            disabled_style: Style::default(),
            focusable: true,
            tab_stop: true,
            focus_key: None,
            on_focus: None,
            on_blur: None,
            on_key: None,
        }
    }

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

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

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

    /// Set gap between options.
    pub fn gap(mut self, gap: u16) -> Self {
        self.gap = gap;
        self
    }

    /// Set layout direction.
    pub fn layout(mut self, layout: RadioLayout) -> Self {
        self.layout = layout;
        self
    }

    /// Set checkbox variant.
    pub fn variant(mut self, variant: CheckboxVariant) -> Self {
        self.variant = variant;
        self
    }

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

    /// Set hover style.
    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 hover style slot directly for composite forwarding.
    pub fn hover_style_slot(mut self, slot: StyleSlot) -> Self {
        self.hover_style = slot;
        self
    }

    /// Set focus style.
    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 focus style slot directly for composite forwarding.
    pub fn focus_style_slot(mut self, slot: StyleSlot) -> Self {
        self.focus_style = slot;
        self
    }

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

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

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

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

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

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

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

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

    /// Control whether the active option participates in tab traversal.
    ///
    /// Non-active options are never tab stops (roving focus).
    pub fn tab_stop(mut self, tab_stop: bool) -> Self {
        self.tab_stop = tab_stop;
        self
    }

    /// Key applied to the active option so focus follows arrow-driven selection.
    ///
    /// When omitted, a key is derived from the option labels so distinct groups
    /// in the same tree do not collide. Override when two groups share the same
    /// labels (or to give the group a stable app-owned key).
    pub fn focus_key(mut self, key: impl Into<Arc<str>>) -> Self {
        self.focus_key = Some(key.into());
        self
    }

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

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

    /// Set focused key handler on the active option (runs before built-in arrows).
    pub fn on_key(mut self, handler: KeyHandler) -> Self {
        self.on_key = Some(handler);
        self
    }
}

impl From<Radio> for Element {
    fn from(radio: Radio) -> Self {
        let len = radio.options.len();
        let active = radio
            .selected
            .filter(|&i| i < len)
            .or_else(|| (len > 0).then_some(0));
        let focus_key = radio
            .focus_key
            .clone()
            .unwrap_or_else(|| derived_focus_key(&radio.options));

        let items: Vec<Element> = radio
            .options
            .into_iter()
            .enumerate()
            .map(|(i, option)| {
                let is_selected = radio.selected == Some(i);
                let is_active = active == Some(i);
                let on_change = radio.on_change.clone();

                let mut checkbox = Checkbox::new(is_selected)
                    .label(option)
                    .variant(radio.variant)
                    .gap(1)
                    .style(radio.style)
                    .hover_style_slot(radio.hover_style)
                    .focus_style_slot(radio.focus_style)
                    .checked_style(radio.checked_style)
                    .unchecked_style(radio.unchecked_style)
                    .label_style(radio.label_style)
                    .padding(radio.padding)
                    .width(radio.width)
                    .height(radio.height)
                    .disabled(radio.disabled)
                    .disabled_style(radio.disabled_style)
                    .focusable(radio.focusable && is_active && !radio.disabled)
                    .tab_stop(radio.tab_stop && is_active && !radio.disabled);

                if is_active {
                    if let Some(cb) = radio.on_focus.clone() {
                        checkbox = checkbox.on_focus(Callback::new(move |_| cb.emit(i)));
                    }
                    if let Some(cb) = radio.on_blur.clone() {
                        checkbox = checkbox.on_blur(Callback::new(move |_| cb.emit(i)));
                    }
                }

                if is_active && !radio.disabled {
                    let change_cb = radio.on_change.clone();
                    let caller_on_key = radio.on_key.clone();
                    let layout = radio.layout;
                    checkbox = checkbox.on_key(KeyHandler::new(move |key: KeyEvent| {
                        if caller_on_key
                            .as_ref()
                            .is_some_and(|handler| handler.handle(key))
                        {
                            return true;
                        }
                        handle_radio_key(key, i, len, layout, &change_cb)
                    }));
                }

                if let Some(cb) = on_change
                    && !radio.disabled
                {
                    let cb_toggle = cb.clone();
                    checkbox = checkbox.on_toggle(Callback::new(move |ev: CheckboxEvent| {
                        if ev.state.is_checked() {
                            cb_toggle.emit(i);
                        }
                    }));

                    checkbox = checkbox.on_click(Callback::new(move |_: MouseEvent| {
                        cb.emit(i);
                    }));
                }

                if is_active {
                    checkbox.key(focus_key.clone())
                } else {
                    checkbox.into()
                }
            })
            .collect();

        match radio.layout {
            RadioLayout::Vertical => {
                let mut stack = VStack::new().gap(radio.gap);
                for item in items {
                    stack = stack.child(item);
                }
                stack.into()
            }
            RadioLayout::Horizontal => {
                let mut stack = HStack::new().gap(radio.gap);
                for item in items {
                    stack = stack.child(item);
                }
                stack.into()
            }
        }
    }
}

fn handle_radio_key(
    key: KeyEvent,
    current: usize,
    len: usize,
    layout: RadioLayout,
    on_change: &Option<Callback<usize>>,
) -> bool {
    if len == 0 || key.mods.ctrl || key.mods.alt || key.mods.super_key {
        return false;
    }

    let next = match (layout, key.code) {
        (RadioLayout::Vertical, KeyCode::Up | KeyCode::Left)
        | (RadioLayout::Horizontal, KeyCode::Left | KeyCode::Up) => {
            Some(current.checked_sub(1).unwrap_or(len - 1))
        }
        (RadioLayout::Vertical, KeyCode::Down | KeyCode::Right)
        | (RadioLayout::Horizontal, KeyCode::Right | KeyCode::Down) => Some((current + 1) % len),
        (_, KeyCode::Home) => Some(0),
        (_, KeyCode::End) => Some(len - 1),
        _ => None,
    };

    let Some(next) = next else {
        return false;
    };

    if next != current
        && let Some(cb) = on_change
    {
        cb.emit(next);
    }
    true
}