-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathDropShadowPanel.cs
236 lines (198 loc) · 6.99 KB
/
DropShadowPanel.cs
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Numerics;
using System.Threading.Tasks;
using Windows.UI;
using Windows.UI.Composition;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Hosting;
using Windows.UI.Xaml.Shapes;
namespace Microsoft.Toolkit.Uwp.UI.Controls
{
/// <summary>
/// The <see cref="DropShadowPanel"/> control allows the creation of a DropShadow for any Xaml FrameworkElement in markup
/// making it easier to add shadows to Xaml without having to directly drop down to Windows.UI.Composition APIs.
/// </summary>
[TemplatePart(Name = PartShadow, Type = typeof(Border))]
public partial class DropShadowPanel : ContentControl
{
private const string PartShadow = "ShadowElement";
private readonly DropShadow _dropShadow;
private readonly SpriteVisual _shadowVisual;
private Border _border;
/// <summary>
/// Initializes a new instance of the <see cref="DropShadowPanel"/> class.
/// </summary>
public DropShadowPanel()
{
this.DefaultStyleKey = typeof(DropShadowPanel);
if (!DesignTimeHelpers.IsRunningInLegacyDesignerMode)
{
Compositor compositor = ElementCompositionPreview.GetElementVisual(this).Compositor;
_shadowVisual = compositor.CreateSpriteVisual();
_dropShadow = compositor.CreateDropShadow();
_shadowVisual.Shadow = _dropShadow;
}
}
/// <summary>
/// Update the visual state of the control when its template is changed.
/// </summary>
protected override void OnApplyTemplate()
{
if (DesignTimeHelpers.IsRunningInLegacyDesignerMode)
{
return;
}
_border = GetTemplateChild(PartShadow) as Border;
if (_border != null)
{
ElementCompositionPreview.SetElementChildVisual(_border, _shadowVisual);
}
ConfigureShadowVisualForCastingElement();
base.OnApplyTemplate();
}
/// <inheritdoc/>
protected override void OnContentChanged(object oldContent, object newContent)
{
if (oldContent != null)
{
if (oldContent is FrameworkElement oldElement)
{
oldElement.SizeChanged -= OnSizeChanged;
}
}
if (newContent != null)
{
if (newContent is FrameworkElement newElement)
{
newElement.SizeChanged += OnSizeChanged;
}
}
UpdateShadowMask();
base.OnContentChanged(oldContent, newContent);
}
private void OnSizeChanged(object sender, SizeChangedEventArgs e)
{
if (!DesignTimeHelpers.IsRunningInLegacyDesignerMode)
{
UpdateShadowSize();
}
}
private void ConfigureShadowVisualForCastingElement()
{
UpdateShadowMask();
if (!DesignTimeHelpers.IsRunningInLegacyDesignerMode)
{
UpdateShadowSize();
}
}
private void OnBlurRadiusChanged(double newValue)
{
if (_dropShadow != null)
{
_dropShadow.BlurRadius = (float)newValue;
}
}
private void OnColorChanged(Color newValue)
{
if (_dropShadow != null)
{
_dropShadow.Color = newValue;
}
}
private void OnOffsetXChanged(double newValue)
{
if (!DesignTimeHelpers.IsRunningInLegacyDesignerMode && _dropShadow != null)
{
UpdateShadowOffset((float)newValue, _dropShadow.Offset.Y, _dropShadow.Offset.Z);
}
}
private void OnOffsetYChanged(double newValue)
{
if (!DesignTimeHelpers.IsRunningInLegacyDesignerMode && _dropShadow != null)
{
UpdateShadowOffset(_dropShadow.Offset.X, (float)newValue, _dropShadow.Offset.Z);
}
}
private void OnOffsetZChanged(double newValue)
{
if (!DesignTimeHelpers.IsRunningInLegacyDesignerMode && _dropShadow != null)
{
UpdateShadowOffset(_dropShadow.Offset.X, _dropShadow.Offset.Y, (float)newValue);
}
}
private void OnShadowOpacityChanged(double newValue)
{
if (_dropShadow != null)
{
_dropShadow.Opacity = (float)newValue;
}
}
private void UpdateShadowMask()
{
if (DesignTimeHelpers.IsRunningInLegacyDesignerMode)
{
return;
}
if (Content != null && IsMasked)
{
CompositionBrush mask = null;
if (Content is Image)
{
mask = ((Image)Content).GetAlphaMask();
}
else if (Content is Shape)
{
mask = ((Shape)Content).GetAlphaMask();
}
else if (Content is TextBlock)
{
mask = ((TextBlock)Content).GetAlphaMask();
}
else if (Content is ImageExBase imageExBase)
{
imageExBase.ImageExInitialized += ImageExInitialized;
if (imageExBase.IsInitialized)
{
imageExBase.ImageExInitialized -= ImageExInitialized;
mask = ((ImageExBase)Content).GetAlphaMask();
}
}
_dropShadow.Mask = mask;
}
else
{
_dropShadow.Mask = null;
}
}
private void ImageExInitialized(object sender, EventArgs e)
{
var imageExBase = (ImageExBase)Content;
imageExBase.ImageExInitialized -= ImageExInitialized;
CompositionBrush mask = ((ImageExBase)Content).GetAlphaMask();
_dropShadow.Mask = mask;
}
private void UpdateShadowOffset(float x, float y, float z)
{
if (_dropShadow != null)
{
_dropShadow.Offset = new Vector3(x, y, z);
}
}
private void UpdateShadowSize()
{
if (_shadowVisual != null)
{
Vector2 newSize = new Vector2(0, 0);
if (Content is FrameworkElement contentFE)
{
newSize = new Vector2((float)contentFE.ActualWidth, (float)contentFE.ActualHeight);
}
_shadowVisual.Size = newSize;
}
}
}
}