2
2
// The .NET Foundation licenses this file to you under the MIT license.
3
3
// See the LICENSE file in the project root for more information.
4
4
5
+ using System ;
5
6
using System . Linq ;
6
7
using System . Numerics ;
7
8
using Windows . Foundation ;
@@ -166,6 +167,16 @@ protected internal override void OnElementContextUninitialized(AttachedShadowEle
166
167
_container . Children . Remove ( context . SpriteVisual ) ;
167
168
}
168
169
170
+ context . SpriteVisual ? . StopAnimation ( "Size" ) ;
171
+
172
+ context . Element . LayoutUpdated -= Element_LayoutUpdated ;
173
+
174
+ if ( context . VisibilityToken != null )
175
+ {
176
+ context . Element . UnregisterPropertyChangedCallback ( UIElement . VisibilityProperty , context . VisibilityToken . Value ) ;
177
+ context . VisibilityToken = null ;
178
+ }
179
+
169
180
base . OnElementContextUninitialized ( context ) ;
170
181
}
171
182
@@ -176,6 +187,50 @@ protected override void SetElementChildVisual(AttachedShadowElementContext conte
176
187
{
177
188
_container . Children . InsertAtTop ( context . SpriteVisual ) ;
178
189
}
190
+
191
+ // Handles size changing and other elements around it updating.
192
+ context . Element . LayoutUpdated -= Element_LayoutUpdated ;
193
+ context . Element . LayoutUpdated += Element_LayoutUpdated ;
194
+
195
+ if ( context . VisibilityToken != null )
196
+ {
197
+ context . Element . UnregisterPropertyChangedCallback ( UIElement . VisibilityProperty , context . VisibilityToken . Value ) ;
198
+ context . VisibilityToken = null ;
199
+ }
200
+
201
+ context . VisibilityToken = context . Element . RegisterPropertyChangedCallback ( UIElement . VisibilityProperty , Element_VisibilityChanged ) ;
202
+ }
203
+
204
+ private void Element_LayoutUpdated ( object sender , object e )
205
+ {
206
+ // Update other shadows to account for layout changes
207
+ CastToElement_SizeChanged ( null , null ) ;
208
+ }
209
+
210
+ private void Element_VisibilityChanged ( DependencyObject sender , DependencyProperty dp )
211
+ {
212
+ if ( sender is FrameworkElement element )
213
+ {
214
+ var context = GetElementContext ( element ) ;
215
+
216
+ if ( element . Visibility == Visibility . Collapsed )
217
+ {
218
+ if ( _container != null && _container . Children . Contains ( context . SpriteVisual ) )
219
+ {
220
+ _container . Children . Remove ( context . SpriteVisual ) ;
221
+ }
222
+ }
223
+ else
224
+ {
225
+ if ( _container != null && ! _container . Children . Contains ( context . SpriteVisual ) )
226
+ {
227
+ _container . Children . InsertAtTop ( context . SpriteVisual ) ;
228
+ }
229
+ }
230
+ }
231
+
232
+ // Update other shadows to account for layout changes
233
+ CastToElement_SizeChanged ( null , null ) ;
179
234
}
180
235
181
236
/// <inheritdoc/>
@@ -255,12 +310,24 @@ protected override CompositionBrush GetShadowMask(AttachedShadowElementContext c
255
310
}
256
311
257
312
// Position our shadow in the correct spot to match the corresponding element.
258
- context . SpriteVisual . Size = context . Element . RenderSize . ToVector2 ( ) ;
259
313
context . SpriteVisual . Offset = context . Element . CoordinatesFrom ( CastTo ) . ToVector3 ( ) ;
260
314
315
+ BindSizeAndScale ( context . SpriteVisual , context . Element ) ;
316
+
261
317
return mask ;
262
318
}
263
319
320
+ private static void BindSizeAndScale ( CompositionObject source , UIElement target )
321
+ {
322
+ var visual = ElementCompositionPreview . GetElementVisual ( target ) ;
323
+ var bindSizeAnimation = source . Compositor . CreateExpressionAnimation ( $ "{ nameof ( visual ) } .Size * { nameof ( visual ) } .Scale.XY") ;
324
+
325
+ bindSizeAnimation . SetReferenceParameter ( nameof ( visual ) , visual ) ;
326
+
327
+ // Start the animation
328
+ source . StartAnimation ( "Size" , bindSizeAnimation ) ;
329
+ }
330
+
264
331
private void CustomMaskedElement_Loaded ( object sender , RoutedEventArgs e )
265
332
{
266
333
var context = GetElementContext ( sender as FrameworkElement ) ;
@@ -274,9 +341,6 @@ private void CustomMaskedElement_Loaded(object sender, RoutedEventArgs e)
274
341
/// <inheritdoc/>
275
342
protected internal override void OnSizeChanged ( AttachedShadowElementContext context , Size newSize , Size previousSize )
276
343
{
277
- var sizeAsVec2 = newSize . ToVector2 ( ) ;
278
-
279
- context . SpriteVisual . Size = sizeAsVec2 ;
280
344
context . SpriteVisual . Offset = context . Element . CoordinatesFrom ( CastTo ) . ToVector3 ( ) ;
281
345
282
346
UpdateShadowClip ( context ) ;
0 commit comments