Skip to content

Commit 09e8e31

Browse files
authored
added blurry background feature to the DrawerHost (#3832)
1 parent f7dadb5 commit 09e8e31

File tree

3 files changed

+112
-21
lines changed

3 files changed

+112
-21
lines changed

src/MainDemo.Wpf/Drawers.xaml

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,55 @@
1010
d:DesignWidth="1080"
1111
mc:Ignorable="d">
1212
<DockPanel>
13-
<TextBlock DockPanel.Dock="Top" Style="{StaticResource PageTitleTextBlock}" Text="Drawer" />
13+
<TextBlock DockPanel.Dock="Top"
14+
Style="{StaticResource PageTitleTextBlock}"
15+
Text="Drawer" />
16+
17+
<StackPanel DockPanel.Dock="Left">
18+
19+
<StackPanel Orientation="Horizontal">
20+
<TextBlock VerticalAlignment="Center" Text="Black Overlay Background" />
21+
<ToggleButton x:Name="BackgroundToggle" Margin="8,0,16,0" />
22+
<TextBlock VerticalAlignment="Center" Text="Primary Overlay Background" />
23+
</StackPanel>
24+
25+
<StackPanel Margin="0,8,0,0" Orientation="Horizontal">
26+
<TextBlock VerticalAlignment="Center" Text="Open Mode" />
27+
<ComboBox SelectedValue="{Binding OpenMode, ElementName=DrawerHost}" SelectedValuePath="Content">
28+
<ComboBoxItem Content="{x:Static materialDesign:DrawerHostOpenMode.Modal}" />
29+
<ComboBoxItem Content="{x:Static materialDesign:DrawerHostOpenMode.Standard}" />
30+
</ComboBox>
31+
</StackPanel>
32+
33+
<StackPanel Margin="0,8,0,0">
34+
35+
<CheckBox x:Name="cbApplyBlurBackground" Content="ApplyBlurBackground" />
36+
37+
<DockPanel>
38+
<Button Click="Drawer_ResetBlur"
39+
Content="{materialDesign:PackIcon Kind=Reload}"
40+
DockPanel.Dock="Right"
41+
Style="{StaticResource MaterialDesignFlatButton}"
42+
ToolTip="Reset the BlurRadius of the Drawers Background to it's default value" />
43+
<Slider x:Name="BlurRadiusSlider"
44+
DockPanel.Dock="Left"
45+
Maximum="64"
46+
Minimum="1"
47+
Style="{StaticResource MaterialDesignDiscreteSlider}" />
48+
</DockPanel>
49+
</StackPanel>
1450

15-
<StackPanel DockPanel.Dock="Top" Orientation="Horizontal">
16-
<TextBlock VerticalAlignment="Center" Text="Black Overlay Background" />
17-
18-
<ToggleButton x:Name="BackgroundToggle" Margin="8,0,16,0" />
19-
20-
<TextBlock VerticalAlignment="Center" Text="Primary Overlay Background" />
21-
22-
<TextBlock Margin="30,0,16,0"
23-
VerticalAlignment="Center"
24-
Text="Open Mode" />
25-
<ComboBox SelectedValue="{Binding OpenMode, ElementName=DrawerHost}" SelectedValuePath="Content">
26-
<ComboBoxItem Content="{x:Static materialDesign:DrawerHostOpenMode.Modal}" />
27-
<ComboBoxItem Content="{x:Static materialDesign:DrawerHostOpenMode.Standard}" />
28-
</ComboBox>
2951
</StackPanel>
3052

31-
<smtx:XamlDisplay MaxHeight="{x:Static system:Double.MaxValue}" UniqueKey="drawers_1">
53+
<smtx:XamlDisplay MaxHeight="{x:Static system:Double.MaxValue}"
54+
DockPanel.Dock="Left"
55+
UniqueKey="drawers_1">
3256
<materialDesign:DrawerHost x:Name="DrawerHost"
3357
Width="480"
3458
Height="480"
3559
Margin="32"
60+
ApplyBlurBackground="{Binding ElementName=cbApplyBlurBackground, Path=IsChecked}"
61+
BlurRadius="{Binding ElementName=BlurRadiusSlider, Path=Value}"
3662
HorizontalAlignment="Center"
3763
VerticalAlignment="Center"
3864
BorderBrush="{DynamicResource MaterialDesignDivider}"

src/MainDemo.Wpf/Drawers.xaml.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
1-
namespace MaterialDesignDemo;
1+
using MaterialDesignThemes.Wpf;
2+
3+
namespace MaterialDesignDemo;
24

35
public partial class Drawers
46
{
5-
public Drawers() => InitializeComponent();
7+
public Drawers()
8+
{
9+
InitializeComponent();
10+
Drawer_ResetBlur(null!, null!);
11+
}
12+
13+
private void Drawer_ResetBlur(object sender, RoutedEventArgs e)
14+
{
15+
BlurRadiusSlider.Value = DrawerHost.DefaultBlurRadius;
16+
}
617
}

src/MaterialDesignThemes.Wpf/DrawerHost.cs

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Windows.Media;
2+
using System.Windows.Media.Effects;
23

34
namespace MaterialDesignThemes.Wpf;
45

@@ -445,6 +446,31 @@ public CornerRadius? BottomDrawerCornerRadius
445446

446447
#endregion
447448

449+
#region Blur effect
450+
public bool ApplyBlurBackground
451+
{
452+
get => (bool)GetValue(ApplyBlurBackgroundProperty);
453+
set => SetValue(ApplyBlurBackgroundProperty, value);
454+
}
455+
public static readonly DependencyProperty ApplyBlurBackgroundProperty = DependencyProperty.Register(
456+
nameof(ApplyBlurBackground), typeof(bool), typeof(DrawerHost), new PropertyMetadata(default(bool), OnBlurPropertyChanged));
457+
458+
public const double DefaultBlurRadius = 16.0;
459+
public double BlurRadius
460+
{
461+
get => (double)GetValue(BlurRadiusProperty);
462+
set => SetValue(BlurRadiusProperty, value);
463+
}
464+
public static readonly DependencyProperty BlurRadiusProperty = DependencyProperty.Register(
465+
nameof(BlurRadius), typeof(double), typeof(DrawerHost), new PropertyMetadata(DefaultBlurRadius, OnBlurPropertyChanged));
466+
467+
private static void OnBlurPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
468+
{
469+
var drawerHost = (DrawerHost)d;
470+
drawerHost.HandleBackgroundBlur();
471+
}
472+
#endregion
473+
448474
#region open drawer events/callbacks
449475

450476
public static readonly RoutedEvent DrawerOpenedEvent =
@@ -593,7 +619,8 @@ private static void IsBottomDrawerOpenPropertyChangedCallback(DependencyObject d
593619
private static void IsDrawerOpenPropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs, Dock dock)
594620
{
595621
var drawerHost = (DrawerHost)dependencyObject;
596-
if (!(bool)dependencyPropertyChangedEventArgs.NewValue)
622+
bool isOpened = (bool)dependencyPropertyChangedEventArgs.NewValue;
623+
if (!isOpened)
597624
{
598625
var args = new DrawerClosingEventArgs(dock, DrawerClosingEvent);
599626
drawerHost.OnDrawerClosing(args);
@@ -604,16 +631,43 @@ private static void IsDrawerOpenPropertyChanged(DependencyObject dependencyObjec
604631
}
605632
}
606633

607-
if (!drawerHost._lockZIndexes && (bool)dependencyPropertyChangedEventArgs.NewValue)
634+
if (!drawerHost._lockZIndexes && isOpened)
608635
drawerHost.PrepareZIndexes(drawerHost._zIndexPropertyLookup[dependencyPropertyChangedEventArgs.Property]);
609636
drawerHost.UpdateVisualStates();
610637

611-
if ((bool)dependencyPropertyChangedEventArgs.NewValue)
638+
drawerHost.HandleBackgroundBlur(isOpened);
639+
640+
if (isOpened)
612641
{
613642
RaiseDrawerOpened(drawerHost, dock);
614643
}
615644
}
616645

646+
private void HandleBackgroundBlur(bool? isOpened = null)
647+
{
648+
isOpened ??= IsAnyDrawerOpen();
649+
650+
if (Content is UIElement drawerContent)
651+
{
652+
if (ApplyBlurBackground && isOpened.Value)
653+
{
654+
drawerContent.Effect = new BlurEffect()
655+
{
656+
Radius = BlurRadius
657+
};
658+
}
659+
else
660+
{
661+
drawerContent.Effect = null;
662+
}
663+
}
664+
665+
bool IsAnyDrawerOpen()
666+
{
667+
return IsLeftDrawerOpen || IsTopDrawerOpen || IsRightDrawerOpen || IsBottomDrawerOpen;
668+
}
669+
}
670+
617671
private static void RaiseDrawerOpened(DrawerHost drawerHost, Dock dock)
618672
{
619673
//multiple ways of calling back that the drawer has opened:

0 commit comments

Comments
 (0)