working, separated canvas from form fully
This commit is contained in:
@@ -19,43 +19,35 @@ pub fn render_canvas(
|
||||
inputs: &[&String],
|
||||
theme: &Theme,
|
||||
is_edit_mode: bool,
|
||||
total_count: u64,
|
||||
current_position: u64,
|
||||
) {
|
||||
// Get canvas areas from form
|
||||
let (label_area, input_container_area) = super::form::render_form(
|
||||
f,
|
||||
area,
|
||||
theme,
|
||||
total_count,
|
||||
current_position
|
||||
);
|
||||
// Create input container with dynamic border
|
||||
// Split area into columns
|
||||
let columns = Layout::default()
|
||||
.direction(Direction::Horizontal)
|
||||
.constraints([Constraint::Percentage(30), Constraint::Percentage(70)])
|
||||
.split(area);
|
||||
|
||||
// Input container styling
|
||||
let input_container = Block::default()
|
||||
.borders(Borders::ALL)
|
||||
.border_style(if is_edit_mode {
|
||||
if form_state.has_unsaved_changes {
|
||||
Style::default().fg(theme.warning)
|
||||
form_state.has_unsaved_changes.then(|| theme.warning).unwrap_or(theme.accent)
|
||||
} else {
|
||||
Style::default().fg(theme.accent)
|
||||
}
|
||||
} else {
|
||||
Style::default().fg(theme.secondary)
|
||||
theme.secondary
|
||||
})
|
||||
.style(Style::default().bg(theme.bg));
|
||||
|
||||
// Calculate input container dimensions
|
||||
let input_block_area = Rect {
|
||||
x: input_container_area.x,
|
||||
y: input_container_area.y,
|
||||
width: input_container_area.width,
|
||||
// Input block dimensions
|
||||
let input_block = Rect {
|
||||
x: columns[1].x,
|
||||
y: columns[1].y,
|
||||
width: columns[1].width,
|
||||
height: fields.len() as u16 + 2,
|
||||
};
|
||||
|
||||
f.render_widget(&input_container, input_block_area);
|
||||
f.render_widget(&input_container, input_block);
|
||||
|
||||
// Split input area into rows
|
||||
let input_area = input_container.inner(input_block_area);
|
||||
// Input rows layout
|
||||
let input_area = input_container.inner(input_block);
|
||||
let input_rows = Layout::default()
|
||||
.direction(Direction::Vertical)
|
||||
.constraints(vec![Constraint::Length(1); fields.len()])
|
||||
@@ -65,17 +57,17 @@ pub fn render_canvas(
|
||||
for (i, field) in fields.iter().enumerate() {
|
||||
let label = Paragraph::new(Line::from(Span::styled(
|
||||
format!("{}:", field),
|
||||
Style::default().fg(theme.fg),
|
||||
)));
|
||||
Style::default().fg(theme.fg)),
|
||||
));
|
||||
f.render_widget(label, Rect {
|
||||
x: label_area.x,
|
||||
y: input_block_area.y + 1 + i as u16,
|
||||
width: label_area.width,
|
||||
x: columns[0].x,
|
||||
y: input_block.y + 1 + i as u16,
|
||||
width: columns[0].width,
|
||||
height: 1,
|
||||
});
|
||||
}
|
||||
|
||||
// Render input fields and cursor
|
||||
// Render inputs and cursor
|
||||
for (i, input) in inputs.iter().enumerate() {
|
||||
let is_active = i == *current_field;
|
||||
let input_display = Paragraph::new(input.as_str())
|
||||
|
||||
@@ -6,14 +6,21 @@ use ratatui::{
|
||||
Frame,
|
||||
};
|
||||
use crate::config::colors::Theme;
|
||||
use crate::ui::form::FormState;
|
||||
use super::canvas::render_canvas; // Changed to canvas
|
||||
|
||||
pub fn render_form(
|
||||
f: &mut Frame,
|
||||
area: Rect,
|
||||
form_state: &FormState,
|
||||
fields: &[&str],
|
||||
current_field: &usize,
|
||||
inputs: &[&String],
|
||||
theme: &Theme,
|
||||
is_edit_mode: bool,
|
||||
total_count: u64,
|
||||
current_position: u64,
|
||||
) -> (Rect, Rect) {
|
||||
) {
|
||||
// Create Adresar card
|
||||
let adresar_card = Block::default()
|
||||
.borders(Borders::ALL)
|
||||
@@ -23,33 +30,37 @@ pub fn render_form(
|
||||
|
||||
f.render_widget(adresar_card, area);
|
||||
|
||||
// Define the inner area for the form (inside the card)
|
||||
// Define inner area
|
||||
let inner_area = area.inner(Margin {
|
||||
horizontal: 1,
|
||||
vertical: 1,
|
||||
});
|
||||
|
||||
// Create a vertical layout for the entire form content
|
||||
// Create main layout
|
||||
let main_layout = Layout::default()
|
||||
.direction(Direction::Vertical)
|
||||
.constraints([
|
||||
Constraint::Length(1), // For count and position
|
||||
Constraint::Min(1), // For form fields
|
||||
Constraint::Length(1),
|
||||
Constraint::Min(1),
|
||||
])
|
||||
.split(inner_area);
|
||||
|
||||
// Render the count and position at the very top
|
||||
let count_position_text = format!("Total: {} | Current Position: {}", total_count, current_position);
|
||||
let count_position_paragraph = Paragraph::new(count_position_text)
|
||||
// Render count/position
|
||||
let count_position_text = format!("Total: {} | Position: {}", total_count, current_position);
|
||||
let count_para = Paragraph::new(count_position_text)
|
||||
.style(Style::default().fg(theme.fg))
|
||||
.alignment(Alignment::Left);
|
||||
f.render_widget(count_position_paragraph, main_layout[0]);
|
||||
f.render_widget(count_para, main_layout[0]);
|
||||
|
||||
// Return label area and input area
|
||||
let columns = Layout::default()
|
||||
.direction(Direction::Horizontal)
|
||||
.constraints([Constraint::Percentage(30), Constraint::Percentage(70)])
|
||||
.split(main_layout[1]);
|
||||
|
||||
(columns[0], columns[1])
|
||||
// Delegate input handling to canvas
|
||||
render_canvas(
|
||||
f,
|
||||
main_layout[1],
|
||||
form_state,
|
||||
fields,
|
||||
current_field,
|
||||
inputs,
|
||||
theme,
|
||||
is_edit_mode,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -38,7 +38,7 @@ impl FormState {
|
||||
let fields: Vec<&str> = self.fields.iter().map(|s| s.as_str()).collect();
|
||||
let values: Vec<&String> = self.values.iter().collect();
|
||||
|
||||
crate::components::handlers::canvas::render_canvas(
|
||||
crate::components::handlers::form::render_form(
|
||||
f,
|
||||
area,
|
||||
self,
|
||||
|
||||
Reference in New Issue
Block a user