canvas library usage instead of internal canvas on the form, others are still using canvas from state. Needed debugging because its not working yet

This commit is contained in:
Priec
2025-07-29 15:20:00 +02:00
parent f24156775a
commit db938a2c8d
12 changed files with 677 additions and 107 deletions

View File

@@ -3,7 +3,7 @@ use crate::components::common::autocomplete;
use crate::components::handlers::canvas::render_canvas;
use crate::config::colors::themes::Theme;
use crate::state::app::highlight::HighlightState;
use crate::state::pages::canvas_state::CanvasState;
use canvas::CanvasState;
use crate::state::pages::form::FormState;
use ratatui::{
layout::{Alignment, Constraint, Direction, Layout, Margin, Rect},
@@ -64,7 +64,7 @@ pub fn render_form(
f.render_widget(count_para, main_layout[0]);
// Get the active field's rect from render_canvas
let active_field_rect = render_canvas(
let active_field_rect = crate::components::handlers::canvas::render_canvas_library(
f,
main_layout[1],
form_state,

View File

@@ -9,13 +9,15 @@ use ratatui::{
};
use crate::config::colors::themes::Theme;
use crate::state::app::highlight::HighlightState;
use crate::state::pages::canvas_state::CanvasState;
use crate::state::pages::canvas_state::CanvasState as LegacyCanvasState;
use canvas::CanvasState as LibraryCanvasState;
use std::cmp::{max, min};
/// Render canvas for legacy CanvasState (AddTableState, LoginState, RegisterState, AddLogicState)
pub fn render_canvas(
f: &mut Frame,
area: Rect,
form_state: &impl CanvasState,
form_state: &impl LegacyCanvasState,
fields: &[&str],
current_field_idx: &usize,
inputs: &[&String],
@@ -23,12 +25,75 @@ pub fn render_canvas(
is_edit_mode: bool,
highlight_state: &HighlightState,
) -> Option<Rect> {
render_canvas_impl(
f,
area,
fields,
current_field_idx,
inputs,
theme,
is_edit_mode,
highlight_state,
form_state.current_cursor_pos(),
form_state.has_unsaved_changes(),
|i| form_state.get_display_value_for_field(i).to_string(),
|i| form_state.has_display_override(i),
)
}
/// Render canvas for library CanvasState (FormState)
pub fn render_canvas_library(
f: &mut Frame,
area: Rect,
form_state: &impl LibraryCanvasState,
fields: &[&str],
current_field_idx: &usize,
inputs: &[&String],
theme: &Theme,
is_edit_mode: bool,
highlight_state: &HighlightState,
) -> Option<Rect> {
render_canvas_impl(
f,
area,
fields,
current_field_idx,
inputs,
theme,
is_edit_mode,
highlight_state,
form_state.current_cursor_pos(),
form_state.has_unsaved_changes(),
|i| form_state.get_display_value_for_field(i).to_string(),
|i| form_state.has_display_override(i),
)
}
/// Internal implementation shared by both render functions
fn render_canvas_impl<F1, F2>(
f: &mut Frame,
area: Rect,
fields: &[&str],
current_field_idx: &usize,
inputs: &[&String],
theme: &Theme,
is_edit_mode: bool,
highlight_state: &HighlightState,
current_cursor_pos: usize,
has_unsaved_changes: bool,
get_display_value: F1,
has_display_override: F2,
) -> Option<Rect>
where
F1: Fn(usize) -> String,
F2: Fn(usize) -> bool,
{
let columns = Layout::default()
.direction(Direction::Horizontal)
.constraints([Constraint::Percentage(30), Constraint::Percentage(70)])
.split(area);
let border_style = if form_state.has_unsaved_changes() {
let border_style = if has_unsaved_changes {
Style::default().fg(theme.warning)
} else if is_edit_mode {
Style::default().fg(theme.accent)
@@ -75,17 +140,16 @@ pub fn render_canvas(
for (i, _input) in inputs.iter().enumerate() {
let is_active = i == *current_field_idx;
let current_cursor_pos = form_state.current_cursor_pos();
// Use the trait method to get display value
let text = form_state.get_display_value_for_field(i);
// Use the provided closure to get display value
let text = get_display_value(i);
let text_len = text.chars().count();
let line: Line;
match highlight_state {
HighlightState::Off => {
line = Line::from(Span::styled(
text,
&text,
if is_active {
Style::default().fg(theme.highlight)
} else {
@@ -141,11 +205,11 @@ pub fn render_canvas(
Span::styled(after, normal_style_in_highlight),
]);
} else {
line = Line::from(Span::styled(text, highlight_style));
line = Line::from(Span::styled(&text, highlight_style));
}
} else {
line = Line::from(Span::styled(
text,
&text,
if is_active { normal_style_in_highlight } else { normal_style_outside }
));
}
@@ -158,10 +222,10 @@ pub fn render_canvas(
let normal_style_outside = Style::default().fg(theme.fg);
if i >= start_field && i <= end_field {
line = Line::from(Span::styled(text, highlight_style));
line = Line::from(Span::styled(&text, highlight_style));
} else {
line = Line::from(Span::styled(
text,
&text,
if is_active { normal_style_in_highlight } else { normal_style_outside }
));
}
@@ -174,14 +238,13 @@ pub fn render_canvas(
if is_active {
active_field_input_rect = Some(input_rows[i]);
// --- CORRECTED CURSOR POSITIONING LOGIC ---
// Use the new generic trait method to check for an override.
let cursor_x = if form_state.has_display_override(i) {
// Use the provided closure to check for display override
let cursor_x = if has_display_override(i) {
// If an override exists, place the cursor at the end.
input_rows[i].x + text.chars().count() as u16
} else {
// Otherwise, use the real cursor position.
input_rows[i].x + form_state.current_cursor_pos() as u16
input_rows[i].x + current_cursor_pos as u16
};
let cursor_y = input_rows[i].y;
f.set_cursor_position((cursor_x, cursor_y));