Skip to main content

ratatui_command_palette/render/
inline.rs

1use ratatui::buffer::Buffer;
2use ratatui::layout::{Constraint, Direction, Layout, Rect};
3use ratatui::style::{Color, Style};
4use ratatui::widgets::{Block, Borders, Widget};
5
6use super::PaletteRenderer;
7use super::parts::{render_query, render_rows};
8use crate::view::PaletteView;
9
10/// Inline dropdown command palette renderer.
11///
12/// This renderer draws a compact bordered dropdown inside the supplied area and
13/// does not clear content outside that area. It is intended for embedding below
14/// another input or toolbar. See the [`render`](super) module for the renderer
15/// contract shared by all built-in renderers.
16#[derive(Clone, Debug)]
17pub struct InlineDropdownRenderer {
18    title: String,
19}
20
21impl Default for InlineDropdownRenderer {
22    fn default() -> Self {
23        Self::new()
24    }
25}
26
27impl InlineDropdownRenderer {
28    /// Creates an [`InlineDropdownRenderer`] with the default title.
29    pub fn new() -> Self {
30        Self {
31            title: "Commands".into(),
32        }
33    }
34
35    /// Sets the dropdown border title.
36    ///
37    /// # Examples
38    ///
39    /// ```
40    /// use ratatui_command_palette::render::InlineDropdownRenderer;
41    ///
42    /// let renderer = InlineDropdownRenderer::new().title("Palette");
43    ///
44    /// assert!(format!("{renderer:?}").contains("Palette"));
45    /// ```
46    pub fn title(mut self, title: impl Into<String>) -> Self {
47        self.title = title.into();
48        self
49    }
50}
51
52impl PaletteRenderer for InlineDropdownRenderer {
53    fn render(&self, area: Rect, buf: &mut Buffer, view: &PaletteView) {
54        let block = Block::new()
55            .title(self.title.as_str())
56            .borders(Borders::ALL)
57            .border_style(Style::new().fg(Color::DarkGray));
58        let inner = block.inner(area);
59        block.render(area, buf);
60
61        if inner.height == 0 || inner.width == 0 {
62            return;
63        }
64
65        let sections = Layout::default()
66            .direction(Direction::Vertical)
67            .constraints([Constraint::Length(1), Constraint::Min(0)])
68            .split(inner);
69
70        render_query(sections[0], buf, view);
71        render_rows(sections[1], buf, view);
72    }
73}