use std::num::NonZeroU32;
use std::ops::Range;
use lune_pty::PtySize;
use lune_terminal::{CellPosition, MAX_CELLS, PixelPosition, PixelSize};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct PixelRect {
pub origin: PixelPosition,
pub size: PixelSize,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct GridGeometry {
size: PtySize,
cell: Option<PixelSize>,
}
impl GridGeometry {
pub fn fit(available: PixelSize, cell: PixelSize) -> Option<Self> {
let columns = u16::try_from(available.width.get() / cell.width).ok()?;
let rows = u16::try_from(available.height.get() / cell.height).ok()?;
if columns == 0 || rows == 0 || u32::from(columns) * u32::from(rows) > MAX_CELLS as u32 {
return None;
}
let pixel_width = u16::try_from(u32::from(columns) * cell.width.get()).ok()?;
let pixel_height = u16::try_from(u32::from(rows) * cell.height.get()).ok()?;
Some(Self {
size: PtySize {
rows,
columns,
pixel_width,
pixel_height,
},
cell: Some(cell),
})
}
pub(crate) fn from_pty_size(size: PtySize) -> Self {
let cell = exact_cell_extent(size.pixel_width, size.columns)
.zip(exact_cell_extent(size.pixel_height, size.rows))
.map(|(width, height)| PixelSize { width, height });
Self { size, cell }
}
pub fn pty_size(self) -> PtySize {
self.size
}
pub fn text_area_pixels(self) -> Option<PixelSize> {
let width = NonZeroU32::new(u32::from(self.size.pixel_width))?;
let height = NonZeroU32::new(u32::from(self.size.pixel_height))?;
Some(PixelSize { width, height })
}
pub fn cell_pixels(self) -> Option<PixelSize> {
self.cell
}
pub(crate) fn contains(self, pixel: PixelPosition) -> bool {
pixel.x < u32::from(self.size.pixel_width) && pixel.y < u32::from(self.size.pixel_height)
}
pub fn cell_at(self, pixel: PixelPosition) -> Option<CellPosition> {
let cell = self.cell?;
self.contains(pixel).then(|| CellPosition {
row: (pixel.y / cell.height) as usize,
column: (pixel.x / cell.width) as usize,
})
}
pub fn cell_center_pixel(self, cell: CellPosition) -> Option<PixelPosition> {
let metrics = self.cell?;
if cell.row >= usize::from(self.size.rows) || cell.column >= usize::from(self.size.columns)
{
return None;
}
Some(PixelPosition {
x: cell.column as u32 * metrics.width.get() + metrics.width.get() / 2,
y: cell.row as u32 * metrics.height.get() + metrics.height.get() / 2,
})
}
#[inline]
pub fn row_rect(self, row: usize, columns: Range<usize>) -> Option<PixelRect> {
let cell = self.cell?;
let end = columns.end.min(usize::from(self.size.columns));
if row >= usize::from(self.size.rows) || columns.start >= end {
return None;
}
let width = NonZeroU32::new((end - columns.start) as u32 * cell.width.get())?;
Some(PixelRect {
origin: PixelPosition {
x: columns.start as u32 * cell.width.get(),
y: row as u32 * cell.height.get(),
},
size: PixelSize {
width,
height: cell.height,
},
})
}
}
fn exact_cell_extent(pixels: u16, cells: u16) -> Option<NonZeroU32> {
if cells == 0 || !pixels.is_multiple_of(cells) {
return None;
}
NonZeroU32::new(u32::from(pixels / cells))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn cell_center_pixel_round_trips_inside_grid_and_rejects_out_of_bounds() {
let cell = PixelSize {
width: NonZeroU32::new(10).unwrap(),
height: NonZeroU32::new(20).unwrap(),
};
let available = PixelSize {
width: NonZeroU32::new(80).unwrap(),
height: NonZeroU32::new(40).unwrap(),
};
let grid = GridGeometry::fit(available, cell).unwrap();
let target = CellPosition { row: 1, column: 3 };
let center = grid.cell_center_pixel(target).unwrap();
assert_eq!(center, PixelPosition { x: 35, y: 30 });
assert_eq!(grid.cell_at(center), Some(target));
assert!(
grid.cell_center_pixel(CellPosition { row: 2, column: 0 })
.is_none()
);
assert!(
grid.cell_center_pixel(CellPosition { row: 0, column: 8 })
.is_none()
);
}
}