Skip to content

Commit 2a675e3

Browse files
authored
DialogHost now properly respect DialogTheme (#2490)
1 parent e0b2f7b commit 2a675e3

File tree

4 files changed

+121
-7
lines changed

4 files changed

+121
-7
lines changed

MaterialDesignThemes.UITests/MaterialDesignSpec.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
namespace MaterialDesignThemes.UITests
1+
using System.Threading.Tasks;
2+
using System.Windows.Media;
3+
using MaterialDesignColors.ColorManipulation;
4+
using Xunit;
5+
6+
namespace MaterialDesignThemes.UITests
27
{
38
public static class MaterialDesignSpec
49
{
@@ -13,5 +18,13 @@ public static class MaterialDesignSpec
1318
/// https://www.material.io/design/usability/accessibility.html#color-and-contrast
1419
/// </summary>
1520
public const double MinimumContrastLargeText = 3.0;
21+
22+
public static void AssertContrastRatio(Color foreground, Color background, double minimumContrastRatio)
23+
{
24+
const double tollerance = 0.1;
25+
26+
var ratio = ColorAssist.ContrastRatio(foreground, background);
27+
Assert.True(ratio >= minimumContrastRatio - tollerance, $"Contrast ratio '{ratio}' is less than {minimumContrastRatio} with a tollerance of 0.1");
28+
}
1629
}
1730
}

MaterialDesignThemes.UITests/WPF/DialogHosts/DialogHostTests.cs

Lines changed: 82 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010
using Xunit;
1111
using Xunit.Abstractions;
1212

13+
using static MaterialDesignThemes.UITests.MaterialDesignSpec;
14+
1315
namespace MaterialDesignThemes.UITests.WPF.DialogHosts
1416
{
1517
public class DialogHostTests : TestBase
16-
{
18+
{
1719
public DialogHostTests(ITestOutputHelper output) : base(output)
1820
{
1921
}
@@ -101,7 +103,7 @@ public async Task FontSettingsSholdInheritIntoDialog()
101103
</Grid>");
102104
var showButton1 = await grid.GetElement<Button>("ShowButton1");
103105
var showButton2 = await grid.GetElement<Button>("ShowButton2");
104-
106+
105107
await showButton1.LeftClick();
106108
await showButton2.LeftClick();
107109
await Task.Delay(300);
@@ -163,5 +165,83 @@ await Wait.For(async () =>
163165
Assert.Equal(Colors.Red, await card2.GetBackgroundColor());
164166
});
165167
}
168+
169+
[Theory]
170+
[InlineData(BaseTheme.Inherit)]
171+
[InlineData(BaseTheme.Dark)]
172+
[InlineData(BaseTheme.Light)]
173+
public async Task DialogBackgroundShouldInheritThemeBackground(BaseTheme dialogTheme)
174+
{
175+
await using var recorder = new TestRecorder(App);
176+
177+
IVisualElement grid = await LoadXaml<Grid>($@"
178+
<Grid>
179+
<Grid.ColumnDefinitions>
180+
<ColumnDefinition />
181+
<ColumnDefinition />
182+
</Grid.ColumnDefinitions>
183+
184+
<materialDesign:DialogHost DialogTheme=""{dialogTheme}"" x:Name=""DialogHost1"">
185+
<materialDesign:DialogHost.DialogContent>
186+
<TextBlock Text=""Some Text"" x:Name=""TextBlock1"" Margin=""50"" />
187+
</materialDesign:DialogHost.DialogContent>
188+
<Button Content=""Show Dialog"" x:Name=""ShowButton1"" Command=""{{x:Static materialDesign:DialogHost.OpenDialogCommand}}"" />
189+
</materialDesign:DialogHost>
190+
191+
<materialDesign:DialogHost Style=""{{StaticResource MaterialDesignEmbeddedDialogHost}}"" DialogTheme=""{dialogTheme}"" x:Name=""DialogHost2"" Grid.Column=""1"">
192+
<materialDesign:DialogHost.DialogContent>
193+
<TextBlock Text=""Some Text"" x:Name=""TextBlock2"" Margin=""50"" />
194+
</materialDesign:DialogHost.DialogContent>
195+
<Button Content=""Show Dialog"" x:Name=""ShowButton2"" Command=""{{x:Static materialDesign:DialogHost.OpenDialogCommand}}"" />
196+
</materialDesign:DialogHost>
197+
198+
</Grid>");
199+
var showButton1 = await grid.GetElement<Button>("ShowButton1");
200+
var showButton2 = await grid.GetElement<Button>("ShowButton2");
201+
202+
await showButton1.LeftClick();
203+
await showButton2.LeftClick();
204+
205+
var dialogHost1 = await grid.GetElement<DialogHost>("DialogHost1");
206+
var dialogHost2 = await grid.GetElement<DialogHost>("DialogHost2");
207+
208+
var card1 = await Wait.For(async () => await dialogHost1.GetElement<Card>("PART_PopupContentElement"));
209+
var card2 = await Wait.For(async () => await dialogHost2.GetElement<Card>("PART_PopupContentElement"));
210+
211+
IResource paperResource1 = await card1.GetResource("MaterialDesignPaper");
212+
var paperBrush1 = paperResource1.GetAs<SolidColorBrush>();
213+
Assert.NotNull(paperBrush1);
214+
paperBrush1!.Freeze();
215+
IResource paperResource2 = await card1.GetResource("MaterialDesignPaper");
216+
var paperBrush2 = paperResource2.GetAs<SolidColorBrush>();
217+
Assert.NotNull(paperBrush2);
218+
paperBrush2!.Freeze();
219+
220+
Assert.Equal(paperBrush1.Color, await card1.GetBackgroundColor());
221+
Assert.Equal(paperBrush2.Color, await card2.GetBackgroundColor());
222+
223+
var textBlock1 = await dialogHost1.GetElement<TextBlock>("TextBlock1");
224+
var textBlock2 = await dialogHost2.GetElement<TextBlock>("TextBlock2");
225+
226+
await Wait.For(async () =>
227+
{
228+
Color? foreground1 = await textBlock1.GetForegroundColor();
229+
Assert.NotNull(foreground1);
230+
AssertContrastRatio(
231+
foreground1.Value,
232+
await textBlock1.GetEffectiveBackground(),
233+
MinimumContrastSmallText);
234+
});
235+
236+
await Wait.For(async () =>
237+
{
238+
Color? foreground2 = await textBlock2.GetForegroundColor();
239+
Assert.NotNull(foreground2);
240+
AssertContrastRatio(
241+
foreground2.Value,
242+
await textBlock2.GetEffectiveBackground(),
243+
MinimumContrastSmallText);
244+
});
245+
}
166246
}
167247
}

MaterialDesignThemes.Wpf/DialogHost.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ public Brush? OverlayBackground
507507
}
508508

