| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
use makeover_tui::makeover_layout::Bevel as BevelKind; |
| 43 |
use makeover_tui::{Fidelity, paint_bevel}; |
| 44 |
use ratatui::buffer::Buffer; |
| 45 |
use ratatui::layout::Rect; |
| 46 |
use ratatui::widgets::Widget; |
| 47 |
|
| 48 |
use crate::theme::{Theme, fidelity}; |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 57 |
pub enum Elevation { |
| 58 |
|
| 59 |
|
| 60 |
Flush, |
| 61 |
|
| 62 |
Raised, |
| 63 |
|
| 64 |
|
| 65 |
Sunken, |
| 66 |
} |
| 67 |
|
| 68 |
impl Elevation { |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
const fn kind(self) -> Option<BevelKind> { |
| 88 |
match self { |
| 89 |
Elevation::Flush => None, |
| 90 |
Elevation::Raised => Some(BevelKind::Raised), |
| 91 |
Elevation::Sunken => Some(BevelKind::Inset), |
| 92 |
} |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
pub struct Bevel<'a> { |
| 102 |
theme: &'a Theme, |
| 103 |
elevation: Elevation, |
| 104 |
fidelity: Option<Fidelity>, |
| 105 |
} |
| 106 |
|
| 107 |
impl<'a> Bevel<'a> { |
| 108 |
pub fn new(theme: &'a Theme, elevation: Elevation) -> Self { |
| 109 |
Self { |
| 110 |
theme, |
| 111 |
elevation, |
| 112 |
fidelity: None, |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
#[must_use] |
| 123 |
pub fn fidelity(mut self, fidelity: Fidelity) -> Self { |
| 124 |
self.fidelity = Some(fidelity); |
| 125 |
self |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
impl Widget for Bevel<'_> { |
| 130 |
fn render(self, area: Rect, buf: &mut Buffer) { |
| 131 |
let Some(kind) = self.elevation.kind() else { |
| 132 |
return; |
| 133 |
}; |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
if area.width < 2 || area.height < 2 { |
| 139 |
return; |
| 140 |
} |
| 141 |
let palette = self.theme.palette(self.fidelity.unwrap_or_else(fidelity)); |
| 142 |
paint_bevel(buf, area, kind, &palette); |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
#[cfg(test)] |
| 147 |
mod tests { |
| 148 |
use super::*; |
| 149 |
use ratatui::style::Color; |
| 150 |
|
| 151 |
fn theme() -> Theme { |
| 152 |
crate::theme::test_theme(crate::theme::Mode::Light) |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
fn render(elevation: Elevation, w: u16, h: u16) -> Buffer { |
| 159 |
let area = Rect::new(0, 0, w, h); |
| 160 |
let mut buf = Buffer::empty(area); |
| 161 |
Bevel::new(&theme(), elevation) |
| 162 |
.fidelity(Fidelity::TrueColor) |
| 163 |
.render(area, &mut buf); |
| 164 |
buf |
| 165 |
} |
| 166 |
|
| 167 |
fn glyphs(buf: &Buffer, area: Rect) -> Vec<String> { |
| 168 |
(area.y..area.bottom()) |
| 169 |
.map(|y| { |
| 170 |
(area.x..area.right()) |
| 171 |
.map(|x| buf[(x, y)].symbol()) |
| 172 |
.collect() |
| 173 |
}) |
| 174 |
.collect() |
| 175 |
} |
| 176 |
|
| 177 |
#[test] |
| 178 |
fn a_raised_bevel_draws_the_outline_and_leaves_the_middle_alone() { |
| 179 |
let buf = render(Elevation::Raised, 5, 4); |
| 180 |
assert_eq!( |
| 181 |
glyphs(&buf, Rect::new(0, 0, 5, 4)), |
| 182 |
vec!["▛▀▀▀▀", "▌ ▐", "▌ ▐", "▄▄▄▄▟"], |
| 183 |
); |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
#[test] |
| 189 |
fn raised_is_lit_from_the_top_left() { |
| 190 |
let buf = render(Elevation::Raised, 4, 3); |
| 191 |
let t = theme(); |
| 192 |
assert_eq!(buf[(0u16, 0u16)].fg, t.makeover.bevel_light); |
| 193 |
assert_eq!(buf[(1u16, 0u16)].fg, t.makeover.bevel_light); |
| 194 |
assert_eq!(buf[(0u16, 1u16)].fg, t.makeover.bevel_light); |
| 195 |
assert_eq!(buf[(3u16, 2u16)].fg, t.makeover.bevel_dark); |
| 196 |
assert_eq!(buf[(2u16, 2u16)].fg, t.makeover.bevel_dark); |
| 197 |
assert_eq!(buf[(3u16, 1u16)].fg, t.makeover.bevel_dark); |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
#[test] |
| 204 |
fn sunken_is_raised_with_the_tones_exchanged() { |
| 205 |
let (raised, sunken) = ( |
| 206 |
render(Elevation::Raised, 4, 3), |
| 207 |
render(Elevation::Sunken, 4, 3), |
| 208 |
); |
| 209 |
let area = Rect::new(0, 0, 4, 3); |
| 210 |
assert_eq!(glyphs(&raised, area), glyphs(&sunken, area)); |
| 211 |
|
| 212 |
let t = theme(); |
| 213 |
let swap = |c: Color| match c { |
| 214 |
c if c == t.makeover.bevel_light => t.makeover.bevel_dark, |
| 215 |
c if c == t.makeover.bevel_dark => t.makeover.bevel_light, |
| 216 |
other => other, |
| 217 |
}; |
| 218 |
for y in area.y..area.bottom() { |
| 219 |
for x in area.x..area.right() { |
| 220 |
assert_eq!(swap(raised[(x, y)].fg), sunken[(x, y)].fg, "fg at {x},{y}"); |
| 221 |
assert_eq!(swap(raised[(x, y)].bg), sunken[(x, y)].bg, "bg at {x},{y}"); |
| 222 |
} |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
#[test] |
| 228 |
fn the_transition_corners_carry_both_tones() { |
| 229 |
let buf = render(Elevation::Raised, 4, 3); |
| 230 |
let t = theme(); |
| 231 |
let top_right = &buf[(3u16, 0u16)]; |
| 232 |
assert_eq!(top_right.fg, t.makeover.bevel_light); |
| 233 |
assert_eq!(top_right.bg, t.makeover.bevel_dark); |
| 234 |
let bottom_left = &buf[(0u16, 2u16)]; |
| 235 |
assert_eq!(bottom_left.fg, t.makeover.bevel_dark); |
| 236 |
assert_eq!(bottom_left.bg, t.makeover.bevel_light); |
| 237 |
} |
| 238 |
|
| 239 |
#[test] |
| 240 |
fn flush_draws_nothing() { |
| 241 |
let buf = render(Elevation::Flush, 4, 3); |
| 242 |
assert_eq!( |
| 243 |
glyphs(&buf, Rect::new(0, 0, 4, 3)), |
| 244 |
vec![" ", " ", " "] |
| 245 |
); |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
#[test] |
| 251 |
fn an_area_too_small_to_have_two_sides_is_left_alone() { |
| 252 |
for (w, h) in [(1, 4), (4, 1), (1, 1)] { |
| 253 |
let buf = render(Elevation::Raised, w, h); |
| 254 |
let area = Rect::new(0, 0, w, h); |
| 255 |
let blank: Vec<String> = (0..h).map(|_| " ".repeat(w as usize)).collect(); |
| 256 |
assert_eq!(glyphs(&buf, area), blank, "{w}x{h}"); |
| 257 |
} |
| 258 |
} |
| 259 |
} |
| 260 |
|