we compiled but buffer doesnt work

This commit is contained in:
filipriec
2025-08-29 18:11:27 +02:00
parent 58f109ca91
commit 16dd460469
13 changed files with 256 additions and 166 deletions

View File

@@ -22,6 +22,7 @@ use crate::buffer::state::BufferState;
use crate::state::app::state::AppState;
use crate::state::pages::auth::AuthState;
use crate::bottom_panel::layout::{bottom_panel_constraints, render_bottom_panel};
use canvas::FormEditor;
use ratatui::{
layout::{Constraint, Direction, Layout},
Frame,
@@ -114,7 +115,7 @@ pub fn render_ui(
app_state,
state,
),
Page::Form(state) => {
Page::Form(path) => {
let (sidebar_area, form_actual_area) =
calculate_sidebar_layout(app_state.ui.show_sidebar, main_content_area);
if let Some(sidebar_rect) = sidebar_area {
@@ -143,16 +144,24 @@ pub fn render_ui(
.split(form_actual_area)[1]
};
render_form_page(
f,
form_render_area,
app_state,
state,
app_state.current_view_table_name.as_deref().unwrap_or(""),
theme,
state.total_count,
state.current_position,
);
if let Some(editor) = app_state.editor_for_path_ref(path) {
let (total_count, current_position) =
if let Some(fs) = app_state.form_state_for_path_ref(path) {
(fs.total_count, fs.current_position)
} else {
(0, 1)
};
render_form_page(
f,
form_render_area,
editor,
app_state.current_view_table_name.as_deref().unwrap_or(""),
theme,
total_count,
current_position,
);
}
}
}
@@ -186,6 +195,7 @@ pub fn render_ui(
theme,
current_fps,
app_state,
router,
navigation_state,
event_handler_command_input,
event_handler_command_mode_active,

View File

@@ -118,10 +118,10 @@ pub async fn run_ui() -> Result<()> {
FormState::new(initial_profile.clone(), initial_table.clone(), initial_field_defs)
});
buffer_state.update_history(AppView::Form(path.clone()));
router.navigate(Page::Form(path));
router.navigate(Page::Form(path.clone()));
// Fetch initial count using app_state accessor
if let Some(form_state) = app_state.active_form_state_mut(&buffer_state) {
if let Some(form_state) = app_state.form_state_for_path(&path) {
UiService::fetch_and_set_table_count(&mut grpc_client, form_state)
.await
.context(format!(
@@ -154,9 +154,11 @@ pub async fn run_ui() -> Result<()> {
let mut table_just_switched = false;
loop {
let position_before_event = app_state.active_form_state_mut(&buffer_state)
.map(|fs| fs.current_position)
.unwrap_or(1);
let position_before_event = if let Page::Form(path) = &router.current {
app_state.form_state_for_path(path).map(|fs| fs.current_position).unwrap_or(1)
} else {
1
};
let mut event_processed = false;
// --- CHANNEL RECEIVERS ---
@@ -181,16 +183,18 @@ pub async fn run_ui() -> Result<()> {
// --- ADDED: For live form autocomplete ---
match event_handler.autocomplete_result_receiver.try_recv() {
Ok(hits) => {
if let Some(form_state) = app_state.form_state_mut() {
if form_state.autocomplete_active {
form_state.autocomplete_suggestions = hits;
form_state.autocomplete_loading = false;
if !form_state.autocomplete_suggestions.is_empty() {
form_state.selected_suggestion_index = Some(0);
} else {
form_state.selected_suggestion_index = None;
if let Page::Form(path) = &router.current {
if let Some(form_state) = app_state.form_state_for_path(path) {
if form_state.autocomplete_active {
form_state.autocomplete_suggestions = hits;
form_state.autocomplete_loading = false;
if !form_state.autocomplete_suggestions.is_empty() {
form_state.selected_suggestion_index = Some(0);
} else {
form_state.selected_suggestion_index = None;
}
event_handler.command_message = format!("Found {} suggestions.", form_state.autocomplete_suggestions.len());
}
event_handler.command_message = format!("Found {} suggestions.", form_state.autocomplete_suggestions.len());
}
}
needs_redraw = true;
@@ -218,9 +222,9 @@ pub async fn run_ui() -> Result<()> {
|| app_state.ui.show_search_palette
|| event_handler.navigation_state.active;
if !overlay_active {
if let Page::Form(_) = &router.current {
if let Page::Form(path) = &router.current {
if !app_state.ui.focus_outside_canvas {
if let Some(editor) = app_state.active_form_editor_mut(&buffer_state) {
if let Some(editor) = app_state.editor_for_path(path) {
match editor.handle_key_event(*key_event) {
KeyEventOutcome::Consumed(Some(msg)) => {
event_handler.command_message = msg;
@@ -245,9 +249,7 @@ pub async fn run_ui() -> Result<()> {
}
}
// Get form state from app_state and pass to handle_event
let form_state = app_state.form_state_mut().unwrap();
// Call handle_event directly
let event_outcome_result = event_handler.handle_event(
event,
&config,
@@ -272,20 +274,22 @@ pub async fn run_ui() -> Result<()> {
}
EventOutcome::DataSaved(save_outcome, message) => {
event_handler.command_message = message;
// Clone form_state to avoid double borrow
let mut temp_form_state = app_state.form_state().unwrap().clone();
if let Err(e) = UiService::handle_save_outcome(
save_outcome,
&mut grpc_client,
&mut app_state,
&mut temp_form_state,
).await {
event_handler.command_message =
format!("Error handling save outcome: {}", e);
}
// Update app_state with changes
if let Some(form_state) = app_state.form_state_mut() {
*form_state = temp_form_state;
if let Page::Form(path) = &router.current {
if let Some(mut temp_form_state) = app_state.form_state_for_path(path).cloned() {
if let Err(e) = UiService::handle_save_outcome(
save_outcome,
&mut grpc_client,
&mut app_state,
&mut temp_form_state,
).await {
event_handler.command_message =
format!("Error handling save outcome: {}", e);
}
// Update app_state with changes
if let Some(form_state) = app_state.form_state_for_path(path) {
*form_state = temp_form_state;
}
}
}
}
EventOutcome::ButtonSelected { .. } => {}
@@ -296,7 +300,7 @@ pub async fn run_ui() -> Result<()> {
let table_name = parts[1].to_string();
app_state.set_current_view_table(profile_name, table_name);
buffer_state.update_history(AppView::Form);
buffer_state.update_history(AppView::Form(path.clone()));
event_handler.command_message = format!("Loading table: {}", path);
} else {
event_handler.command_message = format!("Invalid table path: {}", path);
@@ -382,7 +386,7 @@ pub async fn run_ui() -> Result<()> {
router.navigate(Page::Intro(intro_state.clone()));
}
AppView::Login => {
// Do not re-create the page every frame. If were already on Login,
// Do not re-create the page every frame. If we're already on Login,
// keep it. If we just switched into Login, create it once and
// inject the keymap.
if let Page::Login(_) = &router.current {
@@ -440,16 +444,15 @@ pub async fn run_ui() -> Result<()> {
}
AppView::AddTable => router.navigate(Page::AddTable(admin_state.add_table_state.clone())),
AppView::AddLogic => router.navigate(Page::AddLogic(admin_state.add_logic_state.clone())),
AppView::Form => {
if let Some(form_state) = app_state.form_state().cloned() {
router.navigate(Page::Form(form_state));
}
AppView::Form(path) => {
// Router now carries the path; just navigate with it
router.navigate(Page::Form(path.clone()));
}
AppView::Scratch => {}
}
}
if let Page::Form(_) = &router.current {
if let Page::Form(current_path) = &router.current {
let current_view_profile = app_state.current_view_profile_name.clone();
let current_view_table = app_state.current_view_table_name.clone();
@@ -475,9 +478,10 @@ pub async fn run_ui() -> Result<()> {
{
Ok(new_form_state) => {
// Set the new form state and fetch count
app_state.set_form_state(new_form_state, &config);
let path = format!("{}/{}", prof_name, tbl_name);
app_state.ensure_form_editor(&path, &config, || new_form_state);
if let Some(form_state) = app_state.form_state_mut() {
if let Some(form_state) = app_state.form_state_for_path(&path) {
if let Err(e) = UiService::fetch_and_set_table_count(
&mut grpc_client,
form_state,
@@ -596,18 +600,20 @@ pub async fn run_ui() -> Result<()> {
}
}
let current_position = app_state.form_state()
.map(|fs| fs.current_position)
.unwrap_or(1);
let current_position = if let Page::Form(path) = &router.current {
app_state.form_state_for_path(path).map(|fs| fs.current_position).unwrap_or(1)
} else {
1
};
let position_changed = current_position != position_before_event;
let mut position_logic_needs_redraw = false;
if let Page::Form(form_state) = &mut router.current {
if let Page::Form(path) = &router.current {
if !table_just_switched {
if position_changed && !app_state.is_canvas_edit_mode() {
position_logic_needs_redraw = true;
if let Some(form_state) = app_state.form_state_mut() {
if let Some(form_state) = app_state.form_state_for_path(path) {
if form_state.current_position > form_state.total_count {
form_state.reset_to_empty();
event_handler.command_message = format!(
@@ -643,7 +649,7 @@ pub async fn run_ui() -> Result<()> {
event_handler.ideal_cursor_column.min(max_cursor_pos);
}
} else if !position_changed && !app_state.is_canvas_edit_mode() {
if let Some(form_state) = app_state.form_state_mut() {
if let Some(form_state) = app_state.form_state_for_path(path) {
let current_input_str = form_state.get_current_input();
let current_input_len = current_input_str.chars().count();
let max_cursor_pos = if current_input_len > 0 {
@@ -709,8 +715,10 @@ pub async fn run_ui() -> Result<()> {
terminal.show_cursor()?;
} else {
// Inside canvas → let canvas handle it
if let Some(editor) = &app_state.form_editor {
let _ = CursorManager::update_for_mode(editor.mode());
if let Page::Form(path) = &router.current {
if let Some(editor) = app_state.editor_for_path(path) {
let _ = CursorManager::update_for_mode(editor.mode());
}
}
if let Page::Login(page) = &router.current {
let _ = CursorManager::update_for_mode(page.editor.mode());
@@ -727,16 +735,9 @@ pub async fn run_ui() -> Result<()> {
}
}
// Workaround for borrow checker
let current_dir = app_state.current_dir.clone();
let form_state_clone = if let Page::Form(path) = &router.current {
app_state.form_state_for_path(path).cloned()
} else {
None
};
if let Some(form_state_clone) = form_state_clone {
terminal.draw(|f| {
terminal
.draw(|f| {
render_ui(
f,
&mut router,
@@ -753,7 +754,6 @@ pub async fn run_ui() -> Result<()> {
})
.context("Terminal draw call failed")?;
needs_redraw = false;
}
}
let now = Instant::now();