Skip to main content

lv_tui/
render.rs

1use crate::buffer::Buffer;
2use crate::geom::{Pos, Rect};
3use crate::node::NodeId;
4use crate::style::{Border, Style, TextAlign, TextTruncate, TextWrap};
5
6/// 渲染上下文——组件通过它输出内容到缓冲区
7pub struct RenderCx<'a> {
8    pub rect: Rect,
9    pub buffer: &'a mut Buffer,
10    pub cursor: Pos,
11    pub style: Style,
12    /// 当前焦点节点
13    pub focused_id: Option<NodeId>,
14    /// 裁剪区域(覆盖 rect 的默认裁剪,用于 Scroll 等)
15    pub clip_rect: Option<Rect>,
16    /// 文本换行模式
17    pub wrap: TextWrap,
18    /// 文本截断模式
19    pub truncate: TextTruncate,
20    /// 文本对齐
21    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    /// 检查指定节点是否为当前焦点
44    pub fn is_focused(&self, id: NodeId) -> bool {
45        self.focused_id == Some(id)
46    }
47
48    /// 计算对齐偏移(根据当前 align 设置和有效裁剪区域)
49    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    /// 写入文本使用的实际裁剪区域
60    fn effective_clip(&self) -> Rect {
61        self.clip_rect.unwrap_or(self.rect)
62    }
63
64    /// 换行时使用的裁剪(放宽垂直限制)
65    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); // 垂直不裁剪
69        }
70        clip
71    }
72
73    /// 在 cursor 位置写入文本,推进 x(考虑宽字符和截断)
74    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    /// 写入文本后将 cursor 移到下一行行首(支持换行和截断)
106    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); // 换行时放宽垂直裁剪
118        }
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    /// 修改当前渲染样式
188    pub fn set_style(&mut self, style: Style) {
189        self.style = style;
190    }
191
192    /// 绘制边框(使用当前样式,遵循 clip_rect)
193    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}