509509
public static readonly DependencyProperty DialogBackgroundProperty = DependencyProperty.Register(
510-
nameof(DialogBackground), typeof(Brush), typeof(DialogHost), new PropertyMetadata(Brushes.White));
510+
nameof(DialogBackground), typeof(Brush), typeof(DialogHost), new PropertyMetadata(null));
511511

512512
/// <summary>
513513
/// Represents the brush for the Dialog's background

MaterialDesignThemes.Wpf/Themes/MaterialDesignTheme.DialogHost.xaml

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@
155155
Margin="{TemplateBinding DialogMargin}"
156156
wpf:ShadowAssist.ShadowDepth="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(wpf:ShadowAssist.ShadowDepth)}"
157157
UniformCornerRadius="4"
158-
Background="{Binding DialogBackground, RelativeSource={RelativeSource TemplatedParent}}"
158+
Tag="{TemplateBinding DialogBackground}"
159159
TextElement.Foreground="{DynamicResource MaterialDesignBody}"
160160
FocusManager.IsFocusScope="False"
161161
Foreground="{DynamicResource MaterialDesignBody}"
@@ -168,6 +168,16 @@
168168
ContentTemplate="{TemplateBinding DialogContentTemplate}"
169169
ContentTemplateSelector="{TemplateBinding DialogContentTemplateSelector}"
170170
ContentStringFormat="{TemplateBinding DialogContentStringFormat}">
171+
<wpf:Card.Style>
172+
<Style TargetType="wpf:Card" BasedOn="{StaticResource {x:Type wpf:Card}}">
173+
<Setter Property="Background" Value="{Binding Tag, RelativeSource={RelativeSource Self}}" />
174+
<Style.Triggers>
175+
<Trigger Property="Tag" Value="{x:Null}">
176+
<Setter Property="Background" Value="{DynamicResource MaterialDesignPaper}" />
177+
</Trigger>
178+
</Style.Triggers>
179+
</Style>
180+
</wpf:Card.Style>
171181
<wpf:Card.RenderTransform>
172182
<TransformGroup>
173183
<ScaleTransform x:Name="CardScaleTransform"
@@ -335,7 +345,8 @@
335345
x:Name="ContentPresenter" Opacity="1"
336346
Content="{TemplateBinding ContentControl.Content}" ContentTemplate="{TemplateBinding ContentControl.ContentTemplate}" ContentStringFormat="{TemplateBinding ContentControl.ContentStringFormat}" />
337347

338-
<Grid x:Name="PART_ContentCoverGrid" Background="{Binding OverlayBackground, RelativeSource={RelativeSource TemplatedParent}}"
348+
<Grid x:Name="PART_ContentCoverGrid"
349+
Background="{Binding OverlayBackground, RelativeSource={RelativeSource TemplatedParent}}"
339350
Opacity="0" IsHitTestVisible="False" Focusable="False">
340351
<Grid.Style>
341352
<Style TargetType="Grid">
@@ -356,7 +367,7 @@
356367
Margin="{TemplateBinding DialogMargin}"
357368
wpf:ShadowAssist.ShadowDepth="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(wpf:ShadowAssist.ShadowDepth)}"
358369
UniformCornerRadius="4"
359-
Background="{Binding DialogBackground, RelativeSource={RelativeSource TemplatedParent}}"
370+
Tag="{TemplateBinding DialogBackground}"
360371
TextElement.Foreground="{DynamicResource MaterialDesignBody}"
361372
FocusManager.IsFocusScope="False"
362373
Foreground="{DynamicResource MaterialDesignBody}"
@@ -375,6 +386,16 @@
375386
ScaleY="0" />
376387
</TransformGroup>
377388
</wpf:Card.RenderTransform>
389+
<wpf:Card.Style>
390+
<Style TargetType="wpf:Card" BasedOn="{StaticResource {x:Type wpf:Card}}">
391+
<Setter Property="Background" Value="{Binding Tag, RelativeSource={RelativeSource Self}}" />
392+
<Style.Triggers>
393+
<Trigger Property="Tag" Value="{x:Null}">
394+
<Setter Property="Background" Value="{DynamicResource MaterialDesignPaper}" />
395+
</Trigger>
396+
</Style.Triggers>
397+
</Style>
398+
</wpf:Card.Style>
378399
</wpf:Card>
379400
</Grid>
380401

0 commit comments

Comments
 (0)