ferroui-base 0.4.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

//! `CompositionPathGeometry`.
//!
//! Defined by the FerroUI overlay (scripts/composition-schema-winui.xml).

use bitflags::bitflags;
use crate::rendering::composition::CompositionPath;
use crate::rendering::composition::animations::ICompositionAnimation;
use crate::rendering::composition::expressions::ExpressionVariant;
use crate::rendering::composition::server::CompositionProperty;
use crate::rendering::composition::server::CompositionPropertyOf;
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::cell::RefCell;
use std::sync::Arc;
use std::sync::OnceLock;
use std::time::Duration;
use super::CompositionGeometryHooks;
use super::CompositionGeometryProps;
use super::ServerCompositionGeometryHooks;
use super::ServerCompositionGeometryProps;

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

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

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

/// The partial methods of `CompositionPathGeometry`: implemented by the class that embeds
/// [`CompositionPathGeometryProps`]. Every hook defaults to doing nothing.
pub trait CompositionPathGeometryHooks: CompositionGeometryHooks {
    fn initialize_defaults_extra(&self) {}

    fn validate_path_change(&self, _old_value: &Option<Arc<CompositionPath>>, _new_value: &Option<Arc<CompositionPath>>) {}
    fn on_path_changed(&self) {}
    fn on_path_changing(&self) {}
}

/// The UI-thread properties of `CompositionPathGeometry`.
pub struct CompositionPathGeometryProps {
    base: CompositionGeometryProps,
    changed_fields_of_composition_path_geometry: Cell<CompositionPathGeometryChangedFields>,
    path: RefCell<Option<Arc<CompositionPath>>>,
}

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

impl CompositionPathGeometryProps {
    /// Creates the properties with every field at the default of its type.
    /// The defaults of the schema are applied by
    /// [`initialize_defaults`](Self::initialize_defaults).
    pub fn new() -> Self {
        Self {
            base: CompositionGeometryProps::new(),
            changed_fields_of_composition_path_geometry: Cell::new(CompositionPathGeometryChangedFields::empty()),
            path: RefCell::new(None),
        }
    }

    /// The properties of the base class.
    pub fn base(&self) -> &CompositionGeometryProps {
        &self.base
    }

    /// The `InitializeDefaults` calls of the constructor chain: the base
    /// classes first, then this class.
    pub fn initialize_defaults(&self, host: &dyn CompositionPathGeometryHooks) {
        self.base.initialize_defaults(host);
        self.initialize_own_defaults(host);
    }

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

    /// `Path` (FerroUI overlay).
    pub fn path(&self) -> Option<Arc<CompositionPath>> {
        self.path.borrow().clone()
    }

    /// Sets `Path` (FerroUI overlay).
    pub fn set_path(&self, host: &dyn CompositionPathGeometryHooks, value: Option<Arc<CompositionPath>>) {
        let mut changed = false;
        let old_value = self.path.borrow().clone();
        if old_value.differs(&value) {
            CompositionPathGeometryHooks::validate_path_change(host, &old_value, &value);
            CompositionPathGeometryHooks::on_path_changing(host);
            changed = true;
            // Update the backing value
            *self.path.borrow_mut() = value.clone();
            // Register object for serialization in the next batch
            self.changed_fields_of_composition_path_geometry.set(self.changed_fields_of_composition_path_geometry.get() | CompositionPathGeometryChangedFields::PATH);
            host.register_for_serialization();
        }
        *self.path.borrow_mut() = value;
        if changed {
            CompositionPathGeometryHooks::on_path_changed(host);
        }
    }

    /// Connects an animation with the named property. Returns `false` when
    /// neither this class nor its generated bases have an animated property
    /// of that name: the caller continues with its hand-written base.
    pub fn start_animation(
        &self,
        host: &dyn CompositionPathGeometryHooks,
        property_name: &str,
        animation: &dyn ICompositionAnimation,
        final_value: Option<ExpressionVariant>,
    ) -> bool {
        self.base.start_animation(host, property_name, animation, final_value)
    }

