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

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

use bitflags::bitflags;
use crate::Matrix;
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::Cell;
use std::sync::OnceLock;
use std::time::Duration;

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

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

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

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

    fn on_value_changed(&self) {}
    fn on_value_changing(&self) {}
}

/// The server-side properties of `ServerCompositionSimpleTransform`.
pub struct ServerCompositionSimpleTransformProps {
    value: Cell<Matrix>,
}

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

impl ServerCompositionSimpleTransformProps {
    /// 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 {
            value: Cell::new(Default::default()),
        }
    }

    /// 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 `Value`.
    pub fn id_of_value_property() -> &'static CompositionPropertyOf<Matrix> {
        static PROPERTY: OnceLock<CompositionPropertyOf<Matrix>> = OnceLock::new();
        PROPERTY.get_or_init(|| {
            CompositionProperty::register::<ServerCompositionSimpleTransformProps, Matrix>(
                "Value",
                "ServerCompositionSimpleTransform",
                |obj| props_of::<ServerCompositionSimpleTransformProps>(obj).value.get(),
                |obj, v| props_of::<ServerCompositionSimpleTransformProps>(obj).value.set(v),
                None,
                None,
            )
        })
    }

    /// `Value`.
    pub fn value(&self) -> Matrix {
        self.value.get()
    }

    pub fn set_value(&self, host: &dyn ServerCompositionSimpleTransformHooks, value: Matrix) {
        let old_value = self.value.get();
        let mut changed = false;
        if old_value.differs(&value) {
            ServerCompositionSimpleTransformHooks::on_value_changing(host);
            changed = true;
        }
        host.set_value(
            Self::id_of_value_property().base(),
            ServerValueChange {
                equal: old_value.default_equals(&value),
                old_resource: None,
                new_resource: None,
                assign: &mut || self.value.set(value),
            },
        );
        if changed {
            ServerCompositionSimpleTransformHooks::on_value_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 ServerCompositionSimpleTransformHooks,
        reader: &mut BatchStreamReader<'_>,
        _committed_at: Duration,
    ) {
        ServerCompositionSimpleTransformHooks::deserialize_changes_extra(host, reader);
        let changed = reader.read::<CompositionSimpleTransformChangedFields>();
        if changed.contains(CompositionSimpleTransformChangedFields::VALUE) {
            let value = reader.read::<Matrix>();
            self.set_value(host, value);
        }
        ServerCompositionSimpleTransformHooks::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<'_>,
        value: Matrix,
    ) {
        writer.write(CompositionSimpleTransformChangedFields::VALUE);
        writer.write(value);
    }
}