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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
use taffy::{
CompactLength,
prelude::{auto, length},
};
use crate::{Tui, TuiBuilderLogic, TuiId, tid};
/// Required parameters to correctly draw grid with virtual rows
pub struct VirtualGridRowHelperParams {
/// Header row count that needs to be skipped in the grid
pub header_row_count: u16,
/// Data row count in the grid excluding any header rows
pub row_count: usize,
}
/// Helper to draw grid with virtual rows
pub struct VirtualGridRowHelper;
/// Information about grid row that needs to be drawn
pub struct VirtualGridRow {
/// Index of data from 0..row_count
pub idx: usize,
/// Row position in the grid
///
/// Use [`GridRow::set_grid_row`] to retrieve closure that will set the style.
pub grid_row: u16,
}
impl VirtualGridRow {
/// Retrieve closure that can be used in `tui.mut_style(_)` to set grid_row parameter.
#[inline]
pub fn grid_row_setter(&self) -> impl Fn(&mut taffy::Style) {
let grid_row = self.grid_row;
move |style: &mut taffy::Style| {
style.grid_row = taffy::style_helpers::line(grid_row as i16);
}
}
/// Retrieve closure that can be used to generate unique ids for elements in the row
#[inline]
pub fn id_gen(&self) -> impl FnMut() -> TuiId {
let idx = self.idx;
let mut col_idx = 0;
move || {
col_idx += 1;
tid(("cell", idx, col_idx))
}
}
}
const fn round_up_to_pow2(value: usize, pow2: u8) -> usize {
value.saturating_add((1 << pow2) - 1) & !((1 << pow2) - 1)
}
const fn round_down_to_pow2(value: usize, pow2: u8) -> usize {
value & !((1 << pow2) - 1)
}
impl VirtualGridRowHelper {
/// Show virtual grid rows.
///
/// Closure receives information about grid row that needs to be drawn.
/// All virtual rows should have equal heaight. One row will be used to estimate height of all rows.
#[cfg_attr(feature = "tracing", tracing::instrument(level = "trace", skip_all))]
pub fn show<F>(params: VirtualGridRowHelperParams, tui: &mut Tui, mut draw_line: F)
where
F: FnMut(&mut Tui, VirtualGridRow),
{
let VirtualGridRowHelperParams {
row_count,
header_row_count,
} = params;
if row_count == 0 {
return;
}
// 1-based grid_row counter
let mut grid_row = header_row_count + 1;
// Draw first row for reference
draw_line(tui, VirtualGridRow { idx: 0, grid_row });
if row_count == 1 {
return;
}
let node_id = tui.current_node();
let min_location = (tui.taffy_container().full_container_with(false).min
- tui.current_viewport_content().min)
.y;
let (top_offset, row_height, gap) = {
let state = tui.taffy_state();
let style = state.taffy_tree().style(node_id).unwrap();
let gap = {
let raw = style.gap.height.into_raw();
match raw.tag() {
CompactLength::LENGTH_TAG => raw.value(),
CompactLength::PERCENT_TAG => 0.,
_ => 0.,
}
};
let mut top_offset = match style.overflow.y {
taffy::Overflow::Visible | taffy::Overflow::Clip | taffy::Overflow::Hidden => {
min_location
}
taffy::Overflow::Scroll => 0.,
};
let layout_detailed_info = state.taffy_tree().detailed_layout_info(node_id);
match layout_detailed_info {
taffy::DetailedLayoutInfo::Grid(detailed_grid_info) => {
// Calculate header offset
for idx in 0..((grid_row - 1) as usize) {
if let Some(row_size) = detailed_grid_info.rows.sizes.get(idx) {
top_offset += row_size;
}
if let Some(gutter) = detailed_grid_info.rows.gutters.get(idx) {
top_offset += gutter;
}
}
let row_height = detailed_grid_info
.rows
.sizes
.get((grid_row - 1) as usize)
.copied()
.unwrap_or(20.);
(top_offset, row_height, gap)
}
taffy::DetailedLayoutInfo::None => (top_offset, 20., gap),
}
};
let full_row_height = row_height + gap;
let scroll_offset = -(tui.last_scroll_offset.y + top_offset);
let visible_rect_size = tui.current_viewport().size().y;
// Round to power of 2 numbers to reduce frequency of taffy layout recalculation
// TODO: Maybe store interval in memory?
let pow2 = 3; // 2^3 = 8
// How many items should be drawn at top and bottom
let buffer = 4.;
let visible_from = round_down_to_pow2(
((scroll_offset / full_row_height).floor() - buffer).max(0.) as usize,
pow2,
)
.clamp(1, row_count);
let visible_to = round_up_to_pow2(
(((scroll_offset + visible_rect_size) / full_row_height).ceil() + buffer).max(0.)
as usize,
pow2,
)
.clamp(visible_from, row_count);
// println!(
// "{} {} {} | {} {} {} {} {}",
// visible_from,
// visible_to,
// row_count,
// row_height,
// gap,
// scroll_offset,
// top_offset,
// visible_rect_size
// );
if visible_from > 1 {
// Draw empty cell from 1..next_visible_from
let row_count_to_hide = visible_from - 1;
let height = (row_count_to_hide as f32) * full_row_height - gap;
grid_row += 1;
let size = taffy::Size {
width: length(0.),
height: length(height),
};
tui.id("top_virtual")
.style(taffy::Style {
min_size: size,
size,
max_size: size,
grid_row: taffy::style_helpers::line(grid_row as i16),
..Default::default()
})
.add_empty();
}
if visible_from < visible_to {
for row_idx in visible_from..visible_to {
grid_row += 1;
draw_line(
tui,
VirtualGridRow {
idx: row_idx,
grid_row,
},
);
}
}
if visible_to < row_count {
// Draw empty cell from visible_to..row_count
let row_count_to_hide = row_count - visible_to;
let height = (row_count_to_hide as f32) * full_row_height - gap;
grid_row += 1;
let size = taffy::Size {
width: auto(),
height: length(height),
};
tui.id("bottom_virtual")
.style(taffy::Style {
min_size: size,
size,
max_size: size,
grid_row: taffy::style_helpers::line(grid_row as i16),
..Default::default()
})
.add_empty();
}
}
}