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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
//! The WinUI geometries of the shapes: `CompositionGeometry` and its
//! subclasses `CompositionRectangleGeometry`,
//! `CompositionRoundedRectangleGeometry`, `CompositionEllipseGeometry`,
//! `CompositionLineGeometry` and `CompositionPathGeometry`, and the
//! `CompositionPath` a path geometry shows.
//!
//! As for the clips, every geometry is one [`CompositionGeometry`] behind an
//! `Rc`, which holds what the concrete class adds in a kind; the concrete
//! classes are typed handles around the same `Rc`. The server builds the
//! platform geometry from the members and caches it until a member changes.

use super::composition_object_impls::composition_object_impls;
use super::generated::{
    CompositionEllipseGeometryHooks, CompositionEllipseGeometryProps, CompositionGeometryHooks,
    CompositionGeometryProps, CompositionLineGeometryHooks, CompositionLineGeometryProps,
    CompositionPathGeometryHooks, CompositionPathGeometryProps, CompositionRectangleGeometryHooks,
    CompositionRectangleGeometryProps, CompositionRoundedRectangleGeometryHooks,
    CompositionRoundedRectangleGeometryProps, ServerCompositionEllipseGeometryProps,
    ServerCompositionLineGeometryProps, ServerCompositionPathGeometryProps, ServerCompositionRectangleGeometryProps,
    ServerCompositionRoundedRectangleGeometryProps,
};
use super::server::{
    IServerObject, ServerCompositionEllipseGeometry, ServerCompositionLineGeometry, ServerCompositionPathGeometry,
    ServerCompositionRectangleGeometry, ServerCompositionRoundedRectangleGeometry, ServerCompositor, ServerObjectId,
};
use super::{CompositionObject, Compositor};
use crate::media::Geometry;
use crate::platform::IGeometryImpl;
use crate::utilities::FormatError;
use crate::{Rect, Vector};
use std::ops::Deref;
use std::rc::{Rc, Weak};
use std::sync::Arc;

/// The path of a [`CompositionPathGeometry`] (WinUI
/// `Microsoft.UI.Composition.CompositionPath`): an immutable platform
/// geometry, made from a FerroUI [`Geometry`] (a `StreamGeometry`, a
/// `PathGeometry`, ...) or from a string of path data.
///
/// Deviation (winui-composition.md): WinUI makes a path from an
/// `IGeometrySource2D` (a Win2D geometry); here the source is a FerroUI
/// geometry, and the path keeps the platform geometry it had when the path
/// was made.
#[derive(Clone)]
pub struct CompositionPath {
    geometry: Arc<dyn IGeometryImpl>,
}

impl CompositionPath {
    /// A path over a platform geometry.
    pub fn new(geometry: Arc<dyn IGeometryImpl>) -> CompositionPath {
        CompositionPath { geometry }
    }

    /// The path of `geometry`, as it is now; `None` when the geometry has no
    /// platform geometry.
    pub fn from_geometry(geometry: &Geometry) -> Option<CompositionPath> {
        geometry.platform_impl().map(CompositionPath::new)
    }

    /// The path described by a string of path data (the mini-language of
    /// `Geometry::parse`).
    pub fn parse(data: &str) -> Result<CompositionPath, FormatError> {
        let geometry = Geometry::parse(data)?;
        let platform = geometry.platform_impl().expect("a parsed stream geometry has a platform geometry");
        Ok(CompositionPath::new(platform))
    }

    /// The platform geometry of the path.
    pub fn geometry(&self) -> &Arc<dyn IGeometryImpl> {
        &self.geometry
    }

    /// The bounds of the path.
    pub fn bounds(&self) -> Rect {
        self.geometry.bounds()
    }
}

impl std::fmt::Debug for CompositionPath {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("CompositionPath").field("bounds", &self.geometry.bounds()).finish()
    }
}

/// What the concrete class of a geometry adds to [`CompositionGeometry`].
pub(crate) enum CompositionGeometryKind {
    Rectangle(CompositionRectangleGeometryProps),
    RoundedRectangle(CompositionRoundedRectangleGeometryProps),
    Ellipse(CompositionEllipseGeometryProps),
    Line(CompositionLineGeometryProps),
    Path(CompositionPathGeometryProps),
}

/// The geometry of a sprite shape or a geometric clip (WinUI
/// `Microsoft.UI.Composition.CompositionGeometry`), with the animatable
/// trimming of its outline: `TrimStart` and `TrimEnd` (fractions of the
/// length of the outline, default 0 and 1) and `TrimOffset` (added to both,
/// wrapping around the outline).
pub struct CompositionGeometry {
    this: Weak<CompositionGeometry>,
    object: CompositionObject,
    kind: CompositionGeometryKind,
}

