Skip to content

Commit aca8e70

Browse files
committed
Added cachemode to shadows for performance
1 parent c4a64c2 commit aca8e70

15 files changed

+162
-71
lines changed

MaterialDesignThemes.MahApps/Themes/MaterialDesignTheme.MahApps.Flyout.xaml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,7 @@
102102
<ControlTemplate x:Key="FlyoutTemplate"
103103
TargetType="{x:Type Controls:Flyout}">
104104
<Grid x:Name="root"
105-
Margin="{TemplateBinding Margin}"
106-
Background="{TemplateBinding Background}"
107-
Effect="{DynamicResource MaterialDesignShadowDepth5}">
105+
Margin="{TemplateBinding Margin}">
108106
<Grid.RenderTransform>
109107
<TranslateTransform />
110108
</Grid.RenderTransform>
@@ -212,6 +210,12 @@
212210
</VisualState>
213211
</VisualStateGroup>
214212
</VisualStateManager.VisualStateGroups>
213+
<AdornerDecorator>
214+
<AdornerDecorator.CacheMode>
215+
<BitmapCache EnableClearType="True" SnapsToDevicePixels="True"/>
216+
</AdornerDecorator.CacheMode>
217+
<Border Background="{TemplateBinding Background}" Effect="{DynamicResource MaterialDesignShadowDepth5}"/>
218+
</AdornerDecorator>
215219
<AdornerDecorator>
216220
<DockPanel FocusVisualStyle="{x:Null}"
217221
Focusable="False">

MaterialDesignThemes.Wpf/Converters/ShadowConverter.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ static ShadowConverter()
2121

