Skip to main content

ferroui_base/rendering/composition/
composition_view_box.rs

1use super::composition_object_impls::composition_object_impls;
2use super::generated::{CompositionViewBoxHooks, CompositionViewBoxProps, ServerCompositionViewBoxProps};
3use super::server::{ServerCompositionViewBox, ServerObjectId};
4use super::{CompositionObject, CompositionStretch, Compositor};
5use crate::Vector;
6use std::rc::{Rc, Weak};
7
8/// The area of the shape coordinates a shape visual or a geometric clip
9/// shows (WinUI `Microsoft.UI.Composition.CompositionViewBox`): the
10/// rectangle `(offset, size)` of the shapes is mapped onto the visual by
11/// `stretch` and placed by the alignment ratios, as a surface brush lays
12/// out its surface. `Offset`, `Size` and the ratios are animatable.
13///
14/// Deviation (winui-composition.md): what is outside the mapped view box is
15/// clipped (the port's reading of WinUI); a view box whose size is zero maps
16/// nothing (the shapes are drawn as if there were no view box).
17pub struct CompositionViewBox {
18    this: Weak<CompositionViewBox>,
19    object: CompositionObject,
20    props: CompositionViewBoxProps,
21}
22
23impl CompositionViewBox {
24    pub(crate) fn new(compositor: &Rc<Compositor>) -> Rc<CompositionViewBox> {
25        let server = compositor.create_server_object(|compositor, _| ServerCompositionViewBox::new(compositor));
26        let view_box = Rc::new_cyclic(|this: &Weak<CompositionViewBox>| CompositionViewBox {
27            this: this.clone(),
28            object: CompositionObject::new(compositor, Some(server)),
29            props: CompositionViewBoxProps::new(),
30        });
31        view_box.props.initialize_defaults(&*view_box);
32        view_box
33    }
34
35    pub fn compositor(&self) -> &Rc<Compositor> {
36        self.object.compositor()
37    }
38
39    /// The embedded `CompositionObject`.
40    pub fn object(&self) -> &CompositionObject {
41        &self.object
42    }
43
44    /// The id of the server-side view box.
45    pub fn server(&self) -> ServerObjectId {
46        self.object.required_server()
47    }
48
49    pub fn is_disposed(&self) -> bool {
50        self.object.is_disposed()
51    }
52
53    pub fn dispose(&self) {
54        self.object.dispose();
55    }
56
57    /// The generated properties of the view box.
58    pub fn props(&self) -> &CompositionViewBoxProps {
59        &self.props
60    }
61
62    /// The part of the free horizontal space left of the mapped view box
63    /// (0 left, 0.5 centre, 1 right; default 0.5).
64    pub fn horizontal_alignment_ratio(&self) -> f32 {
65        self.props.horizontal_alignment_ratio()
66    }
67
68    pub fn set_horizontal_alignment_ratio(&self, value: f32) {
69        self.props.set_horizontal_alignment_ratio(self, value)
70    }
71
72    /// The top left corner of the view box, in shape coordinates.
73    pub fn offset(&self) -> Vector {
74        self.props.offset()
75    }
76
77    pub fn set_offset(&self, value: Vector) {
78        self.props.set_offset(self, value)
79    }
80
81    /// The size of the view box, in shape coordinates.
82    pub fn size(&self) -> Vector {
83        self.props.size()
84    }
85
86    pub fn set_size(&self, value: Vector) {
87        self.props.set_size(self, value)
88    }
89
90    /// How the view box is scaled onto the visual (default `Fill`).
91    pub fn stretch(&self) -> CompositionStretch {
92        self.props.stretch()
93    }
94
95    pub fn set_stretch(&self, value: CompositionStretch) {
96        self.props.set_stretch(self, value)
97    }
98
99    /// The part of the free vertical space above the mapped view box
100    /// (0 top, 0.5 centre, 1 bottom; default 0.5).
101    pub fn vertical_alignment_ratio(&self) -> f32 {
102        self.props.vertical_alignment_ratio()
103    }
104
105    pub fn set_vertical_alignment_ratio(&self, value: f32) {
106        self.props.set_vertical_alignment_ratio(self, value)
107    }
108}
109
110impl CompositionViewBoxHooks for CompositionViewBox {}
111
112composition_object_impls!(
113    CompositionViewBox, "CompositionViewBox",
114    serialize: |this, writer| this.props.serialize_changes_core(this, writer),
115    start_animation: |this, name, animation, final_value| this.props.start_animation(this, name, animation, final_value),
116    composition_property: |_this, name| ServerCompositionViewBoxProps::get_composition_property(name),
117);