use super::model::{Data, Node};
use super::props::modify;
use super::{wake, with};
use crate::backend::{Backend, Native as B, Prop};
use crate::types::*;
pub fn data_update(id: WidgetId, what: Data, f: impl FnOnce(&mut Node)) {
if modify(id, f).is_some() {
push(id, what);
}
}
fn merge(pending: Option<Data>, what: Data) -> Data {
match (pending, what) {
(None, _) => what,
(Some(p), _) if p == what => p,
(Some(_), Data::TreeRows) => Data::TreeRows,
(Some(Data::TableCells), Data::TableRows) | (Some(Data::TableRows), Data::TableCells) => {
Data::TableRows
}
_ => Data::TableAll,
}
}
const MAX_PENDING_CELLS: usize = 256;
pub fn push(id: WidgetId, what: Data) {
let need_wake = with(|r| {
let b = r.nodes.get_mut(&id)?.batch_mut()?;
let mut merged = merge(b.pending, what);
if merged == Data::TableCells && b.cells.len() > MAX_PENDING_CELLS {
merged = Data::TableRows;
}
if merged != Data::TableCells {
b.cells.clear();
}
b.pending = Some(merged);
r.dirty_models.insert(id);
Some(r.schedule())
})
.flatten();
if need_wake == Some(true) {
wake();
}
}
pub fn flush_models() {
let ready: Vec<(WidgetId, Data)> = with(|r| {
let dirty = std::mem::take(&mut r.dirty_models);
dirty
.into_iter()
.filter_map(|id| {
let b = r.nodes.get_mut(&id)?.batch_mut()?;
if b.freeze > 0 {
return None;
}
Some((id, b.pending.take()?))
})
.collect()
})
.unwrap_or_default();
for (id, what) in ready {
send(id, what);
}
}
pub fn freeze(id: WidgetId, on: bool) {
let pending = with(|r| {
let b = r.nodes.get_mut(&id)?.batch_mut()?;
if on {
b.freeze += 1;
None
} else {
b.freeze = b.freeze.saturating_sub(1);
if b.freeze == 0 {
b.pending.take()
} else {
None
}
}
})
.flatten();
if let Some(p) = pending {
send(id, p);
}
}
fn send(id: WidgetId, what: Data) {
enum Snap {
Table {
cols: Option<Vec<Column>>,
rows: Option<Vec<Vec<String>>>,
cells: Vec<(usize, usize, String)>,
sel: Option<usize>,
sort: Option<(usize, bool)>,
send_sel: bool,
send_sort: bool,
},
Tree {
rows: Option<Vec<TreeRow>>,
sel: Option<u64>,
},
}
let Some(snap) = with(|r| {
let n = r.nodes.get_mut(&id)?;
if let Some(t) = n.table_mut() {
let all = what == Data::TableAll;
let listed = std::mem::take(&mut t.batch.cells);
let cells = if what == Data::TableCells {
listed
.into_iter()
.filter_map(|(r, c)| Some((r, c, t.rows.get(r)?.get(c)?.clone())))
.collect()
} else {
vec![]
};
return Some(Snap::Table {
cols: all.then(|| t.columns.clone()),
rows: (all || what == Data::TableRows).then(|| t.rows.clone()),
cells,
sel: t.selected,
sort: t.sort,
send_sel: all || matches!(what, Data::TableRows | Data::TableSelected),
send_sort: all || what == Data::TableSort,
});
}
let n = r.nodes.get(&id)?;
let t = n.tree()?;
Some(Snap::Tree {
rows: (what == Data::TreeRows).then(|| t.flatten()),
sel: t.selected,
})
})
.flatten() else {
return;
};
match snap {
Snap::Table {
cols,
rows,
cells,
sel,
sort,
send_sel,
send_sort,
} => {
if let Some(c) = cols {
B::set(id, &Prop::Columns(&c));
}
if let Some(r) = rows {
B::set(id, &Prop::Rows(&r));
}
for (row, col, text) in &cells {
B::set(
id,
&Prop::Cell {
row: *row,
col: *col,
text,
},
);
}
if send_sel {
B::set(id, &Prop::Selected(sel));
}
if send_sort {
B::set(id, &Prop::SortIndicator(sort));
}
}
Snap::Tree { rows, sel } => {
if let Some(r) = rows {
B::set(id, &Prop::TreeRows(&r));
}
B::set(id, &Prop::TreeSelected(sel));
}
}
}