use std::{hash::Hash, sync::Arc};
use gpui_kit::{Bounds, Pixels, Point, point, px, size};
use super::{KeyOf, LiveGridDelegate, LiveGridState, key_hash};
use crate::{model::GridRow, store::PinnedEdge};
pub(super) const EDGES: [PinnedEdge; 2] = [PinnedEdge::Top, PinnedEdge::Bottom];
pub(super) const fn band_slot(edge: PinnedEdge) -> usize {
match edge {
PinnedEdge::Top => 0,
PinnedEdge::Bottom => 1,
}
}
pub(crate) const fn band_name(edge: PinnedEdge) -> &'static str {
match edge {
PinnedEdge::Top => "live-grid-pinned-top",
PinnedEdge::Bottom => "live-grid-pinned-bottom",
}
}
pub(crate) fn pinned_key_hash<K: Hash + ?Sized>(edge: PinnedEdge, key: &K) -> u64 {
key_hash(&(band_name(edge), key))
}
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub(crate) struct BodyBands {
top: Bounds<Pixels>,
rows: Bounds<Pixels>,
bottom: Bounds<Pixels>,
row_height: Pixels,
pinned: [usize; 2],
totals: Option<PinnedEdge>,
}
impl BodyBands {
pub(super) fn new(
body: Bounds<Pixels>,
row_height: Pixels,
top: usize,
bottom: usize,
totals: Option<PinnedEdge>,
) -> Self {
let mut bands = Self {
row_height: row_height.max(px(0.)),
pinned: [top, bottom],
totals,
..Self::default()
};
let height = body.size.height.max(px(0.));
let line_height = bands.row_height;
let top_height = (line_height * bands.lines(PinnedEdge::Top) as f32).min(height);
let bottom_height =
(line_height * bands.lines(PinnedEdge::Bottom) as f32).min(height - top_height);
let (x, y, width) = (body.origin.x, body.origin.y, body.size.width);
bands.top = Bounds::new(point(x, y), size(width, top_height));
bands.rows = Bounds::new(
point(x, y + top_height),
size(width, height - top_height - bottom_height),
);
bands.bottom = Bounds::new(
point(x, y + height - bottom_height),
size(width, bottom_height),
);
bands
}
pub(crate) fn rows(&self) -> Bounds<Pixels> {
self.rows
}
pub(crate) fn band(&self, edge: PinnedEdge) -> Bounds<Pixels> {
match edge {
PinnedEdge::Top => self.top,
PinnedEdge::Bottom => self.bottom,
}
}
pub(super) fn lines(&self, edge: PinnedEdge) -> usize {
self.pinned[band_slot(edge)] + usize::from(self.totals == Some(edge))
}
pub(super) fn band_line(&self, edge: PinnedEdge, line: usize) -> BodyLine {
match (self.totals == Some(edge), edge) {
(false, _) => BodyLine::Pinned(edge, line),
(true, PinnedEdge::Top) => match line.checked_sub(1) {
Some(ix) => BodyLine::Pinned(edge, ix),
None => BodyLine::Totals(edge),
},
(true, PinnedEdge::Bottom) if line + 1 == self.lines(edge) => BodyLine::Totals(edge),
(true, PinnedEdge::Bottom) => BodyLine::Pinned(edge, line),
}
}
pub(super) fn line_y(&self, edge: PinnedEdge, line: usize) -> Option<Pixels> {
let band = self.band(edge);
let y = match edge {
PinnedEdge::Top => band.origin.y + self.row_height * line as f32,
PinnedEdge::Bottom => {
band.bottom() - self.row_height * (self.lines(edge).checked_sub(line)?) as f32
}
};
(y < band.bottom() && y + self.row_height > band.origin.y).then_some(y)
}
pub(super) fn visible_lines(
&self,
edge: PinnedEdge,
) -> impl Iterator<Item = (BodyLine, Pixels)> + '_ {
(0..self.lines(edge))
.filter_map(move |line| Some((self.band_line(edge, line), self.line_y(edge, line)?)))
}
fn line_at(&self, edge: PinnedEdge, y: Pixels) -> Option<BodyLine> {
let band = self.band(edge);
if y < band.origin.y || y >= band.bottom() || self.row_height <= px(0.) {
return None;
}
let line = match edge {
PinnedEdge::Top => ((y - band.origin.y) / self.row_height).floor() as usize,
PinnedEdge::Bottom => {
let from_edge = ((band.bottom() - y) / self.row_height).ceil() as usize;
self.lines(edge).checked_sub(from_edge.max(1))?
}
};
(line < self.lines(edge)).then(|| self.band_line(edge, line))
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) enum BodyLine {
Data(usize),
Pinned(PinnedEdge, usize),
Totals(PinnedEdge),
}
pub(super) struct PaintedBands<R> {
rows: [Vec<Arc<R>>; 2],
first: [usize; 2],
}
impl<R> Default for PaintedBands<R> {
fn default() -> Self {
Self {
rows: [Vec::new(), Vec::new()],
first: [0; 2],
}
}
}
impl<R> PaintedBands<R> {
pub(super) fn rows(&self, edge: PinnedEdge) -> &[Arc<R>] {
&self.rows[band_slot(edge)]
}
fn row(&self, edge: PinnedEdge, ix: usize) -> Option<&Arc<R>> {
let slot = band_slot(edge);
self.rows[slot].get(ix.checked_sub(self.first[slot])?)
}
pub(super) fn begin(&mut self, edge: PinnedEdge) {
let slot = band_slot(edge);
self.rows[slot].clear();
self.first[slot] = 0;
}
pub(super) fn push(&mut self, edge: PinnedEdge, ix: usize, row: Arc<R>) {
let slot = band_slot(edge);
if self.rows[slot].is_empty() {
self.first[slot] = ix;
}
debug_assert_eq!(self.first[slot] + self.rows[slot].len(), ix, "no gaps");
self.rows[slot].push(row);
}
}
impl<D: LiveGridDelegate> LiveGridState<D> {
pub fn pinned_keys(&self, edge: PinnedEdge) -> Vec<KeyOf<D>> {
self.painted_pinned
.rows(edge)
.iter()
.map(|row| row.key().clone())
.collect()
}
pub(crate) fn body_bands(&self) -> BodyBands {
BodyBands::new(
self.body_bounds,
self.row_height,
self.snapshot.pinned_rows(PinnedEdge::Top).len(),
self.snapshot.pinned_rows(PinnedEdge::Bottom).len(),
self.totals,
)
}
pub(super) fn rows_bounds(&self) -> Bounds<Pixels> {
self.body_bands().rows()
}
pub(super) fn body_line_at(&self, position: Point<Pixels>) -> Option<BodyLine> {
let bands = self.body_bands();
for edge in EDGES {
if bands.band(edge).contains(&position) {
return bands.line_at(edge, position.y);
}
}
self.line_at(position).map(BodyLine::Data)
}
pub(super) fn pinned_row_at(&self, position: Point<Pixels>) -> Option<(PinnedEdge, KeyOf<D>)> {
let Some(BodyLine::Pinned(edge, ix)) = self.body_line_at(position) else {
return None;
};
let row = self.painted_pinned.row(edge, ix)?;
Some((edge, row.key().clone()))
}
pub(super) fn is_pinned_key(&self, edge: PinnedEdge, key: &KeyOf<D>) -> bool {
self.snapshot
.pinned_rows(edge)
.iter()
.any(|row| row.key() == key)
}
}
#[cfg(test)]
mod tests {
use gpui_kit::{Bounds, point, px, size};
use super::{BodyBands, BodyLine, band_slot, pinned_key_hash};
use crate::{state::key_hash, store::PinnedEdge};
#[test]
fn test_pinned_key_hash() {
for key in [0u32, 1, 42] {
let top = pinned_key_hash(PinnedEdge::Top, &key);
let bottom = pinned_key_hash(PinnedEdge::Bottom, &key);
assert_ne!(top, key_hash(&key), "{key}");
assert_ne!(bottom, key_hash(&key), "{key}");
assert_ne!(top, bottom, "{key}");
}
assert_eq!(
pinned_key_hash(PinnedEdge::Top, "total"),
pinned_key_hash(PinnedEdge::Top, &String::from("total"))
);
assert_eq!(key_hash(&(PinnedEdge::Top, 7u32)), key_hash(&7u32));
}
#[test]
fn test_body_bands_overflow_rule() {
let body = Bounds::new(point(px(10.), px(40.)), size(px(300.), px(260.)));
let row = px(26.);
let bands = BodyBands::new(body, row, 0, 0, None);
assert_eq!(bands.rows(), body);
assert_eq!(bands.band(PinnedEdge::Top).size.height, px(0.));
assert_eq!(bands.band(PinnedEdge::Bottom).size.height, px(0.));
assert_eq!(bands.band(PinnedEdge::Bottom).origin.y, px(300.));
let bands = BodyBands::new(body, row, 1, 1, None);
assert_eq!(
bands.band(PinnedEdge::Top),
Bounds::new(point(px(10.), px(40.)), size(px(300.), px(26.)))
);
assert_eq!(
bands.rows(),
Bounds::new(point(px(10.), px(66.)), size(px(300.), px(208.)))
);
assert_eq!(
bands.band(PinnedEdge::Bottom),
Bounds::new(point(px(10.), px(274.)), size(px(300.), px(26.)))
);
let bands = BodyBands::new(body, row, 6, 6, None);
assert_eq!(bands.band(PinnedEdge::Top).size.height, px(156.));
assert_eq!(bands.band(PinnedEdge::Bottom).size.height, px(104.));
assert_eq!(bands.rows().size.height, px(0.));
let bands = BodyBands::new(body, row, 20, 1, None);
assert_eq!(bands.band(PinnedEdge::Top).size.height, px(260.));
assert_eq!(bands.band(PinnedEdge::Bottom).size.height, px(0.));
assert_eq!(bands.rows().size.height, px(0.));
let bands = BodyBands::new(Bounds::default(), row, 2, 1, None);
assert_eq!(bands.rows().size.height, px(0.));
assert_eq!(bands.band(PinnedEdge::Top).size.height, px(0.));
assert_eq!(band_slot(PinnedEdge::Top), 0);
assert_eq!(band_slot(PinnedEdge::Bottom), 1);
}
#[test]
fn test_body_bands_with_totals() {
let body = Bounds::new(point(px(10.), px(40.)), size(px(300.), px(260.)));
let row = px(26.);
let lines = |bands: &BodyBands, edge| -> Vec<(BodyLine, f32)> {
bands
.visible_lines(edge)
.map(|(line, y)| (line, y.as_f32()))
.collect()
};
use BodyLine::{Pinned, Totals};
use PinnedEdge::{Bottom, Top};
let bands = BodyBands::new(body, row, 1, 1, Some(Bottom));
assert_eq!(bands.band(Bottom).size.height, px(52.));
assert_eq!(bands.rows().size.height, px(182.));
assert_eq!(
lines(&bands, Bottom),
[(Pinned(Bottom, 0), 248.), (Totals(Bottom), 274.)]
);
assert_eq!(lines(&bands, Top), [(Pinned(Top, 0), 40.)]);
let bands = BodyBands::new(body, row, 0, 0, Some(Bottom));
assert_eq!(lines(&bands, Bottom), [(Totals(Bottom), 274.)]);
let bands = BodyBands::new(body, row, 2, 0, Some(Top));
assert_eq!(bands.band(Top).size.height, px(78.));
assert_eq!(
lines(&bands, Top),
[
(Totals(Top), 40.),
(Pinned(Top, 0), 66.),
(Pinned(Top, 1), 92.)
]
);
assert_eq!(bands.line_at(Top, px(40.)), Some(Totals(Top)));
assert_eq!(bands.line_at(Top, px(91.)), Some(Pinned(Top, 0)));
assert_eq!(bands.line_at(Top, px(118.)), None);
let bands = BodyBands::new(body, row, 8, 3, Some(Bottom));
assert_eq!(bands.band(Bottom).size.height, px(52.));
assert_eq!(
lines(&bands, Bottom),
[(Pinned(Bottom, 2), 248.), (Totals(Bottom), 274.)]
);
assert_eq!(bands.line_at(Bottom, px(299.)), Some(Totals(Bottom)));
assert_eq!(bands.line_at(Bottom, px(274.)), Some(Totals(Bottom)));
assert_eq!(bands.line_at(Bottom, px(273.)), Some(Pinned(Bottom, 2)));
assert_eq!(bands.line_at(Bottom, px(300.)), None);
let short = Bounds::new(body.origin, size(px(300.), px(26. * 3. + 13.)));
let bands = BodyBands::new(short, row, 2, 3, Some(Bottom));
assert_eq!(bands.band(Top).size.height, px(52.));
assert_eq!(bands.band(Bottom).size.height, px(39.));
assert_eq!(
lines(&bands, Bottom),
[(Pinned(Bottom, 2), 79.), (Totals(Bottom), 105.)]
);
assert_eq!(bands.line_at(Bottom, px(92.)), Some(Pinned(Bottom, 2)));
let bands = BodyBands::new(short, row, 5, 0, Some(Top));
assert_eq!(bands.rows().size.height, px(0.));
assert_eq!(
lines(&bands, Top),
[
(Totals(Top), 40.),
(Pinned(Top, 0), 66.),
(Pinned(Top, 1), 92.),
(Pinned(Top, 2), 118.)
]
);
}
}