pub fn project_selection<T: Clone + 'static>(
model: &impl Model<Data = T>,
wanted: impl Fn(&T) -> bool,
flag: impl Fn(&mut T) -> &mut bool,
)Expand description
Project a selection into a model’s per-row selected flag.
Rendering reads selected as model data, so every write to the host’s
selection must be followed by a projection into the rows. wanted decides
whether a row should be selected; flag hands back the row’s own flag,
which is both read and written — one accessor, so the read and the write
can never disagree about which field holds the answer.
Only rows whose flag actually changes are written back, so untouched rows don’t re-render.
use slint::VecModel;
use slint_node_editor::selection::project_selection;
#[derive(Clone)]
struct Row { id: i32, selected: bool }
let nodes = VecModel::from(vec![
Row { id: 1, selected: false },
Row { id: 2, selected: false },
]);
let selection = [2];
project_selection(
&nodes,
|node| selection.contains(&node.id),
|node| &mut node.selected,
);