use crate::item_tree::ItemTreeVTable;
use crate::item_tree::TraversalOrder;
pub use crate::items::{StandardListViewItem, TableColumn};
use crate::layout::Orientation;
use crate::lengths::{LogicalLength, RectLengths};
use crate::{Coord, Property, SharedString, SharedVector};
pub use adapters::{FilterModel, MapModel, ReverseModel, SortModel};
use alloc::boxed::Box;
use alloc::rc::Rc;
use alloc::vec::Vec;
use core::cell::{Cell, RefCell};
use core::pin::Pin;
use euclid::num::Zero;
#[allow(unused)]
use euclid::num::{Ceil, Floor};
pub use model_peer::*;
use once_cell::unsync::OnceCell;
use pin_project::pin_project;
mod adapters;
mod model_peer;
type ItemTreeRc<C> = vtable::VRc<crate::item_tree::ItemTreeVTable, C>;
pub trait ModelTracker {
fn attach_peer(&self, peer: ModelPeer);
fn track_row_count_changes(&self);
fn track_row_data_changes(&self, row: usize);
}
impl ModelTracker for () {
fn attach_peer(&self, _peer: ModelPeer) {}
fn track_row_count_changes(&self) {}
fn track_row_data_changes(&self, _row: usize) {}
}
pub trait Model {
type Data;
fn row_count(&self) -> usize;
fn row_data(&self, row: usize) -> Option<Self::Data>;
fn set_row_data(&self, _row: usize, _data: Self::Data) {
#[cfg(feature = "std")]
eprintln!(
"Model::set_row_data called on a model of type {} which does not re-implement this method. \
This happens when trying to modify a read-only model",
core::any::type_name::<Self>(),
);
}
fn model_tracker(&self) -> &dyn ModelTracker;
fn iter(&self) -> ModelIterator<Self::Data>
where
Self: Sized,
{
ModelIterator::new(self)
}
fn as_any(&self) -> &dyn core::any::Any {
&()
}
}
pub trait ModelExt: Model {
fn row_data_tracked(&self, row: usize) -> Option<Self::Data> {
self.model_tracker().track_row_data_changes(row);
self.row_data(row)
}
fn map<F, U>(self, map_function: F) -> MapModel<Self, F>
where
Self: Sized + 'static,
F: Fn(Self::Data) -> U + 'static,
{
MapModel::new(self, map_function)
}
fn filter<F>(self, filter_function: F) -> FilterModel<Self, F>
where
Self: Sized + 'static,
F: Fn(&Self::Data) -> bool + 'static,
{
FilterModel::new(self, filter_function)
}
#[must_use]
fn sort(self) -> SortModel<Self, adapters::AscendingSortHelper>
where
Self: Sized + 'static,
Self::Data: core::cmp::Ord,
{
SortModel::new_ascending(self)
}
fn sort_by<F>(self, sort_function: F) -> SortModel<Self, F>
where
Self: Sized + 'static,
F: FnMut(&Self::Data, &Self::Data) -> core::cmp::Ordering + 'static,
{
SortModel::new(self, sort_function)
}
fn reverse(self) -> ReverseModel<Self>
where
Self: Sized + 'static,
{
ReverseModel::new(self)
}
}
impl<T: Model> ModelExt for T {}
pub struct ModelIterator<'a, T> {
model: &'a dyn Model<Data = T>,
row: usize,
}
impl<'a, T> ModelIterator<'a, T> {
pub fn new(model: &'a dyn Model<Data = T>) -> Self {
Self { model, row: 0 }
}
}
impl<'a, T> Iterator for ModelIterator<'a, T> {
type Item = T;
fn next(&mut self) -> Option<Self::Item> {
if self.row >= self.model.row_count() {
return None;
}
let row = self.row;
self.row += 1;
self.model.row_data(row)
}
fn size_hint(&self) -> (usize, Option<usize>) {
let len = self.model.row_count();
(len, Some(len))
}
fn nth(&mut self, n: usize) -> Option<Self::Item> {
self.row = self.row.checked_add(n)?;
self.next()
}
}
impl<'a, T> ExactSizeIterator for ModelIterator<'a, T> {}
impl<M: Model> Model for Rc<M> {
type Data = M::Data;
fn row_count(&self) -> usize {
(**self).row_count()
}
fn row_data(&self, row: usize) -> Option<Self::Data> {
(**self).row_data(row)
}
fn model_tracker(&self) -> &dyn ModelTracker {
(**self).model_tracker()
}
fn as_any(&self) -> &dyn core::any::Any {
(**self).as_any()
}
fn set_row_data(&self, row: usize, data: Self::Data) {
(**self).set_row_data(row, data)
}
}
#[derive(Default)]
pub struct VecModel<T> {
array: RefCell<Vec<T>>,
notify: ModelNotify,
}
impl<T: 'static> VecModel<T> {
pub fn from_slice(slice: &[T]) -> ModelRc<T>
where
T: Clone,
{
ModelRc::new(Self::from(slice.to_vec()))
}
pub fn push(&self, value: T) {
self.array.borrow_mut().push(value);
self.notify.row_added(self.array.borrow().len() - 1, 1)
}
pub fn insert(&self, index: usize, value: T) {
self.array.borrow_mut().insert(index, value);
self.notify.row_added(index, 1)
}
pub fn remove(&self, index: usize) -> T {
let r = self.array.borrow_mut().remove(index);
self.notify.row_removed(index, 1);
r
}
pub fn set_vec(&self, new: impl Into<Vec<T>>) {
*self.array.borrow_mut() = new.into();
self.notify.reset();
}
pub fn extend<I: IntoIterator<Item = T>>(&self, iter: I) {
let mut array = self.array.borrow_mut();
let old_idx = array.len();
array.extend(iter);
let count = array.len() - old_idx;
drop(array);
self.notify.row_added(old_idx, count);
}
pub fn clear(&self) {
self.array.borrow_mut().clear();
self.notify.reset();
}
pub fn swap(&self, a: usize, b: usize) {
if a == b {
return;
}
self.array.borrow_mut().swap(a, b);
self.notify.row_changed(a);
self.notify.row_changed(b);
}
}
impl<T: Clone + 'static> VecModel<T> {
pub fn extend_from_slice(&self, src: &[T]) {
let mut array = self.array.borrow_mut();
let old_idx = array.len();
array.extend_from_slice(src);
drop(array);
self.notify.row_added(old_idx, src.len());
}
}
impl<T> From<Vec<T>> for VecModel<T> {
fn from(array: Vec<T>) -> Self {
VecModel { array: RefCell::new(array), notify: Default::default() }
}
}
impl<T> FromIterator<T> for VecModel<T> {
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
VecModel::from(Vec::from_iter(iter))
}
}
impl<T: Clone + 'static> Model for VecModel<T> {
type Data = T;
fn row_count(&self) -> usize {
self.array.borrow().len()
}
fn row_data(&self, row: usize) -> Option<Self::Data> {
self.array.borrow().get(row).cloned()
}
fn set_row_data(&self, row: usize, data: Self::Data) {
if row < self.row_count() {
self.array.borrow_mut()[row] = data;
self.notify.row_changed(row);
}
}
fn model_tracker(&self) -> &dyn ModelTracker {
&self.notify
}
fn as_any(&self) -> &dyn core::any::Any {
self
}
}
#[derive(Default)]
pub struct SharedVectorModel<T> {
array: RefCell<SharedVector<T>>,
notify: ModelNotify,
}
impl<T: Clone + 'static> SharedVectorModel<T> {
pub fn push(&self, value: T) {
self.array.borrow_mut().push(value);
self.notify.row_added(self.array.borrow().len() - 1, 1)
}
}
impl<T> SharedVectorModel<T> {
pub fn shared_vector(&self) -> SharedVector<T> {
self.array.borrow_mut().clone()
}
}
impl<T> From<SharedVector<T>> for SharedVectorModel<T> {
fn from(array: SharedVector<T>) -> Self {
SharedVectorModel { array: RefCell::new(array), notify: Default::default() }
}
}
impl<T: Clone + 'static> Model for SharedVectorModel<T> {
type Data = T;
fn row_count(&self) -> usize {
self.array.borrow().len()
}
fn row_data(&self, row: usize) -> Option<Self::Data> {
self.array.borrow().get(row).cloned()
}
fn set_row_data(&self, row: usize, data: Self::Data) {
self.array.borrow_mut().make_mut_slice()[row] = data;
self.notify.row_changed(row);
}
fn model_tracker(&self) -> &dyn ModelTracker {
&self.notify
}
fn as_any(&self) -> &dyn core::any::Any {
self
}
}
impl Model for usize {
type Data = i32;
fn row_count(&self) -> usize {
*self
}
fn row_data(&self, row: usize) -> Option<Self::Data> {
(row < self.row_count()).then_some(row as i32)
}
fn as_any(&self) -> &dyn core::any::Any {
self
}
fn model_tracker(&self) -> &dyn ModelTracker {
&()
}
}
impl Model for bool {
type Data = ();
fn row_count(&self) -> usize {
if *self {
1
} else {
0
}
}
fn row_data(&self, row: usize) -> Option<Self::Data> {
(row < self.row_count()).then_some(())
}
fn as_any(&self) -> &dyn core::any::Any {
self
}
fn model_tracker(&self) -> &dyn ModelTracker {
&()
}
}
pub struct ModelRc<T>(Option<Rc<dyn Model<Data = T>>>);
impl<T> core::fmt::Debug for ModelRc<T> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "ModelRc(dyn Model)")
}
}
impl<T> Clone for ModelRc<T> {
fn clone(&self) -> Self {
Self(self.0.clone())
}
}
impl<T> Default for ModelRc<T> {
fn default() -> Self {
Self(None)
}
}
impl<T> core::cmp::PartialEq for ModelRc<T> {
fn eq(&self, other: &Self) -> bool {
match (&self.0, &other.0) {
(None, None) => true,
(Some(a), Some(b)) => core::ptr::eq(
(&**a) as *const dyn Model<Data = T> as *const u8,
(&**b) as *const dyn Model<Data = T> as *const u8,
),
_ => false,
}
}
}
impl<T> ModelRc<T> {
pub fn new(model: impl Model<Data = T> + 'static) -> Self {
Self(Some(Rc::new(model)))
}
}
impl<T, M: Model<Data = T> + 'static> From<Rc<M>> for ModelRc<T> {
fn from(model: Rc<M>) -> Self {
Self(Some(model))
}
}
impl<T> From<Rc<dyn Model<Data = T> + 'static>> for ModelRc<T> {
fn from(model: Rc<dyn Model<Data = T> + 'static>) -> Self {
Self(Some(model))
}
}
impl<T: Clone + 'static> From<&[T]> for ModelRc<T> {
fn from(slice: &[T]) -> Self {
VecModel::from_slice(slice)
}
}
impl<T: Clone + 'static, const N: usize> From<[T; N]> for ModelRc<T> {
fn from(array: [T; N]) -> Self {
VecModel::from_slice(&array)
}
}
impl<T> TryInto<Rc<dyn Model<Data = T>>> for ModelRc<T> {
type Error = ();
fn try_into(self) -> Result<Rc<dyn Model<Data = T>>, Self::Error> {
self.0.ok_or(())
}
}
impl<T> Model for ModelRc<T> {
type Data = T;
fn row_count(&self) -> usize {
self.0.as_ref().map_or(0, |model| model.row_count())
}
fn row_data(&self, row: usize) -> Option<Self::Data> {
self.0.as_ref().and_then(|model| model.row_data(row))
}
fn set_row_data(&self, row: usize, data: Self::Data) {
if let Some(model) = self.0.as_ref() {
model.set_row_data(row, data);
}
}
fn model_tracker(&self) -> &dyn ModelTracker {
self.0.as_ref().map_or(&(), |model| model.model_tracker())
}
fn as_any(&self) -> &dyn core::any::Any {
self.0.as_ref().map_or(&(), |model| model.as_any())
}
}
pub trait RepeatedItemTree:
crate::item_tree::ItemTree + vtable::HasStaticVTable<ItemTreeVTable> + 'static
{
type Data: 'static;
fn update(&self, index: usize, data: Self::Data);
fn init(&self) {}
fn listview_layout(
self: Pin<&Self>,
_offset_y: &mut LogicalLength,
_viewport_width: Pin<&Property<LogicalLength>>,
) {
}
fn box_layout_data(
self: Pin<&Self>,
_orientation: Orientation,
) -> crate::layout::BoxLayoutCellData {
crate::layout::BoxLayoutCellData::default()
}
}
#[derive(Clone, Copy, PartialEq, Debug)]
enum RepeatedInstanceState {
Clean,
Dirty,
}
struct RepeaterInner<C: RepeatedItemTree> {
instances: Vec<(RepeatedInstanceState, Option<ItemTreeRc<C>>)>,
offset: usize,
cached_item_height: LogicalLength,
previous_viewport_y: LogicalLength,
anchor_y: LogicalLength,
}
impl<C: RepeatedItemTree> Default for RepeaterInner<C> {
fn default() -> Self {
RepeaterInner {
instances: Default::default(),
offset: 0,
cached_item_height: Default::default(),
previous_viewport_y: Default::default(),
anchor_y: Default::default(),
}
}
}
#[pin_project]
pub struct RepeaterTracker<T: RepeatedItemTree> {
inner: RefCell<RepeaterInner<T>>,
#[pin]
model: Property<ModelRc<T::Data>>,
#[pin]
is_dirty: Property<bool>,
#[pin]
listview_geometry_tracker: crate::properties::PropertyTracker,
}
impl<T: RepeatedItemTree> ModelChangeListener for RepeaterTracker<T> {
fn row_changed(self: Pin<&Self>, row: usize) {
let mut inner = self.inner.borrow_mut();
let inner = &mut *inner;
if let Some(c) = inner.instances.get_mut(row.wrapping_sub(inner.offset)) {
if !self.model.is_dirty() {
if let Some(comp) = c.1.as_ref() {
let model = self.project_ref().model.get_untracked();
if let Some(data) = model.row_data(row) {
comp.update(row, data);
}
c.0 = RepeatedInstanceState::Clean;
}
} else {
c.0 = RepeatedInstanceState::Dirty;
}
}
}
fn row_added(self: Pin<&Self>, mut index: usize, mut count: usize) {
let mut inner = self.inner.borrow_mut();
if index < inner.offset {
if index + count < inner.offset {
return;
}
count -= inner.offset - index;
index = 0;
} else {
index -= inner.offset;
}
if count == 0 || index > inner.instances.len() {
return;
}
self.is_dirty.set(true);
inner.instances.splice(
index..index,
core::iter::repeat((RepeatedInstanceState::Dirty, None)).take(count),
);
for c in inner.instances[index + count..].iter_mut() {
c.0 = RepeatedInstanceState::Dirty;
}
}
fn row_removed(self: Pin<&Self>, mut index: usize, mut count: usize) {
let mut inner = self.inner.borrow_mut();
if index < inner.offset {
if index + count < inner.offset {
return;
}
count -= inner.offset - index;
index = 0;
} else {
index -= inner.offset;
}
if count == 0 || index >= inner.instances.len() {
return;
}
if (index + count) > inner.instances.len() {
count = inner.instances.len() - index;
}
self.is_dirty.set(true);
inner.instances.drain(index..(index + count));
for c in inner.instances[index..].iter_mut() {
c.0 = RepeatedInstanceState::Dirty;
}
}
fn reset(self: Pin<&Self>) {
self.is_dirty.set(true);
self.inner.borrow_mut().instances.clear();
}
}
impl<C: RepeatedItemTree> Default for RepeaterTracker<C> {
fn default() -> Self {
Self {
inner: Default::default(),
model: Property::new_named(ModelRc::default(), "i_slint_core::Repeater::model"),
is_dirty: Property::new_named(false, "i_slint_core::Repeater::is_dirty"),
listview_geometry_tracker: Default::default(),
}
}
}
#[pin_project]
pub struct Repeater<C: RepeatedItemTree>(#[pin] ModelChangeListenerContainer<RepeaterTracker<C>>);
impl<C: RepeatedItemTree> Default for Repeater<C> {
fn default() -> Self {
Self(Default::default())
}
}
impl<C: RepeatedItemTree + 'static> Repeater<C> {
fn data(self: Pin<&Self>) -> Pin<&RepeaterTracker<C>> {
self.project_ref().0.get()
}
fn model(self: Pin<&Self>) -> ModelRc<C::Data> {
let model = self.data().project_ref().model;
if model.is_dirty() {
*self.data().inner.borrow_mut() = RepeaterInner::default();
self.data().is_dirty.set(true);
let m = model.get();
let peer = self.project_ref().0.model_peer();
m.model_tracker().attach_peer(peer);
m
} else {
model.get()
}
}
pub fn ensure_updated(self: Pin<&Self>, init: impl Fn() -> ItemTreeRc<C>) {
let model = self.model();
if self.data().project_ref().is_dirty.get() {
self.ensure_updated_impl(init, &model, model.row_count());
}
}
fn ensure_updated_impl(
self: Pin<&Self>,
init: impl Fn() -> ItemTreeRc<C>,
model: &ModelRc<C::Data>,
count: usize,
) -> bool {
let mut indices_to_init = Vec::new();
let mut inner = self.0.inner.borrow_mut();
inner.instances.resize_with(count, || (RepeatedInstanceState::Dirty, None));
let offset = inner.offset;
let mut any_items_created = false;
for (i, c) in inner.instances.iter_mut().enumerate() {
if c.0 == RepeatedInstanceState::Dirty {
if c.1.is_none() {
any_items_created = true;
c.1 = Some(init());
indices_to_init.push(i);
};
if let Some(data) = model.row_data(i + offset) {
c.1.as_ref().unwrap().update(i + offset, data);
}
c.0 = RepeatedInstanceState::Clean;
}
}
self.data().is_dirty.set(false);
drop(inner);
let inner = self.0.inner.borrow();
for item in indices_to_init.into_iter().filter_map(|index| inner.instances.get(index)) {
item.1.as_ref().unwrap().init();
}
any_items_created
}
pub fn ensure_updated_listview(
self: Pin<&Self>,
init: impl Fn() -> ItemTreeRc<C>,
viewport_width: Pin<&Property<LogicalLength>>,
viewport_height: Pin<&Property<LogicalLength>>,
viewport_y: Pin<&Property<LogicalLength>>,
listview_width: LogicalLength,
listview_height: Pin<&Property<LogicalLength>>,
) {
self.data().project_ref().is_dirty.get();
self.data().project_ref().is_dirty.set(false);
viewport_width.set(listview_width);
let model = self.model();
let row_count = model.row_count();
if row_count == 0 {
self.0.inner.borrow_mut().instances.clear();
viewport_height.set(LogicalLength::zero());
viewport_y.set(LogicalLength::zero());
return;
}
let listview_height = listview_height.get();
let mut vp_y = viewport_y.get().min(LogicalLength::zero());
let cached_item_height = self.data().inner.borrow_mut().cached_item_height;
let element_height = if cached_item_height > LogicalLength::zero() {
cached_item_height
} else {
let total_height = Cell::new(LogicalLength::zero());
let count = Cell::new(0);
let get_height_visitor = |x: &ItemTreeRc<C>| {
let height = x.as_pin_ref().item_geometry(0).height_length();
count.set(count.get() + 1);
total_height.set(total_height.get() + height);
};
for c in self.data().inner.borrow().instances.iter() {
if let Some(x) = c.1.as_ref() {
get_height_visitor(x);
}
}
if count.get() > 0 {
total_height.get() / (count.get() as Coord)
} else {
{
let mut inner = self.0.inner.borrow_mut();
inner.offset = inner.offset.min(row_count - 1);
}
self.ensure_updated_impl(&init, &model, 1);
if let Some(c) = self.data().inner.borrow().instances.first() {
if let Some(x) = c.1.as_ref() {
get_height_visitor(x);
}
} else {
panic!("Could not determine size of items");
}
total_height.get()
}
};
let data = self.data();
let mut inner = data.inner.borrow_mut();
if inner.offset >= row_count {
inner.offset = row_count - 1;
}
let one_and_a_half_screen = listview_height * 3 as Coord / 2 as Coord;
let first_item_y = inner.anchor_y;
let last_item_bottom = first_item_y + element_height * inner.instances.len() as Coord;
let mut indices_to_init = Vec::new();
let (mut new_offset, mut new_offset_y) = if first_item_y > -vp_y + one_and_a_half_screen
|| last_item_bottom + element_height < -vp_y
{
inner.instances.clear();
inner.offset = ((-vp_y / element_height).get().floor() as usize).min(row_count - 1);
(inner.offset, -vp_y)
} else if vp_y < inner.previous_viewport_y {
let mut it_y = first_item_y;
let mut new_offset = inner.offset;
debug_assert!(it_y <= -vp_y); for (i, c) in inner.instances.iter_mut().enumerate() {
if c.0 == RepeatedInstanceState::Dirty {
if c.1.is_none() {
c.1 = Some(init());
indices_to_init.push(i);
}
if let Some(data) = model.row_data(new_offset) {
c.1.as_ref().unwrap().update(new_offset, data);
}
c.0 = RepeatedInstanceState::Clean;
}
let h = c.1.as_ref().unwrap().as_pin_ref().item_geometry(0).height_length();
if it_y + h >= -vp_y || new_offset + 1 >= row_count {
break;
}
it_y += h;
new_offset += 1;
}
(new_offset, it_y)
} else {
(inner.offset, first_item_y)
};
loop {
while new_offset > inner.offset && new_offset_y > -vp_y {
new_offset -= 1;
new_offset_y -= inner.instances[new_offset - inner.offset]
.1
.as_ref()
.unwrap()
.as_pin_ref()
.item_geometry(0)
.height_length();
}
let mut new_instances = Vec::new();
while new_offset > 0 && new_offset_y > -vp_y {
new_offset -= 1;
let new_instance = init();
if let Some(data) = model.row_data(new_offset) {
new_instance.update(new_offset, data);
}
new_offset_y -= new_instance.as_pin_ref().item_geometry(0).height_length();
new_instances.push(new_instance);
}
if !new_instances.is_empty() {
for x in &mut indices_to_init {
*x += new_instances.len();
}
indices_to_init.extend(0..new_instances.len());
inner.instances.splice(
0..0,
new_instances
.into_iter()
.rev()
.map(|c| (RepeatedInstanceState::Clean, Some(c))),
);
inner.offset = new_offset;
}
assert!(
new_offset >= inner.offset && new_offset <= inner.offset + inner.instances.len()
);
let mut y = new_offset_y;
let mut idx = new_offset;
let instances_begin = new_offset - inner.offset;
for c in &mut inner.instances[instances_begin..] {
if idx >= row_count {
break;
}
if c.0 == RepeatedInstanceState::Dirty {
if c.1.is_none() {
c.1 = Some(init());
indices_to_init.push(instances_begin + idx - new_offset)
}
if let Some(data) = model.row_data(idx) {
c.1.as_ref().unwrap().update(idx, data);
}
c.0 = RepeatedInstanceState::Clean;
}
if let Some(x) = c.1.as_ref() {
x.as_pin_ref().listview_layout(&mut y, viewport_width);
}
idx += 1;
if y >= -vp_y + listview_height {
break;
}
}
while y < -vp_y + listview_height && idx < row_count {
let new_instance = init();
if let Some(data) = model.row_data(idx) {
new_instance.update(idx, data);
}
new_instance.as_pin_ref().listview_layout(&mut y, viewport_width);
indices_to_init.push(inner.instances.len());
inner.instances.push((RepeatedInstanceState::Clean, Some(new_instance)));
idx += 1;
}
if y < -vp_y + listview_height && vp_y < LogicalLength::zero() {
assert!(idx >= row_count);
vp_y = listview_height - y;
continue;
}
if new_offset != inner.offset {
let instances_begin = new_offset - inner.offset;
inner.instances.splice(0..instances_begin, core::iter::empty());
indices_to_init.retain_mut(|idx| {
if *idx < instances_begin {
false
} else {
*idx -= instances_begin;
true
}
});
inner.offset = new_offset;
}
if inner.instances.len() != idx - new_offset {
inner.instances.splice(idx - new_offset.., core::iter::empty());
indices_to_init.retain(|x| *x < idx - new_offset);
}
if inner.instances.is_empty() {
break;
}
inner.cached_item_height = (y - new_offset_y) / inner.instances.len() as Coord;
inner.anchor_y = inner.cached_item_height * inner.offset as Coord;
viewport_height.set(inner.cached_item_height * row_count as Coord);
let new_viewport_y = -inner.anchor_y + vp_y + new_offset_y;
viewport_y.set(new_viewport_y);
inner.previous_viewport_y = new_viewport_y;
break;
}
drop(inner);
let inner = self.0.inner.borrow();
for item in indices_to_init.into_iter().filter_map(|index| inner.instances.get(index)) {
item.1.as_ref().unwrap().init();
}
}
pub fn model_set_row_data(self: Pin<&Self>, row: usize, data: C::Data) {
let model = self.model();
model.set_row_data(row, data);
}
pub fn set_model_binding(&self, binding: impl Fn() -> ModelRc<C::Data> + 'static) {
self.0.model.set_binding(binding);
}
pub fn visit(
&self,
order: TraversalOrder,
mut visitor: crate::item_tree::ItemVisitorRefMut,
) -> crate::item_tree::VisitChildrenResult {
let count = self.0.inner.borrow().instances.len() as u32;
for i in 0..count {
let i = if order == TraversalOrder::BackToFront { i } else { count - i - 1 };
let c = self.0.inner.borrow().instances.get(i as usize).and_then(|c| c.1.clone());
if let Some(c) = c {
if c.as_pin_ref().visit_children_item(-1, order, visitor.borrow_mut()).has_aborted()
{
return crate::item_tree::VisitChildrenResult::abort(i, 0);
}
}
}
crate::item_tree::VisitChildrenResult::CONTINUE
}
pub fn len(&self) -> usize {
self.0.inner.borrow().instances.len()
}
pub fn range(&self) -> core::ops::Range<usize> {
let inner = self.0.inner.borrow();
core::ops::Range { start: inner.offset, end: inner.offset + inner.instances.len() }
}
pub fn instance_at(&self, index: usize) -> Option<ItemTreeRc<C>> {
let inner = self.0.inner.borrow();
inner
.instances
.get(index - inner.offset)
.map(|c| c.1.clone().expect("That was updated before!"))
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
pub fn instances_vec(&self) -> Vec<ItemTreeRc<C>> {
self.0.inner.borrow().instances.iter().flat_map(|x| x.1.clone()).collect()
}
}
impl From<SharedString> for StandardListViewItem {
fn from(value: SharedString) -> Self {
StandardListViewItem { text: value }
}
}
impl From<&str> for StandardListViewItem {
fn from(value: &str) -> Self {
StandardListViewItem { text: value.into() }
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_tracking_model_handle() {
let model: Rc<VecModel<u8>> = Rc::new(Default::default());
let handle = ModelRc::from(model.clone() as Rc<dyn Model<Data = u8>>);
let tracker = Box::pin(crate::properties::PropertyTracker::default());
assert_eq!(
tracker.as_ref().evaluate(|| {
handle.model_tracker().track_row_count_changes();
handle.row_count()
}),
0
);
assert!(!tracker.is_dirty());
model.push(42);
model.push(100);
assert!(tracker.is_dirty());
assert_eq!(
tracker.as_ref().evaluate(|| {
handle.model_tracker().track_row_count_changes();
handle.row_count()
}),
2
);
assert!(!tracker.is_dirty());
model.set_row_data(0, 41);
assert!(!tracker.is_dirty());
model.remove(0);
assert!(tracker.is_dirty());
assert_eq!(
tracker.as_ref().evaluate(|| {
handle.model_tracker().track_row_count_changes();
handle.row_count()
}),
1
);
assert!(!tracker.is_dirty());
model.set_vec(vec![1, 2, 3]);
assert!(tracker.is_dirty());
}
#[test]
fn test_data_tracking() {
let model: Rc<VecModel<u8>> = Rc::new(VecModel::from(vec![0, 1, 2, 3, 4]));
let handle = ModelRc::from(model.clone());
let tracker = Box::pin(crate::properties::PropertyTracker::default());
assert_eq!(
tracker.as_ref().evaluate(|| {
handle.model_tracker().track_row_data_changes(1);
handle.row_data(1).unwrap()
}),
1
);
assert!(!tracker.is_dirty());
model.set_row_data(2, 42);
assert!(!tracker.is_dirty());
model.set_row_data(1, 100);
assert!(tracker.is_dirty());
assert_eq!(
tracker.as_ref().evaluate(|| {
handle.model_tracker().track_row_data_changes(1);
handle.row_data(1).unwrap()
}),
100
);
assert!(!tracker.is_dirty());
model.push(200);
assert!(tracker.is_dirty());
assert_eq!(tracker.as_ref().evaluate(|| { handle.row_data_tracked(1).unwrap() }), 100);
assert!(!tracker.is_dirty());
model.insert(0, 255);
assert!(tracker.is_dirty());
model.set_vec(vec![]);
assert!(tracker.is_dirty());
}
#[derive(Default)]
struct TestView {
changed_rows: RefCell<Vec<(usize, usize)>>,
added_rows: RefCell<Vec<(usize, usize, usize)>>,
removed_rows: RefCell<Vec<(usize, usize, usize)>>,
reset: RefCell<usize>,
model: RefCell<Option<std::rc::Weak<dyn Model<Data = i32>>>>,
}
impl TestView {
fn clear(&self) {
self.changed_rows.borrow_mut().clear();
self.added_rows.borrow_mut().clear();
self.removed_rows.borrow_mut().clear();
*self.reset.borrow_mut() = 0;
}
fn row_count(&self) -> usize {
self.model
.borrow()
.as_ref()
.and_then(|model| model.upgrade())
.map_or(0, |model| model.row_count())
}
}
impl ModelChangeListener for TestView {
fn row_changed(self: Pin<&Self>, row: usize) {
self.changed_rows.borrow_mut().push((row, self.row_count()));
}
fn row_added(self: Pin<&Self>, index: usize, count: usize) {
self.added_rows.borrow_mut().push((index, count, self.row_count()));
}
fn row_removed(self: Pin<&Self>, index: usize, count: usize) {
self.removed_rows.borrow_mut().push((index, count, self.row_count()));
}
fn reset(self: Pin<&Self>) {
*self.reset.borrow_mut() += 1;
}
}
#[test]
fn test_vecmodel_set_vec() {
let view = Box::pin(ModelChangeListenerContainer::<TestView>::default());
let model = Rc::new(VecModel::from(vec![1i32, 2, 3, 4]));
model.model_tracker().attach_peer(Pin::as_ref(&view).model_peer());
*view.model.borrow_mut() =
Some(std::rc::Rc::downgrade(&(model.clone() as Rc<dyn Model<Data = i32>>)));
model.push(5);
assert!(view.changed_rows.borrow().is_empty());
assert_eq!(&*view.added_rows.borrow(), &[(4, 1, 5)]);
assert!(view.removed_rows.borrow().is_empty());
assert_eq!(*view.reset.borrow(), 0);
view.clear();
model.set_vec(vec![6, 7, 8]);
assert!(view.changed_rows.borrow().is_empty());
assert!(view.added_rows.borrow().is_empty());
assert!(view.removed_rows.borrow().is_empty());
assert_eq!(*view.reset.borrow(), 1);
view.clear();
model.extend_from_slice(&[9, 10, 11]);
assert!(view.changed_rows.borrow().is_empty());
assert_eq!(&*view.added_rows.borrow(), &[(3, 3, 6)]);
assert!(view.removed_rows.borrow().is_empty());
assert_eq!(*view.reset.borrow(), 0);
view.clear();
model.extend([12, 13]);
assert!(view.changed_rows.borrow().is_empty());
assert_eq!(&*view.added_rows.borrow(), &[(6, 2, 8)]);
assert!(view.removed_rows.borrow().is_empty());
assert_eq!(*view.reset.borrow(), 0);
view.clear();
assert_eq!(model.iter().collect::<Vec<_>>(), vec![6, 7, 8, 9, 10, 11, 12, 13]);
model.swap(1, 1);
assert!(view.changed_rows.borrow().is_empty());
assert!(view.added_rows.borrow().is_empty());
assert!(view.removed_rows.borrow().is_empty());
assert_eq!(*view.reset.borrow(), 0);
view.clear();
model.swap(1, 2);
assert_eq!(&*view.changed_rows.borrow(), &[(1, 8), (2, 8)]);
assert!(view.added_rows.borrow().is_empty());
assert!(view.removed_rows.borrow().is_empty());
assert_eq!(*view.reset.borrow(), 0);
view.clear();
assert_eq!(model.iter().collect::<Vec<_>>(), vec![6, 8, 7, 9, 10, 11, 12, 13]);
}
#[test]
fn test_vecmodel_clear() {
let view = Box::pin(ModelChangeListenerContainer::<TestView>::default());
let model = Rc::new(VecModel::from(vec![1, 2, 3, 4]));
model.model_tracker().attach_peer(Pin::as_ref(&view).model_peer());
*view.model.borrow_mut() =
Some(std::rc::Rc::downgrade(&(model.clone() as Rc<dyn Model<Data = i32>>)));
model.clear();
assert_eq!(*view.reset.borrow(), 1);
assert_eq!(model.row_count(), 0);
}
#[test]
fn test_vecmodel_swap() {
let view = Box::pin(ModelChangeListenerContainer::<TestView>::default());
let model = Rc::new(VecModel::from(vec![1, 2, 3, 4]));
model.model_tracker().attach_peer(Pin::as_ref(&view).model_peer());
*view.model.borrow_mut() =
Some(std::rc::Rc::downgrade(&(model.clone() as Rc<dyn Model<Data = i32>>)));
model.swap(1, 1);
assert!(view.changed_rows.borrow().is_empty());
assert!(view.added_rows.borrow().is_empty());
assert!(view.removed_rows.borrow().is_empty());
assert_eq!(*view.reset.borrow(), 0);
view.clear();
model.swap(1, 2);
assert_eq!(&*view.changed_rows.borrow(), &[(1, 4), (2, 4)]);
assert!(view.added_rows.borrow().is_empty());
assert!(view.removed_rows.borrow().is_empty());
assert_eq!(*view.reset.borrow(), 0);
view.clear();
}
#[test]
fn modeliter_in_bounds() {
struct TestModel {
length: usize,
max_requested_row: Cell<usize>,
notify: ModelNotify,
}
impl Model for TestModel {
type Data = usize;
fn row_count(&self) -> usize {
self.length
}
fn row_data(&self, row: usize) -> Option<usize> {
self.max_requested_row.set(self.max_requested_row.get().max(row));
(row < self.length).then(|| row)
}
fn model_tracker(&self) -> &dyn ModelTracker {
&self.notify
}
}
let model = Rc::new(TestModel {
length: 10,
max_requested_row: Cell::new(0),
notify: Default::default(),
});
assert_eq!(model.iter().max().unwrap(), 9);
assert_eq!(model.max_requested_row.get(), 9);
}
}