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

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

use bitflags::bitflags;
use crate::Vector;
use crate::rendering::composition::animations::AnimationInstanceFactory;
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::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;
use super::CompositionGeometryHooks;
use super::CompositionGeometryProps;
use super::ServerCompositionGeometryHooks;
use super::ServerCompositionGeometryProps;

bitflags! {
    /// The fields of `CompositionRectangleGeometry` that changed since its last serialization.
    #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
    pub struct CompositionRectangleGeometryChangedFields: u8 {
        const OFFSET = 1;
        const OFFSET_ANIMATED = 2;
        const SIZE = 4;
        const SIZE_ANIMATED = 8;
    }
}

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

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

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

    fn validate_offset_change(&self, _old_value: Vector, _new_value: Vector) {}
    fn on_offset_changed(&self) {}
    fn on_offset_changing(&self) {}

    fn validate_size_change(&self, _old_value: Vector, _new_value: Vector) {}
    fn on_size_changed(&self) {}
    fn on_size_changing(&self) {}
}

/// The UI-thread properties of `CompositionRectangleGeometry`.
pub struct CompositionRectangleGeometryProps {
    base: CompositionGeometryProps,
    changed_fields_of_composition_rectangle_geometry: Cell<CompositionRectangleGeometryChangedFields>,
    offset: Cell<Vector>,
    size: Cell<Vector>,
}

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