impl CompositionGeometry {
    fn create(
        compositor: &Rc<Compositor>,
        kind: CompositionGeometryKind,
        server: impl FnOnce(&Rc<ServerCompositor>) -> Rc<dyn IServerObject> + Send + 'static,
    ) -> Rc<CompositionGeometry> {
        let server = compositor.create_server_object(move |compositor, _| server(compositor));
        let geometry = Rc::new_cyclic(|this: &Weak<CompositionGeometry>| CompositionGeometry {
            this: this.clone(),
            object: CompositionObject::new(compositor, Some(server)),
            kind,
        });
        match &geometry.kind {
            CompositionGeometryKind::Rectangle(props) => props.initialize_defaults(&*geometry),
            CompositionGeometryKind::RoundedRectangle(props) => props.initialize_defaults(&*geometry),
            CompositionGeometryKind::Ellipse(props) => props.initialize_defaults(&*geometry),
            CompositionGeometryKind::Line(props) => props.initialize_defaults(&*geometry),
            CompositionGeometryKind::Path(props) => props.initialize_defaults(&*geometry),
        }
        geometry
    }

    /// The generated properties of `CompositionGeometry`.
    pub fn geometry_props(&self) -> &CompositionGeometryProps {
        match &self.kind {
            CompositionGeometryKind::Rectangle(props) => props.base(),
            CompositionGeometryKind::RoundedRectangle(props) => props.base(),
            CompositionGeometryKind::Ellipse(props) => props.base(),
            CompositionGeometryKind::Line(props) => props.base(),
            CompositionGeometryKind::Path(props) => props.base(),
        }
    }

    pub fn compositor(&self) -> &Rc<Compositor> {
        self.object.compositor()
    }

    /// The embedded `CompositionObject`.
    pub fn object(&self) -> &CompositionObject {
        &self.object
    }

    /// The id of the server-side geometry.
    pub fn server(&self) -> ServerObjectId {
        self.object.required_server()
    }

    pub fn is_disposed(&self) -> bool {
        self.object.is_disposed()
    }

    pub fn dispose(&self) {
        self.object.dispose();
    }

    /// The name of the concrete class, for messages.
    pub fn type_name(&self) -> &'static str {
        match &self.kind {
            CompositionGeometryKind::Rectangle(_) => "CompositionRectangleGeometry",
            CompositionGeometryKind::RoundedRectangle(_) => "CompositionRoundedRectangleGeometry",
            CompositionGeometryKind::Ellipse(_) => "CompositionEllipseGeometry",
            CompositionGeometryKind::Line(_) => "CompositionLineGeometry",
            CompositionGeometryKind::Path(_) => "CompositionPathGeometry",
        }
    }

    /// Where the drawn part of the outline ends, as a fraction of its length
    /// (default 1).
    pub fn trim_end(&self) -> f32 {
        self.geometry_props().trim_end()
    }

    pub fn set_trim_end(&self, value: f32) {
        self.geometry_props().set_trim_end(self, value)
    }

    /// How far along the outline both trim points are moved, as a fraction
    /// of its length; the drawn part wraps around the start of the outline.
    pub fn trim_offset(&self) -> f32 {
        self.geometry_props().trim_offset()
    }

    pub fn set_trim_offset(&self, value: f32) {
        self.geometry_props().set_trim_offset(self, value)
    }

    /// Where the drawn part of the outline starts, as a fraction of its
    /// length (default 0).
    pub fn trim_start(&self) -> f32 {
        self.geometry_props().trim_start()
    }

    pub fn set_trim_start(&self, value: f32) {
        self.geometry_props().set_trim_start(self, value)
    }
}

impl CompositionGeometryHooks for CompositionGeometry {}

impl CompositionRectangleGeometryHooks for CompositionGeometry {}

impl CompositionRoundedRectangleGeometryHooks for CompositionGeometry {}

impl CompositionEllipseGeometryHooks for CompositionGeometry {}

impl CompositionLineGeometryHooks for CompositionGeometry {}

impl CompositionPathGeometryHooks for CompositionGeometry {}

