use bitflags::bitflags;
use crate::rendering::composition::CompositionPath;
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::ServerValueChange;
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::cell::RefCell;
use std::sync::Arc;
use std::sync::OnceLock;
use std::time::Duration;
use super::CompositionGeometryHooks;
use super::CompositionGeometryProps;
use super::ServerCompositionGeometryHooks;
use super::ServerCompositionGeometryProps;
bitflags! {
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct CompositionPathGeometryChangedFields: u8 {
const PATH = 1;
}
}
impl BatchValue for CompositionPathGeometryChangedFields {
#[inline]
fn write_to(&self, out: &mut Vec<u8>) {
self.bits().write_to(out);
}
#[inline]
fn read_from(input: &mut BatchValueReader<'_>) -> Self {
CompositionPathGeometryChangedFields::from_bits_retain(u8::read_from(input))
}
}
pub trait CompositionPathGeometryHooks: CompositionGeometryHooks {
fn initialize_defaults_extra(&self) {}
fn validate_path_change(&self, _old_value: &Option<Arc<CompositionPath>>, _new_value: &Option<Arc<CompositionPath>>) {}
fn on_path_changed(&self) {}
fn on_path_changing(&self) {}
}
pub struct CompositionPathGeometryProps {
base: CompositionGeometryProps,
changed_fields_of_composition_path_geometry: Cell<CompositionPathGeometryChangedFields>,
path: RefCell<Option<Arc<CompositionPath>>>,
}
impl Default for CompositionPathGeometryProps {
fn default() -> Self {
Self::new()
}
}
impl CompositionPathGeometryProps {
pub fn new() -> Self {
Self {
base: CompositionGeometryProps::new(),
changed_fields_of_composition_path_geometry: Cell::new(CompositionPathGeometryChangedFields::empty()),
path: RefCell::new(None),
}
}
pub fn base(&self) -> &CompositionGeometryProps {
&self.base
}
pub fn initialize_defaults(&self, host: &dyn CompositionPathGeometryHooks) {
self.base.initialize_defaults(host);
self.initialize_own_defaults(host);
}
pub fn initialize_own_defaults(&self, host: &dyn CompositionPathGeometryHooks) {
CompositionPathGeometryHooks::initialize_defaults_extra(host);
}
pub fn path(&self) -> Option<Arc<CompositionPath>> {
self.path.borrow().clone()
}
pub fn set_path(&self, host: &dyn CompositionPathGeometryHooks, value: Option<Arc<CompositionPath>>) {
let mut changed = false;
let old_value = self.path.borrow().clone();
if old_value.differs(&value) {
CompositionPathGeometryHooks::validate_path_change(host, &old_value, &value);
CompositionPathGeometryHooks::on_path_changing(host);
changed = true;
*self.path.borrow_mut() = value.clone();
self.changed_fields_of_composition_path_geometry.set(self.changed_fields_of_composition_path_geometry.get() | CompositionPathGeometryChangedFields::PATH);
host.register_for_serialization();
}
*self.path.borrow_mut() = value;
if changed {
CompositionPathGeometryHooks::on_path_changed(host);
}
}
pub fn start_animation(
&self,
host: &dyn CompositionPathGeometryHooks,
property_name: &str,
animation: &dyn ICompositionAnimation,
final_value: Option<ExpressionVariant>,
) -> bool {
self.base.start_animation(host, property_name, animation, final_value)
}
pub fn serialize_changes_core(&self, host: &dyn CompositionPathGeometryHooks, writer: &mut BatchStreamWriter<'_>) {
self.base.serialize_changes_core(host, writer);
let changed = self.changed_fields_of_composition_path_geometry.get();
writer.write(changed);
if changed.contains(CompositionPathGeometryChangedFields::PATH) {
writer.write_value(self.path.borrow().clone());
}
self.changed_fields_of_composition_path_geometry.set(CompositionPathGeometryChangedFields::empty());
}
}
pub trait ServerCompositionPathGeometryHooks: ServerCompositionGeometryHooks {
fn deserialize_changes_extra(&self, _reader: &mut BatchStreamReader<'_>) {}
fn on_fields_deserialized(&self, _changed: CompositionPathGeometryChangedFields) {}
fn on_path_changed(&self) {}
fn on_path_changing(&self) {}
}
pub struct ServerCompositionPathGeometryProps {
base: ServerCompositionGeometryProps,
path: RefCell<Option<Arc<CompositionPath>>>,
}
impl Default for ServerCompositionPathGeometryProps {
fn default() -> Self {
Self::new()
}
}
impl ServerCompositionPathGeometryProps {
pub fn new() -> Self {
Self {
base: ServerCompositionGeometryProps::new(),
path: RefCell::new(None),
}
}
pub fn base(&self) -> &ServerCompositionGeometryProps {
&self.base
}
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)
}
pub fn id_of_path_property() -> &'static CompositionPropertyOf<Option<Arc<CompositionPath>>> {
static PROPERTY: OnceLock<CompositionPropertyOf<Option<Arc<CompositionPath>>>> = OnceLock::new();
PROPERTY.get_or_init(|| {
CompositionProperty::register::<ServerCompositionPathGeometryProps, Option<Arc<CompositionPath>>>(
"Path",
"ServerCompositionPathGeometry",
|obj| props_of::<ServerCompositionPathGeometryProps>(obj).path.borrow().clone(),
|obj, v| *props_of::<ServerCompositionPathGeometryProps>(obj).path.borrow_mut() = v,
None,
None,
)
})
}
pub fn path(&self) -> Option<Arc<CompositionPath>> {
self.path.borrow().clone()
}
pub fn set_path(&self, host: &dyn ServerCompositionPathGeometryHooks, value: Option<Arc<CompositionPath>>) {
let old_value = self.path.borrow().clone();
let mut changed = false;
if old_value.differs(&value) {
ServerCompositionPathGeometryHooks::on_path_changing(host);
changed = true;
}
host.set_value(
Self::id_of_path_property().base(),
ServerValueChange {
equal: old_value.default_equals(&value),
old_resource: None,
new_resource: None,
assign: &mut || *self.path.borrow_mut() = value.clone(),
},
);
if changed {
ServerCompositionPathGeometryHooks::on_path_changed(host);
}
}
pub fn deserialize_changes_core(
&self,
host: &dyn ServerCompositionPathGeometryHooks,
reader: &mut BatchStreamReader<'_>,
committed_at: Duration,
) {
self.base.deserialize_changes_core(host, reader, committed_at);
ServerCompositionPathGeometryHooks::deserialize_changes_extra(host, reader);
let changed = reader.read::<CompositionPathGeometryChangedFields>();
if changed.contains(CompositionPathGeometryChangedFields::PATH) {
let value = reader.read_value::<Arc<CompositionPath>>();
self.set_path(host, value);
}
ServerCompositionPathGeometryHooks::on_fields_deserialized(host, changed);
}
pub fn get_composition_property(name: &str) -> Option<&'static CompositionProperty> {
ServerCompositionGeometryProps::get_composition_property(name)
}
}