ferroui-base 0.3.0

The base library of FerroUI: the property system, layout, input, styling, media, text, animation, data binding and the compositor
Documentation
// @generated by scripts/generate_composition.py — do not edit

//! `CompositionVisualCollection`.
//!
//! Schema attributes: custom constructor on the UI thread.

use crate::rendering::composition::ICompositionObject;
use crate::rendering::composition::ICompositionObjectHost;
use crate::rendering::composition::server::IServerAnimatedPropertyHost;
use crate::rendering::composition::server::ServerList;
use crate::rendering::composition::transport::BatchStreamReader;
use crate::rendering::composition::transport::BatchStreamWriter;
use crate::rendering::composition::transport::ServerListProxyHelper;
use std::any::Any;
use std::any::TypeId;
use std::rc::Rc;
use std::time::Duration;

/// The partial methods of `CompositionVisualCollection`: implemented by the class that embeds
/// [`CompositionVisualCollectionProps`]. `TItem` is the UI-thread class of the items (`CompositionVisual`).
/// Every hook defaults to doing nothing.
pub trait CompositionVisualCollectionHooks<TItem: ICompositionObject + ?Sized>: ICompositionObjectHost {
    fn initialize_defaults_extra(&self) {}
    fn on_before_added(&self, _item: &Rc<TItem>) {}
    fn on_added(&self, _item: &Rc<TItem>) {}
    fn on_removed(&self, _item: &Rc<TItem>) {}
    fn on_before_clear(&self) {}
    fn on_before_replace(&self, _old_item: &Rc<TItem>, _new_item: &Rc<TItem>) {}
    fn on_replace(&self, _old_item: &Rc<TItem>, _new_item: &Rc<TItem>) {}
    fn on_clear(&self) {}
}

/// The UI-thread list of `CompositionVisualCollection`.
pub struct CompositionVisualCollectionProps<TItem: ICompositionObject + ?Sized> {
    list: ServerListProxyHelper<TItem>,
}

impl<TItem: ICompositionObject + ?Sized> Default for CompositionVisualCollectionProps<TItem> {
    fn default() -> Self {
        Self::new()
    }
}

impl<TItem: ICompositionObject + ?Sized> CompositionVisualCollectionProps<TItem> {
    pub fn new() -> Self {
        Self { list: ServerListProxyHelper::new() }
    }

    /// The `InitializeDefaults` calls of the constructor chain.
    pub fn initialize_defaults(&self, host: &dyn CompositionVisualCollectionHooks<TItem>) {
        self.initialize_own_defaults(host);
    }

    /// `InitializeDefaults` of this class alone.
    pub fn initialize_own_defaults(&self, host: &dyn CompositionVisualCollectionHooks<TItem>) {
        CompositionVisualCollectionHooks::initialize_defaults_extra(host);
    }

    /// A snapshot of the items, for enumeration.
    pub fn items(&self) -> Vec<Rc<TItem>> {
        self.list.items()
    }

    pub fn add(&self, host: &dyn CompositionVisualCollectionHooks<TItem>, item: Rc<TItem>) {
        CompositionVisualCollectionHooks::on_before_added(host, &item);
        self.list.add(host, item.clone());
        CompositionVisualCollectionHooks::on_added(host, &item);
    }

    pub fn clear(&self, host: &dyn CompositionVisualCollectionHooks<TItem>) {
        CompositionVisualCollectionHooks::on_before_clear(host);
        self.list.clear(host);
        CompositionVisualCollectionHooks::on_clear(host);
    }

    pub fn contains(&self, item: &Rc<TItem>) -> bool {
        self.list.contains(item)
    }

    pub fn copy_to(&self, array: &mut [Option<Rc<TItem>>], array_index: usize) {
        self.list.copy_to(array, array_index)
    }

    pub fn remove(&self, host: &dyn CompositionVisualCollectionHooks<TItem>, item: &Rc<TItem>) -> bool {
        let removed = self.list.remove(host, item);
        if removed {
            CompositionVisualCollectionHooks::on_removed(host, item);
        }
        removed
    }

    pub fn count(&self) -> usize {
        self.list.count()
    }

    pub fn is_read_only(&self) -> bool {
        self.list.is_read_only()
    }

    pub fn index_of(&self, item: &Rc<TItem>) -> Option<usize> {
        self.list.index_of(item)
    }

    pub fn insert(&self, host: &dyn CompositionVisualCollectionHooks<TItem>, index: usize, item: Rc<TItem>) {
        CompositionVisualCollectionHooks::on_before_added(host, &item);
        self.list.insert(host, index, item.clone());
        CompositionVisualCollectionHooks::on_added(host, &item);
    }

    pub fn remove_at(&self, host: &dyn CompositionVisualCollectionHooks<TItem>, index: usize) {
        let item = self.list.get(index);
        self.list.remove_at(host, index);
        CompositionVisualCollectionHooks::on_removed(host, &item);
    }

    pub fn get(&self, index: usize) -> Rc<TItem> {
        self.list.get(index)
    }

    pub fn set(&self, host: &dyn CompositionVisualCollectionHooks<TItem>, index: usize, value: Rc<TItem>) {
        let old = self.list.get(index);
        CompositionVisualCollectionHooks::on_before_replace(host, &old, &value);
        self.list.set(host, index, value.clone());
        CompositionVisualCollectionHooks::on_replace(host, &old, &value);
    }

    /// Writes the changes of the object: the list, then the base class.
    pub fn serialize_changes_core(&self, _host: &dyn CompositionVisualCollectionHooks<TItem>, writer: &mut BatchStreamWriter<'_>) {
        self.list.serialize(writer);
    }
}

/// The partial methods of `ServerCompositionVisualCollection`: implemented by the class that embeds
/// [`ServerCompositionVisualCollectionProps`]. Every hook defaults to doing nothing.
pub trait ServerCompositionVisualCollectionHooks: IServerAnimatedPropertyHost {
}

/// The server-side properties of `ServerCompositionVisualCollection`.
pub struct ServerCompositionVisualCollectionProps {
    list: ServerList,
}

impl Default for ServerCompositionVisualCollectionProps {
    fn default() -> Self {
        Self::new()
    }
}

impl ServerCompositionVisualCollectionProps {
    /// Creates the properties with every field at the default of its type; the
    /// values of the UI thread arrive with the first batch.
    pub fn new() -> Self {
        Self {
            list: ServerList::new(),
        }
    }

    /// The items: server objects of class `ServerCompositionVisual`.
    pub fn list(&self) -> &ServerList {
        &self.list
    }

    /// This block or the block of a base class, by type: what
    /// `IServerObject::get_props` of the embedding class returns.
    pub fn find_props(&self, type_id: TypeId) -> Option<&dyn Any> {
        if type_id == TypeId::of::<Self>() {
            return Some(self);
        }
        None
    }

    /// Reads the changes of the object: the base class first, then the
    /// changed-fields mask and the changed members of this class.
    pub fn deserialize_changes_core(
        &self,
        host: &dyn ServerCompositionVisualCollectionHooks,
        reader: &mut BatchStreamReader<'_>,
        _committed_at: Duration,
    ) {
        let compositor = host.server_compositor();
        self.list.deserialize_changes_core(reader, compositor.as_ref());
    }
}