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);
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)>, brand: bool) -> Option<Color> {
if !contains(rect, pointer) {
return None;
}
Some(if brand {
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)), false),
Some(ROW_HOVER_BG_DARK)
);
assert_eq!(
row_hover_bg(r, Some((2, 0)), true),
Some(ROW_HOVER_BG_BLACK)
);
assert_eq!(row_hover_bg(r, Some((2, 5)), false), None);
assert_eq!(row_hover_bg(r, None, true), None);
}
}