use ratatui::layout::Rect;
use ratatui::style::Color;
const ROW_HOVER_BG_DARK: Color = Color::Rgb(0x2b, 0x31, 0x42);
const ROW_HOVER_BG_BLACK: Color = Color::Rgb(0x1a, 0x1a, 0x1a);
const ROW_HOVER_BG_LIGHT: Color = Color::Rgb(0xf2, 0xf2, 0xf2);
pub fn contains(rect: Rect, pointer: Option<(u16, u16)>) -> bool {
pointer.is_some_and(|(px, py)| {
px >= rect.x
&& px < rect.x.saturating_add(rect.width)
&& py >= rect.y
&& py < rect.y.saturating_add(rect.height)
})
}
pub fn row_hover_bg(
rect: Rect,
pointer: Option<(u16, u16)>,
theme: crate::theme::Theme,
) -> Option<Color> {
contains(rect, pointer).then(|| hover_bg(theme))
}
pub fn hover_bg(theme: crate::theme::Theme) -> Color {
if theme.is_light() {
ROW_HOVER_BG_LIGHT
} else if theme.gradient() {
ROW_HOVER_BG_BLACK
} else {
ROW_HOVER_BG_DARK
}
}
#[cfg(test)]
mod tests {
use super::*;
fn rect(x: u16, y: u16, w: u16, h: u16) -> Rect {
Rect {
x,
y,
width: w,
height: h,
}
}
#[test]
fn contains_is_inclusive_of_the_top_left_and_exclusive_of_the_far_edges() {
let r = rect(5, 3, 10, 1);
assert!(contains(r, Some((5, 3))), "top-left corner is inside");
assert!(contains(r, Some((14, 3))), "last column is inside");
assert!(!contains(r, Some((15, 3))), "one past the width is outside");
assert!(!contains(r, Some((5, 4))), "one past the height is outside");
assert!(!contains(r, Some((4, 3))), "one before the left is outside");
}
#[test]
fn contains_is_false_without_a_pointer() {
assert!(!contains(rect(0, 0, 4, 4), None));
}
#[test]
fn row_hover_bg_is_theme_aware_and_only_fires_over_the_rect() {
let r = rect(0, 0, 8, 1);
assert_eq!(
row_hover_bg(r, Some((2, 0)), crate::theme::Theme::DARK_BLUE),
Some(ROW_HOVER_BG_DARK)
);
assert_eq!(
row_hover_bg(r, Some((2, 0)), crate::theme::Theme::BLACK),
Some(ROW_HOVER_BG_BLACK)
);
assert_eq!(
row_hover_bg(r, Some((2, 5)), crate::theme::Theme::DARK_BLUE),
None
);
assert_eq!(row_hover_bg(r, None, crate::theme::Theme::BLACK), None);
}
}