use std::{hash::Hash, sync::Arc};
use super::{
Interaction, Length, Name, PartStyle, Recipe,
table::{
CellEdit, CellRange, ColumnLayout, ColumnMenuLabels, FilterLabels, GridPage, GridQuery, GridRequest, GroupKey,
SourceLabels, SourceRow, SummaryLabels, Table, TableColumn, TableCount, TableFilters, TableSort,
},
};
use crate::{
app::{ctx::Ctx, theme::Density},
core::{Signal, SignalValue},
node::Element,
};
pub struct DataGrid<R, K = usize> {
table: Table<R, K>,
}
impl<X: Send + Sync + 'static> DataGrid<SourceRow<X>, usize> {
pub fn on_demand<Fut>(
columns: impl IntoIterator<Item = TableColumn<X>>,
total: usize,
fetch: impl Fn(GridRequest) -> Fut + Send + Sync + 'static,
) -> Self
where
Fut: std::future::Future<Output = Result<GridPage<X>, String>> + Send + 'static,
{
DataGrid {
table: Table::over_source(columns, total, fetch).as_grid(),
}
}
}
impl<R: Send + Sync + 'static> DataGrid<R, usize> {
pub fn new(columns: impl IntoIterator<Item = TableColumn<R>>, rows: impl IntoIterator<Item = R>) -> Self {
Self {
table: Table::new(columns, rows).as_grid(),
}
}
pub fn new_shared(columns: impl IntoIterator<Item = TableColumn<R>>, rows: Arc<Vec<R>>) -> Self {
Self {
table: Table::new_shared(columns, rows).as_grid(),
}
}
pub fn row_key<K: Clone + Eq + Hash + Send + Sync + 'static>(
self,
key: impl Fn(&R) -> K + Send + Sync + 'static,
) -> DataGrid<R, K> {
DataGrid {
table: self.table.row_key(key),
}
}
}
impl<R: Send + Sync + 'static, K: Clone + Eq + Hash + Send + Sync + 'static> DataGrid<R, K> {
fn with(self, change: impl FnOnce(Table<R, K>) -> Table<R, K>) -> Self {
Self {
table: change(self.table),
}
}
pub fn on_edit(self, handler: impl Fn(&CellEdit<K>) -> bool + Send + Sync + 'static) -> Self {
self.with(|table| table.add_on_edit(handler))
}
pub fn cell_selection(self, range: Signal<Option<CellRange<K>>>) -> Self
where
Option<CellRange<K>>: SignalValue,
{
let now = range.get();
self.with(|table| table.cell_selection(now, Arc::new(move |next| range.set(next))))
}
pub fn cell_selection_controlled(
self,
range: Option<CellRange<K>>,
on_change: impl Fn(Option<CellRange<K>>) + Send + Sync + 'static,
) -> Self {
self.with(|table| table.cell_selection(range, Arc::new(on_change)))
}
pub fn on_cell_selection_change(self, handler: impl Fn(Option<&CellRange<K>>) + Send + Sync + 'static) -> Self {
self.with(|table| table.add_on_cell_selection_change(handler))
}
pub fn column_layout(self, layout: Signal<ColumnLayout>) -> Self
where
ColumnLayout: SignalValue,
{
let now = layout.get();
self.with(|table| table.column_layout(Some(now), Arc::new(move |next| layout.set(next))))
}
pub fn column_layout_controlled(
self,
layout: ColumnLayout,
on_change: impl Fn(ColumnLayout) + Send + Sync + 'static,
) -> Self {
self.with(|table| table.column_layout(Some(layout), Arc::new(on_change)))
}
pub fn on_column_layout_change(self, handler: impl Fn(&ColumnLayout) + Send + Sync + 'static) -> Self {
self.with(|table| table.add_on_column_layout_change(handler))
}
pub fn column_menu(self, menu: bool) -> Self {
self.with(|table| table.set_column_menu(menu))
}
pub fn tree(self, parent: impl Fn(&R) -> Option<K> + Send + Sync + 'static) -> Self {
self.with(|table| table.set_tree(parent))
}
pub fn expanded(self, open: Signal<Vec<K>>) -> Self
where
Vec<K>: SignalValue,
{
let now = open.get();
self.with(|table| table.set_expanded(now, Arc::new(move |next| open.set(next))))
}
pub fn expanded_controlled(
self,
open: impl IntoIterator<Item = K>,
on_change: impl Fn(Vec<K>) + Send + Sync + 'static,
) -> Self {
let now = open.into_iter().collect();
self.with(|table| table.set_expanded(now, Arc::new(on_change)))
}
pub fn on_expanded_change(self, handler: impl Fn(&K, bool) + Send + Sync + 'static) -> Self {
self.with(|table| table.add_on_expanded_change(handler))
}
pub fn detail(self, detail: impl Fn(&R) -> Element + Send + Sync + 'static) -> Self {
self.with(|table| table.set_detail(detail))
}
pub fn details(self, open: Signal<Vec<K>>) -> Self
where
Vec<K>: SignalValue,
{
let now = open.get();
self.with(|table| table.set_details_open(now, Arc::new(move |next| open.set(next))))
}
pub fn details_controlled(
self,
open: impl IntoIterator<Item = K>,
on_change: impl Fn(Vec<K>) + Send + Sync + 'static,
) -> Self {
let now = open.into_iter().collect();
self.with(|table| table.set_details_open(now, Arc::new(on_change)))
}
pub fn on_detail_change(self, handler: impl Fn(&K, bool) + Send + Sync + 'static) -> Self {
self.with(|table| table.add_on_detail_change(handler))
}
pub fn group_by<S: AsRef<str>>(self, columns: impl IntoIterator<Item = S>) -> Self {
let columns = columns.into_iter().map(|id| Arc::from(id.as_ref())).collect();
self.with(|table| table.set_group_by(columns))
}
pub fn open(self, open: Signal<Vec<GroupKey>>) -> Self
where
Vec<GroupKey>: SignalValue,
{
let now = open.get();
self.with(|table| table.set_open_groups(now, Arc::new(move |next| open.set(next))))
}
pub fn open_controlled(
self,
open: impl IntoIterator<Item = GroupKey>,
on_change: impl Fn(Vec<GroupKey>) + Send + Sync + 'static,
) -> Self {
let now = open.into_iter().collect();
self.with(|table| table.set_open_groups(now, Arc::new(on_change)))
}
pub fn on_open_change(self, handler: impl Fn(&GroupKey, bool) + Send + Sync + 'static) -> Self {
self.with(|table| table.add_on_open_change(handler))
}
pub fn column_menu_labels(self, labels: ColumnMenuLabels) -> Self {
self.with(|table| table.set_column_menu_labels(labels))
}
pub fn menu_trigger_label(self, label: impl Fn(&str) -> String + Send + Sync + 'static) -> Self {
self.with(|table| table.set_menu_trigger_label(label))
}
pub fn group_label(self, label: impl Fn(&str, &str, usize) -> String + Send + Sync + 'static) -> Self {
self.with(|table| table.set_group_label(label))
}
pub fn summary_labels(self, labels: SummaryLabels) -> Self {
self.with(|table| table.set_summary_labels(labels))
}
pub fn row_numbers_label(self, label: &str) -> Self {
self.with(|table| table.set_row_numbers_label(label))
}
pub fn row_numbers(self, numbers: bool) -> Self {
self.with(|table| table.set_row_numbers(numbers))
}
pub fn read_only(self, read_only: bool) -> Self {
self.with(|table| table.set_read_only(read_only))
}
pub fn sort(self, sort: Signal<Vec<TableSort>>) -> Self
where
Vec<TableSort>: SignalValue,
{
self.with(|table| table.sort(sort))
}
pub fn sort_controlled(
self,
sort: Vec<TableSort>,
on_change: impl Fn(Vec<TableSort>) + Send + Sync + 'static,
) -> Self {
self.with(|table| table.sort_controlled(sort, on_change))
}
pub fn on_sort_change(self, handler: impl Fn(&[TableSort]) + Send + Sync + 'static) -> Self {
self.with(|table| table.on_sort_change(handler))
}
pub fn selected(self, selected: Signal<Vec<K>>) -> Self
where
Vec<K>: SignalValue,
{
self.with(|table| table.selected(selected))
}
pub fn selected_controlled(
self,
selected: impl IntoIterator<Item = K>,
on_change: impl Fn(Vec<K>) + Send + Sync + 'static,
) -> Self {
self.with(|table| table.selected_controlled(selected, on_change))
}
pub fn on_selection_change(self, handler: impl Fn(&[K]) + Send + Sync + 'static) -> Self {
self.with(|table| table.on_selection_change(handler))
}
pub fn checkboxes(self, checkboxes: bool) -> Self {
self.with(|table| table.checkboxes(checkboxes))
}
pub fn on_row_click(self, handler: impl Fn(&K) + Send + Sync + 'static) -> Self {
self.with(|table| table.on_row_click(handler))
}
pub fn on_row_activate(self, handler: impl Fn(&K) + Send + Sync + 'static) -> Self {
self.with(|table| table.on_row_activate(handler))
}
pub fn resizable(self, resizable: bool) -> Self {
self.with(|table| table.resizable(resizable))
}
pub fn on_column_resize(self, handler: impl Fn(&str, f32) + Send + Sync + 'static) -> Self {
self.with(|table| table.on_column_resize(handler))
}
pub fn on_column_resize_end(self, handler: impl Fn(&str, f32) + Send + Sync + 'static) -> Self {
self.with(|table| table.on_column_resize_end(handler))
}
pub fn resize_label(self, label: impl Fn(&str) -> String + Send + Sync + 'static) -> Self {
self.with(|table| table.resize_label(label))
}
pub fn density(self, density: impl Into<Density>) -> Self {
self.with(|table| table.density(density))
}
pub fn height(self, height: impl Into<Length>) -> Self {
self.with(|table| table.height(height))
}
pub fn max_height(self, height: impl Into<Length>) -> Self {
self.with(|table| table.max_height(height))
}
pub fn empty_text(self, text: &str) -> Self {
self.with(|table| table.empty_text(text))
}
pub fn empty(self, empty: impl Into<Element>) -> Self {
self.with(|table| table.empty(empty))
}
pub fn loading(self, loading: bool) -> Self {
self.with(|table| table.loading(loading))
}
pub fn loading_rows(self, rows: usize) -> Self {
self.with(|table| table.loading_rows(rows))
}
pub fn pagination(self, pagination: impl Into<Element>) -> Self {
self.with(|table| table.pagination(pagination))
}
pub fn count(self, count: bool) -> Self {
self.with(|table| table.count(count))
}
pub fn total(self, total: usize) -> Self {
self.with(|table| table.total(total))
}
pub fn count_text(self, text: impl Fn(TableCount) -> String + Send + Sync + 'static) -> Self {
self.with(|table| table.count_text(text))
}
pub fn filters(self, filters: Signal<TableFilters>) -> Self
where
TableFilters: SignalValue,
{
self.with(|table| table.filters(filters))
}
pub fn filters_controlled(
self,
filters: TableFilters,
on_change: impl Fn(TableFilters) + Send + Sync + 'static,
) -> Self {
self.with(|table| table.filters_controlled(filters, on_change))
}
pub fn on_filters_change(self, handler: impl Fn(&TableFilters) + Send + Sync + 'static) -> Self {
self.with(|table| table.on_filters_change(handler))
}
pub fn search(self, query: Signal<String>) -> Self
where
String: SignalValue,
{
self.with(|table| table.search(query))
}
pub fn search_controlled(self, query: &str, on_change: impl Fn(String) + Send + Sync + 'static) -> Self {
self.with(|table| table.search_controlled(query, on_change))
}
pub fn on_search_change(self, handler: impl Fn(&str) + Send + Sync + 'static) -> Self {
self.with(|table| table.on_search_change(handler))
}
pub fn search_options(self, options: super::FindOptions) -> Self {
self.with(|table| table.search_options(options))
}
pub fn on_matches_change(self, handler: impl Fn(super::SearchMatches) + Send + Sync + 'static) -> Self {
self.with(|table| table.on_matches_change(handler))
}
pub fn export(self, export: bool) -> Self {
self.with(|table| table.export(export))
}
pub fn on_export(self, handler: impl Fn(&super::GridExport) + Send + Sync + 'static) -> Self {
self.with(|table| table.on_export(handler))
}
pub fn export_labels(self, labels: super::ExportLabels) -> Self {
self.with(|table| table.export_labels(labels))
}
pub fn toolbar(self, toolbar: bool) -> Self {
self.with(|table| table.set_toolbar(toolbar))
}
pub fn toolbar_labels(self, labels: super::GridBarLabels) -> Self {
self.with(|table| table.set_toolbar_labels(labels))
}
pub fn find_bar(self, find_bar: bool) -> Self {
self.with(|table| table.find_bar(find_bar))
}
pub fn find_bar_with(self, with: impl Fn(super::FindBar) -> super::FindBar + Send + Sync + 'static) -> Self {
self.with(|table| table.find_bar_with(with))
}
pub fn on_query_change(self, handler: impl Fn(&GridQuery) + Send + Sync + 'static) -> Self {
self.with(|table| table.on_query_change(handler))
}
pub fn source_labels(self, labels: SourceLabels) -> Self {
self.with(|table| table.source_labels(labels))
}
pub fn filter_row(self, filter_row: bool) -> Self {
self.with(|table| table.filter_row(filter_row))
}
pub fn filter_labels(self, labels: FilterLabels) -> Self {
self.with(|table| table.filter_labels(labels))
}
pub fn select_all_label(self, label: &str) -> Self {
self.with(|table| table.select_all_label(label))
}
pub fn select_row_label(self, label: &str) -> Self {
self.with(|table| table.select_row_label(label))
}
pub fn label(self, label: &str) -> Self {
self.with(|table| table.label(label))
}
pub fn disabled(self, disabled: bool) -> Self {
self.with(|table| table.disabled(disabled))
}
pub fn state(self, state: impl Into<Name>, on: bool) -> Self {
self.with(|table| table.state(state, on))
}
pub fn style(self, part: impl Into<Name>, style: impl FnOnce(PartStyle) -> PartStyle) -> Self {
self.with(|table| table.style(part, style))
}
pub fn rules(self, rules: &Recipe) -> Self {
self.with(|table| table.rules(rules))
}
pub fn force(self, interaction: Option<Interaction>) -> Self {
self.with(|table| table.force(interaction))
}
pub fn id(self, id: impl Into<Arc<str>>) -> Self {
self.with(|table| table.id(id))
}
pub fn key(self, key: impl Into<Arc<str>>) -> Self {
self.with(|table| table.key(key))
}
pub fn build(self, theme: &crate::app::theme::Theme) -> Element {
self.table.build(theme)
}
pub fn mount(self, ctx: &mut Ctx) -> Element {
self.table.mount(ctx)
}
}
impl<R: Send + Sync + 'static, K: Clone + Eq + Hash + Send + Sync + 'static> crate::ui::Styled for DataGrid<R, K> {
type Component = super::Table<R, K>;
fn rules(self, rules: &crate::ui::Recipe) -> Self {
Self::rules(self, rules)
}
fn state(self, state: impl Into<crate::ui::Name>, on: bool) -> Self {
Self::state(self, state, on)
}
}
impl<R: Send + Sync + 'static, K: Clone + Eq + Hash + Send + Sync + 'static> From<DataGrid<R, K>> for Element {
fn from(grid: DataGrid<R, K>) -> Self {
grid.table.into()
}
}