ferroui-base 0.4.0

The base library of FerroUI: the property system, layout, input, styling, media, text, animation, data binding and the compositor
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
// @generated by scripts/generate_composition.py — do not edit

//! `CompositionLight`.
//!
//! Defined by the FerroUI overlay (scripts/composition-schema-winui.xml).
//!
//! Schema attributes: abstract.

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

bitflags! {
    /// The fields of `CompositionLight` that changed since its last serialization.
    #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
    pub struct CompositionLightChangedFields: u8 {
        const TARGETS = 1;
        const EXCLUSIONS_FROM_TARGETS = 2;
        const IS_ENABLED = 4;
    }
}

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

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

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

    fn validate_targets_change(&self, _old_value: &Option<Rc<dyn ICompositionObject>>, _new_value: &Option<Rc<dyn ICompositionObject>>) {}
    fn on_targets_changed(&self) {}
    fn on_targets_changing(&self) {}

    fn validate_exclusions_from_targets_change(&self, _old_value: &Option<Rc<dyn ICompositionObject>>, _new_value: &Option<Rc<dyn ICompositionObject>>) {}
    fn on_exclusions_from_targets_changed(&self) {}
    fn on_exclusions_from_targets_changing(&self) {}

    fn validate_is_enabled_change(&self, _old_value: bool, _new_value: bool) {}
    fn on_is_enabled_changed(&self) {}
    fn on_is_enabled_changing(&self) {}
}

/// The UI-thread properties of `CompositionLight`.
pub struct CompositionLightProps {
    changed_fields_of_composition_light: Cell<CompositionLightChangedFields>,
    targets: RefCell<Option<Rc<dyn ICompositionObject>>>,
    exclusions_from_targets: RefCell<Option<Rc<dyn ICompositionObject>>>,
    is_enabled: Cell<bool>,
}

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

impl CompositionLightProps {
    /// 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_light: Cell::new(CompositionLightChangedFields::empty()),
            targets: RefCell::new(None),
            exclusions_from_targets: RefCell::new(None),
            is_enabled: 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 CompositionLightHooks) {
        self.initialize_own_defaults(host);
    }

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

    /// `Targets` (internal, FerroUI overlay).
    pub fn targets(&self) -> Option<Rc<dyn ICompositionObject>> {
        self.targets.borrow().clone()
    }

    /// Sets `Targets` (internal, FerroUI overlay).
    pub fn set_targets(&self, host: &dyn CompositionLightHooks, value: Option<Rc<dyn ICompositionObject>>) {
        let mut changed = false;
        let old_value = self.targets.borrow().clone();
        if old_value.differs(&value) {
            CompositionLightHooks::validate_targets_change(host, &old_value, &value);
            CompositionLightHooks::on_targets_changing(host);
            changed = true;
            // Update the backing value
            *self.targets.borrow_mut() = value.clone();
            // Register object for serialization in the next batch
            self.changed_fields_of_composition_light.set(self.changed_fields_of_composition_light.get() | CompositionLightChangedFields::TARGETS);
            host.register_for_serialization();
        }
        *self.targets.borrow_mut() = value;
        if changed {
            CompositionLightHooks::on_targets_changed(host);
        }
    }

    /// `ExclusionsFromTargets` (internal, FerroUI overlay).
    pub fn exclusions_from_targets(&self) -> Option<Rc<dyn ICompositionObject>> {
        self.exclusions_from_targets.borrow().clone()
    }

    /// Sets `ExclusionsFromTargets` (internal, FerroUI overlay).
    pub fn set_exclusions_from_targets(&self, host: &dyn CompositionLightHooks, value: Option<Rc<dyn ICompositionObject>>) {
        let mut changed = false;
        let old_value = self.exclusions_from_targets.borrow().clone();
        if old_value.differs(&value) {
            CompositionLightHooks::validate_exclusions_from_targets_change(host, &old_value, &value);
            CompositionLightHooks::on_exclusions_from_targets_changing(host);
            changed = true;
            // Update the backing value
            *self.exclusions_from_targets.borrow_mut() = value.clone();
            // Register object for serialization in the next batch
            self.changed_fields_of_composition_light.set(self.changed_fields_of_composition_light.get() | CompositionLightChangedFields::EXCLUSIONS_FROM_TARGETS);
            host.register_for_serialization();
        }
        *self.exclusions_from_targets.borrow_mut() = value;
        if changed {
            CompositionLightHooks::on_exclusions_from_targets_changed(host);
        }
    }

    /// `IsEnabled` (default `true`, FerroUI overlay).
    pub fn is_enabled(&self) -> bool {
        self.is_enabled.get()
    }

    /// Sets `IsEnabled` (default `true`, FerroUI overlay).
    pub fn set_is_enabled(&self, host: &dyn CompositionLightHooks, value: bool) {
        let mut changed = false;
        let old_value = self.is_enabled.get();
        if old_value.differs(&value) {
            CompositionLightHooks::validate_is_enabled_change(host, old_value, value);
            CompositionLightHooks::on_is_enabled_changing(host);
            changed = true;
            // Update the backing value
            self.is_enabled.set(value);
            // Register object for serialization in the next batch
            self.changed_fields_of_composition_light.set(self.changed_fields_of_composition_light.get() | CompositionLightChangedFields::IS_ENABLED);
            host.register_for_serialization();
        }
        self.is_enabled.set(value);
        if changed {
            CompositionLightHooks::on_is_enabled_changed(host);
        }
    }

    /// 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 CompositionLightHooks, writer: &mut BatchStreamWriter<'_>) {
        let changed = self.changed_fields_of_composition_light.get();
        writer.write(changed);
        if changed.contains(CompositionLightChangedFields::TARGETS) {
            writer.write_server_object(self.targets.borrow().clone().map(|o| o.server()));
        }
        if changed.contains(CompositionLightChangedFields::EXCLUSIONS_FROM_TARGETS) {
            writer.write_server_object(self.exclusions_from_targets.borrow().clone().map(|o| o.server()));
        }
        if changed.contains(CompositionLightChangedFields::IS_ENABLED) {
            writer.write(self.is_enabled.get());
        }
        self.changed_fields_of_composition_light.set(CompositionLightChangedFields::empty());
    }
}

