Skip to content

Commit 79fba6b

Browse files
committed
Switched default to NaN
1 parent 36f46be commit 79fba6b

File tree

2 files changed

+102
-10
lines changed

2 files changed

+102
-10
lines changed

Microsoft.Toolkit.Uwp.UI/Triggers/ControlSizeTrigger.cs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
using System.Linq;
56
using Windows.UI.Xaml;
67

78
namespace Microsoft.Toolkit.Uwp.UI.Triggers
@@ -49,7 +50,7 @@ public double MaxWidth
4950
nameof(MaxWidth),
5051
typeof(double),
5152
typeof(ControlSizeTrigger),
52-
new PropertyMetadata(double.PositiveInfinity, (d, e) => ((ControlSizeTrigger)d).UpdateTrigger()));
53+
new PropertyMetadata(double.NaN, (d, e) => ((ControlSizeTrigger)d).UpdateTrigger()));
5354

5455
/// <summary>
5556
/// Gets or sets the min width at which to trigger.
@@ -69,7 +70,7 @@ public double MinWidth
6970
nameof(MinWidth),
7071
typeof(double),
7172
typeof(ControlSizeTrigger),
72-
new PropertyMetadata(0.0, (d, e) => ((ControlSizeTrigger)d).UpdateTrigger()));
73+
new PropertyMetadata(double.NaN, (d, e) => ((ControlSizeTrigger)d).UpdateTrigger()));
7374

7475
/// <summary>
7576
/// Gets or sets the max height at which to trigger.
@@ -89,7 +90,7 @@ public double MaxHeight
8990
nameof(MaxHeight),
9091
typeof(double),
9192
typeof(ControlSizeTrigger),
92-
new PropertyMetadata(double.PositiveInfinity, (d, e) => ((ControlSizeTrigger)d).UpdateTrigger()));
93+
new PropertyMetadata(double.NaN, (d, e) => ((ControlSizeTrigger)d).UpdateTrigger()));
9394

9495
/// <summary>
9596
/// Gets or sets the min height at which to trigger.
@@ -109,7 +110,7 @@ public double MinHeight
109110
nameof(MinHeight),
110111
typeof(double),
111112
typeof(ControlSizeTrigger),
112-
new PropertyMetadata(0.0, (d, e) => ((ControlSizeTrigger)d).UpdateTrigger()));
113+
new PropertyMetadata(double.NaN, (d, e) => ((ControlSizeTrigger)d).UpdateTrigger()));
113114

114115
/// <summary>
115116
/// Gets or sets the element whose width will observed
@@ -173,13 +174,17 @@ private void UpdateTrigger()
173174
return;
174175
}
175176

176-
bool activate = MinWidth <= TargetElement.ActualWidth &&
177-
TargetElement.ActualWidth < MaxWidth &&
178-
MinHeight <= TargetElement.ActualHeight &&
179-
TargetElement.ActualHeight < MaxHeight;
177+
var elementWidth = TargetElement.ActualWidth;
178+
var elementHeight = TargetElement.ActualHeight;
180179

181-
IsActive = activate;
182-
SetActive(activate);
180+
var results = new bool?[4];
181+
results[0] = double.IsNaN(MinHeight) ? null : MinHeight <= elementHeight;
182+
results[1] = double.IsNaN(MinWidth) ? null : MinWidth <= elementWidth;
183+
results[2] = double.IsNaN(MaxHeight) ? null : elementHeight < MaxHeight;
184+
results[3] = double.IsNaN(MaxWidth) ? null : elementWidth < MaxWidth;
185+
186+
IsActive = results.All(x => x is null) ? false : !results.Any(x => x == false);
187+
SetActive(IsActive);
183188
}
184189
}
185190
}

UnitTests/UnitTests.UWP/UI/Triggers/Test_ControlSizeTrigger.cs

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,93 @@ await App.DispatcherQueue.EnqueueAsync(() =>
3737
});
3838
}
3939

40+
[DataTestMethod]
41+
[DataRow(450, 450, false)]
42+
[DataRow(400, 400, false)]
43+
[DataRow(500, 500, false)]
44+
[DataRow(399, 400, false)]
45+
[DataRow(400, 399, false)]
46+
public async Task ControlSizeNullTriggerTest(double width, double height, bool expectedResult)
47+
{
48+
await App.DispatcherQueue.EnqueueAsync(() =>
49+
{
50+
Grid grid = CreateGrid(width, height);
51+
var trigger = new ControlSizeTrigger();
52+
53+
trigger.TargetElement = grid;
54+
55+
Assert.AreEqual(expectedResult, trigger.IsActive);
56+
});
57+
}
58+
59+
[DataTestMethod]
60+
[DataRow(400, 400, true)]
61+
[DataRow(400, 399, false)]
62+
public async Task ControlSizeMinHeightTriggerTest(double width, double height, bool expectedResult)
63+
{
64+
await App.DispatcherQueue.EnqueueAsync(() =>
65+
{
66+
Grid grid = CreateGrid(width, height);
67+
var trigger = new ControlSizeTrigger();
68+
69+
trigger.TargetElement = grid;
70+
trigger.MinHeight = 400;
71+
72+
Assert.AreEqual(expectedResult, trigger.IsActive);
73+
});
74+
}
75+
76+
[DataTestMethod]
77+
[DataRow(399, 400, false)]
78+
[DataRow(400, 400, true)]
79+
public async Task ControlSizeMinWidthTriggerTest(double width, double height, bool expectedResult)
80+
{
81+
await App.DispatcherQueue.EnqueueAsync(() =>
82+
{
83+
Grid grid = CreateGrid(width, height);
84+
var trigger = new ControlSizeTrigger();
85+
86+
trigger.TargetElement = grid;
87+
trigger.MinWidth = 400;
88+
89+
Assert.AreEqual(expectedResult, trigger.IsActive);
90+
});
91+
}
92+
93+
[DataTestMethod]
94+
[DataRow(450, 450, false)]
95+
[DataRow(450, 449, true)]
96+
public async Task ControlSizeMaxHeightTriggerTest(double width, double height, bool expectedResult)
97+
{
98+
await App.DispatcherQueue.EnqueueAsync(() =>
99+
{
100+
Grid grid = CreateGrid(width, height);
101+
var trigger = new ControlSizeTrigger();
102+
103+
trigger.TargetElement = grid;
104+
trigger.MaxHeight = 450;
105+
106+
Assert.AreEqual(expectedResult, trigger.IsActive);
107+
});
108+
}
109+
110+
[DataTestMethod]
111+
[DataRow(450, 450, false)]
112+
[DataRow(449, 450, true)]
113+
public async Task ControlSizeMaxWidthTriggerTest(double width, double height, bool expectedResult)
114+
{
115+
await App.DispatcherQueue.EnqueueAsync(() =>
116+
{
117+
Grid grid = CreateGrid(width, height);
118+
var trigger = new ControlSizeTrigger();
119+
120+
trigger.TargetElement = grid;
121+
trigger.MaxWidth = 450;
122+
123+
Assert.AreEqual(expectedResult, trigger.IsActive);
124+
});
125+
}
126+
40127
private Grid CreateGrid(double width, double height)
41128
{
42129
var grid = new Grid()

0 commit comments

Comments
 (0)