use crate::buffer::Buffer;
use crate::geom::{Pos, Rect};
use crate::node::NodeId;
use crate::style::{Border, Style, TextAlign, TextTruncate, TextWrap};
pub struct RenderCx<'a> {
pub rect: Rect,
pub buffer: &'a mut Buffer,
pub cursor: Pos,
pub style: Style,
pub focused_id: Option<NodeId>,
pub clip_rect: Option<Rect>,
pub wrap: TextWrap,
pub truncate: TextTruncate,
pub align: TextAlign,
}
impl<'a> RenderCx<'a> {
pub fn new(rect: Rect, buffer: &'a mut Buffer, style: Style) -> Self {
let cursor = Pos {
x: rect.x,
y: rect.y,
};
Self {
rect,
buffer,
cursor,
style,
focused_id: None,
clip_rect: None,
wrap: TextWrap::None,
truncate: TextTruncate::None,
align: TextAlign::Left,
}
}
pub fn is_focused(&self, id: NodeId) -> bool {
self.focused_id == Some(id)
}
pub fn align_offset(&self, text_width: u16) -> u16 {
let clip = self.effective_clip();
let available = clip.x.saturating_add(clip.width).saturating_sub(self.rect.x);
match self.align {
TextAlign::Left => 0,
TextAlign::Center => available.saturating_sub(text_width) / 2,
TextAlign::Right => available.saturating_sub(text_width),
}
}
fn effective_clip(&self) -> Rect {
self.clip_rect.unwrap_or(self.rect)
}
fn wrap_clip(&self) -> Rect {
let mut clip = self.effective_clip();
if self.wrap == TextWrap::Char {
clip.height = u16::MAX.saturating_sub(clip.y); }
clip
}
pub fn text(&mut self, text: impl AsRef<str>) {
let text = text.as_ref();
let clip = self.effective_clip();
let available = clip.x.saturating_add(clip.width).saturating_sub(self.cursor.x);
if self.wrap == TextWrap::None && self.truncate == TextTruncate::Ellipsis {
let total: u16 = text.chars().map(|c| unicode_width::UnicodeWidthChar::width(c).unwrap_or(0) as u16).sum();
if total > available && available >= 1 {
let mut used: u16 = 0;
let mut bytes = 0;
for ch in text.chars() {
let w = unicode_width::UnicodeWidthChar::width(ch).unwrap_or(0) as u16;
if used + w > available.saturating_sub(1) { break; }
used += w;
bytes += ch.len_utf8();
}
if bytes > 0 {
self.buffer.write_text(self.cursor, clip, &text[..bytes], &self.style);
self.cursor.x = self.cursor.x.saturating_add(used);
}
self.buffer.write_text(self.cursor, clip, "…", &self.style);
self.cursor.x = self.cursor.x.saturating_add(1);
return;
}
}
self.buffer.write_text(self.cursor, clip, text, &self.style);
let width: u16 = text.chars().map(|c| unicode_width::UnicodeWidthChar::width(c).unwrap_or(0) as u16).sum();
self.cursor.x = self.cursor.x.saturating_add(width);
}
pub fn line(&mut self, text: impl AsRef<str>) {
let text = text.as_ref();
let clip = self.wrap_clip();
let available = if clip.width >= self.cursor.x.saturating_sub(clip.x) {
clip.width.saturating_sub(self.cursor.x.saturating_sub(clip.x))
} else {
0
};
let saved_clip = self.clip_rect;
if self.wrap == TextWrap::Char {
self.clip_rect = Some(clip); }
match self.wrap {
TextWrap::None => {
let tw: u16 = text.chars().map(|c| unicode_width::UnicodeWidthChar::width(c).unwrap_or(0) as u16).sum();
self.cursor.x = self.rect.x.saturating_add(self.align_offset(tw));
match self.truncate {
TextTruncate::None => {
self.text(text);
}
TextTruncate::Ellipsis => {
let total = tw;
if total > available && available >= 1 {
let mut used: u16 = 0;
let mut bytes = 0;
for ch in text.chars() {
let w = unicode_width::UnicodeWidthChar::width(ch).unwrap_or(0) as u16;
if used + w > available.saturating_sub(1) { break; }
used += w;
bytes += ch.len_utf8();
}
if bytes > 0 {
self.text(&text[..bytes]);
}
self.text("…");
} else {
self.text(text);
}
}
}
self.cursor.y = self.cursor.y.saturating_add(1);
self.cursor.x = self.rect.x;
}
TextWrap::Char => {
let mut remaining = text;
loop {
let line_widths: Vec<(usize, u16)> = remaining
.char_indices()
.map(|(i, c)| (i, unicode_width::UnicodeWidthChar::width(c).unwrap_or(0) as u16))
.filter(|&(_, w)| w > 0)
.collect();
let mut used: u16 = 0;
let mut bytes = 0;
for &(byte_idx, w) in &line_widths {
if used + w > available { break; }
used += w;
bytes = byte_idx + remaining[byte_idx..].chars().next().map(|c| c.len_utf8()).unwrap_or(0);
}
if bytes == 0 { break; }
let line_text = &remaining[..bytes];
let lw: u16 = line_text.chars().map(|c| unicode_width::UnicodeWidthChar::width(c).unwrap_or(0) as u16).sum();
self.cursor.x = self.rect.x.saturating_add(self.align_offset(lw));
self.text(line_text);
remaining = &remaining[bytes..];
if remaining.is_empty() { break; }
self.cursor.y = self.cursor.y.saturating_add(1);
self.cursor.x = self.rect.x;
}
if text.is_empty() {
self.cursor.y = self.cursor.y.saturating_add(1);
self.cursor.x = self.rect.x;
}
}
}
self.clip_rect = saved_clip;
}
pub fn set_style(&mut self, style: Style) {
self.style = style;
}
pub fn draw_border(&mut self, border: Border) {
let clip = self.effective_clip();
self.buffer.draw_border(clip, border, &self.style);
}
}