rungui 0.4.0

Small portable native GUI library: a thin layer over GTK3, Win32 and Cocoa with hand-declared FFI
Documentation
//! Table/tree model pushes to the backend (with batching via `freeze`).

use super::model::{Data, Node};
use super::props::modify;
use super::{wake, with};
use crate::backend::{Backend, Native as B, Prop};
use crate::types::*;

// ---------------------------------------------------------------- table/tree model pushes

/// Mutate a table/tree model, then re-send `what` to the backend (deferred while frozen).
pub fn data_update(id: WidgetId, what: Data, f: impl FnOnce(&mut Node)) {
    if modify(id, f).is_some() {
        push(id, what);
    }
}

/// Combine two kinds of pending update into one that covers both.
fn merge(pending: Option<Data>, what: Data) -> Data {
    match (pending, what) {
        (None, _) => what,
        (Some(p), _) if p == what => p,
        (Some(_), Data::TreeRows) => Data::TreeRows,
        // all rows include the cells
        (Some(Data::TableCells), Data::TableRows) | (Some(Data::TableRows), Data::TableCells) => {
            Data::TableRows
        }
        _ => Data::TableAll,
    }
}

/// More changed cells than this are sent as the whole table instead.
const MAX_PENDING_CELLS: usize = 256;

/// Note that part of the model of table/tree `id` changed. The backend is updated at the next
/// loop turn (see [`flush_models`]), so any number of mutations costs one transfer of the model.
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();
    }
}

/// Send every changed table/tree model to the backend, except those inside a `batch` (they are
/// sent when the batch ends).
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);
    }
}

/// Start/stop a batch (nested calls count). Stopping the last level sends what was deferred.
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>,
            /// The whole selection when the table is in multi-select mode.
            multi: Option<Vec<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 {
                // cells that vanished with a later shrinking of the table are skipped
                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.sel.first(),
                multi: t.sel.multi.then(|| t.sel.items.clone()),
                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,
            multi,
            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 {
                match &multi {
                    Some(m) => B::set(id, &Prop::Selection(m)),
                    None => 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));
        }
    }
}