forked from rhysd/tui-textarea
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwidget.rs
166 lines (146 loc) · 5.73 KB
/
widget.rs
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
use crate::ratatui::buffer::Buffer;
use crate::ratatui::layout::Rect;
use crate::ratatui::text::Text;
use crate::ratatui::widgets::{Paragraph, Widget};
use crate::textarea::TextArea;
use crate::util::num_digits;
use std::cmp;
use std::sync::atomic::{AtomicU64, Ordering};
// &mut 'a (u16, u16, u16, u16) is not available since Renderer instance totally takes over the ownership of TextArea
// instance. In the case, the TextArea instance cannot be accessed from any other objects since it is mutablly
// borrowed.
//
// `tui::terminal::Frame::render_stateful_widget` would be an assumed way to render a stateful widget. But at this
// point we stick with using `tui::terminal::Frame::render_widget` because it is simpler API. Users don't need to
// manage states of textarea instances separately.
// https://docs.rs/tui/latest/tui/terminal/struct.Frame.html#method.render_stateful_widget
#[derive(Default, Debug)]
pub struct Viewport(AtomicU64);
impl Clone for Viewport {
fn clone(&self) -> Self {
let u = self.0.load(Ordering::Relaxed);
Viewport(AtomicU64::new(u))
}
}
impl Viewport {
pub fn scroll_top(&self) -> (u16, u16) {
let u = self.0.load(Ordering::Relaxed);
((u >> 16) as u16, u as u16)
}
pub fn rect(&self) -> (u16, u16, u16, u16) {
let u = self.0.load(Ordering::Relaxed);
let width = (u >> 48) as u16;
let height = (u >> 32) as u16;
let row = (u >> 16) as u16;
let col = u as u16;
(row, col, width, height)
}
pub fn position(&self) -> (u16, u16, u16, u16) {
let (row_top, col_top, width, height) = self.rect();
let row_bottom = row_top.saturating_add(height).saturating_sub(1);
let col_bottom = col_top.saturating_add(width).saturating_sub(1);
(
row_top,
col_top,
cmp::max(row_top, row_bottom),
cmp::max(col_top, col_bottom),
)
}
fn store(&self, row: u16, col: u16, width: u16, height: u16) {
// Pack four u16 values into one u64 value
let u =
((width as u64) << 48) | ((height as u64) << 32) | ((row as u64) << 16) | col as u64;
self.0.store(u, Ordering::Relaxed);
}
pub fn scroll(&mut self, rows: i16, cols: i16) {
fn apply_scroll(pos: u16, delta: i16) -> u16 {
if delta >= 0 {
pos.saturating_add(delta as u16)
} else {
pos.saturating_sub(-delta as u16)
}
}
let u = self.0.get_mut();
let row = apply_scroll((*u >> 16) as u16, rows);
let col = apply_scroll(*u as u16, cols);
*u = (*u & 0xffff_ffff_0000_0000) | ((row as u64) << 16) | (col as u64);
}
}
pub struct Renderer<'a>(&'a TextArea<'a>);
impl<'a> Renderer<'a> {
pub fn new(textarea: &'a TextArea<'a>) -> Self {
Self(textarea)
}
#[inline]
fn text(&self, top_row: usize, height: usize) -> Text<'a> {
let lines_len = self.0.lines().len();
let lnum_len = num_digits(lines_len);
let bottom_row = cmp::min(top_row + height, lines_len);
let mut lines = Vec::with_capacity(bottom_row - top_row);
for (i, line) in self.0.lines()[top_row..bottom_row].iter().enumerate() {
lines.push(self.0.line_spans(line.as_str(), top_row + i, lnum_len));
}
Text::from(lines)
}
}
impl<'a> Widget for Renderer<'a> {
fn render(self, area: Rect, buf: &mut Buffer) {
let Rect { width, height, .. } = if let Some(b) = self.0.block() {
b.inner(area)
} else {
area
};
fn next_scroll_top(prev_top: u16, cursor: u16, length: u16) -> u16 {
if cursor < prev_top {
cursor
} else if prev_top + length <= cursor {
cursor + 1 - length
} else {
prev_top
}
}
let cursor = self.0.cursor();
let (top_row, top_col) = self.0.viewport.scroll_top();
let top_row = next_scroll_top(top_row, cursor.0 as u16, height);
let top_col = next_scroll_top(top_col, cursor.1 as u16, width);
let (text, style) = if !self.0.placeholder.is_empty() && self.0.is_empty() {
#[cfg(any(
feature = "tuirs-crossterm",
feature = "tuirs-termion",
feature = "tuirs-no-backend",
))]
let text = Text::from(self.0.placeholder.as_str());
#[cfg(not(any(
feature = "tuirs-crossterm",
feature = "tuirs-termion",
feature = "tuirs-no-backend",
)))]
let text = if self.0.show_placeholder_with_cursor {
let mut text = self.text(top_row as usize, height as usize);
text.push_span(self.0.placeholder.as_str());
text
} else {
Text::from(self.0.placeholder.as_str())
};
(text, self.0.placeholder_style)
} else {
(self.text(top_row as usize, height as usize), self.0.style())
};
// To get fine control over the text color and the surrrounding block they have to be rendered separately
// see https://github.com/ratatui-org/ratatui/issues/144
let mut text_area = area;
let mut inner = Paragraph::new(text)
.style(style)
.alignment(self.0.alignment());
if let Some(b) = self.0.block() {
text_area = b.inner(area);
b.clone().render(area, buf)
}
if top_col != 0 {
inner = inner.scroll((0, top_col));
}
// Store scroll top position for rendering on the next tick
self.0.viewport.store(top_row, top_col, width, height);
inner.render(text_area, buf);
}
}