use bitflags::bitflags;
use crate::RelativePoint;
use crate::rendering::composition::expressions::ExpressionVariant;
use crate::rendering::composition::server::CompositionProperty;
use crate::rendering::composition::server::CompositionPropertyOf;
use crate::rendering::composition::server::IServerPropertyHost;
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::sync::OnceLock;
use std::time::Duration;
bitflags! {
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct CompositionSimpleLinearGradientBrushChangedFields: u8 {
const START_POINT = 1;
const END_POINT = 2;
}
}
impl BatchValue for CompositionSimpleLinearGradientBrushChangedFields {
#[inline]
fn write_to(&self, out: &mut Vec<u8>) {
self.bits().write_to(out);
}
#[inline]
fn read_from(input: &mut BatchValueReader<'_>) -> Self {
CompositionSimpleLinearGradientBrushChangedFields::from_bits_retain(u8::read_from(input))
}
}
pub trait ServerCompositionSimpleLinearGradientBrushHooks: IServerPropertyHost {
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: CompositionSimpleLinearGradientBrushChangedFields) {}
fn on_start_point_changed(&self) {}
fn on_start_point_changing(&self) {}
fn on_end_point_changed(&self) {}
fn on_end_point_changing(&self) {}
}
pub struct ServerCompositionSimpleLinearGradientBrushProps {
start_point: Cell<RelativePoint>,
end_point: Cell<RelativePoint>,
}
impl Default for ServerCompositionSimpleLinearGradientBrushProps {
fn default() -> Self {
Self::new()
}
}
impl ServerCompositionSimpleLinearGradientBrushProps {
pub fn new() -> Self {
Self {
start_point: Cell::new(Default::default()),
end_point: Cell::new(Default::default()),
}
}
pub fn find_props(&self, type_id: TypeId) -> Option<&dyn Any> {
if type_id == TypeId::of::<Self>() {
return Some(self);
}
None
}
pub fn id_of_start_point_property() -> &'static CompositionPropertyOf<RelativePoint> {
static PROPERTY: OnceLock<CompositionPropertyOf<RelativePoint>> = OnceLock::new();
PROPERTY.get_or_init(|| {
CompositionProperty::register::<ServerCompositionSimpleLinearGradientBrushProps, RelativePoint>(
"StartPoint",
"ServerCompositionSimpleLinearGradientBrush",
|obj| props_of::<ServerCompositionSimpleLinearGradientBrushProps>(obj).start_point.get(),
|obj, v| props_of::<ServerCompositionSimpleLinearGradientBrushProps>(obj).start_point.set(v),
Some(|obj| ExpressionVariant::create(props_of::<ServerCompositionSimpleLinearGradientBrushProps>(obj).start_point.get())),
None,
)
})
}
pub fn start_point(&self) -> RelativePoint {
self.start_point.get()
}
pub fn set_start_point(&self, host: &dyn ServerCompositionSimpleLinearGradientBrushHooks, value: RelativePoint) {
let old_value = self.start_point.get();
let mut changed = false;
if old_value.differs(&value) {
ServerCompositionSimpleLinearGradientBrushHooks::on_start_point_changing(host);
changed = true;
}
host.set_value(
Self::id_of_start_point_property().base(),
ServerValueChange {
equal: old_value.default_equals(&value),
old_resource: None,
new_resource: None,
assign: &mut || self.start_point.set(value),
},
);
if changed {
ServerCompositionSimpleLinearGradientBrushHooks::on_start_point_changed(host);
}
}
pub fn id_of_end_point_property() -> &'static CompositionPropertyOf<RelativePoint> {
static PROPERTY: OnceLock<CompositionPropertyOf<RelativePoint>> = OnceLock::new();
PROPERTY.get_or_init(|| {
CompositionProperty::register::<ServerCompositionSimpleLinearGradientBrushProps, RelativePoint>(
"EndPoint",
"ServerCompositionSimpleLinearGradientBrush",
|obj| props_of::<ServerCompositionSimpleLinearGradientBrushProps>(obj).end_point.get(),
|obj, v| props_of::<ServerCompositionSimpleLinearGradientBrushProps>(obj).end_point.set(v),
Some(|obj| ExpressionVariant::create(props_of::<ServerCompositionSimpleLinearGradientBrushProps>(obj).end_point.get())),
None,
)
})
}
pub fn end_point(&self) -> RelativePoint {
self.end_point.get()
}
pub fn set_end_point(&self, host: &dyn ServerCompositionSimpleLinearGradientBrushHooks, value: RelativePoint) {
let old_value = self.end_point.get();
let mut changed = false;
if old_value.differs(&value) {
ServerCompositionSimpleLinearGradientBrushHooks::on_end_point_changing(host);
changed = true;
}
host.set_value(
Self::id_of_end_point_property().base(),
ServerValueChange {
equal: old_value.default_equals(&value),
old_resource: None,
new_resource: None,
assign: &mut || self.end_point.set(value),
},
);
if changed {
ServerCompositionSimpleLinearGradientBrushHooks::on_end_point_changed(host);
}
}
pub fn deserialize_changes_core(
&self,
host: &dyn ServerCompositionSimpleLinearGradientBrushHooks,
reader: &mut BatchStreamReader<'_>,
committed_at: Duration,
) {
ServerCompositionSimpleLinearGradientBrushHooks::base_deserialize_changes_core(host, reader, committed_at);
ServerCompositionSimpleLinearGradientBrushHooks::deserialize_changes_extra(host, reader);
let changed = reader.read::<CompositionSimpleLinearGradientBrushChangedFields>();
if changed.contains(CompositionSimpleLinearGradientBrushChangedFields::START_POINT) {
let value = reader.read::<RelativePoint>();
self.set_start_point(host, value);
}
if changed.contains(CompositionSimpleLinearGradientBrushChangedFields::END_POINT) {
let value = reader.read::<RelativePoint>();
self.set_end_point(host, value);
}
ServerCompositionSimpleLinearGradientBrushHooks::on_fields_deserialized(host, changed);
}
pub fn serialize_all_changes(
writer: &mut BatchStreamWriter<'_>,
start_point: RelativePoint,
end_point: RelativePoint,
) {
writer.write(CompositionSimpleLinearGradientBrushChangedFields::START_POINT | CompositionSimpleLinearGradientBrushChangedFields::END_POINT);
writer.write(start_point);
writer.write(end_point);
}
}