diff --git a/client/src/components/admin/add_logic.rs b/client/src/components/admin/add_logic.rs index 9499203..12ba015 100644 --- a/client/src/components/admin/add_logic.rs +++ b/client/src/components/admin/add_logic.rs @@ -72,37 +72,39 @@ pub fn render_add_logic( // === SCRIPT EDITOR AUTOCOMPLETE RENDERING === if add_logic_state.script_editor_autocomplete_active && !add_logic_state.script_editor_suggestions.is_empty() { - if let Some((trigger_line, trigger_col)) = add_logic_state.script_editor_trigger_position { - // For now, we'll position the autocomplete at a simple offset from the trigger - // Since we can't easily get viewport info, we'll position it relatively - // This is a simplified approach - in a real implementation you'd want proper viewport tracking - - // Account for TextArea's block borders (1 for each side) - let block_offset_x = 1; - let block_offset_y = 1; + // Get the current cursor position from textarea + let current_cursor = { + let editor_borrow = add_logic_state.script_content_editor.borrow(); + editor_borrow.cursor() // Returns (row, col) as (usize, usize) + }; + + let (cursor_line, cursor_col) = current_cursor; + + // Account for TextArea's block borders (1 for each side) + let block_offset_x = 1; + let block_offset_y = 1; + + // Position autocomplete at current cursor position + // Add 1 to column to position dropdown right after the cursor + let autocomplete_x = cursor_col + 1; + let autocomplete_y = cursor_line; + + let input_rect = Rect { + x: (inner_area.x + block_offset_x + autocomplete_x as u16).min(inner_area.right().saturating_sub(20)), + y: (inner_area.y + block_offset_y + autocomplete_y as u16).min(inner_area.bottom().saturating_sub(5)), + width: 1, // Minimum width for positioning + height: 1, + }; - // Simple positioning: assume trigger is visible and use direct coordinates - // This works for small scripts but may need improvement for larger ones - let visible_line = trigger_line; - let visible_col = trigger_col + 1 + add_logic_state.script_editor_filter_text.len(); - - let input_rect = Rect { - x: (inner_area.x + block_offset_x + visible_col as u16).min(inner_area.right().saturating_sub(20)), - y: (inner_area.y + block_offset_y + visible_line as u16 + 1).min(inner_area.bottom().saturating_sub(5)), - width: 1, // Minimum width for positioning - height: 1, - }; - - // Render autocomplete dropdown - autocomplete::render_autocomplete_dropdown( - f, - input_rect, - f.area(), // Full frame area for clamping - theme, - &add_logic_state.script_editor_suggestions, - add_logic_state.script_editor_selected_suggestion_index, - ); - } + // Render autocomplete dropdown + autocomplete::render_autocomplete_dropdown( + f, + input_rect, + f.area(), // Full frame area for clamping + theme, + &add_logic_state.script_editor_suggestions, + add_logic_state.script_editor_selected_suggestion_index, + ); } return; // Exit early for fullscreen mode