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

//! `CompositionSurfaceVisual`.

use bitflags::bitflags;
use crate::rendering::composition::ICompositionObject;
use crate::rendering::composition::animations::ICompositionAnimation;
use crate::rendering::composition::expressions::ExpressionVariant;
use crate::rendering::composition::server::AsServerRenderResource;
use crate::rendering::composition::server::CompositionProperty;
use crate::rendering::composition::server::CompositionPropertyOf;
use crate::rendering::composition::server::IServerAnimatedPropertyHost;
use crate::rendering::composition::server::IServerObject;
use crate::rendering::composition::server::ServerPropertyValue;
use crate::rendering::composition::server::ServerValueChange;
use crate::rendering::composition::server::props_of;
use crate::rendering::composition::server::read_server_object;
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::rc::Rc;
use std::sync::OnceLock;
use std::time::Duration;
use super::CompositionContainerVisualHooks;
use super::CompositionContainerVisualProps;

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

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

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

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

    fn validate_surface_change(&self, _old_value: &Option<Rc<dyn ICompositionObject>>, _new_value: &Option<Rc<dyn ICompositionObject>>) {}
    fn on_surface_changed(&self) {}
    fn on_surface_changing(&self) {}
}

/// The UI-thread properties of `CompositionSurfaceVisual`.
pub struct CompositionSurfaceVisualProps {
    base: CompositionContainerVisualProps,
    changed_fields_of_composition_surface_visual: Cell<CompositionSurfaceVisualChangedFields>,
    surface: RefCell<Option<Rc<dyn ICompositionObject>>>,
}

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

impl CompositionSurfaceVisualProps {
    /// 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: CompositionContainerVisualProps::new(),
            changed_fields_of_composition_surface_visual: Cell::new(CompositionSurfaceVisualChangedFields::empty()),
            surface: RefCell::new(None),
        }
    }

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

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

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

    /// `Surface`.
    pub fn surface(&self) -> Option<Rc<dyn ICompositionObject>> {
        self.surface.borrow().clone()
    }

    /// Sets `Surface`.
    pub fn set_surface(&self, host: &dyn CompositionSurfaceVisualHooks, value: Option<Rc<dyn ICompositionObject>>) {
        let mut changed = false;
        let old_value = self.surface.borrow().clone();
        if old_value.differs(&value) {
            CompositionSurfaceVisualHooks::validate_surface_change(host, &old_value, &value);
            CompositionSurfaceVisualHooks::on_surface_changing(host);
            changed = true;
            // Update the backing value
            *self.surface.borrow_mut() = value.clone();
            // Register object for serialization in the next batch
            self.changed_fields_of_composition_surface_visual.set(self.changed_fields_of_composition_surface_visual.get() | CompositionSurfaceVisualChangedFields::SURFACE);
            host.register_for_serialization();
        }
        *self.surface.borrow_mut() = value;
        if changed {
            CompositionSurfaceVisualHooks::on_surface_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 CompositionSurfaceVisualHooks,
        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 CompositionSurfaceVisualHooks, writer: &mut BatchStreamWriter<'_>) {
        self.base.serialize_changes_core(host, writer);
        let changed = self.changed_fields_of_composition_surface_visual.get();
        writer.write(changed);
        if changed.contains(CompositionSurfaceVisualChangedFields::SURFACE) {
            writer.write_server_object(self.surface.borrow().clone().map(|o| o.server()));
        }
        self.changed_fields_of_composition_surface_visual.set(CompositionSurfaceVisualChangedFields::empty());
    }
}

/// The partial methods of `ServerCompositionSurfaceVisual`: implemented by the class that embeds
/// [`ServerCompositionSurfaceVisualProps`]. Every hook defaults to doing nothing.
pub trait ServerCompositionSurfaceVisualHooks: IServerAnimatedPropertyHost {
    /// `base.DeserializeChangesCore(reader, committedAt)`: reads the changes of
    /// the hand-written base class `ServerSizeDependantVisual`.
    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: CompositionSurfaceVisualChangedFields) {}

    fn on_surface_changed(&self) {}
    fn on_surface_changing(&self) {}
}

/// The server-side properties of `ServerCompositionSurfaceVisual`.
pub struct ServerCompositionSurfaceVisualProps {
    surface: RefCell<Option<Rc<dyn IServerObject>>>,
}

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

impl ServerCompositionSurfaceVisualProps {
    /// 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 {
            surface: RefCell::new(None),
        }
    }

    /// 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 `Surface`.
    pub fn id_of_surface_property() -> &'static CompositionPropertyOf<Option<Rc<dyn IServerObject>>> {
        static PROPERTY: OnceLock<CompositionPropertyOf<Option<Rc<dyn IServerObject>>>> = OnceLock::new();
        PROPERTY.get_or_init(|| {
            CompositionProperty::register::<ServerCompositionSurfaceVisualProps, Option<Rc<dyn IServerObject>>>(
                "Surface",
                "ServerCompositionSurfaceVisual",
                |obj| props_of::<ServerCompositionSurfaceVisualProps>(obj).surface.borrow().clone(),
                |obj, v| *props_of::<ServerCompositionSurfaceVisualProps>(obj).surface.borrow_mut() = v,
                None,
                None,
            )
        })
    }

    /// `Surface`.
    pub fn surface(&self) -> Option<Rc<dyn IServerObject>> {
        self.surface.borrow().clone()
    }

    pub fn set_surface(&self, host: &dyn ServerCompositionSurfaceVisualHooks, value: Option<Rc<dyn IServerObject>>) {
        let old_value = self.surface.borrow().clone();
        let mut changed = false;
        if old_value.differs(&value) {
            ServerCompositionSurfaceVisualHooks::on_surface_changing(host);
            changed = true;
        }
        host.set_value(
            Self::id_of_surface_property().base(),
            ServerValueChange {
                equal: old_value.default_equals(&value),
                old_resource: old_value.as_server_render_resource(),
                new_resource: value.as_server_render_resource(),
                assign: &mut || *self.surface.borrow_mut() = value.clone(),
            },
        );
        if changed {
            ServerCompositionSurfaceVisualHooks::on_surface_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 ServerCompositionSurfaceVisualHooks,
        reader: &mut BatchStreamReader<'_>,
        committed_at: Duration,
    ) {
        let compositor = host.server_compositor();
        ServerCompositionSurfaceVisualHooks::base_deserialize_changes_core(host, reader, committed_at);
        ServerCompositionSurfaceVisualHooks::deserialize_changes_extra(host, reader);
        let changed = reader.read::<CompositionSurfaceVisualChangedFields>();
        if changed.contains(CompositionSurfaceVisualChangedFields::SURFACE) {
            let value = read_server_object(reader, compositor.as_ref());
            self.set_surface(host, value);
        }
        ServerCompositionSurfaceVisualHooks::on_fields_deserialized(host, changed);
    }
}