1use ratatui::buffer::Buffer;
4use ratatui::layout::{Alignment, Rect};
5use ratatui::style::Style;
6use ratatui::widgets::{Block, BorderType, Borders};
7
8use crate::{FrameMode, Role, Theme};
9
10const KITTY_PLACEHOLDER: char = '\u{10EEEE}';
12
13fn framed(
14 title: &str,
15 border_type: BorderType,
16 [fill, border, title_style]: [Style; 3],
17) -> Block<'static> {
18 let block = Block::new()
19 .borders(Borders::ALL)
20 .border_type(border_type)
21 .style(fill)
22 .border_style(border);
23 if title.is_empty() {
24 return block;
25 }
26 block
27 .title(format!(" {title} "))
28 .title_style(title_style)
29 .title_alignment(Alignment::Center)
30}
31
32#[must_use]
36pub(crate) fn dialog(theme: &Theme, title: &str, mode: FrameMode) -> Block<'static> {
37 let fill = theme.style(Role::Dialog);
38 let frame = theme.style(Role::DialogFrame);
39 match mode {
40 FrameMode::Dragging => {
41 let icon = theme.style(Role::DialogIcon);
42 framed(title, BorderType::Plain, [fill, icon, icon])
43 }
44 FrameMode::Passive => framed(title, BorderType::Plain, [fill; 3]),
45 FrameMode::Active => {
46 let styles = [fill, frame, theme.style(Role::DialogTitle)];
47 framed(title, BorderType::Double, styles)
48 }
49 }
50}
51
52#[must_use]
54pub fn menu_box(theme: &Theme) -> Block<'static> {
55 let menu = theme.style(Role::Menu);
56 framed("", BorderType::Plain, [menu; 3])
57}
58
59#[must_use]
61pub(crate) fn list_box(theme: &Theme) -> Block<'static> {
62 let list = theme.style(Role::List);
63 framed("", BorderType::Plain, [list; 3])
64}
65
66pub(crate) fn shadow(buf: &mut Buffer, area: Rect, theme: &Theme) {
69 let style = Style::reset().patch(theme.style(Role::Shadow));
70 let right = Rect::new(area.right(), area.y.saturating_add(1), 2, area.height);
71 let below = Rect::new(area.x.saturating_add(2), area.bottom(), area.width, 1);
72 for strip in [right, below] {
73 for position in strip.intersection(buf.area).positions() {
74 if let Some(cell) = buf.cell_mut(position)
75 && !cell.symbol().starts_with(KITTY_PLACEHOLDER)
76 {
77 cell.set_style(style);
78 }
79 }
80 }
81}
82
83#[cfg(test)]
84mod tests;