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
use super::composition_object_impls::composition_object_impls;
use super::generated::{DropShadowHooks, DropShadowProps, ServerDropShadowProps};
use super::server::{ServerDropShadow, ServerObjectId};
use super::{CompositionBrush, CompositionDropShadowSourcePolicy, CompositionObject, Compositor, ICompositionObject};
use crate::media::Color;
use crate::Vector3D;
use std::rc::{Rc, Weak};
/// The shadow of a sprite or layer visual (WinUI
/// `Microsoft.UI.Composition.DropShadow`), with animatable `BlurRadius`,
/// `Color`, `Offset` and `Opacity`.
///
/// On a sprite visual the shadow is cast by the rectangle of the visual (a
/// box shadow of the drawing context), by the alpha of its
/// [`mask`](Self::mask) (any composition brush, such as the brush of
/// `GetAlphaMask` of a shape, a text block or an image), or, with the
/// `InheritFromVisualContent` [`source_policy`](Self::source_policy), by the
/// alpha of the brush of the visual. On a layer visual it is cast by the
/// composed children, or by the mask. The shadow is drawn under the content
/// and nothing of it is removed; the Z of the offset moves it as the
/// renderers project that depth for the visual (a visual turned out of its
/// plane).
///
/// Deviation (winui-composition.md): without effects in the drawing context
/// the shadow is the box shadow of the rectangle. WinUI's `CompositionShadow`
/// base class has no members and is not a separate type.
pub struct DropShadow {
this: Weak<DropShadow>,
object: CompositionObject,
props: DropShadowProps,
}
impl DropShadow {
pub(crate) fn new(compositor: &Rc<Compositor>) -> Rc<DropShadow> {
let server = compositor.create_server_object(|compositor, _| ServerDropShadow::new(compositor));
let shadow = Rc::new_cyclic(|this: &Weak<DropShadow>| DropShadow {
this: this.clone(),
object: CompositionObject::new(compositor, Some(server)),
props: DropShadowProps::new(),
});
shadow.props.initialize_defaults(&*shadow);
shadow
}
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 shadow.
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 generated properties of the shadow.
pub fn props(&self) -> &DropShadowProps {
&self.props
}
/// The radius of the blur (default 9).
pub fn blur_radius(&self) -> f32 {
self.props.blur_radius()
}
pub fn set_blur_radius(&self, value: f32) {
self.props.set_blur_radius(self, value)
}
/// The colour of the shadow (default black).
pub fn color(&self) -> Color {
self.props.color()
}
pub fn set_color(&self, value: Color) {
self.props.set_color(self, value)
}
/// The offset of the shadow from the visual (WinUI `Vector3`). The Z is
/// a depth in the space of the visual: it moves the shadow only where
/// the visual is turned out of its plane (`Orientation`, `RotationAxis`),
/// as the renderers project that visual.
pub fn offset(&self) -> Vector3D {
self.props.offset()
}
pub fn set_offset(&self, value: Vector3D) {
self.props.set_offset(self, value)
}
/// The opacity of the shadow (default 1).
pub fn opacity(&self) -> f32 {
self.props.opacity()
}
pub fn set_opacity(&self, value: f32) {
self.props.set_opacity(self, value)
}
/// The brush whose alpha shapes the shadow, painted over the area of the
/// visual; `None` for the shape of the visual. Ignored with the
/// `InheritFromVisualContent` source policy.
pub fn mask(&self) -> Option<Rc<CompositionBrush>> {
self.props.mask().and_then(|mask| mask.into_any_rc().downcast::<CompositionBrush>().ok())
}
pub fn set_mask(&self, value: Option<Rc<CompositionBrush>>) {
self.props.set_mask(self, value.map(|mask| mask as Rc<dyn ICompositionObject>))
}
/// What the shadow is cast from: `Default` for the mask (or the shape of
/// the visual), `InheritFromVisualContent` for the alpha of the content
/// of the visual (the brush of a sprite visual, the children of a layer
/// visual).
pub fn source_policy(&self) -> CompositionDropShadowSourcePolicy {
self.props.source_policy()
}
pub fn set_source_policy(&self, value: CompositionDropShadowSourcePolicy) {
self.props.set_source_policy(self, value)
}
}
/// `InitializeDefaultsExtra` activates the server object: done when the
/// server object is created.
impl DropShadowHooks for DropShadow {}
composition_object_impls!(
DropShadow, "DropShadow",
serialize: |this, writer| this.props.serialize_changes_core(this, writer),
start_animation: |this, name, animation, final_value| this.props.start_animation(this, name, animation, final_value),
composition_property: |_this, name| ServerDropShadowProps::get_composition_property(name),
);