90 lines
2.6 KiB
Rust
90 lines
2.6 KiB
Rust
// src/components/handlers/canvas.rs
|
|
use ratatui::{
|
|
widgets::{Paragraph, Block, Borders},
|
|
layout::{Layout, Constraint, Direction, Rect},
|
|
style::Style,
|
|
text::{Line, Span},
|
|
Frame,
|
|
prelude::Alignment,
|
|
};
|
|
use crate::config::colors::themes::Theme;
|
|
use crate::state::pages::form::FormState;
|
|
|
|
pub fn render_canvas(
|
|
f: &mut Frame,
|
|
area: Rect,
|
|
form_state: &FormState,
|
|
fields: &[&str],
|
|
current_field: &usize,
|
|
inputs: &[&String],
|
|
theme: &Theme,
|
|
is_edit_mode: bool,
|
|
) {
|
|
// 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 {
|
|
form_state.has_unsaved_changes.then(|| theme.warning).unwrap_or(theme.accent)
|
|
} else {
|
|
theme.secondary
|
|
})
|
|
.style(Style::default().bg(theme.bg));
|
|
|
|
// 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);
|
|
|
|
// 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()])
|
|
.split(input_area);
|
|
|
|
// Render labels
|
|
for (i, field) in fields.iter().enumerate() {
|
|
let label = Paragraph::new(Line::from(Span::styled(
|
|
format!("{}:", field),
|
|
Style::default().fg(theme.fg)),
|
|
));
|
|
f.render_widget(label, Rect {
|
|
x: columns[0].x,
|
|
y: input_block.y + 1 + i as u16,
|
|
width: columns[0].width,
|
|
height: 1,
|
|
});
|
|
}
|
|
|
|
// 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())
|
|
.alignment(Alignment::Left)
|
|
.style(if is_active {
|
|
Style::default().fg(theme.highlight)
|
|
} else {
|
|
Style::default().fg(theme.fg)
|
|
});
|
|
|
|
f.render_widget(input_display, input_rows[i]);
|
|
|
|
if is_active {
|
|
let cursor_x = input_rows[i].x + form_state.current_cursor_pos as u16;
|
|
let cursor_y = input_rows[i].y;
|
|
f.set_cursor_position((cursor_x, cursor_y));
|
|
}
|
|
}
|
|
}
|