fresh/view/controls/toggle/
render.rs1use ratatui::layout::Rect;
4use ratatui::style::Style;
5use ratatui::text::{Line, Span};
6use ratatui::widgets::Paragraph;
7use ratatui::Frame;
8
9use super::{FocusState, ToggleColors, ToggleLayout, ToggleState};
10
11pub fn render_toggle(
22 frame: &mut Frame,
23 area: Rect,
24 state: &ToggleState,
25 colors: &ToggleColors,
26) -> ToggleLayout {
27 render_toggle_aligned(frame, area, state, colors, None)
28}
29
30pub fn render_toggle_aligned(
42 frame: &mut Frame,
43 area: Rect,
44 state: &ToggleState,
45 colors: &ToggleColors,
46 label_width: Option<u16>,
47) -> ToggleLayout {
48 if area.height == 0 || area.width < 4 {
49 return ToggleLayout {
50 checkbox_area: Rect::default(),
51 full_area: area,
52 };
53 }
54
55 let (bracket_color, _check_color, label_color) = match state.focus {
56 FocusState::Normal => (colors.bracket, colors.checkmark, colors.label),
57 FocusState::Focused => (colors.focused, colors.checkmark, colors.focused),
58 FocusState::Hovered => (colors.focused, colors.checkmark, colors.focused),
59 FocusState::Disabled => (colors.disabled, colors.disabled, colors.disabled),
60 };
61
62 let checkbox = if state.checked { "[x]" } else { "[ ]" };
63
64 let actual_label_width = label_width.unwrap_or(state.label.len() as u16);
66 let padded_label = format!(
67 "{:width$}",
68 state.label,
69 width = actual_label_width as usize
70 );
71
72 let line = Line::from(vec![
73 Span::styled(padded_label, Style::default().fg(label_color)),
74 Span::styled(": ", Style::default().fg(label_color)),
75 Span::styled(checkbox, Style::default().fg(bracket_color)),
76 ]);
77
78 let paragraph = Paragraph::new(line);
79 frame.render_widget(paragraph, area);
80
81 let checkbox_start = area.x + actual_label_width + 2; let checkbox_area = Rect::new(checkbox_start, area.y, 3.min(area.width), 1);
84
85 let full_width = (actual_label_width + 2 + 3).min(area.width);
87 let full_area = Rect::new(area.x, area.y, full_width, 1);
88
89 ToggleLayout {
90 checkbox_area,
91 full_area,
92 }
93}