use std::{ops::Range, sync::Arc};
use gpui_kit::component::IconNamed;
use gpui_kit::{App, Context, Pixels, Point, SharedString, Window, px};
use super::{
CELL_PADDING, ColumnOf, LiveGridDelegate, LiveGridState, SelectionOrigin,
disclosure::INDENT,
events::{LayoutOrigin, LayoutPart, LayoutParts},
frame::{LEAD_ICON_LANE, cell_icon_size, cell_image_size},
header::{HEADER_SORT_LANE, header_icon_lane, header_label_style},
images::ImageFit,
pinned::{BodyLine, EDGES},
style::AggregateLine,
summary::{SummaryLine, SummaryMeasure, TOTALS_WEIGHT},
};
use crate::{
actions::{
AutofitColumn, AutofitColumns, FreezeColumnLeft, FreezeColumnRight, HideColumn,
MoveColumnLeft, MoveColumnRight, ShowAllColumns, UnfreezeColumn,
},
engine::DisplayRow,
model::{CellValue, ColumnIx, ColumnSet, GridColumns, GridRow, NumberFormat, display_text_in},
policy::{AutofitContext, AutofitPolicy, ColumnMoveContext, ColumnMovePolicy, ColumnOrder},
};
pub const MIN_COLUMN_WIDTH: Pixels = px(10.);
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum ColumnAlign {
#[default]
Auto,
Start,
End,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum ColumnZone {
FrozenLeft,
#[default]
Scrolling,
FrozenRight,
}
impl ColumnZone {
pub(crate) const ALL: [Self; 3] = [Self::FrozenLeft, Self::Scrolling, Self::FrozenRight];
pub(crate) const fn slot(self) -> usize {
self as usize
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct ViewColumnIx(u16);
impl ViewColumnIx {
pub const fn new(ix: u16) -> Self {
Self(ix)
}
pub const fn get(self) -> usize {
self.0 as usize
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum CellContent {
#[default]
Text,
Element,
Control,
}
impl CellContent {
pub(crate) const fn key_value(self) -> &'static str {
match self {
Self::Text => "text",
Self::Element => "element",
Self::Control => "control",
}
}
}
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum HeaderLabel {
#[default]
Title,
IconAndTitle(SharedString),
Icon(SharedString),
}
#[derive(Clone, Debug)]
pub struct GridColumn<C> {
pub(super) column: C,
pub(super) title: SharedString,
pub(super) width: Pixels,
pub(super) align: ColumnAlign,
pub(super) min_width: Pixels,
pub(super) max_width: Pixels,
pub(super) zone: ColumnZone,
pub(super) visible: bool,
pub(super) resizable: bool,
pub(super) movable: bool,
pub(super) sortable: bool,
pub(super) groupable: bool,
pub(super) content: CellContent,
pub(super) header_label: HeaderLabel,
pub(super) hideable: bool,
pub(super) internal: bool,
pub(super) number_format: NumberFormat,
pub(super) image_fit: ImageFit,
pub(super) image_corner_radius: Pixels,
}
impl<C: GridColumns> GridColumn<C> {
pub fn new(column: C, title: impl Into<SharedString>) -> Self {
Self {
column,
title: title.into(),
width: px(100.),
align: ColumnAlign::Auto,
min_width: MIN_COLUMN_WIDTH,
max_width: Pixels::MAX,
zone: ColumnZone::Scrolling,
visible: true,
resizable: true,
movable: true,
sortable: true,
groupable: true,
content: CellContent::Text,
header_label: HeaderLabel::Title,
hideable: true,
internal: false,
number_format: NumberFormat::new(),
image_fit: ImageFit::Contain,
image_corner_radius: px(0.),
}
}
pub fn with_width(mut self, width: Pixels) -> Self {
self.width = width;
self
}
pub fn with_align(mut self, align: ColumnAlign) -> Self {
self.align = align;
self
}
pub fn with_min_width(mut self, width: Pixels) -> Self {
self.min_width = width.max(MIN_COLUMN_WIDTH);
self
}
pub fn with_max_width(mut self, width: Pixels) -> Self {
self.max_width = width;
self
}
pub fn with_zone(mut self, zone: ColumnZone) -> Self {
self.zone = zone;
self
}
pub fn with_visible(mut self, visible: bool) -> Self {
self.visible = visible;
self
}
pub fn with_resizable(mut self, resizable: bool) -> Self {
self.resizable = resizable;
self
}
pub fn with_movable(mut self, movable: bool) -> Self {
self.movable = movable;
self
}
pub fn with_sortable(mut self, sortable: bool) -> Self {
self.sortable = sortable;
self
}
pub fn with_groupable(mut self, groupable: bool) -> Self {
self.groupable = groupable;
self
}
pub fn with_content(mut self, content: CellContent) -> Self {
self.content = content;
self
}
pub fn with_icon(mut self, icon: impl IconNamed) -> Self {
self.header_label = HeaderLabel::IconAndTitle(icon.path());
self
}
pub fn with_icon_only(mut self, icon: impl IconNamed) -> Self {
self.header_label = HeaderLabel::Icon(icon.path());
self
}
pub fn with_hideable(mut self, hideable: bool) -> Self {
self.hideable = hideable;
self
}
pub fn with_internal(mut self, internal: bool) -> Self {
self.internal = internal;
self
}
pub fn with_number_format(mut self, format: NumberFormat) -> Self {
self.number_format = format;
self
}
pub fn with_image_fit(mut self, fit: ImageFit) -> Self {
self.image_fit = fit;
self
}
pub fn with_image_corner_radius(mut self, radius: Pixels) -> Self {
self.image_corner_radius = radius;
self
}
pub fn column(&self) -> C {
self.column
}
pub fn title(&self) -> &SharedString {
&self.title
}
pub fn width(&self) -> Pixels {
self.width
}
pub fn min_width(&self) -> Pixels {
self.min_width
}
pub fn max_width(&self) -> Pixels {
self.max_width
}
pub fn align(&self) -> ColumnAlign {
self.align
}
pub fn zone(&self) -> ColumnZone {
self.zone
}
pub fn is_visible(&self) -> bool {
self.visible
}
pub fn is_resizable(&self) -> bool {
self.resizable
}
pub fn is_movable(&self) -> bool {
self.movable
}
pub fn is_sortable(&self) -> bool {
self.sortable
}
pub fn is_groupable(&self) -> bool {
self.groupable
}
pub fn content(&self) -> CellContent {
self.content
}
pub fn header_label(&self) -> &HeaderLabel {
&self.header_label
}
pub fn icon(&self) -> Option<&SharedString> {
match &self.header_label {
HeaderLabel::Title => None,
HeaderLabel::IconAndTitle(icon) | HeaderLabel::Icon(icon) => Some(icon),
}
}
pub fn is_hideable(&self) -> bool {
self.hideable
}
pub fn number_format(&self) -> NumberFormat {
self.number_format
}
pub fn is_internal(&self) -> bool {
self.internal
}
pub fn image_fit(&self) -> ImageFit {
self.image_fit
}
pub fn image_corner_radius(&self) -> Pixels {
self.image_corner_radius
}
pub(crate) fn clamp_width(&self, width: Pixels) -> Pixels {
let min = self.min_width.max(MIN_COLUMN_WIDTH);
if !f32::from(width).is_finite() || width <= px(0.) {
return min;
}
width.min(self.max_width).max(min)
}
}
#[derive(Clone, Debug)]
pub struct ColumnBand<C> {
id: &'static str,
title: SharedString,
children: Vec<BandChild<C>>,
}
#[derive(Clone, Debug)]
enum BandChild<C> {
Column(GridColumn<C>),
Band(ColumnBand<C>),
}
impl<C: GridColumns> ColumnBand<C> {
pub fn new(id: &'static str, title: impl Into<SharedString>) -> Self {
Self {
id,
title: title.into(),
children: Vec::new(),
}
}
pub fn column(mut self, column: GridColumn<C>) -> Self {
self.children.push(BandChild::Column(column));
self
}
pub fn band(mut self, band: ColumnBand<C>) -> Self {
self.children.push(BandChild::Band(band));
self
}
pub fn id(&self) -> &'static str {
self.id
}
pub fn title(&self) -> &SharedString {
&self.title
}
}
#[derive(Clone, Debug)]
pub struct GridSchema<C> {
columns: Vec<GridColumn<C>>,
chains: Vec<Box<[&'static str]>>,
bands: Vec<(&'static str, SharedString)>,
frozen: usize,
}
impl<C: GridColumns> Default for GridSchema<C> {
fn default() -> Self {
Self {
columns: Vec::new(),
chains: Vec::new(),
bands: Vec::new(),
frozen: 0,
}
}
}
impl<C: GridColumns> GridSchema<C> {
pub fn new() -> Self {
Self::default()
}
pub fn column(mut self, column: GridColumn<C>) -> Self {
self.push_column(column, &[]);
self
}
pub fn band(mut self, band: ColumnBand<C>) -> Self {
self.push_band(band, &mut Vec::new());
self
}
pub fn with_frozen(mut self, count: usize) -> Self {
self.frozen = count;
self
}
fn push_column(&mut self, column: GridColumn<C>, chain: &[&'static str]) {
let ix = column.column.ix();
assert!(ix.get() < 128, "a grid supports at most 128 columns");
assert!(
!self
.columns
.iter()
.any(|c| c.column == column.column || c.column.id() == column.column.id()),
"column {:?} (id {:?}) is already in the schema",
column.column,
column.column.id(),
);
self.columns.push(column);
self.chains.push(chain.into());
}
fn push_band(&mut self, band: ColumnBand<C>, chain: &mut Vec<&'static str>) {
assert!(
!self.bands.iter().any(|(id, _)| *id == band.id),
"band {:?} is already in the schema",
band.id,
);
self.bands.push((band.id, band.title));
chain.push(band.id);
for child in band.children {
match child {
BandChild::Column(column) => self.push_column(column, chain),
BandChild::Band(band) => self.push_band(band, chain),
}
}
chain.pop();
}
}
#[derive(Clone, Debug, Default)]
pub(crate) struct ZoneColumns {
pub(crate) columns: Vec<ColumnIx>,
pub(crate) offsets: Vec<Pixels>,
}
impl ZoneColumns {
pub(crate) fn len(&self) -> usize {
self.columns.len()
}
pub(crate) fn content_width(&self) -> Pixels {
self.offsets.last().copied().unwrap_or_default()
}
pub(crate) fn width(&self, i: usize) -> Pixels {
self.offsets[i + 1] - self.offsets[i]
}
pub(crate) fn range(&self, left: Pixels, right: Pixels) -> Range<usize> {
let n = self.len();
if n == 0 || right <= left {
return 0..0;
}
let start = self.offsets[1..].partition_point(|&end| end <= left);
let end = self.offsets[..n].partition_point(|&start| start < right);
start..end.max(start)
}
pub(crate) fn index_at(&self, x: Pixels) -> Option<usize> {
let ix = self.offsets.partition_point(|&o| o <= x);
(ix >= 1 && ix <= self.len()).then(|| ix - 1)
}
}
#[derive(Clone, Debug)]
pub(crate) struct BandRun {
pub(crate) band: &'static str,
pub(crate) title: SharedString,
pub(crate) zone: ColumnZone,
pub(crate) level: usize,
pub(crate) columns: Range<usize>,
pub(crate) movable: bool,
}
#[derive(Clone, Debug)]
pub(crate) struct ZoneFrame {
pub(crate) zone: ColumnZone,
pub(crate) x: Pixels,
pub(crate) width: Pixels,
pub(crate) scroll: Pixels,
pub(crate) columns: Range<usize>,
}
impl ZoneFrame {
pub(crate) fn column_x(&self, zone: &ZoneColumns, i: usize) -> Pixels {
self.x + zone.offsets[i] - self.scroll
}
pub(crate) fn right(&self) -> Pixels {
self.x + self.width
}
}
#[derive(Clone, Debug)]
pub(crate) struct ZoneGeometry {
pub(crate) zones: [ZoneFrame; 3],
}
impl ZoneGeometry {
pub(crate) fn zone(&self, zone: ColumnZone) -> &ZoneFrame {
&self.zones[zone.slot()]
}
pub(crate) fn zone_at(&self, x: Pixels) -> Option<ColumnZone> {
let [left, scrolling, right] = &self.zones;
if right.width > px(0.) && x >= right.x && x < right.right() {
Some(ColumnZone::FrozenRight)
} else if left.width > px(0.) && x >= left.x && x < left.right() {
Some(ColumnZone::FrozenLeft)
} else if scrolling.width > px(0.) && x >= scrolling.x && x < scrolling.right() {
Some(ColumnZone::Scrolling)
} else {
None
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum Boundary {
Gap(usize),
Here,
Refused,
}
pub(crate) struct ZoneMove<'a> {
context: ColumnMoveContext<'a>,
members: Vec<ColumnIx>,
span: Range<usize>,
contiguous: bool,
visible_at: Vec<usize>,
}
impl ZoneMove<'_> {
pub(crate) fn resolve(&self, k: usize, policy: &dyn ColumnMovePolicy) -> Boundary {
let n = self.visible_at.len();
let check = |gap: usize| {
if self.contiguous && self.span.start <= gap && gap <= self.span.end {
Some(Boundary::Here)
} else if policy.is_allowed(&self.context, self.span.clone(), gap) {
Some(Boundary::Gap(gap))
} else {
None
}
};
let found = if k < n {
let low = if k > 0 { self.visible_at[k - 1] + 1 } else { 0 };
(low..=self.visible_at[k]).rev().find_map(check)
} else if k == n {
let low = n.checked_sub(1).map_or(0, |last| self.visible_at[last] + 1);
(low..=self.context.columns().len()).find_map(check)
} else {
None
};
found.unwrap_or(Boundary::Refused)
}
}
#[derive(Clone)]
pub(crate) struct ColumnLayout<C> {
schema: Vec<Option<GridColumn<C>>>,
declared: Vec<(ColumnIx, ColumnZone)>,
chains: Vec<Box<[&'static str]>>,
bands: Vec<(&'static str, SharedString)>,
depth: usize,
order: Vec<ColumnIx>,
ends: [usize; 3],
widths: Vec<Pixels>,
zones: Vec<ColumnZone>,
visible: ColumnSet,
internal: ColumnSet,
zone_columns: [ZoneColumns; 3],
zone_starts: [usize; 3],
view_ix: Vec<u16>,
runs: Vec<BandRun>,
}
const NO_VIEW_IX: u16 = u16::MAX;
impl<C: GridColumns> ColumnLayout<C> {
pub(crate) fn new(schema: GridSchema<C>) -> Self {
let count = C::all().len();
let mut columns: Vec<Option<GridColumn<C>>> = (0..count).map(|_| None).collect();
let mut chains: Vec<Box<[&'static str]>> = vec![Box::default(); count];
let mut widths = vec![px(0.); count];
let mut zones = vec![ColumnZone::Scrolling; count];
let mut visible = ColumnSet::EMPTY;
let mut internal = ColumnSet::EMPTY;
let mut declared = Vec::with_capacity(schema.columns.len());
let depth = schema.chains.iter().map(|chain| chain.len()).max();
for (position, (column, chain)) in schema.columns.into_iter().zip(schema.chains).enumerate()
{
let ix = column.column.ix();
let zone = if position < schema.frozen {
ColumnZone::FrozenLeft
} else {
column.zone
};
widths[ix.get()] = column.clamp_width(column.width);
zones[ix.get()] = zone;
if column.internal {
internal.insert(ix);
} else if column.visible {
visible.insert(ix);
}
chains[ix.get()] = chain;
declared.push((ix, zone));
columns[ix.get()] = Some(column);
}
let mut layout = Self {
schema: columns,
declared,
chains,
bands: schema.bands,
depth: depth.unwrap_or(0),
order: Vec::new(),
ends: [0; 3],
widths,
zones,
visible,
internal,
zone_columns: Default::default(),
zone_starts: [0; 3],
view_ix: vec![NO_VIEW_IX; count],
runs: Vec::new(),
};
layout.reset_order();
layout
}
fn reset_order(&mut self) {
self.order.clear();
for &(ix, zone) in &self.declared {
self.order.push(ix);
self.zones[ix.get()] = zone;
}
self.settle();
if self.visible.is_empty()
&& let Some(&first) = self.order.iter().find(|ix| !self.internal.contains(**ix))
{
self.visible.insert(first);
self.settle();
}
}
pub(crate) fn reset(&mut self, policy: &dyn ColumnMovePolicy) {
self.reset_order();
self.normalize(policy);
}
pub(crate) fn normalize(&mut self, policy: &dyn ColumnMovePolicy) {
policy.normalize(&mut ColumnOrder::new(
&mut self.order,
&mut self.zones,
&self.chains,
));
self.settle();
}
fn settle(&mut self) {
let zones = &self.zones;
self.order.sort_by_key(|ix| zones[ix.get()].slot());
for zone in ColumnZone::ALL {
self.ends[zone.slot()] = self
.order
.partition_point(|ix| zones[ix.get()].slot() <= zone.slot());
}
self.view_ix.fill(NO_VIEW_IX);
let mut view = 0;
for zone in ColumnZone::ALL {
let range = self.zone_range(zone);
let columns = &mut self.zone_columns[zone.slot()];
columns.columns.clear();
columns.offsets.clear();
self.zone_starts[zone.slot()] = view;
let mut x = px(0.);
columns.offsets.push(x);
for &ix in &self.order[range] {
if self.visible.contains(ix) {
columns.columns.push(ix);
x += self.widths[ix.get()];
columns.offsets.push(x);
self.view_ix[ix.get()] = view as u16;
view += 1;
}
}
}
self.runs.clear();
for zone in ColumnZone::ALL {
let columns = &self.zone_columns[zone.slot()];
for level in 0..self.depth {
let mut i = 0;
while i < columns.len() {
let Some(&band) = self.chains[columns.columns[i].get()].get(level) else {
i += 1;
continue;
};
let start = i;
while i < columns.len()
&& self.chains[columns.columns[i].get()].get(level) == Some(&band)
{
i += 1;
}
let title = self
.bands
.iter()
.find(|(id, _)| *id == band)
.map(|(_, title)| title.clone())
.unwrap_or_default();
let movable = columns.columns[start..i]
.iter()
.all(|ix| self.column(*ix).movable);
self.runs.push(BandRun {
band,
title,
zone,
level,
columns: start..i,
movable,
});
}
}
}
}
fn zone_range(&self, zone: ColumnZone) -> Range<usize> {
let start = match zone.slot() {
0 => 0,
slot => self.ends[slot - 1],
};
start..self.ends[zone.slot()]
}
pub(crate) fn get(&self, ix: ColumnIx) -> Option<&GridColumn<C>> {
self.schema.get(ix.get()).and_then(Option::as_ref)
}
pub(crate) fn column(&self, ix: ColumnIx) -> &GridColumn<C> {
self.get(ix).expect("the column is in the schema")
}
pub(crate) fn order(&self) -> &[ColumnIx] {
&self.order
}
pub(crate) fn zone_order(&self, zone: ColumnZone) -> &[ColumnIx] {
&self.order[self.zone_range(zone)]
}
pub(crate) fn width(&self, ix: ColumnIx) -> Pixels {
self.widths.get(ix.get()).copied().unwrap_or_default()
}
pub(crate) fn is_visible(&self, ix: ColumnIx) -> bool {
self.visible.contains(ix)
}
pub(crate) fn zone_of(&self, ix: ColumnIx) -> ColumnZone {
self.zones.get(ix.get()).copied().unwrap_or_default()
}
pub(crate) fn visible_set(&self) -> ColumnSet {
self.visible
}
#[inline]
pub(crate) fn number_format(&self, ix: ColumnIx) -> NumberFormat {
self.schema
.get(ix.get())
.and_then(Option::as_ref)
.map_or_else(NumberFormat::new, |column| column.number_format)
}
pub(crate) fn number_formats(&self) -> Arc<[NumberFormat]> {
(0..self.schema.len())
.map(|ix| self.number_format(ColumnIx::new(ix as u16)))
.collect()
}
pub(crate) fn is_internal(&self, ix: ColumnIx) -> bool {
self.internal.contains(ix)
}
pub(crate) fn any_hidden(&self) -> bool {
self.order
.iter()
.any(|ix| !self.visible.contains(*ix) && !self.internal.contains(*ix))
}
pub(crate) fn visible(&self) -> impl Iterator<Item = ColumnIx> + '_ {
self.zone_columns
.iter()
.flat_map(|zone| zone.columns.iter().copied())
}
pub(crate) fn view_column_ix(&self, ix: ColumnIx) -> Option<ViewColumnIx> {
let view = *self.view_ix.get(ix.get())?;
(view != NO_VIEW_IX).then_some(ViewColumnIx(view))
}
pub(crate) fn column_at(&self, view: ViewColumnIx) -> Option<ColumnIx> {
let mut view = view.get();
for zone in &self.zone_columns {
if view < zone.len() {
return Some(zone.columns[view]);
}
view -= zone.len();
}
None
}
pub(crate) fn zone_position(&self, ix: ColumnIx) -> Option<(ColumnZone, usize)> {
let view = self.view_column_ix(ix)?.get();
let zone = self.zone_of(ix);
Some((zone, view - self.zone_starts[zone.slot()]))
}
pub(crate) fn zone_columns(&self, zone: ColumnZone) -> &ZoneColumns {
&self.zone_columns[zone.slot()]
}
pub(crate) fn runs(&self) -> &[BandRun] {
&self.runs
}
pub(crate) fn depth(&self) -> usize {
self.depth
}
pub(crate) fn bands_of(&self, ix: ColumnIx) -> &[&'static str] {
self.chains.get(ix.get()).map_or(&[], |chain| chain)
}
pub(crate) fn band_title(&self, id: &str) -> Option<&SharedString> {
self.bands
.iter()
.find(|(band, _)| *band == id)
.map(|(_, title)| title)
}
pub(crate) fn total_width(&self) -> Pixels {
self.zone_columns
.iter()
.map(ZoneColumns::content_width)
.fold(px(0.), |total, width| total + width)
}
pub(crate) fn geometry(&self, viewport_width: Pixels, scroll_x: Pixels) -> ZoneGeometry {
let viewport = viewport_width.max(px(0.));
let [left, scrolling, right] = &self.zone_columns;
let left_width = left.content_width().min(viewport);
let right_width = right.content_width().min(viewport - left_width);
let scroll_width = (viewport - left_width - right_width).max(px(0.));
ZoneGeometry {
zones: [
ZoneFrame {
zone: ColumnZone::FrozenLeft,
x: px(0.),
width: left_width,
scroll: px(0.),
columns: left.range(px(0.), left_width),
},
ZoneFrame {
zone: ColumnZone::Scrolling,
x: left_width,
width: scroll_width,
scroll: scroll_x,
columns: scrolling.range(scroll_x, scroll_x + scroll_width),
},
ZoneFrame {
zone: ColumnZone::FrozenRight,
x: viewport - right_width,
width: right_width,
scroll: px(0.),
columns: right.range(px(0.), right_width),
},
],
}
}
pub(crate) fn column_at_x(
&self,
x: Pixels,
viewport_width: Pixels,
scroll_x: Pixels,
) -> Option<ColumnIx> {
let geometry = self.geometry(viewport_width, scroll_x);
let zone = geometry.zone_at(x)?;
let frame = geometry.zone(zone);
let columns = self.zone_columns(zone);
columns
.index_at(x - frame.x + frame.scroll)
.map(|i| columns.columns[i])
}
pub(crate) fn move_context(&self, zone: ColumnZone) -> ColumnMoveContext<'_> {
ColumnMoveContext::new(zone, self.zone_order(zone), self.visible, &self.chains)
}
pub(crate) fn zone_move(&self, zone: ColumnZone, members: Vec<ColumnIx>) -> ZoneMove<'_> {
let order = self.zone_order(zone);
let positions: Vec<usize> = members
.iter()
.filter_map(|member| order.iter().position(|ix| ix == member))
.collect();
let span = match (positions.iter().min(), positions.iter().max()) {
(Some(&first), Some(&last)) => first..last + 1,
_ => 0..0,
};
let visible_at = order
.iter()
.enumerate()
.filter(|(_, ix)| self.visible.contains(**ix))
.map(|(position, _)| position)
.collect();
ZoneMove {
context: self.move_context(zone),
contiguous: span.len() == members.len(),
members,
span,
visible_at,
}
}
pub(crate) fn band_segment(
&self,
zone: ColumnZone,
band: &'static str,
first: ColumnIx,
) -> Vec<ColumnIx> {
let order = self.zone_order(zone);
let is_member = |ix: &ColumnIx| self.bands_of(*ix).contains(&band);
let Some(position) = order.iter().position(|ix| *ix == first) else {
return Vec::new();
};
if !is_member(&first) {
return Vec::new();
}
let start = order[..position]
.iter()
.rposition(|ix| !is_member(ix))
.map_or(0, |p| p + 1);
let end = order[position..]
.iter()
.position(|ix| !is_member(ix))
.map_or(order.len(), |p| position + p);
order[start..end].to_vec()
}
pub(crate) fn band_members(&self, zone: ColumnZone, band: &'static str) -> Vec<ColumnIx> {
self.zone_order(zone)
.iter()
.copied()
.filter(|ix| self.bands_of(*ix).contains(&band))
.collect()
}
pub(crate) fn set_width(&mut self, ix: ColumnIx, width: Pixels) -> bool {
let Some(column) = self.get(ix) else {
return false;
};
let width = column.clamp_width(width);
if self.widths[ix.get()] == width {
return false;
}
self.widths[ix.get()] = width;
self.settle();
true
}
pub(crate) fn set_visible(&mut self, ix: ColumnIx, visible: bool) -> Option<bool> {
self.get(ix)?;
if visible && self.internal.contains(ix) {
return None;
}
if self.visible.contains(ix) == visible {
return Some(false);
}
if visible {
self.visible.insert(ix);
} else {
if self.visible.len() == 1 {
return None;
}
let visible = self.visible;
self.visible = column_set(
self.order
.iter()
.copied()
.filter(|c| *c != ix && visible.contains(*c)),
);
}
self.settle();
Some(true)
}
pub(crate) fn show_all(&mut self) -> ColumnSet {
let mut shown = ColumnSet::EMPTY;
for &ix in &self.order {
if !self.visible.contains(ix) && !self.internal.contains(ix) {
shown.insert(ix);
self.visible.insert(ix);
}
}
if !shown.is_empty() {
self.settle();
}
shown
}
pub(crate) fn set_zone(
&mut self,
ix: ColumnIx,
zone: ColumnZone,
policy: &dyn ColumnMovePolicy,
) -> Vec<ColumnIx> {
if self.get(ix).is_none() {
return Vec::new();
}
let current = self.zone_of(ix);
if current == zone {
return Vec::new();
}
let order = self.zone_order(current);
let position = order
.iter()
.position(|c| *c == ix)
.expect("a schema column is in the order");
let block = policy.zone_block(&self.move_context(current), ix);
let block = if block.start <= position && position < block.end && block.end <= order.len() {
block
} else {
position..position + 1
};
let members = order[block].to_vec();
self.order.retain(|c| !members.contains(c));
for member in &members {
self.zones[member.get()] = zone;
}
let zones = &self.zones;
let at = if zone.slot() < current.slot() {
self.order
.partition_point(|c| zones[c.get()].slot() <= zone.slot())
} else {
self.order
.partition_point(|c| zones[c.get()].slot() < zone.slot())
};
self.order.splice(at..at, members.iter().copied());
self.settle();
members
}
pub(crate) fn move_members(
&mut self,
zone: ColumnZone,
members: &[ColumnIx],
gap: usize,
) -> bool {
let range = self.zone_range(zone);
let order = &self.order[range.clone()];
let gap = gap.min(order.len());
let before = order[..gap].iter().filter(|c| members.contains(c)).count();
let mut moved: Vec<ColumnIx> = order
.iter()
.copied()
.filter(|c| !members.contains(c))
.collect();
let at = gap - before;
moved.splice(at..at, members.iter().copied());
if moved == order {
return false;
}
self.order.splice(range, moved);
self.settle();
true
}
pub(crate) fn move_before(
&mut self,
zone: ColumnZone,
members: Vec<ColumnIx>,
before: Option<ColumnIx>,
policy: &dyn ColumnMovePolicy,
) -> Option<bool> {
if members.is_empty() || members.iter().any(|ix| self.zone_of(*ix) != zone) {
return None;
}
let order = self.zone_order(zone);
let gap = match before {
Some(before) => order.iter().position(|c| *c == before)?,
None => order.len(),
};
let shift = self.zone_move(zone, members);
if shift.contiguous && shift.span.start <= gap && gap <= shift.span.end {
return Some(false);
}
if !policy.is_allowed(&shift.context, shift.span.clone(), gap) {
return None;
}
let members = shift.members;
Some(self.move_members(zone, &members, gap))
}
}
#[derive(Clone, Copy, Debug, Default)]
pub(super) struct LayoutDelta {
pub(super) parts: LayoutParts,
pub(super) columns: ColumnSet,
pub(super) reveal: ColumnSet,
}
impl LayoutDelta {
pub(super) fn new(part: LayoutPart, columns: impl IntoIterator<Item = ColumnIx>) -> Self {
let columns = column_set(columns);
Self {
parts: part.into(),
columns,
reveal: ColumnSet::EMPTY,
}
}
pub(super) fn revealing(mut self) -> Self {
self.reveal = self.columns;
self
}
pub(super) fn is_empty(&self) -> bool {
self.parts.is_empty()
}
pub(super) fn union_with(&mut self, other: Self) {
self.parts.union_with(other.parts);
self.columns.union_with(other.columns);
self.reveal.union_with(other.reveal);
}
}
fn column_set(columns: impl IntoIterator<Item = ColumnIx>) -> ColumnSet {
columns.into_iter().fold(ColumnSet::EMPTY, |mut set, ix| {
set.insert(ix);
set
})
}
pub(super) fn column_of<C: GridColumns>(ix: ColumnIx) -> C {
C::all()[ix.get()]
}
impl<D: LiveGridDelegate> LiveGridState<D> {
pub fn with_column_move_policy(mut self, policy: impl ColumnMovePolicy) -> Self {
self.column_move = Box::new(policy);
self.columns.reset(&*self.column_move);
self.apply_initial_layout()
}
pub fn with_autofit_policy(mut self, policy: impl AutofitPolicy) -> Self {
self.autofit = Box::new(policy);
self
}
pub fn columns(&self) -> impl Iterator<Item = ColumnOf<D>> + '_ {
self.columns.order().iter().map(|ix| column_of(*ix))
}
pub fn visible_columns(&self) -> impl Iterator<Item = ColumnOf<D>> + '_ {
self.columns.visible().map(column_of)
}
pub fn column_width(&self, column: ColumnOf<D>) -> Pixels {
self.columns.width(column.ix())
}
pub fn is_column_visible(&self, column: ColumnOf<D>) -> bool {
self.columns.is_visible(column.ix())
}
pub fn column_zone(&self, column: ColumnOf<D>) -> ColumnZone {
self.columns.zone_of(column.ix())
}
pub fn view_column_ix(&self, column: ColumnOf<D>) -> Option<ViewColumnIx> {
self.columns.view_column_ix(column.ix())
}
pub fn column_at(&self, ix: ViewColumnIx) -> Option<ColumnOf<D>> {
self.columns.column_at(ix).map(column_of)
}
pub fn current_column(&self) -> Option<ColumnOf<D>> {
self.current_column
.filter(|ix| self.columns.is_visible(*ix))
.map(column_of)
}
pub(super) fn set_current_column(&mut self, column: ColumnIx) {
self.current_column = Some(column);
}
pub fn set_column_width(&mut self, column: ColumnOf<D>, width: Pixels, cx: &mut Context<Self>) {
self.change_layout(LayoutOrigin::Programmatic, cx, |layout, _| {
if layout.set_width(column.ix(), width) {
LayoutDelta::new(LayoutPart::Width, [column.ix()])
} else {
LayoutDelta::default()
}
});
}
pub fn set_column_visible(
&mut self,
column: ColumnOf<D>,
visible: bool,
cx: &mut Context<Self>,
) -> bool {
self.set_column_visible_from(column.ix(), visible, LayoutOrigin::Programmatic, cx)
}
fn set_column_visible_from(
&mut self,
ix: ColumnIx,
visible: bool,
origin: LayoutOrigin,
cx: &mut Context<Self>,
) -> bool {
let mut refused = false;
self.change_layout(origin, cx, |layout, _| {
match layout.set_visible(ix, visible) {
Some(true) => LayoutDelta::new(LayoutPart::Visibility, [ix]),
Some(false) => LayoutDelta::default(),
None => {
refused = true;
LayoutDelta::default()
}
}
});
!refused
}
pub fn show_all_columns(&mut self, cx: &mut Context<Self>) {
self.show_all_columns_from(LayoutOrigin::Programmatic, cx);
}
pub(super) fn show_all_columns_from(&mut self, origin: LayoutOrigin, cx: &mut Context<Self>) {
self.change_layout(origin, cx, |layout, _| {
let shown = layout.show_all();
if shown.is_empty() {
LayoutDelta::default()
} else {
LayoutDelta {
parts: LayoutPart::Visibility.into(),
columns: shown,
reveal: ColumnSet::EMPTY,
}
}
});
}
pub fn set_column_zone(
&mut self,
column: ColumnOf<D>,
zone: ColumnZone,
cx: &mut Context<Self>,
) {
self.set_column_zone_from(column.ix(), zone, LayoutOrigin::Programmatic, cx);
}
fn set_column_zone_from(
&mut self,
ix: ColumnIx,
zone: ColumnZone,
origin: LayoutOrigin,
cx: &mut Context<Self>,
) {
self.change_layout(origin, cx, |layout, policy| {
let moved = layout.set_zone(ix, zone, policy);
if moved.is_empty() {
return LayoutDelta::default();
}
let mut delta = LayoutDelta::new(LayoutPart::Zone, moved);
delta.parts = delta.parts.with(LayoutPart::Order);
delta.revealing()
});
}
pub fn move_column(
&mut self,
column: ColumnOf<D>,
before: Option<ColumnOf<D>>,
cx: &mut Context<Self>,
) -> bool {
let ix = column.ix();
if self.columns.get(ix).is_none() {
return false;
}
let zone = self.columns.zone_of(ix);
self.move_before(zone, vec![ix], before.map(|c| c.ix()), cx)
}
pub fn move_band(
&mut self,
band: &'static str,
before: Option<ColumnOf<D>>,
cx: &mut Context<Self>,
) -> bool {
let Some(&first) = self
.columns
.order()
.iter()
.find(|ix| self.columns.bands_of(**ix).contains(&band))
else {
return false;
};
let zone = self.columns.zone_of(first);
let members = self.columns.band_members(zone, band);
self.move_before(zone, members, before.map(|c| c.ix()), cx)
}
fn move_before(
&mut self,
zone: ColumnZone,
members: Vec<ColumnIx>,
before: Option<ColumnIx>,
cx: &mut Context<Self>,
) -> bool {
let mut refused = false;
self.change_layout(LayoutOrigin::Programmatic, cx, |layout, policy| {
let moved = members.clone();
match layout.move_before(zone, members, before, policy) {
Some(true) => LayoutDelta::new(LayoutPart::Order, moved).revealing(),
Some(false) => LayoutDelta::default(),
None => {
refused = true;
LayoutDelta::default()
}
}
});
!refused
}
pub fn autofit_column(&mut self, column: ColumnOf<D>, cx: &mut Context<Self>) {
self.request_autofit(column_set([column.ix()]), LayoutOrigin::Programmatic, cx);
}
pub fn autofit_columns(&mut self, cx: &mut Context<Self>) {
self.request_autofit(self.columns.visible_set(), LayoutOrigin::Programmatic, cx);
}
pub(super) fn request_autofit(
&mut self,
columns: ColumnSet,
origin: LayoutOrigin,
cx: &mut Context<Self>,
) {
let pending = self
.pending_autofit
.get_or_insert((ColumnSet::EMPTY, origin));
pending.0.union_with(columns);
pending.1 = origin;
cx.notify();
}
pub(super) fn apply_pending_autofit(
&mut self,
needed_lines: usize,
window: &Window,
cx: &App,
) -> Option<(LayoutDelta, LayoutOrigin)> {
let loading = self.projection.is_empty()
&& !self.snapshot.is_empty()
&& (self.rebuild_task.is_some() || self.pending != super::PendingWork::None);
if loading {
return None;
}
let (requested, origin) = self.pending_autofit.take()?;
let projection = self.projection.clone();
let snapshot = self.snapshot.clone();
let start = self.scroll_row.min(projection.len());
let mut end = start;
let mut lines = 0;
let mut summaries = Vec::new();
while lines < needed_lines && end < projection.len() {
match projection.line(end) {
Some(DisplayRow::Data { row, .. }) => {
lines += usize::from(snapshot.row(row).is_some());
}
Some(
line @ (DisplayRow::GroupCaption { group, .. }
| DisplayRow::GroupFooter { group, .. }),
) => {
lines += 1;
if let Some(group) = projection.groups().get(group) {
summaries.push(SummaryLine {
line: if matches!(line, DisplayRow::GroupFooter { .. }) {
AggregateLine::Footer(group)
} else {
AggregateLine::Caption(group)
},
aggregates: group.aggregates(),
weight: None,
label: None,
});
}
}
None => {}
}
end += 1;
}
let bands = self.body_bands();
let mut pinned: Vec<Arc<D::Row>> = Vec::new();
for edge in EDGES {
for (line, _) in bands.visible_lines(edge) {
match line {
BodyLine::Pinned(_, ix) => {
pinned.extend(snapshot.pinned_rows(edge).get(ix).cloned());
}
BodyLine::Totals(_) => summaries.push(SummaryLine {
line: AggregateLine::Totals(edge),
aggregates: projection.totals(),
weight: Some(TOTALS_WEIGHT),
label: self
.columns
.visible()
.next()
.map(|first| (self.summary_texts.total(), first)),
}),
BodyLine::Data(_) => {}
}
}
}
let (font, font_size) = header_label_style(window);
let mut widths = Vec::new();
let mut buffer = String::new();
let mut arena = Vec::new();
let icon_width = cell_icon_size(self.row_height);
let image_width = cell_image_size(self.row_height);
let tree_column = projection.is_tree().then(|| self.tree_column()).flatten();
let targets: Vec<ColumnIx> = self
.columns
.visible()
.filter(|ix| requested.contains(*ix))
.collect();
for ix in targets {
let column: ColumnOf<D> = column_of(ix);
let schema_column = self.columns.column(ix);
let title = schema_column.title.clone();
let lane = if schema_column.sortable {
HEADER_SORT_LANE
} else {
px(0.)
};
let icon = header_icon_lane(&schema_column.header_label, self.row_height);
let title_width = match schema_column.header_label {
HeaderLabel::Icon(_) => px(0.),
HeaderLabel::Title | HeaderLabel::IconAndTitle(_) => self
.header
.labels
.width(ix, &title, &font, font_size, window),
};
let header = icon + title_width + lane;
let summary_width = SummaryMeasure {
delegate: &self.delegate,
app: cx,
text_cache: &mut self.text_cache,
buffer: &mut buffer,
arena: &mut arena,
format: self.columns.number_format(ix),
}
.width(&summaries, column, window);
let text_cache = &mut self.text_cache;
let format = self.columns.number_format(ix);
let mut measure_text = |row: &D::Row| {
let value = row.value(column);
match value {
CellValue::Icon(_) => return icon_width,
CellValue::Image(_) => return image_width,
_ => {}
}
let text = display_text_in(value, format, &mut buffer);
if text.is_empty() {
return px(0.);
}
arena.clear();
text_cache
.regular()
.layout(text, window, &mut arena)
.width()
};
let pinned_width = pinned
.iter()
.map(|row| measure_text(row))
.fold(px(0.), Pixels::max);
let mut measure = |display_ix: usize| {
let DisplayRow::Data { row, depth } = projection.line(display_ix)? else {
return None;
};
let row = snapshot.row(row)?;
let indent = if tree_column == Some(ix) {
INDENT * f32::from(depth) + LEAD_ICON_LANE
} else {
px(0.)
};
Some(measure_text(row) + indent)
};
let mut context =
AutofitContext::new(header, start..end, projection.len(), &mut measure)
.with_pinned_width(pinned_width)
.with_summary_width(summary_width);
let content = self.autofit.content_width(&mut context);
widths.push((ix, content.ceil() + CELL_PADDING * 2. + px(1.)));
}
let delta = self.relayout(|layout, _| {
let mut delta = LayoutDelta::default();
for (ix, width) in widths {
if layout.set_width(ix, width) {
delta.union_with(LayoutDelta::new(LayoutPart::Width, [ix]));
}
}
delta
});
(!delta.is_empty()).then_some((delta, origin))
}
pub(super) fn change_layout(
&mut self,
origin: LayoutOrigin,
cx: &mut Context<Self>,
change: impl FnOnce(&mut ColumnLayout<ColumnOf<D>>, &dyn ColumnMovePolicy) -> LayoutDelta,
) -> bool {
let visible = self.columns.visible_set();
let delta = self.relayout(change);
if delta.is_empty() {
return false;
}
self.emit_layout_change(delta, origin, cx);
if self.columns.visible_set() != visible {
self.visible_columns_changed(cx);
}
cx.notify();
true
}
pub(super) fn relayout(
&mut self,
change: impl FnOnce(&mut ColumnLayout<ColumnOf<D>>, &dyn ColumnMovePolicy) -> LayoutDelta,
) -> LayoutDelta {
let anchor = self.scroll_anchor();
let delta = change(&mut self.columns, &*self.column_move);
if delta.is_empty() {
return delta;
}
self.restore_scroll_anchor(anchor);
if !delta.reveal.is_empty() {
self.reveal_columns(delta.reveal);
}
self.clamp_scroll();
delta
}
pub(super) fn emit_layout_change(
&mut self,
delta: LayoutDelta,
origin: LayoutOrigin,
cx: &mut Context<Self>,
) {
cx.emit(super::LiveGridEvent::LayoutChange {
parts: delta.parts,
columns: delta.columns,
origin,
});
let origin = match origin {
LayoutOrigin::User => SelectionOrigin::User,
LayoutOrigin::Programmatic => SelectionOrigin::Programmatic,
};
self.move_focus_off_hidden(origin, cx);
}
pub(super) fn visible_columns_changed(&mut self, cx: &mut Context<Self>) {
self.move_focus_off_hidden(SelectionOrigin::Programmatic, cx);
self.quick_filter_columns_changed(cx);
}
pub(crate) fn column_at_position(&self, position: Point<Pixels>) -> Option<ColumnIx> {
let x = position.x - self.body_bounds.origin.x;
self.columns
.column_at_x(x, self.body_bounds.size.width, self.scroll_x)
}
fn move_current_column(&mut self, step: isize, cx: &mut Context<Self>) {
let Some(ix) = self
.current_column
.filter(|ix| self.columns.is_visible(*ix))
else {
return;
};
let Some((zone, i)) = self.columns.zone_position(ix) else {
return;
};
let boundary = if step < 0 {
match i.checked_sub(1) {
Some(k) => k,
None => return,
}
} else {
i + 2
};
let Boundary::Gap(gap) = self
.columns
.zone_move(zone, vec![ix])
.resolve(boundary, &*self.column_move)
else {
return;
};
self.change_layout(LayoutOrigin::User, cx, |layout, _| {
if layout.move_members(zone, &[ix], gap) {
LayoutDelta::new(LayoutPart::Order, [ix]).revealing()
} else {
LayoutDelta::default()
}
});
}
fn current_visible_column(&self) -> Option<ColumnIx> {
self.current_column
.filter(|ix| self.columns.is_visible(*ix))
}
pub(super) fn on_autofit_columns(
&mut self,
_: &AutofitColumns,
_: &mut Window,
cx: &mut Context<Self>,
) {
self.request_autofit(self.columns.visible_set(), LayoutOrigin::User, cx);
}
pub(super) fn on_autofit_column(
&mut self,
_: &AutofitColumn,
_: &mut Window,
cx: &mut Context<Self>,
) {
if let Some(ix) = self.current_visible_column() {
self.request_autofit(column_set([ix]), LayoutOrigin::User, cx);
}
}
pub(super) fn on_hide_column(
&mut self,
_: &HideColumn,
_: &mut Window,
cx: &mut Context<Self>,
) {
if let Some(ix) = self.current_visible_column()
&& self.columns.column(ix).hideable
{
self.set_column_visible_from(ix, false, LayoutOrigin::User, cx);
}
}
pub(super) fn on_show_all_columns(
&mut self,
_: &ShowAllColumns,
_: &mut Window,
cx: &mut Context<Self>,
) {
self.show_all_columns_from(LayoutOrigin::User, cx);
}
pub(super) fn on_freeze_column_left(
&mut self,
_: &FreezeColumnLeft,
_: &mut Window,
cx: &mut Context<Self>,
) {
if let Some(ix) = self.current_visible_column() {
self.set_column_zone_from(ix, ColumnZone::FrozenLeft, LayoutOrigin::User, cx);
}
}
pub(super) fn on_freeze_column_right(
&mut self,
_: &FreezeColumnRight,
_: &mut Window,
cx: &mut Context<Self>,
) {
if let Some(ix) = self.current_visible_column() {
self.set_column_zone_from(ix, ColumnZone::FrozenRight, LayoutOrigin::User, cx);
}
}
pub(super) fn on_unfreeze_column(
&mut self,
_: &UnfreezeColumn,
_: &mut Window,
cx: &mut Context<Self>,
) {
if let Some(ix) = self.current_visible_column() {
self.set_column_zone_from(ix, ColumnZone::Scrolling, LayoutOrigin::User, cx);
}
}
pub(super) fn on_move_column_left(
&mut self,
_: &MoveColumnLeft,
_: &mut Window,
cx: &mut Context<Self>,
) {
self.move_current_column(-1, cx);
}
pub(super) fn on_move_column_right(
&mut self,
_: &MoveColumnRight,
_: &mut Window,
cx: &mut Context<Self>,
) {
self.move_current_column(1, cx);
}
}
#[cfg(test)]
mod tests {
use std::time::Duration;
use gpui_kit::px;
use super::{
ColumnAlign, ColumnBand, ColumnLayout, ColumnZone, GridColumn, GridSchema,
MIN_COLUMN_WIDTH, ViewColumnIx,
};
use crate::{
highlight::HighlightPolicy,
model::{ColumnIx, GridColumns},
policy::{BandedMove, ColumnMovePolicy, FreeMove},
state::GridTuning,
store::StoreOptions,
};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
struct TestColumn(u16);
static ALL: [TestColumn; 12] = {
let mut all = [TestColumn(0); 12];
let mut ix = 0;
while ix < 12 {
all[ix] = TestColumn(ix as u16);
ix += 1;
}
all
};
const IDS: [&str; 12] = [
"c0", "c1", "c2", "c3", "c4", "c5", "c6", "c7", "c8", "c9", "c10", "c11",
];
const BANDS: [&str; 12] = [
"b0", "b1", "b2", "b3", "b4", "b5", "b6", "b7", "b8", "b9", "b10", "b11",
];
impl GridColumns for TestColumn {
fn all() -> &'static [Self] {
&ALL
}
fn id(self) -> &'static str {
IDS[self.0 as usize]
}
fn ix(self) -> ColumnIx {
ColumnIx::new(self.0)
}
}
fn ix(i: u16) -> ColumnIx {
ColumnIx::new(i)
}
fn column(i: u16) -> GridColumn<TestColumn> {
GridColumn::new(TestColumn(i), IDS[i as usize])
}
fn zone_order(layout: &ColumnLayout<TestColumn>, zone: ColumnZone) -> Vec<u16> {
layout
.zone_order(zone)
.iter()
.map(|c| c.get() as u16)
.collect()
}
fn visible(layout: &ColumnLayout<TestColumn>) -> Vec<u16> {
layout.visible().map(|c| c.get() as u16).collect()
}
#[test]
fn test_grid_schema_builder() {
let bid = GridColumn::new(TestColumn(1), "Bid")
.with_width(px(80.))
.with_align(ColumnAlign::End)
.with_min_width(px(40.))
.with_max_width(px(300.))
.with_zone(ColumnZone::FrozenRight)
.with_visible(false)
.with_resizable(false)
.with_movable(false)
.with_sortable(false)
.with_groupable(false);
assert_eq!(bid.column(), TestColumn(1));
assert_eq!(bid.title().as_ref(), "Bid");
assert_eq!(bid.width(), px(80.));
assert_eq!(bid.align(), ColumnAlign::End);
assert_eq!(bid.min_width(), px(40.));
assert_eq!(bid.max_width(), px(300.));
assert_eq!(bid.zone(), ColumnZone::FrozenRight);
assert!(!bid.is_visible());
assert!(!bid.is_resizable());
assert!(!bid.is_movable());
assert!(!bid.is_sortable());
assert!(!bid.is_groupable());
let plain = column(2);
assert_eq!(plain.width(), px(100.));
assert_eq!(plain.align(), ColumnAlign::Auto);
assert_eq!(plain.min_width(), MIN_COLUMN_WIDTH);
assert_eq!(plain.zone(), ColumnZone::Scrolling);
assert!(plain.is_visible() && plain.is_resizable() && plain.is_movable());
assert!(plain.is_sortable() && plain.is_groupable());
let band = ColumnBand::new("quote", "Quote")
.column(column(2))
.band(ColumnBand::new("depth", "Depth").column(column(3)));
assert_eq!(band.id(), "quote");
assert_eq!(band.title().as_ref(), "Quote");
let layout = ColumnLayout::new(
GridSchema::new()
.with_frozen(2)
.column(column(0).with_zone(ColumnZone::FrozenRight))
.band(band)
.column(column(4).with_zone(ColumnZone::FrozenRight)),
);
assert_eq!(zone_order(&layout, ColumnZone::FrozenLeft), [0, 2]);
assert_eq!(zone_order(&layout, ColumnZone::Scrolling), [3]);
assert_eq!(zone_order(&layout, ColumnZone::FrozenRight), [4]);
assert_eq!(layout.bands_of(ix(3)), ["quote", "depth"]);
assert_eq!(layout.depth(), 2);
let tuning = GridTuning::default();
assert_eq!(tuning.min_rebuild_interval, Duration::from_millis(100));
assert_eq!(tuning.pull_interval, Duration::from_millis(8));
let tuning = tuning
.with_min_rebuild_interval(Duration::from_millis(250))
.with_pull_interval(Duration::from_millis(16));
assert_eq!(tuning.min_rebuild_interval, Duration::from_millis(250));
assert_eq!(tuning.pull_interval, Duration::from_millis(16));
let options = StoreOptions::default();
assert_eq!(options.highlight(), HighlightPolicy::default());
assert_eq!(options.publish_interval(), Duration::from_millis(8));
assert_eq!(
options.publish_interval(),
GridTuning::default().pull_interval
);
let highlight = HighlightPolicy::new(Duration::from_secs(2), 4);
let options = options
.with_highlight(highlight)
.with_publish_interval(Duration::from_millis(10));
assert_eq!(options.highlight(), highlight);
assert_eq!(options.publish_interval(), Duration::from_millis(10));
}
#[test]
#[should_panic(expected = "already in the schema")]
fn test_schema_rejects_repeated_column() {
let _ = GridSchema::new()
.column(column(1))
.band(ColumnBand::new("b", "B").column(column(1)));
}
#[test]
#[should_panic(expected = "band \"b\" is already in the schema")]
fn test_schema_rejects_repeated_band() {
let _ = GridSchema::new()
.band(ColumnBand::new("b", "B").column(column(1)))
.band(ColumnBand::new("b", "B").column(column(2)));
}
fn zoned() -> ColumnLayout<TestColumn> {
ColumnLayout::new(
GridSchema::new()
.column(
column(0)
.with_width(px(50.))
.with_zone(ColumnZone::FrozenLeft),
)
.column(column(1))
.column(column(2).with_width(px(80.)))
.column(column(3).with_width(px(120.)))
.column(
column(4)
.with_width(px(60.))
.with_zone(ColumnZone::FrozenRight),
)
.column(
column(5)
.with_width(px(40.))
.with_zone(ColumnZone::FrozenRight),
),
)
}
#[test]
fn test_column_layout_zones_and_offsets() {
let layout = zoned();
let offsets = |zone| layout.zone_columns(zone).offsets.clone();
assert_eq!(offsets(ColumnZone::FrozenLeft), [px(0.), px(50.)]);
assert_eq!(
offsets(ColumnZone::Scrolling),
[px(0.), px(100.), px(180.), px(300.)]
);
assert_eq!(
offsets(ColumnZone::FrozenRight),
[px(0.), px(60.), px(100.)]
);
assert_eq!(layout.total_width(), px(450.));
let geometry = layout.geometry(px(300.), px(0.));
let [left, scrolling, right] = &geometry.zones;
assert_eq!((left.x, left.width), (px(0.), px(50.)));
assert_eq!((scrolling.x, scrolling.width), (px(50.), px(150.)));
assert_eq!((right.x, right.width), (px(200.), px(100.)));
assert_eq!(scrolling.columns, 0..2);
assert_eq!(right.columns, 0..2);
assert_eq!(layout.geometry(px(300.), px(100.)).zones[1].columns, 1..3);
assert_eq!(layout.geometry(px(300.), px(160.)).zones[1].columns, 1..3);
for scroll_x in [px(0.), px(100.)] {
assert_eq!(layout.column_at_x(px(10.), px(300.), scroll_x), Some(ix(0)));
assert_eq!(
layout.column_at_x(px(210.), px(300.), scroll_x),
Some(ix(4))
);
assert_eq!(
layout.column_at_x(px(270.), px(300.), scroll_x),
Some(ix(5))
);
assert_eq!(layout.column_at_x(px(300.), px(300.), scroll_x), None);
assert_eq!(layout.column_at_x(px(-1.), px(300.), scroll_x), None);
}
assert_eq!(layout.column_at_x(px(60.), px(300.), px(0.)), Some(ix(1)));
assert_eq!(layout.column_at_x(px(60.), px(300.), px(100.)), Some(ix(2)));
assert_eq!(layout.column_at_x(px(199.), px(300.), px(0.)), Some(ix(2)));
let geometry = layout.geometry(px(120.), px(0.));
let [left, scrolling, right] = &geometry.zones;
assert_eq!((left.x, left.width), (px(0.), px(50.)));
assert_eq!((scrolling.width, scrolling.columns.clone()), (px(0.), 0..0));
assert_eq!((right.x, right.width), (px(50.), px(70.)));
assert_eq!(layout.column_at_x(px(60.), px(120.), px(0.)), Some(ix(4)));
assert_eq!(layout.column_at_x(px(115.), px(120.), px(0.)), Some(ix(5)));
let geometry = layout.geometry(px(30.), px(0.));
assert_eq!(geometry.zones[0].width, px(30.));
assert_eq!(geometry.zones[2].width, px(0.));
assert_eq!(layout.column_at_x(px(29.), px(30.), px(0.)), Some(ix(0)));
let layout = ColumnLayout::new(
GridSchema::new()
.column(column(0).with_width(px(50.)))
.column(column(1)),
);
let geometry = layout.geometry(px(120.), px(60.));
assert_eq!(
(geometry.zones[1].x, geometry.zones[1].width),
(px(0.), px(120.))
);
assert_eq!(geometry.zones[1].columns, 1..2);
assert_eq!(layout.column_at_x(px(10.), px(120.), px(60.)), Some(ix(1)));
let empty = ColumnLayout::new(GridSchema::<TestColumn>::new().with_frozen(2));
assert_eq!(empty.total_width(), px(0.));
assert_eq!(empty.geometry(px(200.), px(0.)).zones[1].columns, 0..0);
assert_eq!(empty.column_at_x(px(0.), px(200.), px(0.)), None);
}
#[test]
fn test_zone_change_places_column_at_nearest_edge() {
let layout = || {
ColumnLayout::new(
GridSchema::new()
.with_frozen(2)
.column(column(0))
.column(column(1))
.column(column(2))
.column(column(3))
.column(column(4))
.column(column(5).with_zone(ColumnZone::FrozenRight))
.column(column(6).with_zone(ColumnZone::FrozenRight)),
)
};
let all = |layout: &ColumnLayout<TestColumn>| {
ColumnZone::ALL.map(|zone| zone_order(layout, zone))
};
use ColumnZone::{FrozenLeft as L, FrozenRight as R, Scrolling as S};
for (column, zone, expected) in [
(3, L, [vec![0, 1, 3], vec![2, 4], vec![5, 6]]),
(3, R, [vec![0, 1], vec![2, 4], vec![3, 5, 6]]),
(0, S, [vec![1], vec![0, 2, 3, 4], vec![5, 6]]),
(6, S, [vec![0, 1], vec![2, 3, 4, 6], vec![5]]),
(0, R, [vec![1], vec![2, 3, 4], vec![0, 5, 6]]),
(6, L, [vec![0, 1, 6], vec![2, 3, 4], vec![5]]),
] {
let mut layout = layout();
assert_eq!(layout.set_zone(ix(column), zone, &FreeMove), [ix(column)]);
assert_eq!(all(&layout), expected, "c{column} to {zone:?}");
assert_eq!(layout.zone_of(ix(column)), zone);
assert!(layout.set_zone(ix(column), zone, &FreeMove).is_empty());
}
}
#[test]
fn test_column_width_limits() {
let narrow = column(0).with_min_width(px(5.));
assert_eq!(
narrow.min_width(),
MIN_COLUMN_WIDTH,
"raised to the minimum"
);
let limited = column(1)
.with_width(px(2.))
.with_min_width(px(30.))
.with_max_width(px(200.));
for (width, expected) in [
(px(50.), px(50.)),
(px(1000.), px(200.)),
(px(12.), px(30.)),
(px(0.), px(30.)),
(px(-5.), px(30.)),
(px(f32::NAN), px(30.)),
(px(f32::INFINITY), px(30.)),
] {
assert_eq!(limited.clamp_width(width), expected, "{width:?}");
}
assert_eq!(column(2).clamp_width(px(3.)), MIN_COLUMN_WIDTH);
let mut layout = ColumnLayout::new(GridSchema::new().column(limited).column(column(2)));
assert_eq!(
layout.width(ix(1)),
px(30.),
"the schema width is clamped too"
);
assert!(layout.set_width(ix(1), px(120.)));
assert!(!layout.set_width(ix(1), px(120.)), "unchanged");
assert!(layout.set_width(ix(2), px(f32::NAN)));
assert_eq!(layout.width(ix(2)), MIN_COLUMN_WIDTH);
assert!(!layout.set_width(ix(9), px(50.)), "not in the schema");
assert_eq!(
layout.zone_columns(ColumnZone::Scrolling).offsets[2],
px(130.)
);
}
#[test]
fn test_hide_show_keeps_position() {
let mut layout =
ColumnLayout::new((0..4).fold(GridSchema::new(), |schema, i| schema.column(column(i))));
assert_eq!(layout.set_visible(ix(1), false), Some(true));
assert_eq!(layout.set_visible(ix(1), false), Some(false));
assert_eq!(visible(&layout), [0, 2, 3]);
assert_eq!(zone_order(&layout, ColumnZone::Scrolling), [0, 1, 2, 3]);
assert_eq!(
layout.move_before(ColumnZone::Scrolling, vec![ix(3)], Some(ix(0)), &FreeMove),
Some(true)
);
assert_eq!(zone_order(&layout, ColumnZone::Scrolling), [3, 0, 1, 2]);
assert_eq!(layout.set_visible(ix(1), true), Some(true));
assert_eq!(visible(&layout), [3, 0, 1, 2]);
for i in [0, 1, 2] {
assert_eq!(layout.set_visible(ix(i), false), Some(true));
}
assert_eq!(layout.set_visible(ix(3), false), None);
assert_eq!(visible(&layout), [3]);
let shown = layout.show_all();
assert_eq!(shown.len(), 3);
assert!(shown.contains(ix(0)) && !shown.contains(ix(3)));
assert_eq!(visible(&layout), [3, 0, 1, 2]);
assert!(layout.show_all().is_empty());
assert_eq!(layout.set_visible(ix(9), false), None, "not in the schema");
let layout = ColumnLayout::new(
GridSchema::new()
.column(column(5).with_visible(false))
.column(column(6).with_visible(false)),
);
assert_eq!(visible(&layout), [5]);
}
#[test]
fn test_view_column_ix_round_trip() {
let mut layout = zoned();
layout.set_visible(ix(2), false);
layout.set_visible(ix(5), false);
assert_eq!(visible(&layout), [0, 1, 3, 4]);
for (n, column) in layout.visible().collect::<Vec<_>>().into_iter().enumerate() {
let view = layout.view_column_ix(column).expect("visible");
assert_eq!(view, ViewColumnIx::new(n as u16));
assert_eq!(view.get(), n);
assert_eq!(layout.column_at(view), Some(column));
}
assert_eq!(layout.view_column_ix(ix(2)), None);
assert_eq!(layout.view_column_ix(ix(5)), None);
assert_eq!(layout.view_column_ix(ix(11)), None, "not in the schema");
assert_eq!(layout.column_at(ViewColumnIx::new(4)), None);
assert_eq!(
layout.zone_position(ix(3)),
Some((ColumnZone::Scrolling, 1))
);
assert_eq!(
layout.zone_position(ix(4)),
Some((ColumnZone::FrozenRight, 0))
);
}
fn banded() -> GridSchema<TestColumn> {
GridSchema::new()
.column(column(0))
.band(
ColumnBand::new("b0", "Outer")
.column(column(1))
.band(
ColumnBand::new("b1", "Inner")
.column(column(2))
.column(column(3)),
)
.column(column(4)),
)
.column(column(5))
}
#[test]
fn test_banded_move_keeps_bands_together() {
let mut layout = ColumnLayout::new(banded());
layout.normalize(&BandedMove);
let scrolling =
|layout: &ColumnLayout<TestColumn>| zone_order(layout, ColumnZone::Scrolling);
let zone = ColumnZone::Scrolling;
assert_eq!(
layout.move_before(zone, vec![ix(2)], Some(ix(0)), &BandedMove),
None
);
assert_eq!(
layout.move_before(zone, vec![ix(3)], Some(ix(2)), &BandedMove),
Some(true)
);
assert_eq!(scrolling(&layout), [0, 1, 3, 2, 4, 5]);
let members = layout.band_members(zone, "b0");
assert_eq!(members, [1, 3, 2, 4].map(ix));
assert_eq!(
layout.move_before(zone, members, None, &BandedMove),
Some(true)
);
assert_eq!(scrolling(&layout), [0, 5, 1, 3, 2, 4]);
assert_eq!(
layout.set_zone(ix(2), ColumnZone::FrozenLeft, &BandedMove),
[1, 3, 2, 4].map(ix)
);
assert_eq!(zone_order(&layout, ColumnZone::FrozenLeft), [1, 3, 2, 4]);
assert_eq!(
layout.move_before(
ColumnZone::FrozenLeft,
vec![ix(1)],
Some(ix(1)),
&BandedMove
),
Some(false)
);
let mut layout = ColumnLayout::new(banded());
layout.set_zone(ix(4), ColumnZone::FrozenRight, &FreeMove);
layout.move_before(zone, vec![ix(0)], Some(ix(3)), &FreeMove);
assert_eq!(scrolling(&layout), [1, 2, 0, 3, 5]);
layout.normalize(&BandedMove);
assert_eq!(scrolling(&layout), [1, 2, 3, 4, 0, 5]);
assert!(zone_order(&layout, ColumnZone::FrozenRight).is_empty());
let before = layout.order().to_vec();
layout.normalize(&BandedMove);
assert_eq!(layout.order(), before);
}
#[test]
fn test_free_move_splits_band_runs() {
let mut layout = ColumnLayout::new(banded());
let zone = ColumnZone::Scrolling;
assert_eq!(
layout.move_before(zone, vec![ix(1)], None, &BandedMove),
None
);
assert_eq!(
layout.move_before(zone, vec![ix(1)], None, &FreeMove),
Some(true)
);
assert_eq!(zone_order(&layout, zone), [0, 2, 3, 4, 5, 1]);
let outer: Vec<_> = layout
.runs()
.iter()
.filter(|run| run.band == "b0")
.map(|run| (run.columns.clone(), run.title.clone()))
.collect();
assert_eq!(
outer,
[(1..4, "Outer".into()), (5..6, "Outer".into())],
"two runs with one title"
);
assert_eq!(layout.band_segment(zone, "b0", ix(2)), [2, 3, 4].map(ix));
assert_eq!(layout.band_segment(zone, "b0", ix(1)), [ix(1)]);
assert!(layout.band_segment(zone, "b1", ix(1)).is_empty());
}
#[test]
fn test_band_runs_and_header_rows() {
let mut layout = ColumnLayout::new(banded());
assert_eq!(layout.depth(), 2);
assert_eq!(layout.bands_of(ix(0)).len(), 0);
assert_eq!(layout.bands_of(ix(1)).len(), 1);
assert_eq!(layout.bands_of(ix(2)).len(), 2);
let runs = |layout: &ColumnLayout<TestColumn>| {
layout
.runs()
.iter()
.map(|run| (run.band, run.level, run.columns.clone()))
.collect::<Vec<_>>()
};
assert_eq!(runs(&layout), [("b0", 0, 1..5), ("b1", 1, 2..4)]);
layout.set_visible(ix(2), false);
layout.set_visible(ix(3), false);
assert_eq!(runs(&layout), [("b0", 0, 1..3)]);
layout.set_zone(ix(1), ColumnZone::FrozenRight, &BandedMove);
assert_eq!(runs(&layout), [("b0", 0, 0..2)]);
assert_eq!(layout.runs()[0].zone, ColumnZone::FrozenRight);
let flat = ColumnLayout::new(GridSchema::new().column(column(0)));
assert_eq!(flat.depth(), 0);
assert!(flat.runs().is_empty());
}
fn random_schema(rng: &mut fastrand::Rng) -> (GridSchema<TestColumn>, usize) {
fn items(
rng: &mut fastrand::Rng,
level: usize,
next_column: &mut u16,
next_band: &mut usize,
limit: u16,
) -> Vec<Result<GridColumn<TestColumn>, ColumnBand<TestColumn>>> {
let mut out = Vec::new();
for _ in 0..rng.usize(1..=4) {
if *next_column >= limit {
break;
}
if level < 2 && *next_band < BANDS.len() && rng.u8(..3) == 0 {
let id = BANDS[*next_band];
*next_band += 1;
let children = items(rng, level + 1, next_column, next_band, limit);
let band = children
.into_iter()
.fold(ColumnBand::new(id, id), |band, child| match child {
Ok(column) => band.column(column),
Err(nested) => band.band(nested),
});
out.push(Err(band));
} else {
let i = *next_column;
*next_column += 1;
let zone = match rng.u8(..6) {
0 => ColumnZone::FrozenLeft,
1 => ColumnZone::FrozenRight,
_ => ColumnZone::Scrolling,
};
out.push(Ok(column(i)
.with_width(px(rng.u16(10..200) as f32))
.with_zone(zone)
.with_visible(rng.u8(..5) != 0)));
}
}
out
}
let limit = rng.u16(1..=12);
let (mut next_column, mut next_band) = (0, 0);
let mut schema = GridSchema::new();
while next_column < limit {
for item in items(rng, 0, &mut next_column, &mut next_band, limit) {
schema = match item {
Ok(column) => schema.column(column),
Err(band) => schema.band(band),
};
}
}
(schema, next_band)
}
fn check_invariants(layout: &ColumnLayout<TestColumn>, banded: bool, bands: usize) {
let order = layout.order();
let mut seen = order.to_vec();
seen.sort();
seen.dedup();
assert_eq!(seen.len(), order.len(), "no column twice: {order:?}");
assert!(
order.iter().all(|c| layout.get(*c).is_some()),
"only schema columns"
);
assert!(
order
.windows(2)
.all(|w| layout.zone_of(w[0]).slot() <= layout.zone_of(w[1]).slot()),
"split by zone: {order:?}"
);
assert!(!layout.visible_set().is_empty(), "one column is visible");
if banded {
for band in &BANDS[..bands] {
let positions: Vec<usize> = order
.iter()
.enumerate()
.filter(|(_, c)| layout.bands_of(**c).contains(band))
.map(|(p, _)| p)
.collect();
if let (Some(first), Some(last)) = (positions.first(), positions.last()) {
assert_eq!(last - first + 1, positions.len(), "{band} is contiguous");
let zone = layout.zone_of(order[*first]);
assert!(positions.iter().all(|p| layout.zone_of(order[*p]) == zone));
}
}
}
for zone in ColumnZone::ALL {
let columns = layout.zone_columns(zone);
let mut x = px(0.);
for (i, column) in columns.columns.iter().enumerate() {
assert_eq!(columns.offsets[i], x);
x += layout.width(*column);
}
assert_eq!(columns.content_width(), x);
}
let visible: Vec<ColumnIx> = layout.visible().collect();
assert_eq!(visible.len(), layout.visible_set().len());
for (n, column) in visible.iter().enumerate() {
assert_eq!(
layout.view_column_ix(*column),
Some(ViewColumnIx::new(n as u16))
);
assert_eq!(layout.column_at(ViewColumnIx::new(n as u16)), Some(*column));
}
for column in order.iter().filter(|c| !layout.is_visible(**c)) {
assert_eq!(layout.view_column_ix(*column), None);
}
}
#[test]
fn test_column_ops_match_reference_model() {
for seed in 0..60 {
for banded in [true, false] {
let mut rng = fastrand::Rng::with_seed(seed);
let (schema, bands) = random_schema(&mut rng);
let mut layout = ColumnLayout::new(schema);
let policy: &dyn ColumnMovePolicy = if banded { &BandedMove } else { &FreeMove };
layout.normalize(policy);
let mut model: Vec<(ColumnIx, ColumnZone)> = layout
.order()
.iter()
.map(|c| (*c, layout.zone_of(*c)))
.collect();
check_invariants(&layout, banded, bands);
let columns: Vec<ColumnIx> = layout.order().to_vec();
for step in 0..40 {
let column = columns[rng.usize(..columns.len())];
match rng.u8(..5) {
0 => {
let zone = layout.zone_of(column);
let before = rng
.bool()
.then(|| columns[rng.usize(..columns.len())])
.filter(|before| layout.zone_of(*before) == zone);
let moved = layout.move_before(zone, vec![column], before, policy);
if !banded {
assert!(moved.is_some(), "free moves are never refused");
let at = model.iter().position(|(c, _)| *c == column).unwrap();
let entry = model.remove(at);
let insert = match before {
Some(before) if before != column => {
model.iter().position(|(c, _)| *c == before).unwrap()
}
Some(_) => at,
None => {
model.iter().rposition(|(_, z)| *z == zone).map_or_else(
|| {
model
.iter()
.filter(|(_, z)| z.slot() < zone.slot())
.count()
},
|p| p + 1,
)
}
};
model.insert(insert, entry);
}
}
1 => {
let zone = ColumnZone::ALL[rng.usize(..3)];
let moved = layout.set_zone(column, zone, policy);
if !banded && !moved.is_empty() {
let at = model.iter().position(|(c, _)| *c == column).unwrap();
let (_, from) = model.remove(at);
let insert = if zone.slot() < from.slot() {
model
.iter()
.filter(|(_, z)| z.slot() <= zone.slot())
.count()
} else {
model.iter().filter(|(_, z)| z.slot() < zone.slot()).count()
};
model.insert(insert, (column, zone));
}
}
2 => {
let shown = layout.is_visible(column);
layout.set_visible(column, !shown);
}
3 => {
layout.set_width(column, px(rng.u16(0..300) as f32));
}
_ => {
if let Some(&band) = layout.bands_of(column).first() {
let zone = layout.zone_of(column);
let members = layout.band_members(zone, band);
let before = columns[rng.usize(..columns.len())];
let before = (layout.zone_of(before) == zone).then_some(before);
let changed = layout.move_before(zone, members, before, policy);
let top_level = before.is_none_or(|before| {
layout.bands_of(before).is_empty()
|| layout.bands_of(before).first() == Some(&band)
});
if banded && top_level {
assert!(changed.is_some(), "a whole band moves among items");
} else if !banded && changed == Some(true) {
model = layout
.order()
.iter()
.map(|c| (*c, layout.zone_of(*c)))
.collect();
}
}
}
}
check_invariants(&layout, banded, bands);
if !banded {
let expected: Vec<ColumnIx> = model.iter().map(|(c, _)| *c).collect();
assert_eq!(layout.order(), expected, "seed {seed} step {step}");
}
}
}
}
}
}