use super::*;
pub(super) struct Thaw(pub(super) WidgetId);
impl Drop for Thaw {
fn drop(&mut self) {
core::freeze(self.0, false)
}
}
fn cells<S: AsRef<str>>(c: &[S]) -> Vec<String> {
c.iter().map(|s| s.as_ref().to_string()).collect()
}
impl Table {
pub fn new(parent: impl Into<WidgetId>) -> Table {
make(Table::from_id, Kind::Table, parent, |_| {})
}
pub fn set_columns(&self, cols: &[Column]) {
let v = cols[..cols.len().min(MAX_COLUMNS)].to_vec();
core::data_update(self.id(), core::Data::TableAll, |n| {
if let Some(t) = n.table_mut() {
t.columns = v;
if t.sort.is_some_and(|(c, _)| c >= t.columns.len()) {
t.sort = None;
}
}
});
}
pub fn add_column(&self, col: Column) {
core::data_update(self.id(), core::Data::TableAll, |n| {
if let Some(t) = n.table_mut().filter(|t| t.columns.len() < MAX_COLUMNS) {
t.columns.push(col);
}
});
}
pub fn columns(&self) -> Vec<Column> {
core::read(self.id(), |n| n.table().map(|t| t.columns.clone()))
.flatten()
.unwrap_or_default()
}
pub fn set_rows<S: AsRef<str>>(&self, rows: &[Vec<S>]) {
let v: Vec<Vec<String>> = rows.iter().map(|r| cells(r)).collect();
core::data_update(self.id(), core::Data::TableRows, |n| {
if let Some(t) = n.table_mut() {
t.rows = v;
if t.selected.is_some_and(|i| i >= t.rows.len()) {
t.selected = None;
}
}
});
}
pub fn push_row<S: AsRef<str>>(&self, row: &[S]) {
let v = cells(row);
core::data_update(self.id(), core::Data::TableRows, |n| {
if let Some(t) = n.table_mut() {
t.rows.push(v);
}
});
}
pub fn insert_row<S: AsRef<str>>(&self, index: usize, row: &[S]) {
let v = cells(row);
core::data_update(self.id(), core::Data::TableRows, |n| {
if let Some(t) = n.table_mut() {
let i = index.min(t.rows.len());
t.rows.insert(i, v);
if let Some(s) = t.selected.as_mut() {
if *s >= i {
*s += 1;
}
}
}
});
}
pub fn remove_row(&self, index: usize) {
core::data_update(self.id(), core::Data::TableRows, |n| {
if let Some(t) = n.table_mut() {
if index < t.rows.len() {
t.rows.remove(index);
t.selected = match t.selected {
Some(s) if s == index => None,
Some(s) if s > index => Some(s - 1),
o => o,
};
}
}
});
}
pub fn clear(&self) {
core::data_update(self.id(), core::Data::TableRows, |n| {
if let Some(t) = n.table_mut() {
t.rows.clear();
t.selected = None;
}
});
}
pub fn set_cell(&self, row: usize, col: usize, text: &str) {
core::data_update(self.id(), core::Data::TableCells, |n| {
let Some(t) = n.table_mut() else { return };
let Some(r) = t.rows.get_mut(row) else { return };
if col >= MAX_COLUMNS {
return; }
if r.len() <= col {
r.resize(col + 1, String::new());
}
r[col] = text.to_string();
t.batch.cells.push((row, col));
});
}
pub fn cell(&self, row: usize, col: usize) -> String {
core::read(self.id(), |n| n.table()?.rows.get(row)?.get(col).cloned())
.flatten()
.unwrap_or_default()
}
pub fn row(&self, row: usize) -> Vec<String> {
core::read(self.id(), |n| n.table()?.rows.get(row).cloned())
.flatten()
.unwrap_or_default()
}
pub fn rows(&self) -> Vec<Vec<String>> {
core::read(self.id(), |n| n.table().map(|t| t.rows.clone()))
.flatten()
.unwrap_or_default()
}
pub fn row_count(&self) -> usize {
core::read(self.id(), |n| n.table().map_or(0, |t| t.rows.len())).unwrap_or(0)
}
pub fn batch(&self, f: impl FnOnce(&Table)) {
core::freeze(self.id(), true);
let _g = Thaw(self.id());
f(self)
}
pub fn set_selected(&self, row: Option<usize>) {
core::data_update(self.id(), core::Data::TableSelected, |n| {
if let Some(t) = n.table_mut() {
t.selected = row.filter(|i| *i < t.rows.len());
}
});
}
pub fn selected(&self) -> Option<usize> {
selected(self.id())
}
pub fn selected_row(&self) -> Option<Vec<String>> {
core::read(self.id(), |n| {
let t = n.table()?;
t.rows.get(t.selected?).cloned()
})
.flatten()
}
pub fn set_sort_indicator(&self, s: Option<(usize, bool)>) {
core::data_update(self.id(), core::Data::TableSort, |n| {
if let Some(t) = n.table_mut() {
t.sort = s.filter(|(c, _)| *c < t.columns.len());
}
});
}
pub fn sort_indicator(&self) -> Option<(usize, bool)> {
core::read(self.id(), |n| n.table().and_then(|t| t.sort)).flatten()
}
pub fn on_select(&self, f: impl FnMut(Option<usize>) + 'static) {
on_select(self.id(), f)
}
pub fn on_activate(&self, mut f: impl FnMut(usize) + 'static) {
on(self.id(), Ev::Activated, move |e| {
if let Event::Activated(i) = e {
f(*i)
}
})
}
pub fn on_column_click(&self, mut f: impl FnMut(usize) + 'static) {
on(self.id(), Ev::ColumnClicked, move |e| {
if let Event::ColumnClicked(c) = e {
f(*c)
}
})
}
}