diff --git a/client/src/components/handlers/form.rs b/client/src/components/handlers/form.rs index a5c7ca0..f390c15 100644 --- a/client/src/components/handlers/form.rs +++ b/client/src/components/handlers/form.rs @@ -1,14 +1,13 @@ -// src/components/handlers/form.rs +// src/client/components1/handlers/form.rs use ratatui::{ widgets::{Paragraph, Block, Borders}, layout::{Layout, Constraint, Direction, Rect, Margin, Alignment}, style::Style, - text::{Line, Span, Text}, + text::{Line, Span}, Frame, }; use crate::config::colors::Theme; use crate::ui::form::FormState; -use crate::components::canvas::{self, CanvasState}; pub fn render_form( f: &mut Frame, @@ -21,38 +20,6 @@ pub fn render_form( is_edit_mode: bool, total_count: u64, current_position: u64, -) { - // Split the main area into canvas and container sections - let main_layout = Layout::default() - .direction(Direction::Vertical) - .constraints([ - Constraint::Length(3), // For header and controls - Constraint::Min(1), // For canvas - ]) - .split(area); - - // Render the header section - render_header(f, main_layout[0], theme, total_count, current_position); - - // Render the canvas area - render_canvas_area( - f, - main_layout[1], - form_state, - fields, - current_field, - inputs, - theme, - is_edit_mode, - ); -} - -fn render_header( - f: &mut Frame, - area: Rect, - theme: &Theme, - total_count: u64, - current_position: u64, ) { // Create Adresar card let adresar_card = Block::default() @@ -63,37 +30,68 @@ fn render_header( f.render_widget(adresar_card, area); - // Define the inner area for the header content + // Define the inner area for the form (inside the card) let inner_area = area.inner(Margin { horizontal: 1, vertical: 1, }); - // Render the count and position + // Create a vertical layout for the entire form content + let main_layout = Layout::default() + .direction(Direction::Vertical) + .constraints([ + Constraint::Length(1), // For count and position + Constraint::Min(1), // For form fields + ]) + .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) .style(Style::default().fg(theme.fg)) .alignment(Alignment::Left); - f.render_widget(count_position_paragraph, inner_area); -} + f.render_widget(count_position_paragraph, main_layout[0]); -fn render_canvas_area( - f: &mut Frame, - area: Rect, - form_state: &FormState, - fields: &[&str], - current_field: &usize, - inputs: &[&String], - theme: &Theme, - is_edit_mode: bool, -) { - // Split canvas area into labels and inputs + // Split the remaining space into label and input columns let columns = Layout::default() .direction(Direction::Horizontal) .constraints([Constraint::Percentage(30), Constraint::Percentage(70)]) - .split(area); + .split(main_layout[1]); - // Render labels + // Create compact input container + 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) // Red color + } else { + Style::default().fg(theme.accent) // Blue color + } + } else { + Style::default().fg(theme.secondary) // Yellow color + }) + .style(Style::default().bg(theme.bg)); + + // Place the input container at the top + let input_container_area = Rect { + x: columns[1].x, + y: columns[1].y, + width: columns[1].width, + height: fields.len() as u16 + 2, // +2 for borders + }; + + f.render_widget(&input_container, input_container_area); + + // Input area inside borders + let input_area = input_container.inner(input_container_area); + + // Split the remaining area for the form inputs + let input_rows = Layout::default() + .direction(Direction::Vertical) + .constraints(vec![Constraint::Length(1); fields.len()]) + .split(input_area); + + // Render labels close to the border for (i, field) in fields.iter().enumerate() { let label = Paragraph::new(Line::from(Span::styled( format!("{}:", field), @@ -101,54 +99,44 @@ fn render_canvas_area( ))); f.render_widget(label, Rect { x: columns[0].x, - y: columns[0].y + i as u16, + y: input_container_area.y + 1 + i as u16, // Align with input rows width: columns[0].width, height: 1, }); } - // Create canvas states for each input field - let canvas_states: Vec = inputs.iter().enumerate().map(|(i, input)| { - CanvasState { - content: Text::raw(input.as_str()), - cursor_position: if i == *current_field { - (form_state.current_cursor_pos as u16, 0) - } else { - (0, 0) - }, - scroll_offset: 0, - } - }).collect(); - - // Render input canvases - let input_height = area.height / fields.len() as u16; - for (i, state) in canvas_states.iter().enumerate() { + // Render inputs with left-aligned text and free cursor movement + for (i, input) in inputs.iter().enumerate() { let is_active = i == *current_field; - let border_style = if is_edit_mode { - if form_state.has_unsaved_changes { - Style::default().fg(theme.warning) + + 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.accent) + Style::default().fg(theme.fg) + }); + + f.render_widget(input_display, input_rows[i]); + + // Position cursor at the correct position in the active field + if is_active && is_edit_mode { // Move cursor logic inside the loop + let cursor_x = input_rows[i].x + input.len() as u16; + let cursor_y = input_rows[i].y; + f.set_cursor_position((cursor_x, cursor_y)); // Updated to set_cursor_position + } + if is_active { + if is_edit_mode { + // Edit mode: cursor at current_cursor_pos instead of end + 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)); // Updated to set_cursor_position + } else { + // Read-only mode: cursor at current_cursor_pos + 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)); // Updated to set_cursor_position } - } else { - Style::default().fg(theme.secondary) - }; - - let canvas_area = Rect { - x: columns[1].x, - y: columns[1].y + i as u16 * input_height, - width: columns[1].width, - height: input_height, - }; - - canvas::render_canvas( - f, - canvas_area, - state, - theme, - is_active, - is_edit_mode, - border_style, - ); + } } }