Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature - Blurry Dialog Background #3738

Merged
Merged
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;
using System.Windows.Media.Effects;

namespace MaterialDesignThemes.Wpf.Converters;
internal sealed class DialogBackgroundBlurConverter : IMultiValueConverter
{
public static readonly DialogBackgroundBlurConverter Instance = new();
public object? Convert(object?[]? values, Type targetType, object? parameter, CultureInfo culture)
{
if (values is [bool isOpen, bool applyBlurBackground, double blurRadius]
&& isOpen
&& applyBlurBackground)
{
return new BlurEffect() { Radius = blurRadius };
}

return null;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the "Build and Test" failing because I am returning null from this converter?
Should this rather be DependencyProperty.UnsetValue or Binding.DoNothing?
I am never sure on which one to use.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@corvinsz It's the analyzers yelling at you here. Basically you need to add a ? to the return type, the values parameter, and the parameter parameter to indicate nullability.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for clarifying @nicolaihenriksen
I'm still doing alot of .NET Framework 4.x work due to legacy projects and I always forget about the nullability in .NET 5+

}
public object?[]? ConvertBack(object? value, Type[] targetTypes, object? parameter, CultureInfo culture) => throw new NotImplementedException();
}
24 changes: 21 additions & 3 deletions src/MaterialDesignThemes.Wpf/DialogHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,24 @@ public Brush? DialogBackground
set => SetValue(DialogBackgroundProperty, value);
}

public bool ApplyBlurBackground
{
get => (bool)GetValue(ApplyBlurBackgroundProperty);
set => SetValue(ApplyBlurBackgroundProperty, value);
}
public static readonly DependencyProperty ApplyBlurBackgroundProperty = DependencyProperty.Register(
nameof(ApplyBlurBackground), typeof(bool), typeof(DialogHost), new PropertyMetadata(default(bool)));


private const double DefaultBlurRadius = 16.0;
public double BlurRadius
{
get => (double)GetValue(BlurRadiusProperty);
set => SetValue(BlurRadiusProperty, value);
}
public static readonly DependencyProperty BlurRadiusProperty = DependencyProperty.Register(
nameof(BlurRadius), typeof(double), typeof(DialogHost), new PropertyMetadata(DefaultBlurRadius));

public override void OnApplyTemplate()
{
if (_contentCoverGrid != null)
Expand All @@ -624,14 +642,14 @@ public static void SetRestoreFocusElement(DependencyObject element, IInputElemen
=> element.SetValue(RestoreFocusElementProperty, value);

public static IInputElement GetRestoreFocusElement(DependencyObject element)
=> (IInputElement) element.GetValue(RestoreFocusElementProperty);
=> (IInputElement)element.GetValue(RestoreFocusElementProperty);

public static readonly DependencyProperty IsRestoreFocusDisabledProperty = DependencyProperty.Register(
nameof(IsRestoreFocusDisabled), typeof(bool), typeof(DialogHost), new PropertyMetadata(false));

public bool IsRestoreFocusDisabled
{
get => (bool) GetValue(IsRestoreFocusDisabledProperty);
get => (bool)GetValue(IsRestoreFocusDisabledProperty);
set => SetValue(IsRestoreFocusDisabledProperty, value);
}

Expand Down Expand Up @@ -958,7 +976,7 @@ private void OnLoaded(object sender, RoutedEventArgs routedEventArgs)

private void OnPreviewGotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{

}

[SecurityCritical]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,15 @@
Content="{TemplateBinding ContentControl.Content}"
ContentStringFormat="{TemplateBinding ContentControl.ContentStringFormat}"
ContentTemplate="{TemplateBinding ContentControl.ContentTemplate}"
Opacity="1" />
Opacity="1">
<ContentPresenter.Effect>
<MultiBinding Converter="{x:Static converters:DialogBackgroundBlurConverter.Instance}">
<Binding Path="IsOpen" RelativeSource="{RelativeSource TemplatedParent}" />
<Binding Path="ApplyBlurBackground" RelativeSource="{RelativeSource TemplatedParent}" />
<Binding Path="BlurRadius" RelativeSource="{RelativeSource TemplatedParent}" />
</MultiBinding>
</ContentPresenter.Effect>
</ContentPresenter>
</AdornerDecorator>
<Grid x:Name="PART_ContentCoverGrid"
Background="Transparent"
Expand Down Expand Up @@ -386,7 +394,15 @@
Content="{TemplateBinding ContentControl.Content}"
ContentStringFormat="{TemplateBinding ContentControl.ContentStringFormat}"
ContentTemplate="{TemplateBinding ContentControl.ContentTemplate}"
Opacity="1" />
Opacity="1">
<ContentPresenter.Effect>
<MultiBinding Converter="{x:Static converters:DialogBackgroundBlurConverter.Instance}">
<Binding Path="IsOpen" RelativeSource="{RelativeSource TemplatedParent}" />
<Binding Path="ApplyBlurBackground" RelativeSource="{RelativeSource TemplatedParent}" />
<Binding Path="BlurRadius" RelativeSource="{RelativeSource TemplatedParent}" />
</MultiBinding>
</ContentPresenter.Effect>
</ContentPresenter>

<Grid x:Name="PART_ContentCoverGrid"
Background="Transparent"
Expand Down
Loading