impl CompositionRectangleGeometryProps {
    /// 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_rectangle_geometry: Cell::new(CompositionRectangleGeometryChangedFields::empty()),
            offset: Cell::new(Default::default()),
            size: Cell::new(Default::default()),
        }
    }

    /// 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 CompositionRectangleGeometryHooks) {
        self.base.initialize_defaults(host);
        self.initialize_own_defaults(host);
    }

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

    /// `Offset` (animated, FerroUI overlay).
    pub fn offset(&self) -> Vector {
        self.offset.get()
    }

    /// Sets `Offset` (animated, FerroUI overlay).
    pub fn set_offset(&self, host: &dyn CompositionRectangleGeometryHooks, value: Vector) {
        let mut changed = false;
        let old_value = self.offset.get();
        if old_value.differs(&value) {
            CompositionRectangleGeometryHooks::validate_offset_change(host, old_value, value);
            CompositionRectangleGeometryHooks::on_offset_changing(host);
            changed = true;
            // Update the backing value
            self.offset.set(value);
            // Register object for serialization in the next batch
            self.changed_fields_of_composition_rectangle_geometry.set(self.changed_fields_of_composition_rectangle_geometry.get() | CompositionRectangleGeometryChangedFields::OFFSET);
            host.register_for_serialization();
            // Reset previous animation if any
            host.pending_animations().remove(ServerCompositionRectangleGeometryProps::id_of_offset_property().base());
            self.changed_fields_of_composition_rectangle_geometry.set(self.changed_fields_of_composition_rectangle_geometry.get() & !CompositionRectangleGeometryChangedFields::OFFSET_ANIMATED);
            // Check for implicit animations
            if let Some(animation) = host.implicit_animation("Offset") {
                // Animation affects only current property
                if let Some(a) = animation.as_composition_animation() {
                    self.changed_fields_of_composition_rectangle_geometry.set(self.changed_fields_of_composition_rectangle_geometry.get() | CompositionRectangleGeometryChangedFields::OFFSET_ANIMATED);
                    host.pending_animations().set(
                        ServerCompositionRectangleGeometryProps::id_of_offset_property().base(),
                        a.create_instance(host.server(), Some(ExpressionVariant::create(value))),
                    );
                }
                // Animation is triggered by the current field, but does not necessary affects it
                host.start_animation_group(&animation, "Offset", ExpressionVariant::create(value));
            }
        }
        self.offset.set(value);
        if changed {
            CompositionRectangleGeometryHooks::on_offset_changed(host);
        }
    }

    /// `Size` (animated, FerroUI overlay).
    pub fn size(&self) -> Vector {
        self.size.get()
    }

    /// Sets `Size` (animated, FerroUI overlay).
    pub fn set_size(&self, host: &dyn CompositionRectangleGeometryHooks, value: Vector) {
        let mut changed = false;
        let old_value = self.size.get();
        if old_value.differs(&value) {
            CompositionRectangleGeometryHooks::validate_size_change(host, old_value, value);
            CompositionRectangleGeometryHooks::on_size_changing(host);
            changed = true;
            // Update the backing value
            self.size.set(value);
            // Register object for serialization in the next batch
            self.changed_fields_of_composition_rectangle_geometry.set(self.changed_fields_of_composition_rectangle_geometry.get() | CompositionRectangleGeometryChangedFields::SIZE);
            host.register_for_serialization();
            // Reset previous animation if any
            host.pending_animations().remove(ServerCompositionRectangleGeometryProps::id_of_size_property().base());
            self.changed_fields_of_composition_rectangle_geometry.set(self.changed_fields_of_composition_rectangle_geometry.get() & !CompositionRectangleGeometryChangedFields::SIZE_ANIMATED);
            // Check for implicit animations
            if let Some(animation) = host.implicit_animation("Size") {
                // Animation affects only current property
                if let Some(a) = animation.as_composition_animation() {
                    self.changed_fields_of_composition_rectangle_geometry.set(self.changed_fields_of_composition_rectangle_geometry.get() | CompositionRectangleGeometryChangedFields::SIZE_ANIMATED);
                    host.pending_animations().set(
                        ServerCompositionRectangleGeometryProps::id_of_size_property().base(),
                        a.create_instance(host.server(), Some(ExpressionVariant::create(value))),
                    );
                }
                // Animation is triggered by the current field, but does not necessary affects it
                host.start_animation_group(&animation, "Size", ExpressionVariant::create(value));
            }
        }
        self.size.set(value);
        if changed {
            CompositionRectangleGeometryHooks::on_size_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 CompositionRectangleGeometryHooks,
        property_name: &str,
        animation: &dyn ICompositionAnimation,
        final_value: Option<ExpressionVariant>,
    ) -> bool {
        if property_name == "Offset" {
            let server = animation.create_instance(host.server(), final_value);
            host.pending_animations().set(ServerCompositionRectangleGeometryProps::id_of_offset_property().base(), server);
            self.changed_fields_of_composition_rectangle_geometry.set(self.changed_fields_of_composition_rectangle_geometry.get() | CompositionRectangleGeometryChangedFields::OFFSET_ANIMATED);
            host.register_for_serialization();
            return true;
        }
        if property_name == "Size" {
            let server = animation.create_instance(host.server(), final_value);
            host.pending_animations().set(ServerCompositionRectangleGeometryProps::id_of_size_property().base(), server);
            self.changed_fields_of_composition_rectangle_geometry.set(self.changed_fields_of_composition_rectangle_geometry.get() | CompositionRectangleGeometryChangedFields::SIZE_ANIMATED);
            host.register_for_serialization();
            return true;
        }
        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 CompositionRectangleGeometryHooks, writer: &mut BatchStreamWriter<'_>) {
        self.base.serialize_changes_core(host, writer);
        let changed = self.changed_fields_of_composition_rectangle_geometry.get();
        writer.write(changed);
        if changed.contains(CompositionRectangleGeometryChangedFields::OFFSET_ANIMATED) {
            writer.write_value(host.pending_animations().get_and_remove(ServerCompositionRectangleGeometryProps::id_of_offset_property().base()));
        } else if changed.contains(CompositionRectangleGeometryChangedFields::OFFSET) {
            writer.write(self.offset.get());
        }
        if changed.contains(CompositionRectangleGeometryChangedFields::SIZE_ANIMATED) {
            writer.write_value(host.pending_animations().get_and_remove(ServerCompositionRectangleGeometryProps::id_of_size_property().base()));
        } else if changed.contains(CompositionRectangleGeometryChangedFields::SIZE) {
            writer.write(self.size.get());
        }
        self.changed_fields_of_composition_rectangle_geometry.set(CompositionRectangleGeometryChangedFields::empty());
    }
}

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

