1use crate::buffer::Buffer;
2use crate::geom::{Pos, Rect};
3use crate::node::NodeId;
4use crate::style::{Border, Style, TextAlign, TextTruncate, TextWrap};
5
6pub struct RenderCx<'a> {
8 pub rect: Rect,
9 pub buffer: &'a mut Buffer,
10 pub cursor: Pos,
11 pub style: Style,
12 pub focused_id: Option<NodeId>,
14 pub clip_rect: Option<Rect>,
16 pub wrap: TextWrap,
18 pub truncate: TextTruncate,
20 pub align: TextAlign,
22}
23
24impl<'a> RenderCx<'a> {
25 pub fn new(rect: Rect, buffer: &'a mut Buffer, style: Style) -> Self {
26 let cursor = Pos {
27 x: rect.x,
28 y: rect.y,
29 };
30 Self {
31 rect,
32 buffer,
33 cursor,
34 style,
35 focused_id: None,
36 clip_rect: None,
37 wrap: TextWrap::None,
38 truncate: TextTruncate::None,
39 align: TextAlign::Left,
40 }
41 }
42
43 pub fn is_focused(&self, id: NodeId) -> bool {
45 self.focused_id == Some(id)
46 }
47
48 pub fn align_offset(&self, text_width: u16) -> u16 {
50 let clip = self.effective_clip();
51 let available = clip.x.saturating_add(clip.width).saturating_sub(self.rect.x);
52 match self.align {
53 TextAlign::Left => 0,
54 TextAlign::Center => available.saturating_sub(text_width) / 2,
55 TextAlign::Right => available.saturating_sub(text_width),
56 }
57 }
58
59 fn effective_clip(&self) -> Rect {
61 self.clip_rect.unwrap_or(self.rect)
62 }
63
64 fn wrap_clip(&self) -> Rect {
66 let mut clip = self.effective_clip();
67 if self.wrap == TextWrap::Char {
68 clip.height = u16::MAX.saturating_sub(clip.y); }
70 clip
71 }
72
73 pub fn text(&mut self, text: impl AsRef<str>) {
75 let text = text.as_ref();
76 let clip = self.effective_clip();
77 let available = clip.x.saturating_add(clip.width).saturating_sub(self.cursor.x);
78
79 if self.wrap == TextWrap::None && self.truncate == TextTruncate::Ellipsis {
80 let total: u16 = text.chars().map(|c| unicode_width::UnicodeWidthChar::width(c).unwrap_or(0) as u16).sum();
81 if total > available && available >= 1 {
82 let mut used: u16 = 0;
83 let mut bytes = 0;
84 for ch in text.chars() {
85 let w = unicode_width::UnicodeWidthChar::width(ch).unwrap_or(0) as u16;
86 if used + w > available.saturating_sub(1) { break; }
87 used += w;
88 bytes += ch.len_utf8();
89 }
90 if bytes > 0 {
91 self.buffer.write_text(self.cursor, clip, &text[..bytes], &self.style);
92 self.cursor.x = self.cursor.x.saturating_add(used);
93 }
94 self.buffer.write_text(self.cursor, clip, "…", &self.style);
95 self.cursor.x = self.cursor.x.saturating_add(1);
96 return;
97 }
98 }
99
100 self.buffer.write_text(self.cursor, clip, text, &self.style);
101 let width: u16 = text.chars().map(|c| unicode_width::UnicodeWidthChar::width(c).unwrap_or(0) as u16).sum();
102 self.cursor.x = self.cursor.x.saturating_add(width);
103 }
104
105 pub fn line(&mut self, text: impl AsRef<str>) {
107 let text = text.as_ref();
108 let clip = self.wrap_clip();
109 let available = if clip.width >= self.cursor.x.saturating_sub(clip.x) {
110 clip.width.saturating_sub(self.cursor.x.saturating_sub(clip.x))
111 } else {
112 0
113 };
114
115 let saved_clip = self.clip_rect;
116 if self.wrap == TextWrap::Char {
117 self.clip_rect = Some(clip); }
119
120 match self.wrap {
121 TextWrap::None => {
122 let tw: u16 = text.chars().map(|c| unicode_width::UnicodeWidthChar::width(c).unwrap_or(0) as u16).sum();
123 self.cursor.x = self.rect.x.saturating_add(self.align_offset(tw));
124 match self.truncate {
125 TextTruncate::None => {
126 self.text(text);
127 }
128 TextTruncate::Ellipsis => {
129 let total = tw;
130 if total > available && available >= 1 {
131 let mut used: u16 = 0;
132 let mut bytes = 0;
133 for ch in text.chars() {
134 let w = unicode_width::UnicodeWidthChar::width(ch).unwrap_or(0) as u16;
135 if used + w > available.saturating_sub(1) { break; }
136 used += w;
137 bytes += ch.len_utf8();
138 }
139 if bytes > 0 {
140 self.text(&text[..bytes]);
141 }
142 self.text("…");
143 } else {
144 self.text(text);
145 }
146 }
147 }
148 self.cursor.y = self.cursor.y.saturating_add(1);
149 self.cursor.x = self.rect.x;
150 }
151 TextWrap::Char => {
152 let mut remaining = text;
153 loop {
154 let line_widths: Vec<(usize, u16)> = remaining
155 .char_indices()
156 .map(|(i, c)| (i, unicode_width::UnicodeWidthChar::width(c).unwrap_or(0) as u16))
157 .filter(|&(_, w)| w > 0)
158 .collect();
159
160 let mut used: u16 = 0;
161 let mut bytes = 0;
162 for &(byte_idx, w) in &line_widths {
163 if used + w > available { break; }
164 used += w;
165 bytes = byte_idx + remaining[byte_idx..].chars().next().map(|c| c.len_utf8()).unwrap_or(0);
166 }
167 if bytes == 0 { break; }
168 let line_text = &remaining[..bytes];
169 let lw: u16 = line_text.chars().map(|c| unicode_width::UnicodeWidthChar::width(c).unwrap_or(0) as u16).sum();
170 self.cursor.x = self.rect.x.saturating_add(self.align_offset(lw));
171 self.text(line_text);
172 remaining = &remaining[bytes..];
173 if remaining.is_empty() { break; }
174 self.cursor.y = self.cursor.y.saturating_add(1);
175 self.cursor.x = self.rect.x;
176 }
177 if text.is_empty() {
178 self.cursor.y = self.cursor.y.saturating_add(1);
179 self.cursor.x = self.rect.x;
180 }
181 }
182 }
183
184 self.clip_rect = saved_clip;
185 }
186
187 pub fn set_style(&mut self, style: Style) {
189 self.style = style;
190 }
191
192 pub fn draw_border(&mut self, border: Border) {
194 let clip = self.effective_clip();
195 self.buffer.draw_border(clip, border, &self.style);
196 }
197}