Move the repeater part that is specific to rust within the rust lib

This commit is contained in:
Olivier Goffart 2020-06-19 10:06:13 +02:00
parent 911d34e777
commit 56a75a69e5
3 changed files with 40 additions and 39 deletions

View file

@ -66,13 +66,15 @@ fn main() {
pub use sixtyfps_rs_macro::sixtyfps;
pub(crate) mod repeater;
/// internal re_exports used by the macro generated
#[doc(hidden)]
pub mod re_exports {
pub use crate::repeater::*;
pub use const_field_offset::{self, FieldOffsets};
pub use once_cell::sync::Lazy;
pub use sixtyfps_corelib::abi::datastructures::*;
pub use sixtyfps_corelib::abi::model::*;
pub use sixtyfps_corelib::abi::primitives::*;
pub use sixtyfps_corelib::abi::properties::Property;
pub use sixtyfps_corelib::abi::signals::Signal;

View file

@ -0,0 +1,37 @@
/// Component that can be instantiated by a repeater.
pub trait RepeatedComponent: sixtyfps_corelib::abi::datastructures::Component {
/// 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<Box<C>>,
}
impl<Data, C> Repeater<C>
where
C: RepeatedComponent<Data = Data>,
{
/// Called when the model is changed
pub fn update_model(&mut self, data: &[Data]) {
self.components.clear();
for (i, d) in data.iter().enumerate() {
let c = C::create();
c.update(i, d);
self.components.push(Box::new(c));
}
}
/// Call the visitor for each component
pub fn visit(&self, mut visitor: sixtyfps_corelib::abi::datastructures::ItemVisitorRefMut) {
for c in &self.components {
c.visit_children_item(-1, visitor.borrow_mut());
}
}
}