Skip to main content

ratatui_command_palette/render/
fullscreen.rs

1use ratatui::buffer::Buffer;
2use ratatui::layout::{Constraint, Direction, Layout, Rect};
3use ratatui::style::{Color, Modifier, Style};
4use ratatui::text::{Line, Span};
5use ratatui::widgets::{Clear, Paragraph, Widget};
6
7use super::PaletteRenderer;
8use super::parts::{render_footer, render_query, render_rows};
9use crate::view::PaletteView;
10
11/// Fullscreen command palette renderer.
12///
13/// The fullscreen renderer clears the whole supplied area and uses a header,
14/// query row, result list, and footer. Applications can pass the entire frame
15/// when command search should become the primary screen. Use it through the
16/// [`PaletteRenderer`](super::PaletteRenderer) trait with a
17/// [`PaletteView`](crate::view::PaletteView) produced by
18/// [`PaletteState::view`](crate::state::PaletteState::view).
19#[derive(Clone, Debug)]
20pub struct FullscreenRenderer {
21    title: String,
22}
23
24impl Default for FullscreenRenderer {
25    fn default() -> Self {
26        Self::new()
27    }
28}
29
30impl FullscreenRenderer {
31    /// Creates a [`FullscreenRenderer`] with the default title.
32    pub fn new() -> Self {
33        Self {
34            title: "Command Palette".into(),
35        }
36    }
37
38    /// Sets the title rendered in the fullscreen header.
39    ///
40    /// # Examples
41    ///
42    /// ```
43    /// use ratatui_command_palette::render::FullscreenRenderer;
44    ///
45    /// let renderer = FullscreenRenderer::new().title("All commands");
46    ///
47    /// assert!(format!("{renderer:?}").contains("All commands"));
48    /// ```
49    pub fn title(mut self, title: impl Into<String>) -> Self {
50        self.title = title.into();
51        self
52    }
53}
54
55impl PaletteRenderer for FullscreenRenderer {
56    fn render(&self, area: Rect, buf: &mut Buffer, view: &PaletteView) {
57        Clear.render(area, buf);
58
59        let sections = Layout::default()
60            .direction(Direction::Vertical)
61            .constraints([
62                Constraint::Length(2),
63                Constraint::Length(1),
64                Constraint::Min(0),
65                Constraint::Length(1),
66            ])
67            .split(area);
68
69        let title = Line::from(vec![
70            Span::styled(
71                self.title.as_str(),
72                Style::new().fg(Color::Cyan).add_modifier(Modifier::BOLD),
73            ),
74            Span::raw("  "),
75            Span::styled("fullscreen", Style::new().fg(Color::DarkGray)),
76        ]);
77        Paragraph::new(title).render(sections[0], buf);
78        render_query(sections[1], buf, view);
79        render_rows(sections[2], buf, view);
80        render_footer(sections[3], buf, view);
81    }
82}