/// The partial methods of `ServerCompositionLight`: implemented by the class that embeds
/// [`ServerCompositionLightProps`]. Every hook defaults to doing nothing.
pub trait ServerCompositionLightHooks: IServerAnimatedPropertyHost {
    fn deserialize_changes_extra(&self, _reader: &mut BatchStreamReader<'_>) {}
    fn on_fields_deserialized(&self, _changed: CompositionLightChangedFields) {}

    fn on_targets_changed(&self) {}
    fn on_targets_changing(&self) {}

    fn on_exclusions_from_targets_changed(&self) {}
    fn on_exclusions_from_targets_changing(&self) {}

    fn on_is_enabled_changed(&self) {}
    fn on_is_enabled_changing(&self) {}
}

/// The server-side properties of `ServerCompositionLight`.
pub struct ServerCompositionLightProps {
    targets: RefCell<Option<Rc<dyn IServerObject>>>,
    exclusions_from_targets: RefCell<Option<Rc<dyn IServerObject>>>,
    is_enabled: Cell<bool>,
}

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

impl ServerCompositionLightProps {
    /// 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 {
            targets: RefCell::new(None),
            exclusions_from_targets: RefCell::new(None),
            is_enabled: 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 `Targets`.
    pub fn id_of_targets_property() -> &'static CompositionPropertyOf<Option<Rc<dyn IServerObject>>> {
        static PROPERTY: OnceLock<CompositionPropertyOf<Option<Rc<dyn IServerObject>>>> = OnceLock::new();
        PROPERTY.get_or_init(|| {
            CompositionProperty::register::<ServerCompositionLightProps, Option<Rc<dyn IServerObject>>>(
                "Targets",
                "ServerCompositionLight",
                |obj| props_of::<ServerCompositionLightProps>(obj).targets.borrow().clone(),
                |obj, v| *props_of::<ServerCompositionLightProps>(obj).targets.borrow_mut() = v,
                None,
                None,
            )
        })
    }

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