composition_object_impls!(
    CompositionGeometry, "CompositionGeometry",
    serialize: |this, writer| match &this.kind {
        CompositionGeometryKind::Rectangle(props) => props.serialize_changes_core(this, writer),
        CompositionGeometryKind::RoundedRectangle(props) => props.serialize_changes_core(this, writer),
        CompositionGeometryKind::Ellipse(props) => props.serialize_changes_core(this, writer),
        CompositionGeometryKind::Line(props) => props.serialize_changes_core(this, writer),
        CompositionGeometryKind::Path(props) => props.serialize_changes_core(this, writer),
    },
    start_animation: |this, name, animation, final_value| match &this.kind {
        CompositionGeometryKind::Rectangle(props) => props.start_animation(this, name, animation, final_value),
        CompositionGeometryKind::RoundedRectangle(props) => props.start_animation(this, name, animation, final_value),
        CompositionGeometryKind::Ellipse(props) => props.start_animation(this, name, animation, final_value),
        CompositionGeometryKind::Line(props) => props.start_animation(this, name, animation, final_value),
        CompositionGeometryKind::Path(props) => props.start_animation(this, name, animation, final_value),
    },
    composition_property: |this, name| match &this.kind {
        CompositionGeometryKind::Rectangle(_) => ServerCompositionRectangleGeometryProps::get_composition_property(name),
        CompositionGeometryKind::RoundedRectangle(_) => {
            ServerCompositionRoundedRectangleGeometryProps::get_composition_property(name)
        }
        CompositionGeometryKind::Ellipse(_) => ServerCompositionEllipseGeometryProps::get_composition_property(name),
        CompositionGeometryKind::Line(_) => ServerCompositionLineGeometryProps::get_composition_property(name),
        CompositionGeometryKind::Path(_) => ServerCompositionPathGeometryProps::get_composition_property(name),
    },
);

