much better, still not it

This commit is contained in:
filipriec
2025-04-12 15:52:12 +02:00
parent 50a329fc0d
commit f4d234089f
2 changed files with 36 additions and 28 deletions

View File

@@ -2,52 +2,57 @@
use crate::config::colors::themes::Theme; use crate::config::colors::themes::Theme;
use ratatui::{ use ratatui::{
buffer::Buffer,
layout::Rect, layout::Rect,
style::{Color, Modifier, Style}, style::{Color, Modifier, Style},
widgets::{List, ListItem, ListState, StatefulWidget, Widget}, widgets::{Block, List, ListItem, ListState},
Frame, Frame,
}; };
/// Renders a bordered list dropdown for autocomplete suggestions. /// Renders an opaque dropdown list for autocomplete suggestions.
pub fn render_autocomplete_dropdown( pub fn render_autocomplete_dropdown(
f: &mut Frame, f: &mut Frame,
area: Rect, // The area where the dropdown should be rendered area: Rect,
theme: &Theme, theme: &Theme,
suggestions: &[String], suggestions: &[String],
selected_index: Option<usize>, selected_index: Option<usize>,
) { ) {
if suggestions.is_empty() { if suggestions.is_empty() {
return; // Don't render if no suggestions return;
} }
// --- Render Background ---
struct GrayBackground;
impl Widget for GrayBackground {
fn render(self, area: Rect, buf: &mut Buffer) {
buf.set_style(area, Style::default().bg(Color::DarkGray)); // Set background
}
}
f.render_widget(GrayBackground, area);
// --- Render Suggestions List (without block/border) --- // Render a solid background block first to ensure opacity
let background_block = Block::default().style(Style::default().bg(Color::DarkGray));
f.render_widget(background_block, area);
// Create list items, ensuring each has a defined background
let items: Vec<ListItem> = suggestions let items: Vec<ListItem> = suggestions
.iter() .iter()
.map(|s| ListItem::new(s.as_str()).style(Style::default().fg(theme.fg))) // Set default text color .enumerate()
.map(|(i, s)| {
let is_selected = selected_index == Some(i);
ListItem::new(s.as_str()).style(if is_selected {
// Style for selected item (highlight background)
Style::default()
.fg(theme.bg) // Text color on highlight
.bg(theme.highlight) // Highlight background
.add_modifier(Modifier::BOLD)
} else {
// Style for non-selected items (matching background block)
Style::default()
.fg(theme.fg) // Text color on gray
.bg(Color::DarkGray) // Explicit gray background
})
})
.collect(); .collect();
let list = List::new(items) // Create the list widget (without its own block)
.highlight_style( let list = List::new(items);
Style::default()
.add_modifier(Modifier::BOLD)
.bg(theme.highlight)
.fg(theme.bg),
)
.highlight_symbol("> "); // Symbol for selected item
// Create a state for the list to handle selection highlighting // State for managing selection highlight (still needed for logic)
let mut list_state = ListState::default(); let mut list_state = ListState::default();
list_state.select(selected_index); list_state.select(selected_index);
// Render the list statefully *over* the background block
f.render_stateful_widget(list, area, &mut list_state); f.render_stateful_widget(list, area, &mut list_state);
} }

View File

@@ -100,12 +100,15 @@ pub fn render_canvas(
if let Some(input_rect) = active_field_input_rect { if let Some(input_rect) = active_field_input_rect {
let selected_index = form_state.get_selected_suggestion_index(); let selected_index = form_state.get_selected_suggestion_index();
// Calculate dropdown area below the active input field // --- Calculate Compact Dropdown Size ---
let dropdown_height = (suggestions.len() as u16).min(5) + 2; // Max 5 suggestions + border 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 { let dropdown_area = Rect {
x: input_rect.x, x: input_rect.x,
y: input_rect.y + 1, // Position below the input line y: input_rect.y + 1,
width: input_rect.width.max(20), // Ensure minimum width width: dropdown_width,
height: dropdown_height, height: dropdown_height,
}; };