/// The server-side properties of `ServerCompositionRectangleGeometry`.
pub struct ServerCompositionRectangleGeometryProps {
    base: ServerCompositionGeometryProps,
    offset: Cell<Vector>,
    size: Cell<Vector>,
}

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

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

    /// 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 `Offset`.
    pub fn id_of_offset_property() -> &'static CompositionPropertyOf<Vector> {
        static PROPERTY: OnceLock<CompositionPropertyOf<Vector>> = OnceLock::new();
        PROPERTY.get_or_init(|| {
            CompositionProperty::register::<ServerCompositionRectangleGeometryProps, Vector>(
                "Offset",
                "ServerCompositionRectangleGeometry",
                |obj| props_of::<ServerCompositionRectangleGeometryProps>(obj).offset.get(),
                |obj, v| props_of::<ServerCompositionRectangleGeometryProps>(obj).offset.set(v),
                Some(|obj| ExpressionVariant::create(props_of::<ServerCompositionRectangleGeometryProps>(obj).offset.get())),
                Some(|obj, v: ExpressionVariant| props_of::<ServerCompositionRectangleGeometryProps>(obj).offset.set(v.cast_or_default::<Vector>())),
            )
        })
    }

    /// `Offset` (animated).
    pub fn offset(&self) -> Vector {
        self.offset.get()
    }

    pub fn set_offset(&self, host: &dyn ServerCompositionRectangleGeometryHooks, value: Vector) {
        self.offset.set(value);
        host.remove_animation_for_property(Self::id_of_offset_property().base());
    }

    /// The composition property of `Size`.
    pub fn id_of_size_property() -> &'static CompositionPropertyOf<Vector> {
        static PROPERTY: OnceLock<CompositionPropertyOf<Vector>> = OnceLock::new();
        PROPERTY.get_or_init(|| {
            CompositionProperty::register::<ServerCompositionRectangleGeometryProps, Vector>(
                "Size",
                "ServerCompositionRectangleGeometry",
                |obj| props_of::<ServerCompositionRectangleGeometryProps>(obj).size.get(),
                |obj, v| props_of::<ServerCompositionRectangleGeometryProps>(obj).size.set(v),
                Some(|obj| ExpressionVariant::create(props_of::<ServerCompositionRectangleGeometryProps>(obj).size.get())),
                Some(|obj, v: ExpressionVariant| props_of::<ServerCompositionRectangleGeometryProps>(obj).size.set(v.cast_or_default::<Vector>())),
            )
        })
    }

    /// `Size` (animated).
    pub fn size(&self) -> Vector {
        self.size.get()
    }

    pub fn set_size(&self, host: &dyn ServerCompositionRectangleGeometryHooks, value: Vector) {
        self.size.set(value);
        host.remove_animation_for_property(Self::id_of_size_property().base());
    }

    /// 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 ServerCompositionRectangleGeometryHooks,
        reader: &mut BatchStreamReader<'_>,
        committed_at: Duration,
    ) {
        self.base.deserialize_changes_core(host, reader, committed_at);
        ServerCompositionRectangleGeometryHooks::deserialize_changes_extra(host, reader);
        let changed = reader.read::<CompositionRectangleGeometryChangedFields>();
        if changed.contains(CompositionRectangleGeometryChangedFields::OFFSET_ANIMATED) {
            let Some(animation) = reader.read_value::<AnimationInstanceFactory>().map(AnimationInstanceFactory::create) else {
                panic!("a batch starts an animation of `Offset` without an animation instance");
            };
            host.set_animated_value(
                Self::id_of_offset_property().base(),
                ExpressionVariant::create(self.offset.get()),
                committed_at,
                animation,
            );
        } else if changed.contains(CompositionRectangleGeometryChangedFields::OFFSET) {
            let value = reader.read::<Vector>();
            self.set_offset(host, value);
        }
        if changed.contains(CompositionRectangleGeometryChangedFields::SIZE_ANIMATED) {
            let Some(animation) = reader.read_value::<AnimationInstanceFactory>().map(AnimationInstanceFactory::create) else {
                panic!("a batch starts an animation of `Size` without an animation instance");
            };
            host.set_animated_value(
                Self::id_of_size_property().base(),
                ExpressionVariant::create(self.size.get()),
                committed_at,
                animation,
            );
        } else if changed.contains(CompositionRectangleGeometryChangedFields::SIZE) {
            let value = reader.read::<Vector>();
            self.set_size(host, value);
        }
        ServerCompositionRectangleGeometryHooks::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> {
        if name == "Offset" {
            return Some(Self::id_of_offset_property().base());
        }
        if name == "Size" {
            return Some(Self::id_of_size_property().base());
        }
        ServerCompositionGeometryProps::get_composition_property(name)
    }
}