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

//! `CompositionLineGeometry`.
//!
//! 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 `CompositionLineGeometry` that changed since its last serialization.
    #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
    pub struct CompositionLineGeometryChangedFields: u8 {
        const START = 1;
        const START_ANIMATED = 2;
        const END = 4;
        const END_ANIMATED = 8;
    }
}

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

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

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

    fn validate_start_change(&self, _old_value: Vector, _new_value: Vector) {}
    fn on_start_changed(&self) {}
    fn on_start_changing(&self) {}

    fn validate_end_change(&self, _old_value: Vector, _new_value: Vector) {}
    fn on_end_changed(&self) {}
    fn on_end_changing(&self) {}
}

/// The UI-thread properties of `CompositionLineGeometry`.
pub struct CompositionLineGeometryProps {
    base: CompositionGeometryProps,
    changed_fields_of_composition_line_geometry: Cell<CompositionLineGeometryChangedFields>,
    start: Cell<Vector>,
    end: Cell<Vector>,
}

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

impl CompositionLineGeometryProps {
    /// 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_line_geometry: Cell::new(CompositionLineGeometryChangedFields::empty()),
            start: Cell::new(Default::default()),
            end: 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 CompositionLineGeometryHooks) {
        self.base.initialize_defaults(host);
        self.initialize_own_defaults(host);
    }

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

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

    /// Sets `Start` (animated, FerroUI overlay).
    pub fn set_start(&self, host: &dyn CompositionLineGeometryHooks, value: Vector) {
        let mut changed = false;
        let old_value = self.start.get();
        if old_value.differs(&value) {
            CompositionLineGeometryHooks::validate_start_change(host, old_value, value);
            CompositionLineGeometryHooks::on_start_changing(host);
            changed = true;
            // Update the backing value
            self.start.set(value);
            // Register object for serialization in the next batch
            self.changed_fields_of_composition_line_geometry.set(self.changed_fields_of_composition_line_geometry.get() | CompositionLineGeometryChangedFields::START);
            host.register_for_serialization();
            // Reset previous animation if any
            host.pending_animations().remove(ServerCompositionLineGeometryProps::id_of_start_property().base());
            self.changed_fields_of_composition_line_geometry.set(self.changed_fields_of_composition_line_geometry.get() & !CompositionLineGeometryChangedFields::START_ANIMATED);
            // Check for implicit animations
            if let Some(animation) = host.implicit_animation("Start") {
                // Animation affects only current property
                if let Some(a) = animation.as_composition_animation() {
                    self.changed_fields_of_composition_line_geometry.set(self.changed_fields_of_composition_line_geometry.get() | CompositionLineGeometryChangedFields::START_ANIMATED);
                    host.pending_animations().set(
                        ServerCompositionLineGeometryProps::id_of_start_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, "Start", ExpressionVariant::create(value));
            }
        }
        self.start.set(value);
        if changed {
            CompositionLineGeometryHooks::on_start_changed(host);
        }
    }

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

    /// Sets `End` (animated, FerroUI overlay).
    pub fn set_end(&self, host: &dyn CompositionLineGeometryHooks, value: Vector) {
        let mut changed = false;
        let old_value = self.end.get();
        if old_value.differs(&value) {
            CompositionLineGeometryHooks::validate_end_change(host, old_value, value);
            CompositionLineGeometryHooks::on_end_changing(host);
            changed = true;
            // Update the backing value
            self.end.set(value);
            // Register object for serialization in the next batch
            self.changed_fields_of_composition_line_geometry.set(self.changed_fields_of_composition_line_geometry.get() | CompositionLineGeometryChangedFields::END);
            host.register_for_serialization();
            // Reset previous animation if any
            host.pending_animations().remove(ServerCompositionLineGeometryProps::id_of_end_property().base());
            self.changed_fields_of_composition_line_geometry.set(self.changed_fields_of_composition_line_geometry.get() & !CompositionLineGeometryChangedFields::END_ANIMATED);
            // Check for implicit animations
            if let Some(animation) = host.implicit_animation("End") {
                // Animation affects only current property
                if let Some(a) = animation.as_composition_animation() {
                    self.changed_fields_of_composition_line_geometry.set(self.changed_fields_of_composition_line_geometry.get() | CompositionLineGeometryChangedFields::END_ANIMATED);
                    host.pending_animations().set(
                        ServerCompositionLineGeometryProps::id_of_end_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, "End", ExpressionVariant::create(value));
            }
        }
        self.end.set(value);
        if changed {
            CompositionLineGeometryHooks::on_end_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 CompositionLineGeometryHooks,
        property_name: &str,
        animation: &dyn ICompositionAnimation,
        final_value: Option<ExpressionVariant>,
    ) -> bool {
        if property_name == "Start" {
            let server = animation.create_instance(host.server(), final_value);
            host.pending_animations().set(ServerCompositionLineGeometryProps::id_of_start_property().base(), server);
            self.changed_fields_of_composition_line_geometry.set(self.changed_fields_of_composition_line_geometry.get() | CompositionLineGeometryChangedFields::START_ANIMATED);
            host.register_for_serialization();
            return true;
        }
        if property_name == "End" {
            let server = animation.create_instance(host.server(), final_value);
            host.pending_animations().set(ServerCompositionLineGeometryProps::id_of_end_property().base(), server);
            self.changed_fields_of_composition_line_geometry.set(self.changed_fields_of_composition_line_geometry.get() | CompositionLineGeometryChangedFields::END_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 CompositionLineGeometryHooks, writer: &mut BatchStreamWriter<'_>) {
        self.base.serialize_changes_core(host, writer);
        let changed = self.changed_fields_of_composition_line_geometry.get();
        writer.write(changed);
        if changed.contains(CompositionLineGeometryChangedFields::START_ANIMATED) {
            writer.write_value(host.pending_animations().get_and_remove(ServerCompositionLineGeometryProps::id_of_start_property().base()));
        } else if changed.contains(CompositionLineGeometryChangedFields::START) {
            writer.write(self.start.get());
        }
        if changed.contains(CompositionLineGeometryChangedFields::END_ANIMATED) {
            writer.write_value(host.pending_animations().get_and_remove(ServerCompositionLineGeometryProps::id_of_end_property().base()));
        } else if changed.contains(CompositionLineGeometryChangedFields::END) {
            writer.write(self.end.get());
        }
        self.changed_fields_of_composition_line_geometry.set(CompositionLineGeometryChangedFields::empty());
    }
}

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

/// The server-side properties of `ServerCompositionLineGeometry`.
pub struct ServerCompositionLineGeometryProps {
    base: ServerCompositionGeometryProps,
    start: Cell<Vector>,
    end: Cell<Vector>,
}

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

impl ServerCompositionLineGeometryProps {
    /// 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(),
            start: Cell::new(Default::default()),
            end: 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 `Start`.
    pub fn id_of_start_property() -> &'static CompositionPropertyOf<Vector> {
        static PROPERTY: OnceLock<CompositionPropertyOf<Vector>> = OnceLock::new();
        PROPERTY.get_or_init(|| {
            CompositionProperty::register::<ServerCompositionLineGeometryProps, Vector>(
                "Start",
                "ServerCompositionLineGeometry",
                |obj| props_of::<ServerCompositionLineGeometryProps>(obj).start.get(),
                |obj, v| props_of::<ServerCompositionLineGeometryProps>(obj).start.set(v),
                Some(|obj| ExpressionVariant::create(props_of::<ServerCompositionLineGeometryProps>(obj).start.get())),
                Some(|obj, v: ExpressionVariant| props_of::<ServerCompositionLineGeometryProps>(obj).start.set(v.cast_or_default::<Vector>())),
            )
        })
    }

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

    pub fn set_start(&self, host: &dyn ServerCompositionLineGeometryHooks, value: Vector) {
        self.start.set(value);
        host.remove_animation_for_property(Self::id_of_start_property().base());
    }

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

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

    pub fn set_end(&self, host: &dyn ServerCompositionLineGeometryHooks, value: Vector) {
        self.end.set(value);
        host.remove_animation_for_property(Self::id_of_end_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 ServerCompositionLineGeometryHooks,
        reader: &mut BatchStreamReader<'_>,
        committed_at: Duration,
    ) {
        self.base.deserialize_changes_core(host, reader, committed_at);
        ServerCompositionLineGeometryHooks::deserialize_changes_extra(host, reader);
        let changed = reader.read::<CompositionLineGeometryChangedFields>();
        if changed.contains(CompositionLineGeometryChangedFields::START_ANIMATED) {
            let Some(animation) = reader.read_value::<AnimationInstanceFactory>().map(AnimationInstanceFactory::create) else {
                panic!("a batch starts an animation of `Start` without an animation instance");
            };
            host.set_animated_value(
                Self::id_of_start_property().base(),
                ExpressionVariant::create(self.start.get()),
                committed_at,
                animation,
            );
        } else if changed.contains(CompositionLineGeometryChangedFields::START) {
            let value = reader.read::<Vector>();
            self.set_start(host, value);
        }
        if changed.contains(CompositionLineGeometryChangedFields::END_ANIMATED) {
            let Some(animation) = reader.read_value::<AnimationInstanceFactory>().map(AnimationInstanceFactory::create) else {
                panic!("a batch starts an animation of `End` without an animation instance");
            };
            host.set_animated_value(
                Self::id_of_end_property().base(),
                ExpressionVariant::create(self.end.get()),
                committed_at,
                animation,
            );
        } else if changed.contains(CompositionLineGeometryChangedFields::END) {
            let value = reader.read::<Vector>();
            self.set_end(host, value);
        }
        ServerCompositionLineGeometryHooks::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 == "Start" {
            return Some(Self::id_of_start_property().base());
        }
        if name == "End" {
            return Some(Self::id_of_end_property().base());
        }
        ServerCompositionGeometryProps::get_composition_property(name)
    }
}