.cargo/
crates/
brain/
diff/
files/
highlight/
repo/
signals/
tui/
tui-core/
src/
ansi/
aligned.rs
2.3 KiB
ansi.rs
2.3 KiB
app.rs
4.2 KiB
backend.rs
3.9 KiB
border.rs
6.5 KiB
buffer.rs
20.6 KiB
cell.rs
7.2 KiB
colors.rs
4.2 KiB
event.rs
3.4 KiB
frame.rs
3.7 KiB
hstack.rs
6.5 KiB
label.rs
5.5 KiB
layout.rs
11.2 KiB
lib.rs
2.4 KiB
primitive.rs
19.4 KiB
table.rs
23.5 KiB
terminal.rs
5.2 KiB
text.rs
3.0 KiB
textarea.rs
6.4 KiB
vstack.rs
16.4 KiB
widget.rs
11.0 KiB
Cargo.toml
298 B
README.md
521 B
tui-termion/
tui-widgets/
scripts/
src/
.gitignore
30 B
.gitsigners
198 B
AGENTS.md
1.9 KiB
CONTRIBUTING
1.7 KiB
Cargo.lock
5.4 KiB
Cargo.toml
1.9 KiB
LICENSE
1.1 KiB
README.md
245 B
STYLE
3.7 KiB
clippy.toml
213 B
crates/tui-core/src/frame.rs
raw
| 1 | //! Render-time frame passed to applications. |
| 2 | //! |
| 3 | //! A frame exposes the current drawing area and mutable buffer for one render |
| 4 | //! pass. Applications render widgets into this frame; terminal diffing and |
| 5 | //! backend flushing happen after the frame is complete. |
| 6 | |
| 7 | use crate::buffer::Buffer; |
| 8 | use crate::layout::{Point, Rect}; |
| 9 | use crate::{StatefulWidget, Widget}; |
| 10 | |
| 11 | /// Frame passed to application render methods. |
| 12 | #[derive(Debug)] |
| 13 | pub struct Frame<'a> { |
| 14 | /// Area available for this frame. |
| 15 | pub area: Rect, |
| 16 | /// Buffer receiving rendered cells. |
| 17 | pub buffer: &'a mut Buffer, |
| 18 | /// Requested cursor position after rendering. |
| 19 | pub cursor: Option<Point>, |
| 20 | } |
| 21 | |
| 22 | impl<'a> Frame<'a> { |
| 23 | /// Creates a frame over a buffer. |
| 24 | pub fn new(area: Rect, buffer: &'a mut Buffer) -> Self { |
| 25 | Self { |
| 26 | area, |
| 27 | buffer, |
| 28 | cursor: None, |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | /// Renders a widget into an area. |
| 33 | pub fn render_widget<W: Widget>(&mut self, mut widget: W, area: Rect) { |
| 34 | widget.render(area, self.buffer); |
| 35 | } |
| 36 | |
| 37 | /// Renders a stateful widget into an area. |
| 38 | pub fn render_stateful_widget<W: StatefulWidget>( |
| 39 | &mut self, |
| 40 | mut widget: W, |
| 41 | area: Rect, |
| 42 | state: &mut W::State, |
| 43 | ) { |
| 44 | let result = widget.render_with_state(area, self.buffer, state); |
| 45 | |
| 46 | if let Some(point) = result.cursor { |
| 47 | self.set_cursor_position(point); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | /// Sets the terminal cursor. |
| 52 | pub fn set_cursor_position(&mut self, point: Point) { |
| 53 | self.cursor = Some(point); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | #[cfg(test)] |
| 58 | mod tests { |
| 59 | use std::cell::Cell; |
| 60 | |
| 61 | use super::*; |
| 62 | use crate::{Constraint, Size, Style}; |
| 63 | |
| 64 | #[derive(Debug)] |
| 65 | struct LayoutProbe { |
| 66 | laid_out: Cell<bool>, |
| 67 | area: Cell<Rect>, |
| 68 | } |
| 69 | |
| 70 | #[derive(Debug)] |
| 71 | struct CursorProbe; |
| 72 | |
| 73 | impl Default for LayoutProbe { |
| 74 | fn default() -> Self { |
| 75 | Self { |
| 76 | laid_out: Cell::new(false), |
| 77 | area: Cell::new(Rect::default()), |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | impl Widget for LayoutProbe { |
| 83 | fn size(&self, parent: Constraint) -> Size { |
| 84 | parent.bounded_size() |
| 85 | } |
| 86 | |
| 87 | fn render(&mut self, area: Rect, buf: &mut Buffer) { |
| 88 | self.laid_out.set(true); |
| 89 | self.area.set(area); |
| 90 | buf.set_str(area.x, area.y, "R", Style::default()); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | impl StatefulWidget for CursorProbe { |
| 95 | type State = Point; |
| 96 | |
| 97 | fn size(&self, parent: Constraint) -> Size { |
| 98 | parent.bounded_size() |
| 99 | } |
| 100 | |
| 101 | fn render_with_state( |
| 102 | &mut self, |
| 103 | area: Rect, |
| 104 | buf: &mut Buffer, |
| 105 | state: &mut Self::State, |
| 106 | ) -> crate::RenderResult { |
| 107 | buf.set_str(area.x, area.y, "C", Style::default()); |
| 108 | *state = Point::new(area.x, area.y); |
| 109 | crate::RenderResult { |
| 110 | cursor: Some(*state), |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | #[test] |
| 116 | fn render_widget_renders_into_area() { |
| 117 | let mut buffer = Buffer::empty(Rect::new(0, 0, 4, 2)); |
| 118 | let area = Rect::new(1, 1, 2, 1); |
| 119 | let mut frame = Frame::new(buffer.area(), &mut buffer); |
| 120 | |
| 121 | frame.render_widget(LayoutProbe::default(), area); |
| 122 | |
| 123 | assert_eq!(buffer.lines(), vec![" ", " R "]); |
| 124 | } |
| 125 | |
| 126 | #[test] |
| 127 | fn render_stateful_widget_sets_requested_cursor() { |
| 128 | let mut buffer = Buffer::empty(Rect::new(0, 0, 4, 2)); |
| 129 | let area = Rect::new(1, 1, 2, 1); |
| 130 | let mut state = Point::new(0, 0); |
| 131 | let mut frame = Frame::new(buffer.area(), &mut buffer); |
| 132 | |
| 133 | frame.render_stateful_widget(CursorProbe, area, &mut state); |
| 134 | |
| 135 | assert_eq!(frame.cursor, Some(Point::new(1, 1))); |
| 136 | assert_eq!(buffer.lines(), vec![" ", " C "]); |
| 137 | } |
| 138 | } |