fresh/view/controls/text_input/
render.rs1use crate::primitives::display_width::{char_width, str_width};
4use ratatui::layout::Rect;
5use ratatui::style::{Modifier, Style};
6use ratatui::text::{Line, Span};
7use ratatui::widgets::Paragraph;
8use ratatui::Frame;
9
10use super::{FocusState, TextInputColors, TextInputLayout, TextInputState};
11
12pub fn render_text_input(
24 frame: &mut Frame,
25 area: Rect,
26 state: &TextInputState,
27 colors: &TextInputColors,
28 field_width: u16,
29) -> TextInputLayout {
30 render_text_input_aligned(frame, area, state, colors, field_width, None)
31}
32
33pub fn render_text_input_aligned(
46 frame: &mut Frame,
47 area: Rect,
48 state: &TextInputState,
49 colors: &TextInputColors,
50 field_width: u16,
51 label_width: Option<u16>,
52) -> TextInputLayout {
53 if area.height == 0 || area.width < 5 {
54 return TextInputLayout::default();
55 }
56
57 let (label_color, text_color, border_color, placeholder_color) = match state.focus {
66 FocusState::Normal => (colors.label, colors.text, colors.border, colors.placeholder),
67 FocusState::Focused => {
68 let border = if state.editing {
69 colors.focused
70 } else {
71 colors.border
72 };
73 (colors.label, colors.text, border, colors.placeholder)
74 }
75 FocusState::Hovered => (
76 colors.label,
77 colors.text,
78 colors.focused,
79 colors.placeholder,
80 ),
81 FocusState::Disabled => (
82 colors.disabled,
83 colors.disabled,
84 colors.disabled,
85 colors.disabled,
86 ),
87 };
88
89 let actual_label_width = label_width.unwrap_or(state.label.len() as u16);
90 let final_label_width = actual_label_width + 2;
91 let actual_field_width = field_width.min(area.width.saturating_sub(final_label_width + 2));
92
93 let (display_text, is_placeholder) = if state.value.is_empty() && !state.placeholder.is_empty()
94 {
95 (&state.placeholder, true)
96 } else {
97 (&state.value, false)
98 };
99
100 let inner_width = actual_field_width.saturating_sub(2) as usize;
101
102 let text_before_cursor = &state.value[..state.cursor.min(state.value.len())];
105 let cursor_visual_pos = str_width(text_before_cursor);
106
107 let scroll_visual_offset = cursor_visual_pos.saturating_sub(inner_width);
109
110 let mut visible_text = String::new();
112 let mut current_visual_pos = 0;
113 for ch in display_text.chars() {
114 let ch_width = char_width(ch);
115 if current_visual_pos + ch_width <= scroll_visual_offset {
117 current_visual_pos += ch_width;
118 continue;
119 }
120 if current_visual_pos - scroll_visual_offset >= inner_width {
122 break;
123 }
124 visible_text.push(ch);
125 current_visual_pos += ch_width;
126 }
127
128 let visible_width = str_width(&visible_text);
130 let padding = " ".repeat(inner_width.saturating_sub(visible_width));
131 let padded = format!("{}{}", visible_text, padding);
132
133 let text_style = if is_placeholder {
134 Style::default().fg(placeholder_color)
135 } else {
136 Style::default().fg(text_color)
137 };
138
139 let padded_label = format!(
140 "{:width$}",
141 state.label,
142 width = actual_label_width as usize
143 );
144
145 let line = Line::from(vec![
146 Span::styled(padded_label, Style::default().fg(label_color)),
147 Span::styled(": ", Style::default().fg(label_color)),
148 Span::styled("[", Style::default().fg(border_color)),
149 Span::styled(padded, text_style),
150 Span::styled("]", Style::default().fg(border_color)),
151 ]);
152
153 let paragraph = Paragraph::new(line);
154 frame.render_widget(paragraph, area);
155
156 let input_start = area.x + final_label_width;
157 let input_area = Rect::new(input_start, area.y, actual_field_width + 2, 1);
158
159 let cursor_pos = if state.focus == FocusState::Focused && state.editing && !is_placeholder {
160 let cursor_visual_in_field = cursor_visual_pos.saturating_sub(scroll_visual_offset);
162 let cursor_x = input_start + 1 + cursor_visual_in_field as u16;
163 if cursor_x < input_start + actual_field_width + 1 {
164 let cursor_area = Rect::new(cursor_x, area.y, 1, 1);
165 let cursor_char = if state.cursor < state.value.len() {
167 crate::primitives::grapheme::grapheme_at(&state.value, state.cursor)
168 .map(|(g, _, _)| g.chars().next().unwrap_or(' '))
169 .unwrap_or(' ')
170 } else {
171 ' '
172 };
173 let cursor_span = Span::styled(
174 cursor_char.to_string(),
175 Style::default()
176 .fg(colors.cursor)
177 .add_modifier(Modifier::REVERSED),
178 );
179 frame.render_widget(Paragraph::new(Line::from(vec![cursor_span])), cursor_area);
180 Some((cursor_x, area.y))
181 } else {
182 None
183 }
184 } else {
185 None
186 };
187
188 TextInputLayout {
189 input_area,
190 full_area: Rect::new(
191 area.x,
192 area.y,
193 input_start - area.x + actual_field_width + 2,
194 1,
195 ),
196 cursor_pos,
197 }
198}