use crate::model::{ColumnId, Grid, RowId};
pub const MAX_PIXMAP_AXIS: u32 = 8192;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Viewport {
pub origin_x: u32,
pub origin_y: u32,
pub width: u32,
pub height: u32,
}
impl Default for Viewport {
fn default() -> Self {
Self::window(960, 360)
}
}
impl Viewport {
#[must_use]
pub const fn window(width: u32, height: u32) -> Self {
Self {
origin_x: 0,
origin_y: 0,
width,
height,
}
}
#[must_use]
pub const fn scrolled(mut self, origin_x: u32, origin_y: u32) -> Self {
self.origin_x = origin_x;
self.origin_y = origin_y;
self
}
#[must_use]
pub fn clamp_to(mut self, metrics: &TableMetrics) -> Self {
let max_x = metrics.content_width.saturating_sub(1);
let body = metrics.row_count.saturating_mul(metrics.row_height);
self.origin_x = self.origin_x.min(max_x);
self.origin_y = self.origin_y.min(body);
self
}
#[must_use]
pub fn scroll_by(self, dx: i32, dy: i32, metrics: &TableMetrics) -> Self {
let nx = (i64::from(self.origin_x) + i64::from(dx)).max(0) as u32;
let ny = (i64::from(self.origin_y) + i64::from(dy)).max(0) as u32;
self.scrolled(nx, ny).clamp_to(metrics)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Pointer {
pub x: u32,
pub y: u32,
}
impl Pointer {
#[must_use]
pub const fn new(x: u32, y: u32) -> Self {
Self { x, y }
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum HitKind {
Header,
Cell,
Sample,
TickX,
TickY,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub struct Hit {
pub row: RowId,
pub col: ColumnId,
pub row_index: Option<u32>,
pub col_index: u32,
pub kind: HitKind,
}
#[derive(Debug, Clone, Copy)]
pub(crate) struct CellBox {
pub row_index: Option<u32>,
pub col_index: u32,
pub row: RowId,
pub col: ColumnId,
pub x: u32,
pub y: u32,
pub w: u32,
pub h: u32,
pub header: bool,
pub kind: HitKind,
}
impl CellBox {
pub(crate) fn contains(self, x: u32, y: u32) -> bool {
x >= self.x
&& y >= self.y
&& x < self.x.saturating_add(self.w)
&& y < self.y.saturating_add(self.h)
}
pub(crate) fn hit(self) -> Hit {
Hit {
row: self.row,
col: self.col,
row_index: self.row_index,
col_index: self.col_index,
kind: self.kind,
}
}
}
#[derive(Debug, Clone, Default)]
pub struct HitMap {
pub(crate) boxes: Vec<CellBox>,
}
impl HitMap {
#[must_use]
pub fn at(&self, x: u32, y: u32) -> Option<Hit> {
self.boxes
.iter()
.rev()
.find(|b| b.contains(x, y))
.map(|b| b.hit())
}
#[must_use]
pub fn len(&self) -> usize {
self.boxes.len()
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.boxes.is_empty()
}
}
pub(crate) const PAD: u32 = 8;
pub(crate) const ROW_H: u32 = 18;
pub(crate) const HEADER_H: u32 = 20;
pub(crate) const CHAR_W: u32 = 6;
pub(crate) const CELL_PAD_X: u32 = 8;
pub(crate) const MIN_COL: u32 = 56;
pub(crate) const MAX_COL: u32 = 240;
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct TableMetrics {
pub content_width: u32,
pub content_height: u32,
pub col_widths: Vec<u32>,
pub row_height: u32,
pub header_height: u32,
pub pad: u32,
pub row_count: u32,
pub col_count: u32,
}
impl TableMetrics {
#[must_use]
pub fn body_top(&self) -> u32 {
self.pad.saturating_add(self.header_height)
}
#[must_use]
pub fn max_origin_x(&self) -> u32 {
self.content_width.saturating_sub(1)
}
#[must_use]
pub fn max_origin_y(&self) -> u32 {
self.row_count.saturating_mul(self.row_height)
}
#[must_use]
pub fn resize_column(&self, index: usize, width: u32) -> Vec<u32> {
let mut widths = self.col_widths.clone();
if let Some(slot) = widths.get_mut(index) {
*slot = width.max(8);
}
widths
}
#[must_use]
pub fn with_resized_column(&self, index: usize, width: u32) -> Self {
let col_widths = self.resize_column(index, width);
let content_width = col_widths
.iter()
.copied()
.fold(0u32, u32::saturating_add)
.saturating_add(self.pad.saturating_mul(2))
.max(1);
Self {
content_width,
col_widths,
..self.clone()
}
}
}
pub(crate) struct TableGeom {
pub pixmap_w: u32,
pub pixmap_h: u32,
pub content_width: u32,
pub content_height: u32,
pub col_widths: Vec<u32>,
pub boxes: Vec<CellBox>,
pub first_row: u32,
pub row_end: u32,
pub origin_x: u32,
pub origin_y: u32,
}
pub(crate) fn measure_col_widths(
grid: &Grid,
placeholder: &str,
override_w: Option<&[u32]>,
) -> Vec<u32> {
let n = grid.columns.len();
let mut col_widths = Vec::with_capacity(n);
let mut buf = String::with_capacity(32);
for (ci, col) in grid.columns.iter().enumerate() {
if let Some(w) = override_w.and_then(|ws| ws.get(ci)).copied() {
col_widths.push(w.max(8));
continue;
}
let mut max_chars = col.header.chars().count();
for row in &grid.rows {
if let Some(cell) = row.cells.get(ci) {
buf.clear();
cell.write_paint(&mut buf, placeholder);
max_chars = max_chars.max(buf.chars().count());
}
}
let w =
((max_chars as u32).saturating_mul(CHAR_W) + CELL_PAD_X * 2).clamp(MIN_COL, MAX_COL);
col_widths.push(w);
}
col_widths
}
pub(crate) fn content_size(n_rows: u32, col_widths: &[u32]) -> (u32, u32) {
let content_w = col_widths
.iter()
.copied()
.fold(0u32, u32::saturating_add)
.saturating_add(PAD.saturating_mul(2))
.max(1);
let content_h = PAD
.saturating_add(HEADER_H)
.saturating_add(n_rows.saturating_mul(ROW_H))
.saturating_add(PAD)
.max(1);
(content_w, content_h)
}
pub(crate) fn metrics(grid: &Grid, placeholder: &str, override_w: Option<&[u32]>) -> TableMetrics {
let col_widths = measure_col_widths(grid, placeholder, override_w);
let n_rows = grid.rows.len() as u32;
let (content_width, content_height) = content_size(n_rows, &col_widths);
TableMetrics {
content_width,
content_height,
col_count: col_widths.len() as u32,
col_widths,
row_height: ROW_H,
header_height: HEADER_H,
pad: PAD,
row_count: n_rows,
}
}
pub(crate) fn layout_visible(
grid: &Grid,
placeholder: &str,
viewport: Viewport,
override_w: Option<&[u32]>,
) -> TableGeom {
let col_widths = measure_col_widths(grid, placeholder, override_w);
let n_rows = grid.rows.len() as u32;
let (content_width, content_height) = content_size(n_rows, &col_widths);
let origin_x = viewport.origin_x.min(content_width.saturating_sub(1));
let body_h = n_rows.saturating_mul(ROW_H);
let origin_y = viewport.origin_y.min(body_h);
let remain_w = content_width.saturating_sub(origin_x).max(1);
let remain_h = PAD
.saturating_add(HEADER_H)
.saturating_add(body_h.saturating_sub(origin_y))
.saturating_add(PAD)
.max(1);
let pixmap_w = viewport.width.clamp(1, MAX_PIXMAP_AXIS).min(remain_w);
let pixmap_h = viewport.height.clamp(1, MAX_PIXMAP_AXIS).min(remain_h);
let body_top = PAD + HEADER_H;
let first_row = origin_y / ROW_H;
let mut row_end = first_row;
if n_rows > 0 && pixmap_h > body_top {
let visible_body = pixmap_h.saturating_sub(body_top);
let skip_px = origin_y % ROW_H;
let rows_fit = visible_body
.saturating_add(skip_px)
.saturating_add(ROW_H.saturating_sub(1))
/ ROW_H;
row_end = first_row.saturating_add(rows_fit).min(n_rows);
} else if n_rows > 0 && pixmap_h > PAD {
row_end = first_row.saturating_add(1).min(n_rows);
}
let mut boxes = Vec::new();
let mut x_content = PAD;
for (ci, col) in grid.columns.iter().enumerate() {
let w = col_widths[ci];
if let Some((x, vis_w)) = clip_axis(x_content, w, origin_x, pixmap_w) {
boxes.push(CellBox {
row_index: None,
col_index: ci as u32,
row: RowId(0),
col: col.id,
x,
y: PAD,
w: vis_w,
h: HEADER_H,
header: true,
kind: HitKind::Header,
});
}
x_content = x_content.saturating_add(w);
}
for ri in first_row..row_end {
let Some(row) = grid.rows.get(ri as usize) else {
break;
};
let y_pm = body_top as i64 + i64::from(ri) * i64::from(ROW_H) - i64::from(origin_y);
let y_bottom = y_pm + i64::from(ROW_H);
let vis_y1 = y_pm.max(i64::from(body_top)).max(0);
let vis_y2 = y_bottom.min(i64::from(pixmap_h));
if vis_y2 <= vis_y1 {
continue;
}
let mut x_content = PAD;
for (ci, col) in grid.columns.iter().enumerate() {
let w = col_widths[ci];
if let Some((x, vis_w)) = clip_axis(x_content, w, origin_x, pixmap_w) {
boxes.push(CellBox {
row_index: Some(ri),
col_index: ci as u32,
row: row.id,
col: col.id,
x,
y: vis_y1 as u32,
w: vis_w,
h: (vis_y2 - vis_y1) as u32,
header: false,
kind: HitKind::Cell,
});
}
x_content = x_content.saturating_add(w);
}
}
TableGeom {
pixmap_w,
pixmap_h,
content_width,
content_height,
col_widths,
boxes,
first_row,
row_end,
origin_x,
origin_y,
}
}
fn clip_axis(content_x: u32, span: u32, origin: u32, pixmap: u32) -> Option<(u32, u32)> {
let x1 = content_x as i64 - origin as i64;
let x2 = x1 + span as i64;
let vis_x1 = x1.max(0);
let vis_x2 = x2.min(pixmap as i64);
if vis_x2 <= vis_x1 {
return None;
}
Some((vis_x1 as u32, (vis_x2 - vis_x1) as u32))
}