working suggestions but position is wrong

This commit is contained in:
filipriec
2025-04-12 16:22:07 +02:00
parent f4d234089f
commit 7b27d00972
4 changed files with 22 additions and 58 deletions

View File

@@ -10,6 +10,7 @@ use ratatui::{
use crate::config::colors::themes::Theme; use crate::config::colors::themes::Theme;
use crate::state::canvas_state::CanvasState; use crate::state::canvas_state::CanvasState;
use crate::components::common::autocomplete; use crate::components::common::autocomplete;
use crate::components::render_autocomplete_dropdown;
pub fn render_canvas( pub fn render_canvas(
f: &mut Frame, f: &mut Frame,
@@ -96,32 +97,7 @@ pub fn render_canvas(
// --- Render Autocomplete Dropdown (if applicable) --- // --- Render Autocomplete Dropdown (if applicable) ---
if let Some(suggestions) = form_state.get_suggestions() { if let Some(suggestions) = form_state.get_suggestions() {
if !suggestions.is_empty() { let selected = form_state.get_selected_suggestion_index();
if let Some(input_rect) = active_field_input_rect { render_autocomplete_dropdown(f, area, theme, suggestions, selected);
let selected_index = form_state.get_selected_suggestion_index();
// --- Calculate Compact Dropdown Size ---
let max_suggestion_width = suggestions.iter().map(|s| s.len()).max().unwrap_or(0) as u16;
let dropdown_width = max_suggestion_width.max(10); // Use longest suggestion width, min 10
let dropdown_height = (suggestions.len() as u16).min(5); // Height matches suggestion count, max 5
// --- End Size Calculation ---
let dropdown_area = Rect {
x: input_rect.x,
y: input_rect.y + 1,
width: dropdown_width,
height: dropdown_height,
};
// Ensure dropdown doesn't go off screen (simple vertical check)
let screen_height = f.size().height;
let clamped_dropdown_area = if dropdown_area.bottom() > screen_height {
Rect::new(dropdown_area.x, dropdown_area.y.saturating_sub(dropdown_height + 1), dropdown_area.width, dropdown_area.height)
} else {
dropdown_area
};
autocomplete::render_autocomplete_dropdown(f, clamped_dropdown_area, theme, suggestions, selected_index);
}
}
} }
} }

View File

@@ -318,21 +318,16 @@ pub async fn execute_edit_action<S: CanvasState + Any + Send>(
Ok("Suggestion changed up".to_string()) Ok("Suggestion changed up".to_string())
} }
"select_suggestion" if register_state.in_suggestion_mode => { "select_suggestion" if register_state.in_suggestion_mode => {
if let Some(selected_index) = register_state.selected_suggestion_index { if let Some(index) = register_state.selected_suggestion_index {
if let Some(selected_role) = register_state.role_suggestions.get(selected_index) { let selected_role = register_state.role_suggestions[index].clone();
register_state.role = selected_role.clone(); register_state.role = selected_role.clone(); // Update the role field
register_state.show_role_suggestions = false; register_state.in_suggestion_mode = false; // Exit suggestion mode
register_state.selected_suggestion_index = None; register_state.show_role_suggestions = false; // Hide suggestions
register_state.role_suggestions.clear(); register_state.selected_suggestion_index = None; // Clear selection
let new_cursor_pos = register_state.role.len(); Ok(format!("Selected role: {}", selected_role)) // Return success message
register_state.set_current_cursor_pos(new_cursor_pos);
*ideal_cursor_column = new_cursor_pos; // Update ideal column
Ok(format!("Selected role: {}", register_state.role))
} else {
Ok("Internal error: Invalid suggestion index".to_string())
}
} else { } else {
Ok("No suggestion selected".to_string()) Ok("No suggestion selected".to_string())
} }
} }
"exit_suggestion_mode" => { // Handle Esc "exit_suggestion_mode" => { // Handle Esc

View File

@@ -85,20 +85,17 @@ pub async fn handle_edit_event(
match action { match action {
"suggestion_up" | "suggestion_down" => { // Mapped to Tab/Shift+Tab "suggestion_up" | "suggestion_down" => { // Mapped to Tab/Shift+Tab
if !register_state.in_suggestion_mode { if !register_state.in_suggestion_mode {
// Enter suggestion mode register_state.update_role_suggestions();
register_state.update_role_suggestions(); // Populate suggestions
if !register_state.role_suggestions.is_empty() { if !register_state.role_suggestions.is_empty() {
register_state.in_suggestion_mode = true; register_state.in_suggestion_mode = true;
register_state.show_role_suggestions = true; register_state.selected_suggestion_index = Some(0);
register_state.selected_suggestion_index = Some(0); // Select first return Ok("Suggestions shown".to_string());
return Ok("Suggestions shown".to_string()); // Consume the event
} else { } else {
return Ok("No suggestions available".to_string()); // Consume, do nothing else return Ok("No suggestions available".to_string());
} }
} }
// If already in suggestion mode, fall through to execute the action via auth_e
} }
_ => {} // Other actions fall through _ => {}
} }
} }
// --- End Special Handling --- // --- End Special Handling ---

View File

@@ -81,18 +81,14 @@ impl RegisterState {
/// Updates role suggestions based on current input. /// Updates role suggestions based on current input.
pub fn update_role_suggestions(&mut self) { pub fn update_role_suggestions(&mut self) {
let current_input = self.role.to_lowercase(); let current_input = self.role.to_lowercase();
if current_input.is_empty() {
self.role_suggestions = AVAILABLE_ROLES.to_vec();
} else {
self.role_suggestions = AVAILABLE_ROLES self.role_suggestions = AVAILABLE_ROLES
.iter() .iter()
.filter(|r| r.to_lowercase().starts_with(&current_input)) .filter(|role| role.to_lowercase().contains(&current_input))
.cloned() .cloned()
.collect(); .collect();
self.show_role_suggestions = !self.role_suggestions.is_empty(); self.show_role_suggestions = !self.role_suggestions.is_empty();
} }
} }
}
impl CanvasState for AuthState { impl CanvasState for AuthState {
fn current_field(&self) -> usize { fn current_field(&self) -> usize {