/// Declares the typed handle of a geometry class: the struct, `Deref` to
/// the shared geometry, `from_geometry` and the accessor of its properties.
macro_rules! geometry_handle {
    ($(#[$meta:meta])* $name:ident, $variant:ident, $props:ident, $what:literal) => {
        $(#[$meta])*
        #[derive(Clone)]
        pub struct $name(Rc<CompositionGeometry>);

        impl Deref for $name {
            type Target = Rc<CompositionGeometry>;

            fn deref(&self) -> &Rc<CompositionGeometry> {
                &self.0
            }
        }

        impl $name {
            #[doc = concat!("The handle of `geometry` as ", $what, ", if it is one.")]
            pub fn from_geometry(geometry: &Rc<CompositionGeometry>) -> Option<$name> {
                matches!(geometry.kind, CompositionGeometryKind::$variant(_)).then(|| $name(geometry.clone()))
            }

            /// The geometry as a [`CompositionGeometry`].
            pub fn geometry(&self) -> &Rc<CompositionGeometry> {
                &self.0
            }

            fn props(&self) -> &$props {
                match &self.0.kind {
                    CompositionGeometryKind::$variant(props) => props,
                    _ => unreachable!(concat!("the handle wraps ", $what)),
                }
            }
        }
    };
}

geometry_handle!(
    /// A rectangle (WinUI `Microsoft.UI.Composition.CompositionRectangleGeometry`):
    /// `(offset, size)`, both animatable (`Offset`, `Size`).
    CompositionRectangleGeometry,
    Rectangle,
    CompositionRectangleGeometryProps,
    "a rectangle geometry"
);

impl CompositionRectangleGeometry {
    pub(crate) fn new(compositor: &Rc<Compositor>) -> CompositionRectangleGeometry {
        CompositionRectangleGeometry(CompositionGeometry::create(
            compositor,
            CompositionGeometryKind::Rectangle(CompositionRectangleGeometryProps::new()),
            |c| ServerCompositionRectangleGeometry::new(c),
        ))
    }

    /// The top left corner of the rectangle.
    pub fn offset(&self) -> Vector {
        self.props().offset()
    }

    pub fn set_offset(&self, value: Vector) {
        self.props().set_offset(&*self.0, value)
    }

    /// The size of the rectangle.
    pub fn size(&self) -> Vector {
        self.props().size()
    }

    pub fn set_size(&self, value: Vector) {
        self.props().set_size(&*self.0, value)
    }
}

geometry_handle!(
    /// A rectangle with rounded corners (WinUI
    /// `Microsoft.UI.Composition.CompositionRoundedRectangleGeometry`):
    /// `(offset, size)` with the elliptical radii `corner_radius`, all
    /// animatable (`CornerRadius`, `Offset`, `Size`). The radii are limited to
    /// half the size.
    CompositionRoundedRectangleGeometry,
    RoundedRectangle,
    CompositionRoundedRectangleGeometryProps,
    "a rounded rectangle geometry"
);

impl CompositionRoundedRectangleGeometry {
    pub(crate) fn new(compositor: &Rc<Compositor>) -> CompositionRoundedRectangleGeometry {
        CompositionRoundedRectangleGeometry(CompositionGeometry::create(
            compositor,
            CompositionGeometryKind::RoundedRectangle(CompositionRoundedRectangleGeometryProps::new()),
            |c| ServerCompositionRoundedRectangleGeometry::new(c),
        ))
    }

    /// The radii of the corners.
    pub fn corner_radius(&self) -> Vector {
        self.props().corner_radius()
    }

    pub fn set_corner_radius(&self, value: Vector) {
        self.props().set_corner_radius(&*self.0, value)
    }

    /// The top left corner of the rectangle.
    pub fn offset(&self) -> Vector {
        self.props().offset()
    }

    pub fn set_offset(&self, value: Vector) {
        self.props().set_offset(&*self.0, value)
    }

    /// The size of the rectangle.
    pub fn size(&self) -> Vector {
        self.props().size()
    }

    pub fn set_size(&self, value: Vector) {
        self.props().set_size(&*self.0, value)
    }
}

geometry_handle!(
    /// An ellipse (WinUI `Microsoft.UI.Composition.CompositionEllipseGeometry`):
    /// its `center` and its radii `radius`, both animatable (`Center`,
    /// `Radius`).
    CompositionEllipseGeometry,
    Ellipse,
    CompositionEllipseGeometryProps,
    "an ellipse geometry"
);

impl CompositionEllipseGeometry {
    pub(crate) fn new(compositor: &Rc<Compositor>) -> CompositionEllipseGeometry {
        CompositionEllipseGeometry(CompositionGeometry::create(
            compositor,
            CompositionGeometryKind::Ellipse(CompositionEllipseGeometryProps::new()),
            |c| ServerCompositionEllipseGeometry::new(c),
        ))
    }

    /// The centre of the ellipse.
    pub fn center(&self) -> Vector {
        self.props().center()
    }

    pub fn set_center(&self, value: Vector) {
        self.props().set_center(&*self.0, value)
    }

    /// The radii of the ellipse.
    pub fn radius(&self) -> Vector {
        self.props().radius()
    }

    pub fn set_radius(&self, value: Vector) {
        self.props().set_radius(&*self.0, value)
    }
}

geometry_handle!(
    /// A line (WinUI `Microsoft.UI.Composition.CompositionLineGeometry`) from
    /// `start` to `end`, both animatable (`Start`, `End`).
    CompositionLineGeometry,
    Line,
    CompositionLineGeometryProps,
    "a line geometry"
);

impl CompositionLineGeometry {
    pub(crate) fn new(compositor: &Rc<Compositor>) -> CompositionLineGeometry {
        CompositionLineGeometry(CompositionGeometry::create(
            compositor,
            CompositionGeometryKind::Line(CompositionLineGeometryProps::new()),
            |c| ServerCompositionLineGeometry::new(c),
        ))
    }

    /// The start point of the line.
    pub fn start(&self) -> Vector {
        self.props().start()
    }

    pub fn set_start(&self, value: Vector) {
        self.props().set_start(&*self.0, value)
    }

    /// The end point of the line.
    pub fn end(&self) -> Vector {
        self.props().end()
    }

    pub fn set_end(&self, value: Vector) {
        self.props().set_end(&*self.0, value)
    }
}

geometry_handle!(
    /// A geometry that shows a [`CompositionPath`] (WinUI
    /// `Microsoft.UI.Composition.CompositionPathGeometry`).
    CompositionPathGeometry,
    Path,
    CompositionPathGeometryProps,
    "a path geometry"
);

impl CompositionPathGeometry {
    pub(crate) fn new(compositor: &Rc<Compositor>) -> CompositionPathGeometry {
        CompositionPathGeometry(CompositionGeometry::create(
            compositor,
            CompositionGeometryKind::Path(CompositionPathGeometryProps::new()),
            |c| ServerCompositionPathGeometry::new(c),
        ))
    }

    pub(crate) fn with_path(compositor: &Rc<Compositor>, path: Option<CompositionPath>) -> CompositionPathGeometry {
        let geometry = Self::new(compositor);
        geometry.set_path(path);
        geometry
    }

    /// The path the geometry shows.
    pub fn path(&self) -> Option<CompositionPath> {
        self.props().path().map(|path| (*path).clone())
    }

    pub fn set_path(&self, value: Option<CompositionPath>) {
        self.props().set_path(&*self.0, value.map(Arc::new))
    }
}