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

//! `CompositionLinearGradientBrush`.

use bitflags::bitflags;
use crate::RelativePoint;
use crate::rendering::composition::ICompositionObjectHost;
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::IServerAnimatedPropertyHost;
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;

bitflags! {
    /// The fields of `CompositionLinearGradientBrush` that changed since its last serialization.
    #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
    pub struct CompositionLinearGradientBrushChangedFields: u8 {
        const START_POINT = 1;
        const START_POINT_ANIMATED = 2;
        const END_POINT = 4;
        const END_POINT_ANIMATED = 8;
    }
}

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

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

/// The partial methods of `CompositionLinearGradientBrush`: implemented by the class that embeds
/// [`CompositionLinearGradientBrushProps`]. Every hook defaults to doing nothing.
pub trait CompositionLinearGradientBrushHooks: ICompositionObjectHost {
    /// `base.SerializeChangesCore(writer)`: serializes the changes of the
    /// hand-written base class `CompositionGradientBrush`.
    fn base_serialize_changes_core(&self, writer: &mut BatchStreamWriter<'_>);

    fn initialize_defaults_extra(&self) {}

    fn validate_start_point_change(&self, _old_value: RelativePoint, _new_value: RelativePoint) {}
    fn on_start_point_changed(&self) {}
    fn on_start_point_changing(&self) {}

    fn validate_end_point_change(&self, _old_value: RelativePoint, _new_value: RelativePoint) {}
    fn on_end_point_changed(&self) {}
    fn on_end_point_changing(&self) {}
}

/// The UI-thread properties of `CompositionLinearGradientBrush`.
pub struct CompositionLinearGradientBrushProps {
    changed_fields_of_composition_linear_gradient_brush: Cell<CompositionLinearGradientBrushChangedFields>,
    start_point: Cell<RelativePoint>,
    end_point: Cell<RelativePoint>,
}

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

impl CompositionLinearGradientBrushProps {
    /// 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 {
            changed_fields_of_composition_linear_gradient_brush: Cell::new(CompositionLinearGradientBrushChangedFields::empty()),
            start_point: Cell::new(Default::default()),
            end_point: Cell::new(Default::default()),
        }
    }

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

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

    /// `StartPoint` (animated).
    pub fn start_point(&self) -> RelativePoint {
        self.start_point.get()
    }

    /// Sets `StartPoint` (animated).
    pub fn set_start_point(&self, host: &dyn CompositionLinearGradientBrushHooks, value: RelativePoint) {
        let mut changed = false;
        let old_value = self.start_point.get();
        if old_value.differs(&value) {
            CompositionLinearGradientBrushHooks::validate_start_point_change(host, old_value, value);
            CompositionLinearGradientBrushHooks::on_start_point_changing(host);
            changed = true;
            // Update the backing value
            self.start_point.set(value);
            // Register object for serialization in the next batch
            self.changed_fields_of_composition_linear_gradient_brush.set(self.changed_fields_of_composition_linear_gradient_brush.get() | CompositionLinearGradientBrushChangedFields::START_POINT);
            host.register_for_serialization();
            // Reset previous animation if any
            host.pending_animations().remove(ServerCompositionLinearGradientBrushProps::id_of_start_point_property().base());
            self.changed_fields_of_composition_linear_gradient_brush.set(self.changed_fields_of_composition_linear_gradient_brush.get() & !CompositionLinearGradientBrushChangedFields::START_POINT_ANIMATED);
            // Check for implicit animations
            if let Some(animation) = host.implicit_animation("StartPoint") {
                // Animation affects only current property
                if let Some(a) = animation.as_composition_animation() {
                    self.changed_fields_of_composition_linear_gradient_brush.set(self.changed_fields_of_composition_linear_gradient_brush.get() | CompositionLinearGradientBrushChangedFields::START_POINT_ANIMATED);
                    host.pending_animations().set(
                        ServerCompositionLinearGradientBrushProps::id_of_start_point_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, "StartPoint", ExpressionVariant::create(value));
            }
        }
        self.start_point.set(value);
        if changed {
            CompositionLinearGradientBrushHooks::on_start_point_changed(host);
        }
    }

    /// `EndPoint` (animated).
    pub fn end_point(&self) -> RelativePoint {
        self.end_point.get()
    }

    /// Sets `EndPoint` (animated).
    pub fn set_end_point(&self, host: &dyn CompositionLinearGradientBrushHooks, value: RelativePoint) {
        let mut changed = false;
        let old_value = self.end_point.get();
        if old_value.differs(&value) {
            CompositionLinearGradientBrushHooks::validate_end_point_change(host, old_value, value);
            CompositionLinearGradientBrushHooks::on_end_point_changing(host);
            changed = true;
            // Update the backing value
            self.end_point.set(value);
            // Register object for serialization in the next batch
            self.changed_fields_of_composition_linear_gradient_brush.set(self.changed_fields_of_composition_linear_gradient_brush.get() | CompositionLinearGradientBrushChangedFields::END_POINT);
            host.register_for_serialization();
            // Reset previous animation if any
            host.pending_animations().remove(ServerCompositionLinearGradientBrushProps::id_of_end_point_property().base());
            self.changed_fields_of_composition_linear_gradient_brush.set(self.changed_fields_of_composition_linear_gradient_brush.get() & !CompositionLinearGradientBrushChangedFields::END_POINT_ANIMATED);
            // Check for implicit animations
            if let Some(animation) = host.implicit_animation("EndPoint") {
                // Animation affects only current property
                if let Some(a) = animation.as_composition_animation() {
                    self.changed_fields_of_composition_linear_gradient_brush.set(self.changed_fields_of_composition_linear_gradient_brush.get() | CompositionLinearGradientBrushChangedFields::END_POINT_ANIMATED);
                    host.pending_animations().set(
                        ServerCompositionLinearGradientBrushProps::id_of_end_point_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, "EndPoint", ExpressionVariant::create(value));
            }
        }
        self.end_point.set(value);
        if changed {
            CompositionLinearGradientBrushHooks::on_end_point_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 CompositionLinearGradientBrushHooks,
        property_name: &str,
        animation: &dyn ICompositionAnimation,
        final_value: Option<ExpressionVariant>,
    ) -> bool {
        if property_name == "StartPoint" {
            let server = animation.create_instance(host.server(), final_value);
            host.pending_animations().set(ServerCompositionLinearGradientBrushProps::id_of_start_point_property().base(), server);
            self.changed_fields_of_composition_linear_gradient_brush.set(self.changed_fields_of_composition_linear_gradient_brush.get() | CompositionLinearGradientBrushChangedFields::START_POINT_ANIMATED);
            host.register_for_serialization();
            return true;
        }
        if property_name == "EndPoint" {
            let server = animation.create_instance(host.server(), final_value);
            host.pending_animations().set(ServerCompositionLinearGradientBrushProps::id_of_end_point_property().base(), server);
            self.changed_fields_of_composition_linear_gradient_brush.set(self.changed_fields_of_composition_linear_gradient_brush.get() | CompositionLinearGradientBrushChangedFields::END_POINT_ANIMATED);
            host.register_for_serialization();
            return true;
        }
        false
    }

    /// 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 CompositionLinearGradientBrushHooks, writer: &mut BatchStreamWriter<'_>) {
        CompositionLinearGradientBrushHooks::base_serialize_changes_core(host, writer);
        let changed = self.changed_fields_of_composition_linear_gradient_brush.get();
        writer.write(changed);
        if changed.contains(CompositionLinearGradientBrushChangedFields::START_POINT_ANIMATED) {
            writer.write_value(host.pending_animations().get_and_remove(ServerCompositionLinearGradientBrushProps::id_of_start_point_property().base()));
        } else if changed.contains(CompositionLinearGradientBrushChangedFields::START_POINT) {
            writer.write(self.start_point.get());
        }
        if changed.contains(CompositionLinearGradientBrushChangedFields::END_POINT_ANIMATED) {
            writer.write_value(host.pending_animations().get_and_remove(ServerCompositionLinearGradientBrushProps::id_of_end_point_property().base()));
        } else if changed.contains(CompositionLinearGradientBrushChangedFields::END_POINT) {
            writer.write(self.end_point.get());
        }
        self.changed_fields_of_composition_linear_gradient_brush.set(CompositionLinearGradientBrushChangedFields::empty());
    }
}

/// The partial methods of `ServerCompositionLinearGradientBrush`: implemented by the class that embeds
/// [`ServerCompositionLinearGradientBrushProps`]. Every hook defaults to doing nothing.
pub trait ServerCompositionLinearGradientBrushHooks: IServerAnimatedPropertyHost {
    /// `base.DeserializeChangesCore(reader, committedAt)`: reads the changes of
    /// the hand-written base class `ServerCompositionGradientBrush`.
    fn base_deserialize_changes_core(&self, reader: &mut BatchStreamReader<'_>, committed_at: Duration);

    fn deserialize_changes_extra(&self, _reader: &mut BatchStreamReader<'_>) {}
    fn on_fields_deserialized(&self, _changed: CompositionLinearGradientBrushChangedFields) {}
}

/// The server-side properties of `ServerCompositionLinearGradientBrush`.
pub struct ServerCompositionLinearGradientBrushProps {
    start_point: Cell<RelativePoint>,
    end_point: Cell<RelativePoint>,
}

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

impl ServerCompositionLinearGradientBrushProps {
    /// 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 {
            start_point: Cell::new(Default::default()),
            end_point: 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 `StartPoint`.
    pub fn id_of_start_point_property() -> &'static CompositionPropertyOf<RelativePoint> {
        static PROPERTY: OnceLock<CompositionPropertyOf<RelativePoint>> = OnceLock::new();
        PROPERTY.get_or_init(|| {
            CompositionProperty::register::<ServerCompositionLinearGradientBrushProps, RelativePoint>(
                "StartPoint",
                "ServerCompositionLinearGradientBrush",
                |obj| props_of::<ServerCompositionLinearGradientBrushProps>(obj).start_point.get(),
                |obj, v| props_of::<ServerCompositionLinearGradientBrushProps>(obj).start_point.set(v),
                Some(|obj| ExpressionVariant::create(props_of::<ServerCompositionLinearGradientBrushProps>(obj).start_point.get())),
                Some(|obj, v: ExpressionVariant| props_of::<ServerCompositionLinearGradientBrushProps>(obj).start_point.set(v.cast_or_default::<RelativePoint>())),
            )
        })
    }

    /// `StartPoint` (animated).
    pub fn start_point(&self) -> RelativePoint {
        self.start_point.get()
    }

    pub fn set_start_point(&self, host: &dyn ServerCompositionLinearGradientBrushHooks, value: RelativePoint) {
        self.start_point.set(value);
        host.remove_animation_for_property(Self::id_of_start_point_property().base());
    }

    /// The composition property of `EndPoint`.
    pub fn id_of_end_point_property() -> &'static CompositionPropertyOf<RelativePoint> {
        static PROPERTY: OnceLock<CompositionPropertyOf<RelativePoint>> = OnceLock::new();
        PROPERTY.get_or_init(|| {
            CompositionProperty::register::<ServerCompositionLinearGradientBrushProps, RelativePoint>(
                "EndPoint",
                "ServerCompositionLinearGradientBrush",
                |obj| props_of::<ServerCompositionLinearGradientBrushProps>(obj).end_point.get(),
                |obj, v| props_of::<ServerCompositionLinearGradientBrushProps>(obj).end_point.set(v),
                Some(|obj| ExpressionVariant::create(props_of::<ServerCompositionLinearGradientBrushProps>(obj).end_point.get())),
                Some(|obj, v: ExpressionVariant| props_of::<ServerCompositionLinearGradientBrushProps>(obj).end_point.set(v.cast_or_default::<RelativePoint>())),
            )
        })
    }

    /// `EndPoint` (animated).
    pub fn end_point(&self) -> RelativePoint {
        self.end_point.get()
    }

    pub fn set_end_point(&self, host: &dyn ServerCompositionLinearGradientBrushHooks, value: RelativePoint) {
        self.end_point.set(value);
        host.remove_animation_for_property(Self::id_of_end_point_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 ServerCompositionLinearGradientBrushHooks,
        reader: &mut BatchStreamReader<'_>,
        committed_at: Duration,
    ) {
        ServerCompositionLinearGradientBrushHooks::base_deserialize_changes_core(host, reader, committed_at);
        ServerCompositionLinearGradientBrushHooks::deserialize_changes_extra(host, reader);
        let changed = reader.read::<CompositionLinearGradientBrushChangedFields>();
        if changed.contains(CompositionLinearGradientBrushChangedFields::START_POINT_ANIMATED) {
            let Some(animation) = reader.read_value::<AnimationInstanceFactory>().map(AnimationInstanceFactory::create) else {
                panic!("a batch starts an animation of `StartPoint` without an animation instance");
            };
            host.set_animated_value(
                Self::id_of_start_point_property().base(),
                ExpressionVariant::create(self.start_point.get()),
                committed_at,
                animation,
            );
        } else if changed.contains(CompositionLinearGradientBrushChangedFields::START_POINT) {
            let value = reader.read::<RelativePoint>();
            self.set_start_point(host, value);
        }
        if changed.contains(CompositionLinearGradientBrushChangedFields::END_POINT_ANIMATED) {
            let Some(animation) = reader.read_value::<AnimationInstanceFactory>().map(AnimationInstanceFactory::create) else {
                panic!("a batch starts an animation of `EndPoint` without an animation instance");
            };
            host.set_animated_value(
                Self::id_of_end_point_property().base(),
                ExpressionVariant::create(self.end_point.get()),
                committed_at,
                animation,
            );
        } else if changed.contains(CompositionLinearGradientBrushChangedFields::END_POINT) {
            let value = reader.read::<RelativePoint>();
            self.set_end_point(host, value);
        }
        ServerCompositionLinearGradientBrushHooks::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 == "StartPoint" {
            return Some(Self::id_of_start_point_property().base());
        }
        if name == "EndPoint" {
            return Some(Self::id_of_end_point_property().base());
        }
        None
    }
}