2222
ShadowsDictionary = new Dictionary<ShadowDepth, DropShadowEffect>
2323
{
24-
{ ShadowDepth.Depth0, (DropShadowEffect)resourceDictionary["MaterialDesignShadowDepth0"] },
24+
{ ShadowDepth.Depth0, null },
2525
{ ShadowDepth.Depth1, (DropShadowEffect)resourceDictionary["MaterialDesignShadowDepth1"] },
2626
{ ShadowDepth.Depth2, (DropShadowEffect)resourceDictionary["MaterialDesignShadowDepth2"] },
2727
{ ShadowDepth.Depth3, (DropShadowEffect)resourceDictionary["MaterialDesignShadowDepth3"] },
@@ -44,6 +44,7 @@ public object ConvertBack(object value, Type targetType, object parameter, Cultu
4444

4545
private static DropShadowEffect Clone(DropShadowEffect dropShadowEffect)
4646
{
47+
if (dropShadowEffect == null) return null;
4748
return new DropShadowEffect()
4849
{
4950
BlurRadius = dropShadowEffect.BlurRadius,

MaterialDesignThemes.Wpf/ShadowAssist.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public ShadowLocalInfo(double standardOpacity)
2929
public static class ShadowAssist
3030
{
3131
public static readonly DependencyProperty ShadowDepthProperty = DependencyProperty.RegisterAttached(
32-
"ShadowDepth", typeof (ShadowDepth), typeof (ShadowAssist), new PropertyMetadata(default(ShadowDepth)));
32+
"ShadowDepth", typeof (ShadowDepth), typeof (ShadowAssist), new FrameworkPropertyMetadata(default(ShadowDepth), FrameworkPropertyMetadataOptions.AffectsRender));
3333

3434
public static void SetShadowDepth(DependencyObject element, ShadowDepth value)
3535
{
@@ -55,7 +55,7 @@ private static ShadowLocalInfo GetLocalInfo(DependencyObject element)
5555
}
5656

5757
public static readonly DependencyProperty DarkenProperty = DependencyProperty.RegisterAttached(
58-
"Darken", typeof (bool), typeof (ShadowAssist), new PropertyMetadata(default(bool), DarkenPropertyChangedCallback));
58+
"Darken", typeof (bool), typeof (ShadowAssist), new FrameworkPropertyMetadata(default(bool), FrameworkPropertyMetadataOptions.AffectsRender, DarkenPropertyChangedCallback));
5959

6060
private static void DarkenPropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
6161
{

MaterialDesignThemes.Wpf/Themes/Generic.xaml

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -381,9 +381,15 @@
381381
<Setter.Value>
382382
<ControlTemplate TargetType="{x:Type local:ColorZone}">
383383
<Grid Background="Transparent">
384-
<Border Background="{TemplateBinding Background}"
385-
CornerRadius="{TemplateBinding CornerRadius}"
386-
Effect="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(local:ShadowAssist.ShadowDepth), Converter={x:Static converters:ShadowConverter.Instance}}" />
384+
<AdornerDecorator>
385+
<AdornerDecorator.CacheMode>
386+
<BitmapCache EnableClearType="True" SnapsToDevicePixels="True"/>
387+
</AdornerDecorator.CacheMode>
388+
<Border Background="{TemplateBinding Background}"
389+
CornerRadius="{TemplateBinding CornerRadius}"
390+
Effect="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(local:ShadowAssist.ShadowDepth), Converter={x:Static converters:ShadowConverter.Instance}}">
391+
</Border>
392+
</AdornerDecorator>
387393
<Border Background="{TemplateBinding Background}"
388394
BorderBrush="{TemplateBinding BorderBrush}"
389395
BorderThickness="{TemplateBinding BorderThickness}"
@@ -733,10 +739,17 @@
733739
<TranslateTransform x:Name="LeftDrawerTranslateTransform"
734740
X="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Grid}, Path=ActualWidth, Converter={StaticResource DrawerOffsetConverter}}" />
735741
</Grid.RenderTransform>
736-
<Border Effect="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(local:ShadowAssist.ShadowDepth), Converter={x:Static converters:ShadowConverter.Instance}}"
737-
Opacity="0"
738-
Background="{TemplateBinding LeftDrawerBackground}"
739-
x:Name="LeftDrawerShadow" />
742+
<AdornerDecorator>
743+
<AdornerDecorator.CacheMode>
744+
<BitmapCache EnableClearType="True" SnapsToDevicePixels="True"/>
745+
</AdornerDecorator.CacheMode>
746+
<Border Effect="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(local:ShadowAssist.ShadowDepth), Converter={x:Static converters:ShadowConverter.Instance}}"
747+
Opacity="0"
748+
Background="{TemplateBinding LeftDrawerBackground}"
749+
x:Name="LeftDrawerShadow">
750+
</Border>
751+
</AdornerDecorator>
752+
740753
<ContentPresenter Content="{TemplateBinding LeftDrawerContent}" ContentTemplate="{TemplateBinding LeftDrawerContentTemplate}" ContentStringFormat="{TemplateBinding LeftDrawerContentStringFormat}" />
741754
</Grid>
742755
</Grid>

MaterialDesignThemes.Wpf/Themes/MaterialDesignTheme.Button.xaml

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,15 @@
6161
</Storyboard>
6262
</ControlTemplate.Resources>
6363
<Grid>
64-
<Border Background="{TemplateBinding Background}"
65-
x:Name="ShadowBorder"
66-
Effect="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(wpf:ShadowAssist.ShadowDepth), Converter={x:Static converters:ShadowConverter.Instance}}" />
64+
<AdornerDecorator>
65+
<AdornerDecorator.CacheMode>
66+
<BitmapCache EnableClearType="True" SnapsToDevicePixels="True"/>
67+
</AdornerDecorator.CacheMode>
68+
<Border Background="{TemplateBinding Background}"
69+
x:Name="ShadowBorder"
70+
Effect="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(wpf:ShadowAssist.ShadowDepth), Converter={x:Static converters:ShadowConverter.Instance}}">
71+
</Border>
72+
</AdornerDecorator>
6773
<Border Background="{TemplateBinding Background}" x:Name="border" CornerRadius="2" />
6874
<Border Background="White" CornerRadius="2" x:Name="ClickBorder" Opacity="0" RenderTransformOrigin="0.5,0.5" >
6975
<Border.RenderTransform>
@@ -237,9 +243,15 @@
237243
<Setter.Value>
238244
<ControlTemplate TargetType="{x:Type Button}">
239245
<Grid>
240-
<Ellipse Fill="{TemplateBinding Background}" Stroke="{TemplateBinding BorderBrush}" StrokeThickness="{TemplateBinding BorderThickness}"
241-
Effect="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(wpf:ShadowAssist.ShadowDepth), Converter={x:Static converters:ShadowConverter.Instance}}"
242-
x:Name="border" />
246+
<AdornerDecorator>
247+
<AdornerDecorator.CacheMode>
248+
<BitmapCache EnableClearType="True" SnapsToDevicePixels="True"/>
249+
</AdornerDecorator.CacheMode>
250+
<Ellipse Fill="{TemplateBinding Background}" Stroke="{TemplateBinding BorderBrush}" StrokeThickness="{TemplateBinding BorderThickness}"
251+
Effect="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(wpf:ShadowAssist.ShadowDepth), Converter={x:Static converters:ShadowConverter.Instance}}"
252+
x:Name="border">
253+
</Ellipse>
254+
</AdornerDecorator>
243255
<wpf:Ripple Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}" Focusable="False"
244256
Feedback="White"
245257
Clip="{Binding ElementName=GeometryEllipse, Path=RenderedGeometry}" ClipToBounds="True"

MaterialDesignThemes.Wpf/Themes/MaterialDesignTheme.Card.xaml

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,17 @@
1111

1212
<ControlTemplate TargetType="{x:Type wpf:Card}" x:Key="CardTemplate">
1313
<Grid Margin="{TemplateBinding Margin}" Background="Transparent">
14-
<Border Effect="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(wpf:ShadowAssist.ShadowDepth), Converter={x:Static converters:ShadowConverter.Instance}}"
15-
CornerRadius="{TemplateBinding UniformCornerRadius}">
16-
<Border Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}"
17-
x:Name="PART_ClipBorder"
18-
Clip="{TemplateBinding ContentClip}" />
19-
</Border>
14+
<AdornerDecorator>
15+
<AdornerDecorator.CacheMode>
16+
<BitmapCache EnableClearType="True" SnapsToDevicePixels="True"/>
17+
</AdornerDecorator.CacheMode>
18+
<Border Effect="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(wpf:ShadowAssist.ShadowDepth), Converter={x:Static converters:ShadowConverter.Instance}}"
19+
CornerRadius="{TemplateBinding UniformCornerRadius}">
20+
<Border Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}"
21+
x:Name="PART_ClipBorder"
22+
Clip="{TemplateBinding ContentClip}" />
23+
</Border>
24+
</AdornerDecorator>
2025
<ContentPresenter
2126
x:Name="ContentPresenter"
2227
Margin="{TemplateBinding Padding}"

MaterialDesignThemes.Wpf/Themes/MaterialDesignTheme.DataGrid.ComboBox.xaml

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,14 @@
153153
</Grid.ColumnDefinitions>
154154
<Popup x:Name="PART_Popup" AllowsTransparency="true" Grid.ColumnSpan="2" IsOpen="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" Margin="1" PopupAnimation="{DynamicResource {x:Static SystemParameters.ComboBoxPopupAnimationKey}}" Placement="Bottom">
155155
<Grid MaxHeight="{TemplateBinding MaxDropDownHeight}" MinWidth="{Binding ActualWidth, ElementName=templateRoot}" UseLayoutRounding="True">
156-
<Border x:Name="shadow" Background="{DynamicResource MaterialDesignPaper}" CornerRadius="2" BorderThickness="1"
157-
Effect="{DynamicResource MaterialDesignShadowDepth2}" />
156+
<AdornerDecorator>
157+
<AdornerDecorator.CacheMode>
158+
<BitmapCache EnableClearType="True" SnapsToDevicePixels="True"/>
159+
</AdornerDecorator.CacheMode>
160+
<Border x:Name="shadow" Background="{DynamicResource MaterialDesignPaper}" CornerRadius="2" BorderThickness="1"
161+
Effect="{DynamicResource MaterialDesignShadowDepth2}">
162+
</Border>
163+
</AdornerDecorator>
158164
<Border x:Name="dropDownBorder" Margin="{Binding ElementName=shadow, Path=Margin, Mode=OneWay}" Background="Transparent"
159165
CornerRadius="2" BorderThickness="1" BorderBrush="{DynamicResource MaterialDesignDivider}">
160166
<ScrollViewer x:Name="DropDownScrollViewer">
@@ -231,8 +237,14 @@
231237
</Grid.ColumnDefinitions>
232238
<Popup x:Name="PART_Popup" AllowsTransparency="true" Grid.ColumnSpan="2" IsOpen="{Binding IsDropDownOpen, RelativeSource={RelativeSource TemplatedParent}}" PopupAnimation="{DynamicResource {x:Static SystemParameters.ComboBoxPopupAnimationKey}}" Placement="Bottom">
233239
<Grid MaxHeight="{TemplateBinding MaxDropDownHeight}" MinWidth="{Binding ActualWidth, ElementName=templateRoot}" UseLayoutRounding="True">
234-
<Border x:Name="shadow" Background="{DynamicResource MaterialDesignPaper}" CornerRadius="2" BorderThickness="1"
235-
Effect="{DynamicResource MaterialDesignShadowDepth2}" />
240+
<AdornerDecorator>
241+
<AdornerDecorator.CacheMode>
242+
<BitmapCache EnableClearType="True" SnapsToDevicePixels="True"/>
243+
</AdornerDecorator.CacheMode>
244+
<Border x:Name="shadow" Background="{DynamicResource MaterialDesignPaper}" CornerRadius="2" BorderThickness="1"
245+
Effect="{DynamicResource MaterialDesignShadowDepth2}">
246+
</Border>
247+
</AdornerDecorator>
236248
<Border x:Name="dropDownBorder" Margin="{Binding ElementName=shadow, Path=Margin, Mode=OneWay}" Background="Transparent"
237249
CornerRadius="2" BorderThickness="1" BorderBrush="{DynamicResource MaterialDesignDivider}">
238250
<ScrollViewer x:Name="DropDownScrollViewer">

MaterialDesignThemes.Wpf/Themes/MaterialDesignTheme.DataGrid.xaml

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,16 @@
7272
PlacementTarget="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGridCell}}}"
7373
PopupAnimation="Fade">
7474
<Grid>
75-
<Border Background="{DynamicResource MaterialDesignPaper}" CornerRadius="2"
76-
Margin="5,5,5,5"
77-
Effect="{StaticResource MaterialDesignShadowDepth2}"
78-
BorderThickness="1" />
75+
<AdornerDecorator>
76+
<AdornerDecorator.CacheMode>
77+
<BitmapCache EnableClearType="True" SnapsToDevicePixels="True"/>
78+
</AdornerDecorator.CacheMode>
79+
<Border Background="{DynamicResource MaterialDesignPaper}" CornerRadius="2"
80+
Margin="5,5,5,5"
81+
Effect="{StaticResource MaterialDesignShadowDepth2}"
82+
BorderThickness="1">
83+
</Border>
84+
</AdornerDecorator>
7985
<Border Padding="16" Background="Transparent" CornerRadius="2"
8086
Margin="5,5,5,5"
8187
BorderBrush="{DynamicResource MaterialDesignDivider}"

MaterialDesignThemes.Wpf/Themes/MaterialDesignTheme.DatePicker.xaml

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,15 @@
7575
<Setter Property="Template">
7676
<Setter.Value>
7777
<ControlTemplate TargetType="{x:Type Calendar}">
78-
<Border Effect="{DynamicResource MaterialDesignShadowDepth4}" Padding="16 8 16 24">
79-
<CalendarItem x:Name="PART_CalendarItem" BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}" Style="{DynamicResource MaterialDesignCalendarItemPortrait}"
80-
/>
81-
</Border>
78+
<AdornerDecorator>
79+
<!-- warning, this will cache the inner item as well, consider separating the shadow from the item if possible -->
80+
<AdornerDecorator.CacheMode>
81+
<BitmapCache EnableClearType="True" SnapsToDevicePixels="True"/>
82+
</AdornerDecorator.CacheMode>
83+
<Border Effect="{DynamicResource MaterialDesignShadowDepth4}" Padding="16 8 16 24">
84+
<CalendarItem x:Name="PART_CalendarItem" BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}" Style="{DynamicResource MaterialDesignCalendarItemPortrait}"/>
85+
</Border>
86+
</AdornerDecorator>
8287
</ControlTemplate>
8388
</Setter.Value>
8489
</Setter>

0 commit comments

Comments
 (0)