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

//! `CompositionConicGradientBrush`.

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 `CompositionConicGradientBrush` that changed since its last serialization.
    #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
    pub struct CompositionConicGradientBrushChangedFields: u8 {
        const ANGLE = 1;
        const ANGLE_ANIMATED = 2;
        const CENTER = 4;
        const CENTER_ANIMATED = 8;
    }
}

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

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

/// The partial methods of `CompositionConicGradientBrush`: implemented by the class that embeds
/// [`CompositionConicGradientBrushProps`]. Every hook defaults to doing nothing.
pub trait CompositionConicGradientBrushHooks: 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_angle_change(&self, _old_value: f64, _new_value: f64) {}
    fn on_angle_changed(&self) {}
    fn on_angle_changing(&self) {}

    fn validate_center_change(&self, _old_value: RelativePoint, _new_value: RelativePoint) {}
    fn on_center_changed(&self) {}
    fn on_center_changing(&self) {}
}

/// The UI-thread properties of `CompositionConicGradientBrush`.
pub struct CompositionConicGradientBrushProps {
    changed_fields_of_composition_conic_gradient_brush: Cell<CompositionConicGradientBrushChangedFields>,
    angle: Cell<f64>,
    center: Cell<RelativePoint>,
}

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

impl CompositionConicGradientBrushProps {
    /// 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_conic_gradient_brush: Cell::new(CompositionConicGradientBrushChangedFields::empty()),
            angle: Cell::new(Default::default()),
            center: 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 CompositionConicGradientBrushHooks) {
        self.initialize_own_defaults(host);
    }

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

    /// `Angle` (animated).
    pub fn angle(&self) -> f64 {
        self.angle.get()
    }

    /// Sets `Angle` (animated).
    pub fn set_angle(&self, host: &dyn CompositionConicGradientBrushHooks, value: f64) {
        let mut changed = false;
        let old_value = self.angle.get();
        if old_value.differs(&value) {
            CompositionConicGradientBrushHooks::validate_angle_change(host, old_value, value);
            CompositionConicGradientBrushHooks::on_angle_changing(host);
            changed = true;
            // Update the backing value
            self.angle.set(value);
            // Register object for serialization in the next batch
            self.changed_fields_of_composition_conic_gradient_brush.set(self.changed_fields_of_composition_conic_gradient_brush.get() | CompositionConicGradientBrushChangedFields::ANGLE);
            host.register_for_serialization();
            // Reset previous animation if any
            host.pending_animations().remove(ServerCompositionConicGradientBrushProps::id_of_angle_property().base());
            self.changed_fields_of_composition_conic_gradient_brush.set(self.changed_fields_of_composition_conic_gradient_brush.get() & !CompositionConicGradientBrushChangedFields::ANGLE_ANIMATED);
            // Check for implicit animations
            if let Some(animation) = host.implicit_animation("Angle") {
                // Animation affects only current property
                if let Some(a) = animation.as_composition_animation() {
                    self.changed_fields_of_composition_conic_gradient_brush.set(self.changed_fields_of_composition_conic_gradient_brush.get() | CompositionConicGradientBrushChangedFields::ANGLE_ANIMATED);
                    host.pending_animations().set(
                        ServerCompositionConicGradientBrushProps::id_of_angle_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, "Angle", ExpressionVariant::create(value));
            }
        }
        self.angle.set(value);
        if changed {
            CompositionConicGradientBrushHooks::on_angle_changed(host);
        }
    }

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

    /// Sets `Center` (animated).
    pub fn set_center(&self, host: &dyn CompositionConicGradientBrushHooks, value: RelativePoint) {
        let mut changed = false;
        let old_value = self.center.get();
        if old_value.differs(&value) {
            CompositionConicGradientBrushHooks::validate_center_change(host, old_value, value);
            CompositionConicGradientBrushHooks::on_center_changing(host);
            changed = true;
            // Update the backing value
            self.center.set(value);
            // Register object for serialization in the next batch
            self.changed_fields_of_composition_conic_gradient_brush.set(self.changed_fields_of_composition_conic_gradient_brush.get() | CompositionConicGradientBrushChangedFields::CENTER);
            host.register_for_serialization();
            // Reset previous animation if any
            host.pending_animations().remove(ServerCompositionConicGradientBrushProps::id_of_center_property().base());
            self.changed_fields_of_composition_conic_gradient_brush.set(self.changed_fields_of_composition_conic_gradient_brush.get() & !CompositionConicGradientBrushChangedFields::CENTER_ANIMATED);
            // Check for implicit animations
            if let Some(animation) = host.implicit_animation("Center") {
                // Animation affects only current property
                if let Some(a) = animation.as_composition_animation() {
                    self.changed_fields_of_composition_conic_gradient_brush.set(self.changed_fields_of_composition_conic_gradient_brush.get() | CompositionConicGradientBrushChangedFields::CENTER_ANIMATED);
                    host.pending_animations().set(
                        ServerCompositionConicGradientBrushProps::id_of_center_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, "Center", ExpressionVariant::create(value));
            }
        }
        self.center.set(value);
        if changed {
            CompositionConicGradientBrushHooks::on_center_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 CompositionConicGradientBrushHooks,
        property_name: &str,
        animation: &dyn ICompositionAnimation,
        final_value: Option<ExpressionVariant>,
    ) -> bool {
        if property_name == "Angle" {
            let server = animation.create_instance(host.server(), final_value);
            host.pending_animations().set(ServerCompositionConicGradientBrushProps::id_of_angle_property().base(), server);
            self.changed_fields_of_composition_conic_gradient_brush.set(self.changed_fields_of_composition_conic_gradient_brush.get() | CompositionConicGradientBrushChangedFields::ANGLE_ANIMATED);
            host.register_for_serialization();
            return true;
        }
        if property_name == "Center" {
            let server = animation.create_instance(host.server(), final_value);
            host.pending_animations().set(ServerCompositionConicGradientBrushProps::id_of_center_property().base(), server);
            self.changed_fields_of_composition_conic_gradient_brush.set(self.changed_fields_of_composition_conic_gradient_brush.get() | CompositionConicGradientBrushChangedFields::CENTER_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 CompositionConicGradientBrushHooks, writer: &mut BatchStreamWriter<'_>) {
        CompositionConicGradientBrushHooks::base_serialize_changes_core(host, writer);
        let changed = self.changed_fields_of_composition_conic_gradient_brush.get();
        writer.write(changed);
        if changed.contains(CompositionConicGradientBrushChangedFields::ANGLE_ANIMATED) {
            writer.write_value(host.pending_animations().get_and_remove(ServerCompositionConicGradientBrushProps::id_of_angle_property().base()));
        } else if changed.contains(CompositionConicGradientBrushChangedFields::ANGLE) {
            writer.write(self.angle.get());
        }
        if changed.contains(CompositionConicGradientBrushChangedFields::CENTER_ANIMATED) {
            writer.write_value(host.pending_animations().get_and_remove(ServerCompositionConicGradientBrushProps::id_of_center_property().base()));
        } else if changed.contains(CompositionConicGradientBrushChangedFields::CENTER) {
            writer.write(self.center.get());
        }
        self.changed_fields_of_composition_conic_gradient_brush.set(CompositionConicGradientBrushChangedFields::empty());
    }
}

/// The partial methods of `ServerCompositionConicGradientBrush`: implemented by the class that embeds
/// [`ServerCompositionConicGradientBrushProps`]. Every hook defaults to doing nothing.
pub trait ServerCompositionConicGradientBrushHooks: 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: CompositionConicGradientBrushChangedFields) {}
}

/// The server-side properties of `ServerCompositionConicGradientBrush`.
pub struct ServerCompositionConicGradientBrushProps {
    angle: Cell<f64>,
    center: Cell<RelativePoint>,
}

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

impl ServerCompositionConicGradientBrushProps {
    /// 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 {
            angle: Cell::new(Default::default()),
            center: 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 `Angle`.
    pub fn id_of_angle_property() -> &'static CompositionPropertyOf<f64> {
        static PROPERTY: OnceLock<CompositionPropertyOf<f64>> = OnceLock::new();
        PROPERTY.get_or_init(|| {
            CompositionProperty::register::<ServerCompositionConicGradientBrushProps, f64>(
                "Angle",
                "ServerCompositionConicGradientBrush",
                |obj| props_of::<ServerCompositionConicGradientBrushProps>(obj).angle.get(),
                |obj, v| props_of::<ServerCompositionConicGradientBrushProps>(obj).angle.set(v),
                Some(|obj| ExpressionVariant::create(props_of::<ServerCompositionConicGradientBrushProps>(obj).angle.get())),
                Some(|obj, v: ExpressionVariant| props_of::<ServerCompositionConicGradientBrushProps>(obj).angle.set(v.cast_or_default::<f64>())),
            )
        })
    }

    /// `Angle` (animated).
    pub fn angle(&self) -> f64 {
        self.angle.get()
    }

    pub fn set_angle(&self, host: &dyn ServerCompositionConicGradientBrushHooks, value: f64) {
        self.angle.set(value);
        host.remove_animation_for_property(Self::id_of_angle_property().base());
    }

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

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

    pub fn set_center(&self, host: &dyn ServerCompositionConicGradientBrushHooks, value: RelativePoint) {
        self.center.set(value);
        host.remove_animation_for_property(Self::id_of_center_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 ServerCompositionConicGradientBrushHooks,
        reader: &mut BatchStreamReader<'_>,
        committed_at: Duration,
    ) {
        ServerCompositionConicGradientBrushHooks::base_deserialize_changes_core(host, reader, committed_at);
        ServerCompositionConicGradientBrushHooks::deserialize_changes_extra(host, reader);
        let changed = reader.read::<CompositionConicGradientBrushChangedFields>();
        if changed.contains(CompositionConicGradientBrushChangedFields::ANGLE_ANIMATED) {
            let Some(animation) = reader.read_value::<AnimationInstanceFactory>().map(AnimationInstanceFactory::create) else {
                panic!("a batch starts an animation of `Angle` without an animation instance");
            };
            host.set_animated_value(
                Self::id_of_angle_property().base(),
                ExpressionVariant::create(self.angle.get()),
                committed_at,
                animation,
            );
        } else if changed.contains(CompositionConicGradientBrushChangedFields::ANGLE) {
            let value = reader.read::<f64>();
            self.set_angle(host, value);
        }
        if changed.contains(CompositionConicGradientBrushChangedFields::CENTER_ANIMATED) {
            let Some(animation) = reader.read_value::<AnimationInstanceFactory>().map(AnimationInstanceFactory::create) else {
                panic!("a batch starts an animation of `Center` without an animation instance");
            };
            host.set_animated_value(
                Self::id_of_center_property().base(),
                ExpressionVariant::create(self.center.get()),
                committed_at,
                animation,
            );
        } else if changed.contains(CompositionConicGradientBrushChangedFields::CENTER) {
            let value = reader.read::<RelativePoint>();
            self.set_center(host, value);
        }
        ServerCompositionConicGradientBrushHooks::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 == "Angle" {
            return Some(Self::id_of_angle_property().base());
        }
        if name == "Center" {
            return Some(Self::id_of_center_property().base());
        }
        None
    }
}