use ratatui::{
buffer::Buffer,
layout::Rect,
style::{Color, Modifier, Style},
};
pub const HEADER_BTN_BG: Color = Color::Rgb(0x1e, 0x3a, 0x6e);
const HEADER_BTN_FG: Color = Color::Rgb(0xe6, 0xed, 0xf5);
pub const HEADER_BTN_HOVER_BG: Color = Color::Rgb(0x2f, 0x5a, 0xa8);
pub const REFRESH_GLYPH: char = '\u{eb37}';
pub const ADD_GLYPH: char = '\u{ea60}';
pub const NEW_FILE_GLYPH: char = '\u{ea7f}';
pub const NEW_FOLDER_GLYPH: char = '\u{ea80}';
pub const COLLAPSE_ALL_GLYPH: char = '\u{eac5}';
pub const MORE_LABEL: &str = " \u{22ef} ";
pub fn render(buf: &mut Buffer, x: u16, y: u16, glyph: char, brand: bool, hovered: bool) {
let fill = if brand {
hovered.then(|| crate::gradient::rgb_color(crate::gradient::POPUP_SEL_BG))
} else if hovered {
Some(HEADER_BTN_HOVER_BG)
} else {
Some(HEADER_BTN_BG)
};
let mut style = Style::default().add_modifier(Modifier::BOLD);
if let Some(c) = fill {
style = style.bg(c);
}
style = if brand {
style.fg(crate::gradient::rgb_color(crate::gradient::PANEL_TITLE_FG))
} else {
style.fg(HEADER_BTN_FG)
};
buf.set_string(x, y, glyph.to_string(), style);
}
pub fn action_style(brand: bool, hovered: bool) -> Style {
if !brand {
return Style::default()
.fg(Color::White)
.bg(HEADER_BTN_BG)
.add_modifier(Modifier::BOLD);
}
if hovered {
Style::default()
.fg(Color::White)
.bg(crate::gradient::rgb_color(crate::gradient::POPUP_SEL_BG))
} else {
Style::default().fg(crate::gradient::rgb_color(crate::gradient::INNER_ACCENT))
}
}
pub fn render_action(
buf: &mut Buffer,
right_edge: u16,
y: u16,
label: &str,
brand: bool,
pointer: Option<(u16, u16)>,
) -> Option<Rect> {
let w = label.chars().count() as u16;
if right_edge < w + 1 {
return None;
}
let x = right_edge - w; let hovered = pointer.is_some_and(|(px, py)| py == y && px >= x && px < x + w);
buf.set_string(x, y, label, action_style(brand, hovered));
Some(Rect {
x,
y,
width: w,
height: 1,
})
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn croft_dark_highlight_wraps_exactly_the_glyph_cell() {
let area = Rect {
x: 0,
y: 0,
width: 6,
height: 1,
};
let mut buf = Buffer::empty(area);
render(&mut buf, 2, 0, REFRESH_GLYPH, false, false);
assert_eq!(
buf[(2, 0)].bg,
HEADER_BTN_BG,
"the glyph cell wears the navy chip"
);
assert_eq!(buf[(2, 0)].symbol().chars().next(), Some(REFRESH_GLYPH));
assert_eq!(
buf[(1, 0)].bg,
Color::Reset,
"no bare highlight cell left of the glyph"
);
assert_eq!(
buf[(3, 0)].bg,
Color::Reset,
"no bare highlight cell right of the glyph"
);
}
#[test]
fn black_theme_idle_pill_is_chipless() {
let area = Rect {
x: 0,
y: 0,
width: 6,
height: 1,
};
let mut buf = Buffer::empty(area);
render(&mut buf, 2, 0, REFRESH_GLYPH, true, false);
assert_eq!(
buf[(2, 0)].bg,
Color::Reset,
"the idle Black-theme pill grows its teal fill only on hover"
);
}
}