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

//! `CompositionSimpleGeometry` (server only).
//!
//! Schema attributes: internal.

use bitflags::bitflags;
use crate::platform::IGeometryImpl;
use crate::rendering::composition::server::CompositionProperty;
use crate::rendering::composition::server::CompositionPropertyOf;
use crate::rendering::composition::server::IServerPropertyHost;
use crate::rendering::composition::server::ServerPropertyValue;
use crate::rendering::composition::server::ServerValueChange;
use crate::rendering::composition::server::props_of;
use crate::rendering::composition::transport::BatchStreamReader;
use crate::rendering::composition::transport::BatchStreamWriter;
use crate::rendering::composition::transport::BatchValue;
use crate::rendering::composition::transport::BatchValueReader;
use std::any::Any;
use std::any::TypeId;
use std::cell::RefCell;
use std::sync::Arc;
use std::sync::OnceLock;
use std::time::Duration;

bitflags! {
    /// The fields of `CompositionSimpleGeometry` that changed since its last serialization.
    #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
    pub struct CompositionSimpleGeometryChangedFields: u8 {
        const GEOMETRY_IMPL = 1;
    }
}

impl BatchValue for CompositionSimpleGeometryChangedFields {
    #[inline]
    fn write_to(&self, out: &mut Vec<u8>) {
        self.bits().write_to(out);
    }

    #[inline]
    fn read_from(input: &mut BatchValueReader<'_>) -> Self {
        CompositionSimpleGeometryChangedFields::from_bits_retain(u8::read_from(input))
    }
}

/// The partial methods of `ServerCompositionSimpleGeometry`: implemented by the class that embeds
/// [`ServerCompositionSimpleGeometryProps`]. Every hook defaults to doing nothing.
pub trait ServerCompositionSimpleGeometryHooks: IServerPropertyHost {
    fn deserialize_changes_extra(&self, _reader: &mut BatchStreamReader<'_>) {}
    fn on_fields_deserialized(&self, _changed: CompositionSimpleGeometryChangedFields) {}

    fn on_geometry_impl_changed(&self) {}
    fn on_geometry_impl_changing(&self) {}
}

/// The server-side properties of `ServerCompositionSimpleGeometry`.
pub struct ServerCompositionSimpleGeometryProps {
    geometry_impl: RefCell<Option<Arc<dyn IGeometryImpl>>>,
}

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

impl ServerCompositionSimpleGeometryProps {
    /// 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 {
            geometry_impl: RefCell::new(None),
        }
    }

    /// 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
    }

    /// The composition property of `GeometryImpl`.
    pub fn id_of_geometry_impl_property() -> &'static CompositionPropertyOf<Option<Arc<dyn IGeometryImpl>>> {
        static PROPERTY: OnceLock<CompositionPropertyOf<Option<Arc<dyn IGeometryImpl>>>> = OnceLock::new();
        PROPERTY.get_or_init(|| {
            CompositionProperty::register::<ServerCompositionSimpleGeometryProps, Option<Arc<dyn IGeometryImpl>>>(
                "GeometryImpl",
                "ServerCompositionSimpleGeometry",
                |obj| props_of::<ServerCompositionSimpleGeometryProps>(obj).geometry_impl.borrow().clone(),
                |obj, v| *props_of::<ServerCompositionSimpleGeometryProps>(obj).geometry_impl.borrow_mut() = v,
                None,
                None,
            )
        })
    }

    /// `GeometryImpl`.
    pub fn geometry_impl(&self) -> Option<Arc<dyn IGeometryImpl>> {
        self.geometry_impl.borrow().clone()
    }

    pub fn set_geometry_impl(&self, host: &dyn ServerCompositionSimpleGeometryHooks, value: Option<Arc<dyn IGeometryImpl>>) {
        let old_value = self.geometry_impl.borrow().clone();
        let mut changed = false;
        if old_value.differs(&value) {
            ServerCompositionSimpleGeometryHooks::on_geometry_impl_changing(host);
            changed = true;
        }
        host.set_value(
            Self::id_of_geometry_impl_property().base(),
            ServerValueChange {
                equal: old_value.default_equals(&value),
                old_resource: None,
                new_resource: None,
                assign: &mut || *self.geometry_impl.borrow_mut() = value.clone(),
            },
        );
        if changed {
            ServerCompositionSimpleGeometryHooks::on_geometry_impl_changed(host);
        }
    }

    /// 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 ServerCompositionSimpleGeometryHooks,
        reader: &mut BatchStreamReader<'_>,
        _committed_at: Duration,
    ) {
        ServerCompositionSimpleGeometryHooks::deserialize_changes_extra(host, reader);
        let changed = reader.read::<CompositionSimpleGeometryChangedFields>();
        if changed.contains(CompositionSimpleGeometryChangedFields::GEOMETRY_IMPL) {
            let value = reader.read_value::<Arc<dyn IGeometryImpl>>();
            self.set_geometry_impl(host, value);
        }
        ServerCompositionSimpleGeometryHooks::on_fields_deserialized(host, changed);
    }

    /// Writes every property of the class, in the form
    /// [`deserialize_changes_core`](Self::deserialize_changes_core) reads.
    pub fn serialize_all_changes(
        writer: &mut BatchStreamWriter<'_>,
        geometry_impl: Option<Arc<dyn IGeometryImpl>>,
    ) {
        writer.write(CompositionSimpleGeometryChangedFields::GEOMETRY_IMPL);
        writer.write_value(geometry_impl);
    }
}