    pub fn set_targets(&self, host: &dyn ServerCompositionLightHooks, value: Option<Rc<dyn IServerObject>>) {
        let old_value = self.targets.borrow().clone();
        let mut changed = false;
        if old_value.differs(&value) {
            ServerCompositionLightHooks::on_targets_changing(host);
            changed = true;
        }
        host.set_value(
            Self::id_of_targets_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.targets.borrow_mut() = value.clone(),
            },
        );
        if changed {
            ServerCompositionLightHooks::on_targets_changed(host);
        }
    }

    /// The composition property of `ExclusionsFromTargets`.
    pub fn id_of_exclusions_from_targets_property() -> &'static CompositionPropertyOf<Option<Rc<dyn IServerObject>>> {
        static PROPERTY: OnceLock<CompositionPropertyOf<Option<Rc<dyn IServerObject>>>> = OnceLock::new();
        PROPERTY.get_or_init(|| {
            CompositionProperty::register::<ServerCompositionLightProps, Option<Rc<dyn IServerObject>>>(
                "ExclusionsFromTargets",
                "ServerCompositionLight",
                |obj| props_of::<ServerCompositionLightProps>(obj).exclusions_from_targets.borrow().clone(),
                |obj, v| *props_of::<ServerCompositionLightProps>(obj).exclusions_from_targets.borrow_mut() = v,
                None,
                None,
            )
        })
    }

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

    pub fn set_exclusions_from_targets(&self, host: &dyn ServerCompositionLightHooks, value: Option<Rc<dyn IServerObject>>) {
        let old_value = self.exclusions_from_targets.borrow().clone();
        let mut changed = false;
        if old_value.differs(&value) {
            ServerCompositionLightHooks::on_exclusions_from_targets_changing(host);
            changed = true;
        }
        host.set_value(
            Self::id_of_exclusions_from_targets_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.exclusions_from_targets.borrow_mut() = value.clone(),
            },
        );
        if changed {
            ServerCompositionLightHooks::on_exclusions_from_targets_changed(host);
        }
    }

    /// The composition property of `IsEnabled`.
    pub fn id_of_is_enabled_property() -> &'static CompositionPropertyOf<bool> {
        static PROPERTY: OnceLock<CompositionPropertyOf<bool>> = OnceLock::new();
        PROPERTY.get_or_init(|| {
            CompositionProperty::register::<ServerCompositionLightProps, bool>(
                "IsEnabled",
                "ServerCompositionLight",
                |obj| props_of::<ServerCompositionLightProps>(obj).is_enabled.get(),
                |obj, v| props_of::<ServerCompositionLightProps>(obj).is_enabled.set(v),
                Some(|obj| ExpressionVariant::create(props_of::<ServerCompositionLightProps>(obj).is_enabled.get())),
                None,
            )
        })
    }

    /// `IsEnabled`.
    pub fn is_enabled(&self) -> bool {
        self.is_enabled.get()
    }

    pub fn set_is_enabled(&self, host: &dyn ServerCompositionLightHooks, value: bool) {
        let old_value = self.is_enabled.get();
        let mut changed = false;
        if old_value.differs(&value) {
            ServerCompositionLightHooks::on_is_enabled_changing(host);
            changed = true;
        }
        host.set_value(
            Self::id_of_is_enabled_property().base(),
            ServerValueChange {
                equal: old_value.default_equals(&value),
                old_resource: None,
                new_resource: None,
                assign: &mut || self.is_enabled.set(value),
            },
        );
        if changed {
            ServerCompositionLightHooks::on_is_enabled_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 ServerCompositionLightHooks,
        reader: &mut BatchStreamReader<'_>,
        _committed_at: Duration,
    ) {
        let compositor = host.server_compositor();
        ServerCompositionLightHooks::deserialize_changes_extra(host, reader);
        let changed = reader.read::<CompositionLightChangedFields>();
        if changed.contains(CompositionLightChangedFields::TARGETS) {
            let value = read_server_object(reader, compositor.as_ref());
            self.set_targets(host, value);
        }
        if changed.contains(CompositionLightChangedFields::EXCLUSIONS_FROM_TARGETS) {
            let value = read_server_object(reader, compositor.as_ref());
            self.set_exclusions_from_targets(host, value);
        }
        if changed.contains(CompositionLightChangedFields::IS_ENABLED) {
            let value = reader.read::<bool>();
            self.set_is_enabled(host, value);
        }
        ServerCompositionLightHooks::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 == "IsEnabled" {
            return Some(Self::id_of_is_enabled_property().base());
        }
        None
    }
}