slint/api/sixtyfps-rs/repeater.rs
Olivier Goffart caca0d0ba4 Put the component in a Pin<>
Removed the drop and create from the ComponentVTable:
since we are not using VBox<ComponentVTable>, this simplifies a bit
the code of the interpreter and everything else.

But there is still a lot of changes everywhere to support that the Component
is pinned.
This is just for the component. Which would be required if later we want
to access the properties as Pin<Property<_>>. But we have not yet ability
to do projections
2020-06-24 14:13:27 +02:00

40 lines
1.2 KiB
Rust

/// Component that can be instantiated by a repeater.
pub trait RepeatedComponent: sixtyfps_corelib::abi::datastructures::Component + Default {
/// The data corresponding to the model
type Data;
/// Update this component at the given index and the given data
fn update(&self, index: usize, data: Self::Data);
}
/// This field is put in a component when using the `for` syntax
/// It helps instantiating the components `C`
#[derive(Default)]
pub struct Repeater<C> {
components: Vec<core::pin::Pin<Box<C>>>,
}
impl<Data, C> Repeater<C>
where
C: RepeatedComponent<Data = Data>,
{
/// Called when the model is changed
pub fn update_model<'a>(&mut self, data: impl Iterator<Item = Data>)
where
Data: 'a,
{
self.components.clear();
for (i, d) in data.enumerate() {
let c = C::default();
c.update(i, d);
self.components.push(Box::pin(c));
}
}
/// Call the visitor for each component
pub fn visit(&self, mut visitor: sixtyfps_corelib::abi::datastructures::ItemVisitorRefMut) {
for c in &self.components {
c.as_ref().visit_children_item(-1, visitor.borrow_mut());
}
}
}