@@ -91,90 +91,69 @@ protected internal override void OnElementContextInitialized(AttachedShadowEleme
91
91
}
92
92
93
93
/// <inheritdoc/>
94
- protected override void SetElementChildVisual ( AttachedShadowElementContext context )
94
+ protected override void OnPropertyChanged ( AttachedShadowElementContext context , DependencyProperty property , object oldValue , object newValue )
95
95
{
96
- if ( context . TryGetResource ( OpacityMaskShapeVisualSurfaceBrushResourceKey , out var opacityMask ) )
96
+ if ( property == CornerRadiusProperty )
97
97
{
98
- var visualSurface = context . GetResource ( OpacityMaskVisualSurfaceResourceKey ) ??
99
- context . AddResource ( OpacityMaskVisualSurfaceResourceKey , context . Compositor . CreateVisualSurface ( ) ) ;
100
- visualSurface . SourceVisual = context . SpriteVisual ;
101
- context . SpriteVisual . RelativeSizeAdjustment = Vector2 . Zero ;
102
- context . SpriteVisual . Size = new Vector2 ( ( float ) context . Element . ActualWidth , ( float ) context . Element . ActualHeight ) ;
103
- visualSurface . SourceOffset = new Vector2 ( - MaxBlurRadius ) ;
104
- visualSurface . SourceSize = new Vector2 ( ( float ) context . Element . ActualWidth , ( float ) context . Element . ActualHeight ) + new Vector2 ( MaxBlurRadius * 2 ) ;
105
-
106
- var surfaceBrush = context . GetResource ( OpacityMaskSurfaceBrushResourceKey ) ??
107
- context . AddResource ( OpacityMaskSurfaceBrushResourceKey , context . Compositor . CreateSurfaceBrush ( ) ) ;
108
- surfaceBrush . Surface = visualSurface ;
109
- surfaceBrush . Stretch = CompositionStretch . None ;
110
-
111
- CompositionMaskBrush maskBrush = context . GetResource ( OpacityMaskBrushResourceKey ) ??
112
- context . AddResource ( OpacityMaskBrushResourceKey , context . Compositor . CreateMaskBrush ( ) ) ;
113
- maskBrush . Source = surfaceBrush ;
114
- maskBrush . Mask = opacityMask ;
98
+ UpdateShadowClip ( context ) ;
99
+ UpdateVisualOpacityMask ( context ) ;
115
100
116
- var visual = context . GetResource ( OpacityMaskVisualResourceKey ) ??
117
- context . AddResource ( OpacityMaskVisualResourceKey , context . Compositor . CreateSpriteVisual ( ) ) ;
118
- visual . RelativeSizeAdjustment = Vector2 . One ;
119
- visual . Offset = new Vector3 ( - MaxBlurRadius , - MaxBlurRadius , 0 ) ;
120
- visual . Size = new Vector2 ( MaxBlurRadius * 2 ) ;
121
- visual . Brush = maskBrush ;
122
- ElementCompositionPreview . SetElementChildVisual ( context . Element , visual ) ;
101
+ var geometry = context . GetResource ( RoundedRectangleGeometryResourceKey ) ;
102
+ if ( geometry != null )
103
+ {
104
+ geometry . CornerRadius = new Vector2 ( ( float ) ( double ) newValue ) ;
105
+ }
106
+ }
107
+ else if ( property == InnerContentClipModeProperty )
108
+ {
109
+ UpdateShadowClip ( context ) ;
110
+ UpdateVisualOpacityMask ( context ) ;
111
+ SetElementChildVisual ( context ) ;
123
112
}
124
113
else
125
114
{
126
- base . SetElementChildVisual ( context ) ;
127
- context . RemoveAndDisposeResource ( OpacityMaskVisualSurfaceResourceKey ) ;
128
- context . RemoveAndDisposeResource ( OpacityMaskSurfaceBrushResourceKey ) ;
129
- context . RemoveAndDisposeResource ( OpacityMaskVisualResourceKey ) ;
130
- context . RemoveAndDisposeResource ( OpacityMaskBrushResourceKey ) ;
115
+ base . OnPropertyChanged ( context , property , oldValue , newValue ) ;
131
116
}
117
+
118
+ base . OnPropertyChanged ( context , property , oldValue , newValue ) ;
132
119
}
133
120
134
- /// <summary>
135
- /// Updates the <see cref="CompositionBrush"/> used to mask <paramref name="context"/>.<see cref="AttachedShadowElementContext.SpriteVisual">SpriteVisual</see>.
136
- /// </summary>
137
- /// <param name="context">The <see cref="AttachedShadowElementContext"/> whose <see cref="SpriteVisual"/> will be masked.</param>
138
- private void UpdateVisualOpacityMask ( AttachedShadowElementContext context )
121
+ /// <inheritdoc/>
122
+ protected override CompositionBrush GetShadowMask ( AttachedShadowElementContext context )
139
123
{
140
- if ( InnerContentClipMode != InnerContentClipMode . CompositionMaskBrush )
124
+ if ( ! SupportsCompositionVisualSurface )
141
125
{
142
- context . RemoveAndDisposeResource ( OpacityMaskShapeVisualResourceKey ) ;
143
- context . RemoveAndDisposeResource ( OpacityMaskGeometryResourceKey ) ;
144
- context . RemoveAndDisposeResource ( OpacityMaskSpriteShapeResourceKey ) ;
145
- context . RemoveAndDisposeResource ( OpacityMaskShapeVisualSurfaceResourceKey ) ;
146
- context . RemoveAndDisposeResource ( OpacityMaskShapeVisualSurfaceBrushResourceKey ) ;
147
- return ;
126
+ return null ;
148
127
}
149
128
150
- var shapeVisual = context . GetResource ( OpacityMaskShapeVisualResourceKey ) ??
151
- context . AddResource ( OpacityMaskShapeVisualResourceKey , context . Compositor . CreateShapeVisual ( ) ) ;
152
-
153
- CompositionRoundedRectangleGeometry geom = context . GetResource ( OpacityMaskGeometryResourceKey ) ??
154
- context . AddResource ( OpacityMaskGeometryResourceKey , context . Compositor . CreateRoundedRectangleGeometry ( ) ) ;
155
- CompositionSpriteShape shape = context . GetResource ( OpacityMaskSpriteShapeResourceKey ) ??
156
- context . AddResource ( OpacityMaskSpriteShapeResourceKey , context . Compositor . CreateSpriteShape ( geom ) ) ;
129
+ // Create rounded rectangle geometry and add it to a shape
130
+ var geometry = context . GetResource ( RoundedRectangleGeometryResourceKey ) ?? context . AddResource (
131
+ RoundedRectangleGeometryResourceKey ,
132
+ context . Compositor . CreateRoundedRectangleGeometry ( ) ) ;
133
+ geometry . CornerRadius = new Vector2 ( ( float ) CornerRadius ) ;
157
134
158
- geom . Offset = new Vector2 ( MaxBlurRadius / 2 ) ;
159
- geom . CornerRadius = new Vector2 ( ( MaxBlurRadius / 2 ) + ( float ) CornerRadius ) ;
160
- shape . StrokeThickness = MaxBlurRadius ;
161
- shape . StrokeBrush = shape . StrokeBrush ?? context . Compositor . CreateColorBrush ( Colors . Black ) ;
135
+ var shape = context . GetResource ( ShapeResourceKey ) ?? context . AddResource ( ShapeResourceKey , context . Compositor . CreateSpriteShape ( geometry ) ) ;
136
+ shape . FillBrush = context . Compositor . CreateColorBrush ( Colors . Black ) ;
162
137
163
- if ( ! shapeVisual . Shapes . Contains ( shape ) )
164
- {
165
- shapeVisual . Shapes . Add ( shape ) ;
166
- }
138
+ // Create a ShapeVisual so that our geometry can be rendered to a visual
139
+ var shapeVisual = context . GetResource ( ShapeVisualResourceKey ) ??
140
+ context . AddResource ( ShapeVisualResourceKey , context . Compositor . CreateShapeVisual ( ) ) ;
141
+ shapeVisual . Shapes . Add ( shape ) ;
167
142
168
- var visualSurface = context . GetResource ( OpacityMaskShapeVisualSurfaceResourceKey ) ??
169
- context . AddResource ( OpacityMaskShapeVisualSurfaceResourceKey , context . Compositor . CreateVisualSurface ( ) ) ;
143
+ // Create a CompositionVisualSurface, which renders our ShapeVisual to a texture
144
+ var visualSurface = context . GetResource ( VisualSurfaceResourceKey ) ??
145
+ context . AddResource ( VisualSurfaceResourceKey , context . Compositor . CreateVisualSurface ( ) ) ;
170
146
visualSurface . SourceVisual = shapeVisual ;
171
147
172
- geom . Size = new Vector2 ( ( float ) context . Element . ActualWidth , ( float ) context . Element . ActualHeight ) + new Vector2 ( MaxBlurRadius ) ;
173
- shapeVisual . Size = visualSurface . SourceSize = new Vector2 ( ( float ) context . Element . ActualWidth , ( float ) context . Element . ActualHeight ) + new Vector2 ( MaxBlurRadius * 2 ) ;
148
+ // Create a CompositionSurfaceBrush to render our CompositionVisualSurface to a brush.
149
+ // Now we have a rounded rectangle brush that can be used on as the mask for our shadow.
150
+ var surfaceBrush = context . GetResource ( SurfaceBrushResourceKey ) ?? context . AddResource (
151
+ SurfaceBrushResourceKey ,
152
+ context . Compositor . CreateSurfaceBrush ( visualSurface ) ) ;
174
153
175
- var surfaceBrush = context . GetResource ( OpacityMaskShapeVisualSurfaceBrushResourceKey ) ??
176
- context . AddResource ( OpacityMaskShapeVisualSurfaceBrushResourceKey , context . Compositor . CreateSurfaceBrush ( ) ) ;
177
- surfaceBrush . Surface = visualSurface ;
154
+ geometry . Size = visualSurface . SourceSize = shapeVisual . Size = context . Element . RenderSize . ToVector2 ( ) ;
155
+
156
+ return surfaceBrush ;
178
157
}
179
158
180
159
/// <inheritdoc/>
@@ -217,70 +196,91 @@ protected override CompositionClip GetShadowClip(AttachedShadowElementContext co
217
196
return clip ;
218
197
}
219
198
220
- /// <inheritdoc/>
221
- protected override CompositionBrush GetShadowMask ( AttachedShadowElementContext context )
199
+ /// <summary>
200
+ /// Updates the <see cref="CompositionBrush"/> used to mask <paramref name="context"/>.<see cref="AttachedShadowElementContext.SpriteVisual">SpriteVisual</see>.
201
+ /// </summary>
202
+ /// <param name="context">The <see cref="AttachedShadowElementContext"/> whose <see cref="SpriteVisual"/> will be masked.</param>
203
+ private void UpdateVisualOpacityMask ( AttachedShadowElementContext context )
222
204
{
223
- if ( ! SupportsCompositionVisualSurface )
205
+ if ( InnerContentClipMode != InnerContentClipMode . CompositionMaskBrush )
224
206
{
225
- return null ;
207
+ context . RemoveAndDisposeResource ( OpacityMaskShapeVisualResourceKey ) ;
208
+ context . RemoveAndDisposeResource ( OpacityMaskGeometryResourceKey ) ;
209
+ context . RemoveAndDisposeResource ( OpacityMaskSpriteShapeResourceKey ) ;
210
+ context . RemoveAndDisposeResource ( OpacityMaskShapeVisualSurfaceResourceKey ) ;
211
+ context . RemoveAndDisposeResource ( OpacityMaskShapeVisualSurfaceBrushResourceKey ) ;
212
+ return ;
226
213
}
227
214
228
- // Create rounded rectangle geometry and add it to a shape
229
- var geometry = context . GetResource ( RoundedRectangleGeometryResourceKey ) ?? context . AddResource (
230
- RoundedRectangleGeometryResourceKey ,
231
- context . Compositor . CreateRoundedRectangleGeometry ( ) ) ;
232
- geometry . CornerRadius = new Vector2 ( ( float ) CornerRadius ) ;
215
+ var shapeVisual = context . GetResource ( OpacityMaskShapeVisualResourceKey ) ??
216
+ context . AddResource ( OpacityMaskShapeVisualResourceKey , context . Compositor . CreateShapeVisual ( ) ) ;
233
217
234
- var shape = context . GetResource ( ShapeResourceKey ) ?? context . AddResource ( ShapeResourceKey , context . Compositor . CreateSpriteShape ( geometry ) ) ;
235
- shape . FillBrush = context . Compositor . CreateColorBrush ( Colors . Black ) ;
218
+ CompositionRoundedRectangleGeometry geom = context . GetResource ( OpacityMaskGeometryResourceKey ) ??
219
+ context . AddResource ( OpacityMaskGeometryResourceKey , context . Compositor . CreateRoundedRectangleGeometry ( ) ) ;
220
+ CompositionSpriteShape shape = context . GetResource ( OpacityMaskSpriteShapeResourceKey ) ??
221
+ context . AddResource ( OpacityMaskSpriteShapeResourceKey , context . Compositor . CreateSpriteShape ( geom ) ) ;
236
222
237
- // Create a ShapeVisual so that our geometry can be rendered to a visual
238
- var shapeVisual = context . GetResource ( ShapeVisualResourceKey ) ??
239
- context . AddResource ( ShapeVisualResourceKey , context . Compositor . CreateShapeVisual ( ) ) ;
240
- shapeVisual . Shapes . Add ( shape ) ;
223
+ geom . Offset = new Vector2 ( MaxBlurRadius / 2 ) ;
224
+ geom . CornerRadius = new Vector2 ( ( MaxBlurRadius / 2 ) + ( float ) CornerRadius ) ;
225
+ shape . StrokeThickness = MaxBlurRadius ;
226
+ shape . StrokeBrush = shape . StrokeBrush ?? context . Compositor . CreateColorBrush ( Colors . Black ) ;
241
227
242
- // Create a CompositionVisualSurface, which renders our ShapeVisual to a texture
243
- var visualSurface = context . GetResource ( VisualSurfaceResourceKey ) ??
244
- context . AddResource ( VisualSurfaceResourceKey , context . Compositor . CreateVisualSurface ( ) ) ;
245
- visualSurface . SourceVisual = shapeVisual ;
228
+ if ( ! shapeVisual . Shapes . Contains ( shape ) )
229
+ {
230
+ shapeVisual . Shapes . Add ( shape ) ;
231
+ }
246
232
247
- // Create a CompositionSurfaceBrush to render our CompositionVisualSurface to a brush.
248
- // Now we have a rounded rectangle brush that can be used on as the mask for our shadow.
249
- var surfaceBrush = context . GetResource ( SurfaceBrushResourceKey ) ?? context . AddResource (
250
- SurfaceBrushResourceKey ,
251
- context . Compositor . CreateSurfaceBrush ( visualSurface ) ) ;
233
+ var visualSurface = context . GetResource ( OpacityMaskShapeVisualSurfaceResourceKey ) ??
234
+ context . AddResource ( OpacityMaskShapeVisualSurfaceResourceKey , context . Compositor . CreateVisualSurface ( ) ) ;
235
+ visualSurface . SourceVisual = shapeVisual ;
252
236
253
- geometry . Size = visualSurface . SourceSize = shapeVisual . Size = context . Element . RenderSize . ToVector2 ( ) ;
237
+ geom . Size = new Vector2 ( ( float ) context . Element . ActualWidth , ( float ) context . Element . ActualHeight ) + new Vector2 ( MaxBlurRadius ) ;
238
+ shapeVisual . Size = visualSurface . SourceSize = new Vector2 ( ( float ) context . Element . ActualWidth , ( float ) context . Element . ActualHeight ) + new Vector2 ( MaxBlurRadius * 2 ) ;
254
239
255
- return surfaceBrush ;
240
+ var surfaceBrush = context . GetResource ( OpacityMaskShapeVisualSurfaceBrushResourceKey ) ??
241
+ context . AddResource ( OpacityMaskShapeVisualSurfaceBrushResourceKey , context . Compositor . CreateSurfaceBrush ( ) ) ;
242
+ surfaceBrush . Surface = visualSurface ;
256
243
}
257
244
258
245
/// <inheritdoc/>
259
- protected override void OnPropertyChanged ( AttachedShadowElementContext context , DependencyProperty property , object oldValue , object newValue )
246
+ protected override void SetElementChildVisual ( AttachedShadowElementContext context )
260
247
{
261
- if ( property == CornerRadiusProperty )
248
+ if ( context . TryGetResource ( OpacityMaskShapeVisualSurfaceBrushResourceKey , out var opacityMask ) )
262
249
{
263
- UpdateShadowClip ( context ) ;
264
- UpdateVisualOpacityMask ( context ) ;
250
+ var visualSurface = context . GetResource ( OpacityMaskVisualSurfaceResourceKey ) ??
251
+ context . AddResource ( OpacityMaskVisualSurfaceResourceKey , context . Compositor . CreateVisualSurface ( ) ) ;
252
+ visualSurface . SourceVisual = context . SpriteVisual ;
253
+ context . SpriteVisual . RelativeSizeAdjustment = Vector2 . Zero ;
254
+ context . SpriteVisual . Size = new Vector2 ( ( float ) context . Element . ActualWidth , ( float ) context . Element . ActualHeight ) ;
255
+ visualSurface . SourceOffset = new Vector2 ( - MaxBlurRadius ) ;
256
+ visualSurface . SourceSize = new Vector2 ( ( float ) context . Element . ActualWidth , ( float ) context . Element . ActualHeight ) + new Vector2 ( MaxBlurRadius * 2 ) ;
265
257
266
- var geometry = context . GetResource ( RoundedRectangleGeometryResourceKey ) ;
267
- if ( geometry != null )
268
- {
269
- geometry . CornerRadius = new Vector2 ( ( float ) ( double ) newValue ) ;
270
- }
271
- }
272
- else if ( property == InnerContentClipModeProperty )
273
- {
274
- UpdateShadowClip ( context ) ;
275
- UpdateVisualOpacityMask ( context ) ;
276
- SetElementChildVisual ( context ) ;
258
+ var surfaceBrush = context . GetResource ( OpacityMaskSurfaceBrushResourceKey ) ??
259
+ context . AddResource ( OpacityMaskSurfaceBrushResourceKey , context . Compositor . CreateSurfaceBrush ( ) ) ;
260
+ surfaceBrush . Surface = visualSurface ;
261
+ surfaceBrush . Stretch = CompositionStretch . None ;
262
+
263
+ CompositionMaskBrush maskBrush = context . GetResource ( OpacityMaskBrushResourceKey ) ??
264
+ context . AddResource ( OpacityMaskBrushResourceKey , context . Compositor . CreateMaskBrush ( ) ) ;
265
+ maskBrush . Source = surfaceBrush ;
266
+ maskBrush . Mask = opacityMask ;
267
+
268
+ var visual = context . GetResource ( OpacityMaskVisualResourceKey ) ??
269
+ context . AddResource ( OpacityMaskVisualResourceKey , context . Compositor . CreateSpriteVisual ( ) ) ;
270
+ visual . RelativeSizeAdjustment = Vector2 . One ;
271
+ visual . Offset = new Vector3 ( - MaxBlurRadius , - MaxBlurRadius , 0 ) ;
272
+ visual . Size = new Vector2 ( MaxBlurRadius * 2 ) ;
273
+ visual . Brush = maskBrush ;
274
+ ElementCompositionPreview . SetElementChildVisual ( context . Element , visual ) ;
277
275
}
278
276
else
279
277
{
280
- base . OnPropertyChanged ( context , property , oldValue , newValue ) ;
278
+ base . SetElementChildVisual ( context ) ;
279
+ context . RemoveAndDisposeResource ( OpacityMaskVisualSurfaceResourceKey ) ;
280
+ context . RemoveAndDisposeResource ( OpacityMaskSurfaceBrushResourceKey ) ;
281
+ context . RemoveAndDisposeResource ( OpacityMaskVisualResourceKey ) ;
282
+ context . RemoveAndDisposeResource ( OpacityMaskBrushResourceKey ) ;
281
283
}
282
-
283
- base . OnPropertyChanged ( context , property , oldValue , newValue ) ;
284
284
}
285
285
286
286
/// <inheritdoc />
0 commit comments