    /// Writes the changes of the object: the base class first, then the
    /// changed-fields mask and the changed members of this class.
    pub fn serialize_changes_core(&self, host: &dyn CompositionPathGeometryHooks, writer: &mut BatchStreamWriter<'_>) {
        self.base.serialize_changes_core(host, writer);
        let changed = self.changed_fields_of_composition_path_geometry.get();
        writer.write(changed);
        if changed.contains(CompositionPathGeometryChangedFields::PATH) {
            writer.write_value(self.path.borrow().clone());
        }
        self.changed_fields_of_composition_path_geometry.set(CompositionPathGeometryChangedFields::empty());
    }
}

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

    fn on_path_changed(&self) {}
    fn on_path_changing(&self) {}
}

/// The server-side properties of `ServerCompositionPathGeometry`.
pub struct ServerCompositionPathGeometryProps {
    base: ServerCompositionGeometryProps,
    path: RefCell<Option<Arc<CompositionPath>>>,
}

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

impl ServerCompositionPathGeometryProps {
    /// 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 {
            base: ServerCompositionGeometryProps::new(),
            path: RefCell::new(None),
        }
    }

    /// The properties of the base class.
    pub fn base(&self) -> &ServerCompositionGeometryProps {
        &self.base
    }

    /// 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);
        }
        self.base.find_props(type_id)
    }

    /// The composition property of `Path`.
    pub fn id_of_path_property() -> &'static CompositionPropertyOf<Option<Arc<CompositionPath>>> {
        static PROPERTY: OnceLock<CompositionPropertyOf<Option<Arc<CompositionPath>>>> = OnceLock::new();
        PROPERTY.get_or_init(|| {
            CompositionProperty::register::<ServerCompositionPathGeometryProps, Option<Arc<CompositionPath>>>(
                "Path",
                "ServerCompositionPathGeometry",
                |obj| props_of::<ServerCompositionPathGeometryProps>(obj).path.borrow().clone(),
                |obj, v| *props_of::<ServerCompositionPathGeometryProps>(obj).path.borrow_mut() = v,
                None,
                None,
            )
        })
    }

    /// `Path`.
    pub fn path(&self) -> Option<Arc<CompositionPath>> {
        self.path.borrow().clone()
    }

    pub fn set_path(&self, host: &dyn ServerCompositionPathGeometryHooks, value: Option<Arc<CompositionPath>>) {
        let old_value = self.path.borrow().clone();
        let mut changed = false;
        if old_value.differs(&value) {
            ServerCompositionPathGeometryHooks::on_path_changing(host);
            changed = true;
        }
        host.set_value(
            Self::id_of_path_property().base(),
            ServerValueChange {
                equal: old_value.default_equals(&value),
                old_resource: None,
                new_resource: None,
                assign: &mut || *self.path.borrow_mut() = value.clone(),
            },
        );
        if changed {
            ServerCompositionPathGeometryHooks::on_path_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 ServerCompositionPathGeometryHooks,
        reader: &mut BatchStreamReader<'_>,
        committed_at: Duration,
    ) {
        self.base.deserialize_changes_core(host, reader, committed_at);
        ServerCompositionPathGeometryHooks::deserialize_changes_extra(host, reader);
        let changed = reader.read::<CompositionPathGeometryChangedFields>();
        if changed.contains(CompositionPathGeometryChangedFields::PATH) {
            let value = reader.read_value::<Arc<CompositionPath>>();
            self.set_path(host, value);
        }
        ServerCompositionPathGeometryHooks::on_fields_deserialized(host, changed);
    }

    /// The composition property with the given name, among the properties
    /// of this class and of its generated bases that have a variant form.
    pub fn get_composition_property(name: &str) -> Option<&'static CompositionProperty> {
        ServerCompositionGeometryProps::get_composition_property(name)
    }
}