1use crate::appearance::TypeScale;
7use crate::model::{ColumnId, Grid, RowId};
8
9pub const MAX_PIXMAP_AXIS: u32 = 8192;
11
12#[derive(Debug, Clone, Copy, PartialEq, Eq)]
14pub struct Viewport {
15 pub origin_x: u32,
17 pub origin_y: u32,
19 pub width: u32,
21 pub height: u32,
23}
24
25impl Default for Viewport {
26 fn default() -> Self {
27 Self::window(960, 360)
28 }
29}
30
31impl Viewport {
32 #[must_use]
34 pub const fn window(width: u32, height: u32) -> Self {
35 Self {
36 origin_x: 0,
37 origin_y: 0,
38 width,
39 height,
40 }
41 }
42
43 #[must_use]
45 pub const fn scrolled(mut self, origin_x: u32, origin_y: u32) -> Self {
46 self.origin_x = origin_x;
47 self.origin_y = origin_y;
48 self
49 }
50
51 #[must_use]
53 pub fn clamp_to(mut self, metrics: &TableMetrics) -> Self {
54 let max_x = metrics.content_width.saturating_sub(1);
55 let body = metrics.row_count.saturating_mul(metrics.row_height);
56 self.origin_x = self.origin_x.min(max_x);
57 self.origin_y = self.origin_y.min(body);
58 self
59 }
60
61 #[must_use]
63 pub fn scroll_by(self, dx: i32, dy: i32, metrics: &TableMetrics) -> Self {
64 let nx = (i64::from(self.origin_x) + i64::from(dx)).max(0) as u32;
65 let ny = (i64::from(self.origin_y) + i64::from(dy)).max(0) as u32;
66 self.scrolled(nx, ny).clamp_to(metrics)
67 }
68}
69
70#[derive(Debug, Clone, Copy, PartialEq, Eq)]
72pub struct Pointer {
73 pub x: u32,
75 pub y: u32,
77}
78
79impl Pointer {
80 #[must_use]
82 pub const fn new(x: u32, y: u32) -> Self {
83 Self { x, y }
84 }
85}
86
87#[derive(Debug, Clone, Copy, PartialEq, Eq)]
89#[non_exhaustive]
90pub enum HitKind {
91 Header,
93 Cell,
95 Sample,
97 TickX,
99 TickY,
101 Node,
103}
104
105#[derive(Debug, Clone, Copy, PartialEq, Eq)]
107#[non_exhaustive]
108pub struct Hit {
109 pub row: RowId,
111 pub col: ColumnId,
113 pub row_index: Option<u32>,
115 pub col_index: u32,
117 pub kind: HitKind,
119}
120
121#[derive(Debug, Clone, Copy)]
123pub(crate) struct CellBox {
124 pub row_index: Option<u32>,
125 pub col_index: u32,
126 pub row: RowId,
127 pub col: ColumnId,
128 pub x: u32,
129 pub y: u32,
130 pub w: u32,
131 pub h: u32,
132 pub header: bool,
133 pub kind: HitKind,
134}
135
136impl CellBox {
137 pub(crate) fn contains(self, x: u32, y: u32) -> bool {
138 x >= self.x
139 && y >= self.y
140 && x < self.x.saturating_add(self.w)
141 && y < self.y.saturating_add(self.h)
142 }
143
144 pub(crate) fn hit(self) -> Hit {
145 Hit {
146 row: self.row,
147 col: self.col,
148 row_index: self.row_index,
149 col_index: self.col_index,
150 kind: self.kind,
151 }
152 }
153}
154
155#[derive(Debug, Clone, Default)]
157pub struct HitMap {
158 pub(crate) boxes: Vec<CellBox>,
159}
160
161impl HitMap {
162 #[must_use]
164 pub fn at(&self, x: u32, y: u32) -> Option<Hit> {
165 self.boxes
166 .iter()
167 .rev()
168 .find(|b| b.contains(x, y))
169 .map(|b| b.hit())
170 }
171
172 #[must_use]
174 pub fn len(&self) -> usize {
175 self.boxes.len()
176 }
177
178 #[must_use]
180 pub fn is_empty(&self) -> bool {
181 self.boxes.is_empty()
182 }
183}
184
185#[derive(Debug, Clone, PartialEq, Eq)]
187#[non_exhaustive]
188pub struct TableMetrics {
189 pub content_width: u32,
191 pub content_height: u32,
193 pub col_widths: Vec<u32>,
195 pub row_height: u32,
197 pub header_height: u32,
199 pub pad: u32,
201 pub row_count: u32,
203 pub col_count: u32,
205}
206
207impl TableMetrics {
208 #[must_use]
210 pub fn body_top(&self) -> u32 {
211 self.pad.saturating_add(self.header_height)
212 }
213
214 #[must_use]
216 pub fn max_origin_x(&self) -> u32 {
217 self.content_width.saturating_sub(1)
218 }
219
220 #[must_use]
222 pub fn max_origin_y(&self) -> u32 {
223 self.row_count.saturating_mul(self.row_height)
224 }
225
226 #[must_use]
228 pub fn resize_column(&self, index: usize, width: u32) -> Vec<u32> {
229 let mut widths = self.col_widths.clone();
230 if let Some(slot) = widths.get_mut(index) {
231 *slot = width.max(8);
232 }
233 widths
234 }
235
236 #[must_use]
238 pub fn with_resized_column(&self, index: usize, width: u32) -> Self {
239 let col_widths = self.resize_column(index, width);
240 let content_width = col_widths
241 .iter()
242 .copied()
243 .fold(0u32, u32::saturating_add)
244 .saturating_add(self.pad.saturating_mul(2))
245 .max(1);
246 Self {
247 content_width,
248 col_widths,
249 ..self.clone()
250 }
251 }
252}
253
254pub(crate) struct TableGeom {
255 pub pixmap_w: u32,
256 pub pixmap_h: u32,
257 pub content_width: u32,
258 pub content_height: u32,
259 pub col_widths: Vec<u32>,
260 pub boxes: Vec<CellBox>,
261 pub first_row: u32,
262 pub row_end: u32,
263 pub origin_x: u32,
264 pub origin_y: u32,
265 pub pad: u32,
266 pub row_h: u32,
267 pub header_h: u32,
268}
269
270pub(crate) fn measure_col_widths(
271 grid: &Grid,
272 placeholder: &str,
273 override_w: Option<&[u32]>,
274 scale: TypeScale,
275) -> Vec<u32> {
276 let n = grid.columns.len();
277 let mut col_widths = Vec::with_capacity(n);
278 let mut buf = String::with_capacity(32);
279 let adv = scale.advance();
280 let pad_x = scale.cell_pad_x();
281 for (ci, col) in grid.columns.iter().enumerate() {
282 if let Some(w) = override_w.and_then(|ws| ws.get(ci)).copied() {
283 col_widths.push(w.max(8));
284 continue;
285 }
286 let mut max_chars = col.header.chars().count();
287 for row in &grid.rows {
288 if let Some(cell) = row.cells.get(ci) {
289 buf.clear();
290 cell.write_paint(&mut buf, placeholder);
291 max_chars = max_chars.max(buf.chars().count());
292 }
293 }
294 let w = ((max_chars as u32).saturating_mul(adv) + pad_x * 2)
295 .clamp(scale.min_col(), scale.max_col());
296 col_widths.push(w);
297 }
298 col_widths
299}
300
301pub(crate) fn content_size(n_rows: u32, col_widths: &[u32], scale: TypeScale) -> (u32, u32) {
302 let pad = scale.pad();
303 let content_w = col_widths
304 .iter()
305 .copied()
306 .fold(0u32, u32::saturating_add)
307 .saturating_add(pad.saturating_mul(2))
308 .max(1);
309 let content_h = pad
310 .saturating_add(scale.header_height())
311 .saturating_add(n_rows.saturating_mul(scale.row_height()))
312 .saturating_add(pad)
313 .max(1);
314 (content_w, content_h)
315}
316
317pub(crate) fn metrics(
318 grid: &Grid,
319 placeholder: &str,
320 override_w: Option<&[u32]>,
321 scale: TypeScale,
322) -> TableMetrics {
323 let col_widths = measure_col_widths(grid, placeholder, override_w, scale);
324 let n_rows = grid.rows.len() as u32;
325 let (content_width, content_height) = content_size(n_rows, &col_widths, scale);
326 TableMetrics {
327 content_width,
328 content_height,
329 col_count: col_widths.len() as u32,
330 col_widths,
331 row_height: scale.row_height(),
332 header_height: scale.header_height(),
333 pad: scale.pad(),
334 row_count: n_rows,
335 }
336}
337
338pub(crate) fn layout_visible(
339 grid: &Grid,
340 placeholder: &str,
341 viewport: Viewport,
342 override_w: Option<&[u32]>,
343 scale: TypeScale,
344) -> TableGeom {
345 let pad = scale.pad();
346 let row_h = scale.row_height();
347 let header_h = scale.header_height();
348 let col_widths = measure_col_widths(grid, placeholder, override_w, scale);
349 let n_rows = grid.rows.len() as u32;
350 let (content_width, content_height) = content_size(n_rows, &col_widths, scale);
351
352 let origin_x = viewport.origin_x.min(content_width.saturating_sub(1));
353 let body_h = n_rows.saturating_mul(row_h);
354 let origin_y = viewport.origin_y.min(body_h);
355
356 let remain_w = content_width.saturating_sub(origin_x).max(1);
357 let remain_h = pad
358 .saturating_add(header_h)
359 .saturating_add(body_h.saturating_sub(origin_y))
360 .saturating_add(pad)
361 .max(1);
362 let pixmap_w = viewport.width.clamp(1, MAX_PIXMAP_AXIS).min(remain_w);
363 let pixmap_h = viewport.height.clamp(1, MAX_PIXMAP_AXIS).min(remain_h);
364
365 let body_top = pad + header_h;
366 let first_row = origin_y.checked_div(row_h).unwrap_or(0);
367 let mut row_end = first_row;
368 if n_rows > 0 && pixmap_h > body_top {
369 let visible_body = pixmap_h.saturating_sub(body_top);
370 let skip_px = origin_y % row_h;
371 let rows_fit = visible_body
372 .saturating_add(skip_px)
373 .saturating_add(row_h.saturating_sub(1))
374 / row_h;
375 row_end = first_row.saturating_add(rows_fit).min(n_rows);
376 } else if n_rows > 0 && pixmap_h > pad {
377 row_end = first_row.saturating_add(1).min(n_rows);
378 }
379
380 let mut boxes = Vec::new();
381 let mut x_content = pad;
382 for (ci, col) in grid.columns.iter().enumerate() {
383 let w = col_widths[ci];
384 if let Some((x, vis_w)) = clip_axis(x_content, w, origin_x, pixmap_w) {
385 boxes.push(CellBox {
386 row_index: None,
387 col_index: ci as u32,
388 row: RowId(0),
389 col: col.id,
390 x,
391 y: pad,
392 w: vis_w,
393 h: header_h,
394 header: true,
395 kind: HitKind::Header,
396 });
397 }
398 x_content = x_content.saturating_add(w);
399 }
400
401 for ri in first_row..row_end {
402 let Some(row) = grid.rows.get(ri as usize) else {
403 break;
404 };
405 let y_pm = body_top as i64 + i64::from(ri) * i64::from(row_h) - i64::from(origin_y);
406 let y_bottom = y_pm + i64::from(row_h);
407 let vis_y1 = y_pm.max(i64::from(body_top)).max(0);
408 let vis_y2 = y_bottom.min(i64::from(pixmap_h));
409 if vis_y2 <= vis_y1 {
410 continue;
411 }
412 let mut x_content = pad;
413 for (ci, col) in grid.columns.iter().enumerate() {
414 let w = col_widths[ci];
415 if let Some((x, vis_w)) = clip_axis(x_content, w, origin_x, pixmap_w) {
416 boxes.push(CellBox {
417 row_index: Some(ri),
418 col_index: ci as u32,
419 row: row.id,
420 col: col.id,
421 x,
422 y: vis_y1 as u32,
423 w: vis_w,
424 h: (vis_y2 - vis_y1) as u32,
425 header: false,
426 kind: HitKind::Cell,
427 });
428 }
429 x_content = x_content.saturating_add(w);
430 }
431 }
432
433 TableGeom {
434 pixmap_w,
435 pixmap_h,
436 content_width,
437 content_height,
438 col_widths,
439 boxes,
440 first_row,
441 row_end,
442 origin_x,
443 origin_y,
444 pad,
445 row_h,
446 header_h,
447 }
448}
449
450fn clip_axis(content_x: u32, span: u32, origin: u32, pixmap: u32) -> Option<(u32, u32)> {
452 let x1 = content_x as i64 - origin as i64;
453 let x2 = x1 + span as i64;
454 let vis_x1 = x1.max(0);
455 let vis_x2 = x2.min(pixmap as i64);
456 if vis_x2 <= vis_x1 {
457 return None;
458 }
459 Some((vis_x1 as u32, (vis_x2 - vis_x1) as u32))
460}