use std::{fmt, hash::Hash, rc::Rc};
use gpui_kit::{
App, Context, Entity, EntityId, FocusHandle, Focusable as _, Pixels, SharedString,
Subscription, WeakEntity, Window,
};
#[cfg(not(target_family = "wasm"))]
use super::export::{ExportJob, ExportRequest};
use super::{
ColumnOf, LiveGridDelegate, LiveGridState,
chooser::ChooserCommand,
columns::{ColumnZone, HeaderLabel},
events::{GroupOrigin, LayoutOrigin, LayoutParts, LiveGridEvent, SelectionOrigin, SortOrigin},
};
#[cfg(not(target_family = "wasm"))]
use crate::export::{CsvFormat, ExportError};
use crate::{
engine::{ExpandDepth, GroupBy, SortDirection},
export::ExportScope,
model::{ColumnId, ColumnIx, GridColumns as _},
persist::{GridLayout, LayoutError, LayoutGroup, LayoutReport, LayoutSort},
policy::ReorderPolicy,
};
#[derive(Clone)]
pub struct LiveGridHandle {
ops: Rc<dyn GridOps>,
}
impl LiveGridHandle {
pub fn new<D: LiveGridDelegate>(grid: &Entity<LiveGridState<D>>) -> Self {
Self {
ops: Rc::new(Ops { grid: grid.clone() }),
}
}
pub fn entity_id(&self) -> EntityId {
self.ops.entity_id()
}
pub fn downgrade(&self) -> WeakLiveGridHandle {
self.ops.downgrade()
}
pub fn focus_handle(&self, cx: &App) -> FocusHandle {
self.ops.focus_handle(cx)
}
pub fn columns(&self, cx: &App) -> Vec<ColumnInfo> {
self.ops.columns(cx)
}
pub fn column(&self, id: &str, cx: &App) -> Option<ColumnInfo> {
self.ops.column(id, cx)
}
pub fn set_column_visible(&self, id: &str, visible: bool, cx: &mut App) -> bool {
self.ops.set_column_visible(id, visible, cx)
}
pub fn show_all_columns(&self, cx: &mut App) {
self.ops.show_all_columns(cx);
}
pub fn move_column(&self, id: &str, before: Option<&str>, cx: &mut App) -> bool {
self.ops.move_column(id, before, cx)
}
pub fn move_band(&self, band: &str, before: Option<&str>, cx: &mut App) -> bool {
self.ops.move_band(band, before, cx)
}
pub fn set_column_zone(&self, id: &str, zone: ColumnZone, cx: &mut App) -> bool {
self.ops.set_column_zone(id, zone, cx)
}
pub fn set_column_width(&self, id: &str, width: Pixels, cx: &mut App) -> bool {
self.ops.set_column_width(id, width, cx)
}
pub fn autofit_columns(&self, cx: &mut App) {
self.ops.autofit_columns(cx);
}
pub fn view_id(&self, cx: &App) -> Option<SharedString> {
self.ops.view_id(cx)
}
pub fn layout(&self, cx: &App) -> GridLayout {
self.ops.layout(cx)
}
pub fn set_layout(
&self,
layout: &GridLayout,
cx: &mut App,
) -> Result<LayoutReport, LayoutError> {
self.ops.set_layout(layout, cx)
}
pub fn reset_layout(&self, cx: &mut App) {
self.ops.reset_layout(cx);
}
pub fn sort(&self, cx: &App) -> Vec<LayoutSort> {
self.ops.sort(cx)
}
pub fn set_sort(&self, sort: impl IntoIterator<Item = LayoutSort>, cx: &mut App) -> bool {
self.ops.set_sort(&sort.into_iter().collect::<Vec<_>>(), cx)
}
pub fn group_by(&self, cx: &App) -> Vec<LayoutGroup> {
self.ops.group_by(cx)
}
pub fn set_group_by(
&self,
levels: impl IntoIterator<Item = LayoutGroup>,
cx: &mut App,
) -> bool {
self.ops
.set_group_by(&levels.into_iter().collect::<Vec<_>>(), cx)
}
pub fn expand_depth(&self, cx: &App) -> ExpandDepth {
self.ops.expand_depth(cx)
}
pub fn set_expand_depth(&self, depth: ExpandDepth, cx: &mut App) {
self.ops.set_expand_depth(depth, cx);
}
pub fn quick_filter(&self, cx: &App) -> SharedString {
self.ops.quick_filter(cx)
}
pub fn set_quick_filter(&self, text: impl Into<SharedString>, cx: &mut App) {
self.ops.set_quick_filter(text.into(), cx);
}
pub fn row_count(&self, cx: &App) -> usize {
self.ops.row_count(cx)
}
pub fn total_row_count(&self, cx: &App) -> usize {
self.ops.total_row_count(cx)
}
pub fn selected_count(&self, cx: &App) -> usize {
self.ops.selected_count(cx)
}
pub fn is_order_held(&self, cx: &App) -> bool {
self.ops.is_order_held(cx)
}
pub fn apply_held_order(&self, cx: &mut App) {
self.ops.apply_held_order(cx);
}
pub fn set_reorder_policy(&self, policy: impl ReorderPolicy, cx: &mut App) {
self.ops.set_reorder_policy(Box::new(policy), cx);
}
pub fn copy_scope(&self, scope: ExportScope, cx: &mut App) -> bool {
self.ops.copy_scope(scope, cx)
}
pub fn is_export_empty(&self, scope: ExportScope, cx: &App) -> bool {
self.ops.is_export_empty(scope, cx)
}
#[cfg(not(target_family = "wasm"))]
pub fn export_to_path(
&self,
scope: ExportScope,
format: CsvFormat,
path: impl Into<std::path::PathBuf>,
cx: &mut App,
) -> Result<ExportJob<std::path::PathBuf>, ExportError> {
self.ops.export_to_path(scope, format, path.into(), cx)
}
pub fn subscribe(
&self,
cx: &mut App,
on_event: impl FnMut(&GridHandleEvent, &mut App) + 'static,
) -> Subscription {
self.ops.subscribe(Box::new(on_event), cx)
}
pub fn observe(&self, cx: &mut App, on_notify: impl FnMut(&mut App) + 'static) -> Subscription {
self.ops.observe(Box::new(on_notify), cx)
}
pub fn show_column_chooser(&self, window: &mut Window, cx: &mut App) {
self.ops.show_column_chooser(window, cx);
}
pub fn is_column_chooser_open(&self, cx: &App) -> bool {
self.ops.is_column_chooser_open(cx)
}
pub(super) fn ops(&self) -> &dyn GridOps {
&*self.ops
}
}
impl fmt::Debug for LiveGridHandle {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("LiveGridHandle")
.field(&self.entity_id())
.finish()
}
}
impl<D: LiveGridDelegate> From<&Entity<LiveGridState<D>>> for LiveGridHandle {
fn from(grid: &Entity<LiveGridState<D>>) -> Self {
Self::new(grid)
}
}
#[derive(Clone)]
pub struct WeakLiveGridHandle {
entity_id: EntityId,
upgrade: Rc<dyn Fn() -> Option<LiveGridHandle>>,
}
impl WeakLiveGridHandle {
pub fn upgrade(&self) -> Option<LiveGridHandle> {
(self.upgrade)()
}
pub fn entity_id(&self) -> EntityId {
self.entity_id
}
}
impl fmt::Debug for WeakLiveGridHandle {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("WeakLiveGridHandle")
.field(&self.entity_id)
.finish()
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum GridHandleEvent {
LayoutChange {
parts: LayoutParts,
origin: LayoutOrigin,
},
SortChange {
origin: SortOrigin,
},
GroupChange {
origin: GroupOrigin,
},
ExpansionChange {
origin: GroupOrigin,
},
ProjectionChange {
row_count: usize,
store_version: u64,
},
SelectionChange {
count: usize,
origin: SelectionOrigin,
},
OrderHeldChange {
held: bool,
},
}
impl GridHandleEvent {
fn from_grid_event<K: Eq + Hash, C>(event: &LiveGridEvent<K, C>) -> Option<Self> {
Some(match event {
LiveGridEvent::LayoutChange { parts, origin, .. } => Self::LayoutChange {
parts: *parts,
origin: *origin,
},
LiveGridEvent::SortChange { origin, .. } => Self::SortChange { origin: *origin },
LiveGridEvent::GroupChange { origin } => Self::GroupChange { origin: *origin },
LiveGridEvent::ExpansionChange { origin, .. } => {
Self::ExpansionChange { origin: *origin }
}
LiveGridEvent::ProjectionChange {
row_count,
store_version,
} => Self::ProjectionChange {
row_count: *row_count,
store_version: *store_version,
},
LiveGridEvent::SelectionChange { selection, origin } => Self::SelectionChange {
count: selection.len(),
origin: *origin,
},
LiveGridEvent::OrderHeldChange { held } => Self::OrderHeldChange { held: *held },
_ => return None,
})
}
}
#[derive(Clone, Debug)]
pub struct ColumnInfo {
id: ColumnId,
title: SharedString,
header_label: HeaderLabel,
zone: ColumnZone,
visible: bool,
width: Pixels,
bands: Box<[BandInfo]>,
hideable: bool,
internal: bool,
movable: bool,
resizable: bool,
sortable: bool,
groupable: bool,
sort: Option<(SortDirection, usize)>,
}
impl ColumnInfo {
pub fn id(&self) -> &ColumnId {
&self.id
}
pub fn title(&self) -> &SharedString {
&self.title
}
pub fn icon(&self) -> Option<&SharedString> {
match &self.header_label {
HeaderLabel::Title => None,
HeaderLabel::IconAndTitle(icon) | HeaderLabel::Icon(icon) => Some(icon),
}
}
pub fn header_label(&self) -> &HeaderLabel {
&self.header_label
}
pub fn zone(&self) -> ColumnZone {
self.zone
}
pub fn is_visible(&self) -> bool {
self.visible
}
pub fn width(&self) -> Pixels {
self.width
}
pub fn bands(&self) -> &[BandInfo] {
&self.bands
}
pub fn is_hideable(&self) -> bool {
self.hideable
}
pub fn is_internal(&self) -> bool {
self.internal
}
pub fn is_movable(&self) -> bool {
self.movable
}
pub fn is_resizable(&self) -> bool {
self.resizable
}
pub fn is_sortable(&self) -> bool {
self.sortable
}
pub fn is_groupable(&self) -> bool {
self.groupable
}
pub fn sort(&self) -> Option<SortDirection> {
self.sort.map(|(direction, _)| direction)
}
pub fn sort_priority(&self) -> Option<usize> {
self.sort.map(|(_, priority)| priority)
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct BandInfo {
id: &'static str,
title: SharedString,
}
impl BandInfo {
pub fn id(&self) -> &'static str {
self.id
}
pub fn title(&self) -> &SharedString {
&self.title
}
}
impl<D: LiveGridDelegate> LiveGridState<D> {
pub fn handle(&self, cx: &Context<Self>) -> LiveGridHandle {
LiveGridHandle::new(&cx.entity())
}
pub(super) fn column_by_id(&self, id: &str) -> Option<ColumnOf<D>> {
<ColumnOf<D> as crate::model::GridColumns>::all()
.iter()
.copied()
.find(|column| column.id() == id)
.filter(|column| self.columns.get(column.ix()).is_some())
}
fn column_info(&self, ix: ColumnIx) -> Option<ColumnInfo> {
let column = self.columns.get(ix)?;
let sort = self
.view
.sort()
.iter()
.enumerate()
.find(|(_, (sorted, _))| sorted.ix() == ix)
.map(|(priority, (_, direction))| (*direction, priority));
Some(ColumnInfo {
id: column.column().column_id(),
title: column.title().clone(),
header_label: column.header_label().clone(),
zone: self.columns.zone_of(ix),
visible: self.columns.is_visible(ix),
width: self.columns.width(ix),
bands: self
.columns
.bands_of(ix)
.iter()
.map(|&id| BandInfo {
id,
title: self
.columns
.band_title(id)
.cloned()
.unwrap_or_else(|| SharedString::new_static(id)),
})
.collect(),
hideable: column.is_hideable(),
internal: column.is_internal(),
movable: column.is_movable(),
resizable: column.is_resizable(),
sortable: column.is_sortable(),
groupable: column.is_groupable(),
sort,
})
}
pub(super) fn band_id(&self, band: &str) -> Option<&'static str> {
self.columns
.order()
.iter()
.flat_map(|ix| self.columns.bands_of(*ix).iter().copied())
.find(|id| *id == band)
}
}
pub(super) trait GridOps {
fn entity_id(&self) -> EntityId;
fn downgrade(&self) -> WeakLiveGridHandle;
fn focus_handle(&self, cx: &App) -> FocusHandle;
fn columns(&self, cx: &App) -> Vec<ColumnInfo>;
fn column(&self, id: &str, cx: &App) -> Option<ColumnInfo>;
fn set_column_visible(&self, id: &str, visible: bool, cx: &mut App) -> bool;
fn show_all_columns(&self, cx: &mut App);
fn move_column(&self, id: &str, before: Option<&str>, cx: &mut App) -> bool;
fn move_band(&self, band: &str, before: Option<&str>, cx: &mut App) -> bool;
fn set_column_zone(&self, id: &str, zone: ColumnZone, cx: &mut App) -> bool;
fn set_column_width(&self, id: &str, width: Pixels, cx: &mut App) -> bool;
fn autofit_columns(&self, cx: &mut App);
fn view_id(&self, cx: &App) -> Option<SharedString>;
fn layout(&self, cx: &App) -> GridLayout;
fn set_layout(&self, layout: &GridLayout, cx: &mut App) -> Result<LayoutReport, LayoutError>;
fn reset_layout(&self, cx: &mut App);
fn sort(&self, cx: &App) -> Vec<LayoutSort>;
fn set_sort(&self, sort: &[LayoutSort], cx: &mut App) -> bool;
fn group_by(&self, cx: &App) -> Vec<LayoutGroup>;
fn set_group_by(&self, levels: &[LayoutGroup], cx: &mut App) -> bool;
fn expand_depth(&self, cx: &App) -> ExpandDepth;
fn set_expand_depth(&self, depth: ExpandDepth, cx: &mut App);
fn quick_filter(&self, cx: &App) -> SharedString;
fn set_quick_filter(&self, text: SharedString, cx: &mut App);
fn row_count(&self, cx: &App) -> usize;
fn total_row_count(&self, cx: &App) -> usize;
fn selected_count(&self, cx: &App) -> usize;
fn is_order_held(&self, cx: &App) -> bool;
fn apply_held_order(&self, cx: &mut App);
fn set_reorder_policy(&self, policy: Box<dyn ReorderPolicy>, cx: &mut App);
fn copy_scope(&self, scope: ExportScope, cx: &mut App) -> bool;
fn is_export_empty(&self, scope: ExportScope, cx: &App) -> bool;
#[cfg(not(target_family = "wasm"))]
fn export_to_path(
&self,
scope: ExportScope,
format: CsvFormat,
path: std::path::PathBuf,
cx: &mut App,
) -> Result<ExportJob<std::path::PathBuf>, ExportError>;
fn subscribe(
&self,
on_event: Box<dyn FnMut(&GridHandleEvent, &mut App)>,
cx: &mut App,
) -> Subscription;
fn observe(&self, on_notify: Box<dyn FnMut(&mut App)>, cx: &mut App) -> Subscription;
fn show_column_chooser(&self, window: &mut Window, cx: &mut App);
fn is_column_chooser_open(&self, cx: &App) -> bool;
fn chooser(
&self,
command: ChooserCommand<'_>,
window: Option<&mut Window>,
cx: &mut App,
) -> bool;
}
struct Ops<D: LiveGridDelegate> {
grid: Entity<LiveGridState<D>>,
}
impl<D: LiveGridDelegate> Ops<D> {
fn on_column(
&self,
id: &str,
cx: &mut App,
command: impl FnOnce(&mut LiveGridState<D>, ColumnOf<D>, &mut Context<LiveGridState<D>>) -> bool,
) -> bool {
self.grid
.update(cx, |grid, cx| match grid.column_by_id(id) {
Some(column) => command(grid, column, cx),
None => false,
})
}
}
impl<D: LiveGridDelegate> GridOps for Ops<D> {
fn entity_id(&self) -> EntityId {
self.grid.entity_id()
}
fn downgrade(&self) -> WeakLiveGridHandle {
let grid: WeakEntity<LiveGridState<D>> = self.grid.downgrade();
WeakLiveGridHandle {
entity_id: self.grid.entity_id(),
upgrade: Rc::new(move || grid.upgrade().map(|grid| LiveGridHandle::new(&grid))),
}
}
fn focus_handle(&self, cx: &App) -> FocusHandle {
self.grid.read(cx).focus_handle(cx)
}
fn columns(&self, cx: &App) -> Vec<ColumnInfo> {
let grid = self.grid.read(cx);
grid.columns
.order()
.iter()
.filter_map(|ix| grid.column_info(*ix))
.collect()
}
fn column(&self, id: &str, cx: &App) -> Option<ColumnInfo> {
let grid = self.grid.read(cx);
grid.column_info(grid.column_by_id(id)?.ix())
}
fn set_column_visible(&self, id: &str, visible: bool, cx: &mut App) -> bool {
self.on_column(id, cx, |grid, column, cx| {
grid.set_column_visible(column, visible, cx)
})
}
fn show_all_columns(&self, cx: &mut App) {
self.grid.update(cx, LiveGridState::show_all_columns);
}
fn move_column(&self, id: &str, before: Option<&str>, cx: &mut App) -> bool {
self.grid.update(cx, |grid, cx| {
let Some(column) = grid.column_by_id(id) else {
return false;
};
let before = match before {
Some(before) => match grid.column_by_id(before) {
Some(before) => Some(before),
None => return false,
},
None => None,
};
grid.move_column(column, before, cx)
})
}
fn move_band(&self, band: &str, before: Option<&str>, cx: &mut App) -> bool {
self.grid.update(cx, |grid, cx| {
let Some(band) = grid.band_id(band) else {
return false;
};
let before = match before {
Some(before) => match grid.column_by_id(before) {
Some(before) => Some(before),
None => return false,
},
None => None,
};
grid.move_band(band, before, cx)
})
}
fn set_column_zone(&self, id: &str, zone: ColumnZone, cx: &mut App) -> bool {
self.on_column(id, cx, |grid, column, cx| {
grid.set_column_zone(column, zone, cx);
true
})
}
fn set_column_width(&self, id: &str, width: Pixels, cx: &mut App) -> bool {
self.on_column(id, cx, |grid, column, cx| {
grid.set_column_width(column, width, cx);
true
})
}
fn autofit_columns(&self, cx: &mut App) {
self.grid.update(cx, LiveGridState::autofit_columns);
}
fn view_id(&self, cx: &App) -> Option<SharedString> {
self.grid.read(cx).view_id().cloned()
}
fn layout(&self, cx: &App) -> GridLayout {
self.grid.read(cx).layout()
}
fn set_layout(&self, layout: &GridLayout, cx: &mut App) -> Result<LayoutReport, LayoutError> {
self.grid.update(cx, |grid, cx| grid.set_layout(layout, cx))
}
fn reset_layout(&self, cx: &mut App) {
self.grid.update(cx, LiveGridState::reset_layout);
}
fn sort(&self, cx: &App) -> Vec<LayoutSort> {
self.grid
.read(cx)
.sort()
.iter()
.map(|(column, direction)| LayoutSort::new(column.column_id(), *direction))
.collect()
}
fn set_sort(&self, sort: &[LayoutSort], cx: &mut App) -> bool {
self.grid.update(cx, |grid, cx| {
let Some(sort) = sort
.iter()
.map(|level| {
Some((
grid.column_by_id(level.column().as_ref())?,
level.direction(),
))
})
.collect::<Option<Vec<_>>>()
else {
return false;
};
grid.set_sort(sort, cx);
true
})
}
fn group_by(&self, cx: &App) -> Vec<LayoutGroup> {
self.grid
.read(cx)
.group_by()
.iter()
.map(|level| LayoutGroup::new(level.id(), level.direction()))
.collect()
}
fn set_group_by(&self, levels: &[LayoutGroup], cx: &mut App) -> bool {
self.grid.update(cx, |grid, cx| {
let Some(levels) = levels
.iter()
.map(|level| {
let column = grid.column_by_id(level.column().as_ref())?;
Some(GroupBy::column(column).with_direction(level.direction()))
})
.collect::<Option<Vec<_>>>()
else {
return false;
};
grid.set_group_by(levels, cx);
true
})
}
fn expand_depth(&self, cx: &App) -> ExpandDepth {
self.grid.read(cx).expand_depth()
}
fn set_expand_depth(&self, depth: ExpandDepth, cx: &mut App) {
self.grid
.update(cx, |grid, cx| grid.set_expand_depth(depth, cx));
}
fn quick_filter(&self, cx: &App) -> SharedString {
self.grid.read(cx).quick_filter().clone()
}
fn set_quick_filter(&self, text: SharedString, cx: &mut App) {
self.grid
.update(cx, |grid, cx| grid.set_quick_filter(text, cx));
}
fn row_count(&self, cx: &App) -> usize {
self.grid.read(cx).projection.row_count()
}
fn total_row_count(&self, cx: &App) -> usize {
self.grid.read(cx).snapshot.len()
}
fn selected_count(&self, cx: &App) -> usize {
self.grid.read(cx).selection().len()
}
fn is_order_held(&self, cx: &App) -> bool {
self.grid.read(cx).is_order_held()
}
fn apply_held_order(&self, cx: &mut App) {
self.grid.update(cx, LiveGridState::apply_held_order);
}
fn set_reorder_policy(&self, policy: Box<dyn ReorderPolicy>, cx: &mut App) {
self.grid.update(cx, |grid, cx| {
grid.reorder = policy;
grid.reconsider_held(cx);
});
}
fn copy_scope(&self, scope: ExportScope, cx: &mut App) -> bool {
self.grid.update(cx, |grid, cx| grid.copy_scope(scope, cx))
}
fn is_export_empty(&self, scope: ExportScope, cx: &App) -> bool {
self.grid.read(cx).is_export_empty(scope)
}
#[cfg(not(target_family = "wasm"))]
fn export_to_path(
&self,
scope: ExportScope,
format: CsvFormat,
path: std::path::PathBuf,
cx: &mut App,
) -> Result<ExportJob<std::path::PathBuf>, ExportError> {
self.grid.update(cx, |grid, cx| {
grid.export_to_path(ExportRequest::new(scope), format, path, cx)
})
}
fn subscribe(
&self,
mut on_event: Box<dyn FnMut(&GridHandleEvent, &mut App)>,
cx: &mut App,
) -> Subscription {
cx.subscribe(&self.grid, move |_, event: &LiveGridEvent<_, _>, cx| {
if let Some(event) = GridHandleEvent::from_grid_event(event) {
on_event(&event, cx);
}
})
}
fn observe(&self, mut on_notify: Box<dyn FnMut(&mut App)>, cx: &mut App) -> Subscription {
cx.observe(&self.grid, move |_, cx| on_notify(cx))
}
fn show_column_chooser(&self, window: &mut Window, cx: &mut App) {
self.grid
.update(cx, |grid, cx| grid.show_column_chooser(window, cx));
}
fn is_column_chooser_open(&self, cx: &App) -> bool {
self.grid.read(cx).is_column_chooser_open()
}
fn chooser(
&self,
command: ChooserCommand<'_>,
window: Option<&mut Window>,
cx: &mut App,
) -> bool {
self.grid
.update(cx, |grid, cx| grid.run_chooser_command(command, window, cx))
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::state::selection::GridSelection;
#[test]
fn test_handle_event_translation() {
let event = |event: LiveGridEvent<u32, u16>| GridHandleEvent::from_grid_event(&event);
assert_eq!(
event(LiveGridEvent::OrderHeldChange { held: true }),
Some(GridHandleEvent::OrderHeldChange { held: true })
);
assert_eq!(
event(LiveGridEvent::ProjectionChange {
row_count: 12,
store_version: 3
}),
Some(GridHandleEvent::ProjectionChange {
row_count: 12,
store_version: 3
})
);
assert_eq!(
event(LiveGridEvent::SelectionChange {
selection: GridSelection::default(),
origin: SelectionOrigin::Programmatic,
}),
Some(GridHandleEvent::SelectionChange {
count: 0,
origin: SelectionOrigin::Programmatic
})
);
assert_eq!(
event(LiveGridEvent::Confirm {
keys: [1].into(),
secondary: false
}),
None
);
}
}