Skip to content

Commit

Permalink
Switched default to NaN
Browse files Browse the repository at this point in the history
  • Loading branch information
dpaulino committed Aug 17, 2021
1 parent 36f46be commit 79fba6b
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 10 deletions.
25 changes: 15 additions & 10 deletions Microsoft.Toolkit.Uwp.UI/Triggers/ControlSizeTrigger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Linq;
using Windows.UI.Xaml;

namespace Microsoft.Toolkit.Uwp.UI.Triggers
Expand Down Expand Up @@ -49,7 +50,7 @@ public double MaxWidth
nameof(MaxWidth),
typeof(double),
typeof(ControlSizeTrigger),
new PropertyMetadata(double.PositiveInfinity, (d, e) => ((ControlSizeTrigger)d).UpdateTrigger()));
new PropertyMetadata(double.NaN, (d, e) => ((ControlSizeTrigger)d).UpdateTrigger()));

/// <summary>
/// Gets or sets the min width at which to trigger.
Expand All @@ -69,7 +70,7 @@ public double MinWidth
nameof(MinWidth),
typeof(double),
typeof(ControlSizeTrigger),
new PropertyMetadata(0.0, (d, e) => ((ControlSizeTrigger)d).UpdateTrigger()));
new PropertyMetadata(double.NaN, (d, e) => ((ControlSizeTrigger)d).UpdateTrigger()));

/// <summary>
/// Gets or sets the max height at which to trigger.
Expand All @@ -89,7 +90,7 @@ public double MaxHeight
nameof(MaxHeight),
typeof(double),
typeof(ControlSizeTrigger),
new PropertyMetadata(double.PositiveInfinity, (d, e) => ((ControlSizeTrigger)d).UpdateTrigger()));
new PropertyMetadata(double.NaN, (d, e) => ((ControlSizeTrigger)d).UpdateTrigger()));

/// <summary>
/// Gets or sets the min height at which to trigger.
Expand All @@ -109,7 +110,7 @@ public double MinHeight
nameof(MinHeight),
typeof(double),
typeof(ControlSizeTrigger),
new PropertyMetadata(0.0, (d, e) => ((ControlSizeTrigger)d).UpdateTrigger()));
new PropertyMetadata(double.NaN, (d, e) => ((ControlSizeTrigger)d).UpdateTrigger()));

/// <summary>
/// Gets or sets the element whose width will observed
Expand Down Expand Up @@ -173,13 +174,17 @@ private void UpdateTrigger()
return;
}

bool activate = MinWidth <= TargetElement.ActualWidth &&
TargetElement.ActualWidth < MaxWidth &&
MinHeight <= TargetElement.ActualHeight &&
TargetElement.ActualHeight < MaxHeight;
var elementWidth = TargetElement.ActualWidth;
var elementHeight = TargetElement.ActualHeight;

IsActive = activate;
SetActive(activate);
var results = new bool?[4];
results[0] = double.IsNaN(MinHeight) ? null : MinHeight <= elementHeight;
results[1] = double.IsNaN(MinWidth) ? null : MinWidth <= elementWidth;
results[2] = double.IsNaN(MaxHeight) ? null : elementHeight < MaxHeight;
results[3] = double.IsNaN(MaxWidth) ? null : elementWidth < MaxWidth;

IsActive = results.All(x => x is null) ? false : !results.Any(x => x == false);
SetActive(IsActive);
}
}
}
87 changes: 87 additions & 0 deletions UnitTests/UnitTests.UWP/UI/Triggers/Test_ControlSizeTrigger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,93 @@ await App.DispatcherQueue.EnqueueAsync(() =>
});
}

[DataTestMethod]
[DataRow(450, 450, false)]
[DataRow(400, 400, false)]
[DataRow(500, 500, false)]
[DataRow(399, 400, false)]
[DataRow(400, 399, false)]
public async Task ControlSizeNullTriggerTest(double width, double height, bool expectedResult)
{
await App.DispatcherQueue.EnqueueAsync(() =>
{
Grid grid = CreateGrid(width, height);
var trigger = new ControlSizeTrigger();
trigger.TargetElement = grid;
Assert.AreEqual(expectedResult, trigger.IsActive);
});
}

[DataTestMethod]
[DataRow(400, 400, true)]
[DataRow(400, 399, false)]
public async Task ControlSizeMinHeightTriggerTest(double width, double height, bool expectedResult)
{
await App.DispatcherQueue.EnqueueAsync(() =>
{
Grid grid = CreateGrid(width, height);
var trigger = new ControlSizeTrigger();
trigger.TargetElement = grid;
trigger.MinHeight = 400;
Assert.AreEqual(expectedResult, trigger.IsActive);
});
}

[DataTestMethod]
[DataRow(399, 400, false)]
[DataRow(400, 400, true)]
public async Task ControlSizeMinWidthTriggerTest(double width, double height, bool expectedResult)
{
await App.DispatcherQueue.EnqueueAsync(() =>
{
Grid grid = CreateGrid(width, height);
var trigger = new ControlSizeTrigger();
trigger.TargetElement = grid;
trigger.MinWidth = 400;
Assert.AreEqual(expectedResult, trigger.IsActive);
});
}

[DataTestMethod]
[DataRow(450, 450, false)]
[DataRow(450, 449, true)]
public async Task ControlSizeMaxHeightTriggerTest(double width, double height, bool expectedResult)
{
await App.DispatcherQueue.EnqueueAsync(() =>
{
Grid grid = CreateGrid(width, height);
var trigger = new ControlSizeTrigger();
trigger.TargetElement = grid;
trigger.MaxHeight = 450;
Assert.AreEqual(expectedResult, trigger.IsActive);
});
}

[DataTestMethod]
[DataRow(450, 450, false)]
[DataRow(449, 450, true)]
public async Task ControlSizeMaxWidthTriggerTest(double width, double height, bool expectedResult)
{
await App.DispatcherQueue.EnqueueAsync(() =>
{
Grid grid = CreateGrid(width, height);
var trigger = new ControlSizeTrigger();
trigger.TargetElement = grid;
trigger.MaxWidth = 450;
Assert.AreEqual(expectedResult, trigger.IsActive);
});
}

private Grid CreateGrid(double width, double height)
{
var grid = new Grid()
Expand Down

0 comments on commit 79fba6b

Please sign in to comment.