fixing form:
This commit is contained in:
@@ -3,7 +3,7 @@ use ratatui::{
|
|||||||
widgets::{Paragraph, Block, Borders},
|
widgets::{Paragraph, Block, Borders},
|
||||||
layout::{Layout, Constraint, Direction, Rect, Margin},
|
layout::{Layout, Constraint, Direction, Rect, Margin},
|
||||||
style::Style,
|
style::Style,
|
||||||
text::{Line, Span, Text},
|
text::{Line, Span},
|
||||||
Frame,
|
Frame,
|
||||||
};
|
};
|
||||||
use crate::client::colors::Theme;
|
use crate::client::colors::Theme;
|
||||||
@@ -32,53 +32,56 @@ pub fn render_form(
|
|||||||
vertical: 1,
|
vertical: 1,
|
||||||
});
|
});
|
||||||
|
|
||||||
// Split the inner area into a single column layout
|
// Split the inner area into two columns: labels and inputs
|
||||||
let form_chunks = Layout::default()
|
let columns = Layout::default()
|
||||||
.direction(Direction::Vertical)
|
.direction(Direction::Horizontal)
|
||||||
.constraints(vec![Constraint::Length(1); fields.len()]) // Each field takes 1 row
|
.constraints([Constraint::Percentage(30), Constraint::Percentage(70)])
|
||||||
.split(inner_area);
|
.split(inner_area);
|
||||||
|
|
||||||
|
// Create a bordered block for the input area
|
||||||
|
let input_block = Block::default()
|
||||||
|
.borders(Borders::ALL)
|
||||||
|
.border_style(Style::default().fg(theme.accent));
|
||||||
|
|
||||||
|
// Render the labels on the left
|
||||||
|
let label_area = columns[0];
|
||||||
for (i, field) in fields.iter().enumerate() {
|
for (i, field) in fields.iter().enumerate() {
|
||||||
let input = inputs[i].clone();
|
|
||||||
let is_active = i == *current_field;
|
|
||||||
|
|
||||||
// Split each row into two parts: label and input
|
|
||||||
let row_chunks = Layout::default()
|
|
||||||
.direction(Direction::Horizontal)
|
|
||||||
.constraints([Constraint::Percentage(30), Constraint::Percentage(70)])
|
|
||||||
.split(form_chunks[i]);
|
|
||||||
|
|
||||||
// Render the label on the left
|
|
||||||
let label = Paragraph::new(Line::from(Span::styled(
|
let label = Paragraph::new(Line::from(Span::styled(
|
||||||
field.to_string(),
|
field.to_string(),
|
||||||
Style::default().fg(theme.fg),
|
Style::default().fg(theme.fg),
|
||||||
)));
|
)));
|
||||||
f.render_widget(label, row_chunks[0]);
|
f.render_widget(label, Rect {
|
||||||
|
x: label_area.x,
|
||||||
|
y: label_area.y + i as u16,
|
||||||
|
width: label_area.width,
|
||||||
|
height: 1,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// Render the input on the right
|
// Render the inputs inside the bordered block
|
||||||
let input_block = Block::default()
|
let input_area = input_block.inner(columns[1]);
|
||||||
.borders(if is_active {
|
f.render_widget(input_block, columns[1]);
|
||||||
Borders::ALL // Add a border around the active field
|
|
||||||
} else {
|
|
||||||
Borders::NONE
|
|
||||||
})
|
|
||||||
.border_style(Style::default().fg(theme.accent)); // Use the accent color for the border
|
|
||||||
|
|
||||||
|
for (i, input) in inputs.iter().enumerate() {
|
||||||
let input_paragraph = Paragraph::new(input.as_str())
|
let input_paragraph = Paragraph::new(input.as_str())
|
||||||
.block(input_block)
|
.style(if i == *current_field {
|
||||||
.style(if is_active {
|
|
||||||
Style::default().fg(theme.highlight)
|
Style::default().fg(theme.highlight)
|
||||||
} else {
|
} else {
|
||||||
Style::default().fg(theme.fg)
|
Style::default().fg(theme.fg)
|
||||||
});
|
});
|
||||||
|
|
||||||
f.render_widget(input_paragraph, row_chunks[1]);
|
f.render_widget(input_paragraph, Rect {
|
||||||
|
x: input_area.x,
|
||||||
|
y: input_area.y + i as u16,
|
||||||
|
width: input_area.width,
|
||||||
|
height: 1,
|
||||||
|
});
|
||||||
|
|
||||||
// Render the cursor in the active field
|
// Render the cursor in the active field
|
||||||
if is_active {
|
if i == *current_field {
|
||||||
let cursor_x = row_chunks[1].x + input.len() as u16 + 1; // Calculate cursor position
|
let cursor_x = input_area.x + input.len() as u16;
|
||||||
let cursor_y = row_chunks[1].y;
|
let cursor_y = input_area.y + i as u16;
|
||||||
f.set_cursor(cursor_x, cursor_y); // Set the cursor position
|
f.set_cursor(cursor_x, cursor_y);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user