implementation of canvas for multiple pages step 1
This commit is contained in:
@@ -8,12 +8,12 @@ use ratatui::{
|
|||||||
prelude::Alignment,
|
prelude::Alignment,
|
||||||
};
|
};
|
||||||
use crate::config::colors::themes::Theme;
|
use crate::config::colors::themes::Theme;
|
||||||
use crate::state::pages::form::FormState;
|
use crate::state::canvas_state::CanvasState;
|
||||||
|
|
||||||
pub fn render_canvas(
|
pub fn render_canvas(
|
||||||
f: &mut Frame,
|
f: &mut Frame,
|
||||||
area: Rect,
|
area: Rect,
|
||||||
form_state: &FormState,
|
form_state: &impl CanvasState,
|
||||||
fields: &[&str],
|
fields: &[&str],
|
||||||
current_field: &usize,
|
current_field: &usize,
|
||||||
inputs: &[&String],
|
inputs: &[&String],
|
||||||
@@ -30,7 +30,7 @@ pub fn render_canvas(
|
|||||||
let input_container = Block::default()
|
let input_container = Block::default()
|
||||||
.borders(Borders::ALL)
|
.borders(Borders::ALL)
|
||||||
.border_style(if is_edit_mode {
|
.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 {
|
} else {
|
||||||
theme.secondary
|
theme.secondary
|
||||||
})
|
})
|
||||||
@@ -81,7 +81,7 @@ pub fn render_canvas(
|
|||||||
f.render_widget(input_display, input_rows[i]);
|
f.render_widget(input_display, input_rows[i]);
|
||||||
|
|
||||||
if is_active {
|
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;
|
let cursor_y = input_rows[i].y;
|
||||||
f.set_cursor_position((cursor_x, cursor_y));
|
f.set_cursor_position((cursor_x, cursor_y));
|
||||||
}
|
}
|
||||||
|
|||||||
44
client/src/state/canvas_state.rs
Normal file
44
client/src/state/canvas_state.rs
Normal file
@@ -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")
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,3 +1,4 @@
|
|||||||
// src/state/mod.rs
|
// src/state/mod.rs
|
||||||
pub mod state;
|
pub mod state;
|
||||||
pub mod pages;
|
pub mod pages;
|
||||||
|
pub mod canvas_state;
|
||||||
|
|||||||
Reference in New Issue
Block a user