From 37b08fdd109c921ab8e5c74e555695c53ebb75c2 Mon Sep 17 00:00:00 2001 From: filipriec Date: Fri, 28 Mar 2025 14:26:18 +0100 Subject: [PATCH] implementation of canvas for multiple pages step 1 --- client/src/components/handlers/canvas.rs | 8 ++--- client/src/state/canvas_state.rs | 44 ++++++++++++++++++++++++ client/src/state/mod.rs | 1 + 3 files changed, 49 insertions(+), 4 deletions(-) create mode 100644 client/src/state/canvas_state.rs diff --git a/client/src/components/handlers/canvas.rs b/client/src/components/handlers/canvas.rs index ac4d5e8..cd1c64d 100644 --- a/client/src/components/handlers/canvas.rs +++ b/client/src/components/handlers/canvas.rs @@ -8,12 +8,12 @@ use ratatui::{ prelude::Alignment, }; use crate::config::colors::themes::Theme; -use crate::state::pages::form::FormState; +use crate::state::canvas_state::CanvasState; pub fn render_canvas( f: &mut Frame, area: Rect, - form_state: &FormState, + form_state: &impl CanvasState, fields: &[&str], current_field: &usize, inputs: &[&String], @@ -30,7 +30,7 @@ pub fn render_canvas( 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) + form_state.has_unsaved_changes().then(|| theme.warning).unwrap_or(theme.accent) } else { theme.secondary }) @@ -81,7 +81,7 @@ pub fn render_canvas( 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_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)); } diff --git a/client/src/state/canvas_state.rs b/client/src/state/canvas_state.rs new file mode 100644 index 0000000..be48f39 --- /dev/null +++ b/client/src/state/canvas_state.rs @@ -0,0 +1,44 @@ +// src/state/canvas_state.rs + +use crate::state::pages::form::FormState; + +pub trait CanvasState { + fn current_field(&self) -> usize; + fn current_cursor_pos(&self) -> usize; + fn has_unsaved_changes(&self) -> bool; + fn inputs(&self) -> Vec<&String>; + fn get_current_input(&self) -> &str; + fn get_current_input_mut(&mut self) -> &mut String; +} + +// Implement for FormState (keep existing form.rs code and add this) +impl CanvasState for FormState { + fn current_field(&self) -> usize { + self.current_field + } + + fn current_cursor_pos(&self) -> usize { + self.current_cursor_pos + } + + fn has_unsaved_changes(&self) -> bool { + self.has_unsaved_changes + } + + fn inputs(&self) -> Vec<&String> { + self.values.iter().collect() + } + + fn get_current_input(&self) -> &str { + self.values + .get(self.current_field) + .map(|s| s.as_str()) + .unwrap_or("") + } + + fn get_current_input_mut(&mut self) -> &mut String { + self.values + .get_mut(self.current_field) + .expect("Invalid current_field index") + } +} diff --git a/client/src/state/mod.rs b/client/src/state/mod.rs index 6dc5624..9e15dce 100644 --- a/client/src/state/mod.rs +++ b/client/src/state/mod.rs @@ -1,3 +1,4 @@ // src/state/mod.rs pub mod state; pub mod pages; +pub mod canvas_state;