Skip to main content

ferroui_base/rendering/composition/
composition_visual_winui.rs

1//! The remaining WinUI members of `Visual` (phase 2, not from upstream):
2//! `RotationAngleInDegrees`, `RotationAxis`, the relative adjustments,
3//! `IsHitTestVisible`, `IsPixelSnappingEnabled`, the 3D modes and
4//! `ParentForTransform`.
5
6use super::{
7    CompositionBackfaceVisibility, CompositionBorderMode, CompositionCompositeMode, CompositionVisual,
8    ICompositionObject,
9};
10use crate::{Vector, Vector3D};
11use std::rc::Rc;
12
13impl CompositionVisual {
14    /// The rotation about the rotation axis in degrees: a view of
15    /// [`rotation_angle`](Self::rotation_angle).
16    ///
17    /// Deviation (winui-composition.md): not an animation target of its own;
18    /// animate `RotationAngle`.
19    pub fn rotation_angle_in_degrees(&self) -> f32 {
20        self.rotation_angle().to_degrees()
21    }
22
23    pub fn set_rotation_angle_in_degrees(&self, value: f32) {
24        self.set_rotation_angle(value.to_radians())
25    }
26
27    /// The axis [`rotation_angle`](Self::rotation_angle) rotates about
28    /// (default `(0, 0, 1)`; animatable as `RotationAxis`).
29    ///
30    /// Deviation (winui-composition.md): an axis along Z (or a zero axis)
31    /// rotates in the plane; any other axis rotates in 3D, of which the 2D
32    /// renderers show the projection, as for `Orientation`.
33    pub fn rotation_axis(&self) -> Vector3D {
34        self.visual_props().rotation_axis()
35    }
36
37    pub fn set_rotation_axis(&self, value: Vector3D) {
38        self.visual_props().set_rotation_axis(self, value)
39    }
40
41    /// The part of the size of the parent added to the offset: the offset of
42    /// the visual is `Offset + RelativeOffsetAdjustment * parent size`
43    /// (animatable as `RelativeOffsetAdjustment`; Z is not adjusted, the
44    /// parent has no depth).
45    pub fn relative_offset_adjustment(&self) -> Vector3D {
46        self.visual_props().relative_offset_adjustment()
47    }
48
49    pub fn set_relative_offset_adjustment(&self, value: Vector3D) {
50        self.visual_props().set_relative_offset_adjustment(self, value)
51    }
52
53    /// The part of the size of the parent added to the size: the size of
54    /// the visual is `Size + RelativeSizeAdjustment * parent size`
55    /// (animatable as `RelativeSizeAdjustment`). `(1, 1)` with a zero `Size`
56    /// makes the visual as large as its parent.
57    pub fn relative_size_adjustment(&self) -> Vector {
58        self.visual_props().relative_size_adjustment()
59    }
60
61    pub fn set_relative_size_adjustment(&self, value: Vector) {
62        self.visual_props().set_relative_size_adjustment(self, value)
63    }
64
65    /// The size of the visual with its relative size adjustment applied: what
66    /// is rendered and hit tested. [`size`](Self::size) is the `Size`
67    /// property alone.
68    pub fn effective_size(&self) -> Vector {
69        let size = self.size();
70        let relative = self.relative_size_adjustment();
71        if relative == Vector::default() {
72            return size;
73        }
74        let parent = self.parent().map(|parent| parent.effective_size()).unwrap_or_default();
75        size + Vector::multiply(relative, parent)
76    }
77
78    /// Whether the visual and its subtree take part in hit testing (default
79    /// `true`).
80    pub fn is_hit_test_visible(&self) -> bool {
81        self.visual_props().is_hit_test_visible()
82    }
83
84    pub fn set_is_hit_test_visible(&self, value: bool) {
85        self.visual_props().set_is_hit_test_visible(self, value)
86    }
87
88    /// Whether the offset of the visual is snapped to device pixels.
89    ///
90    /// Deviation (winui-composition.md): the translation of the transform of
91    /// the visual relative to its parent is rounded to device pixels of its
92    /// target, not its position in the window.
93    pub fn is_pixel_snapping_enabled(&self) -> bool {
94        self.visual_props().is_pixel_snapping_enabled()
95    }
96
97    pub fn set_is_pixel_snapping_enabled(&self, value: bool) {
98        self.visual_props().set_is_pixel_snapping_enabled(self, value)
99    }
100
101    /// How the edges of the visual are antialiased. Stored only.
102    pub fn border_mode(&self) -> CompositionBorderMode {
103        self.visual_props().border_mode()
104    }
105
106    pub fn set_border_mode(&self, value: CompositionBorderMode) {
107        self.visual_props().set_border_mode(self, value)
108    }
109
110    /// Whether the back of the visual is shown when it is turned in 3D.
111    /// Stored only.
112    pub fn backface_visibility(&self) -> CompositionBackfaceVisibility {
113        self.visual_props().backface_visibility()
114    }
115
116    pub fn set_backface_visibility(&self, value: CompositionBackfaceVisibility) {
117        self.visual_props().set_backface_visibility(self, value)
118    }
119
120    /// How the visual is blended with what is behind it. Stored only.
121    pub fn composite_mode(&self) -> CompositionCompositeMode {
122        self.visual_props().composite_mode()
123    }
124
125    pub fn set_composite_mode(&self, value: CompositionCompositeMode) {
126        self.visual_props().set_composite_mode(self, value)
127    }
128
129    /// The visual whose coordinate space the transform of this visual is
130    /// relative to, instead of its parent: the visual is drawn where its
131    /// parent is, in the place its offset and transform give it inside
132    /// `parent_for_transform`.
133    ///
134    /// Deviation (winui-composition.md): the two visuals must be in one tree
135    /// (otherwise the transform is relative to the parent); clips and
136    /// opacity still come from the parent chain.
137    pub fn parent_for_transform(&self) -> Option<Rc<CompositionVisual>> {
138        self.visual_props()
139            .parent_for_transform()
140            .and_then(|visual| visual.into_any_rc().downcast::<CompositionVisual>().ok())
141    }
142
143    pub fn set_parent_for_transform(&self, value: Option<Rc<CompositionVisual>>) {
144        self.visual_props().set_parent_for_transform(self, value.map(|visual| visual as Rc<dyn ICompositionObject>))
145    }
146}