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

[net7.0] [Windows] Fix Slider crash using the same Minimum, Maximum value #12784

Merged
merged 1 commit into from
Jan 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,16 @@
Clicked="OnUpdateMinimumButtonClicked" />
<Button
Text="Update Maximum"
Clicked="OnUpdateMaximumButtonClicked" />
</HorizontalStackLayout>
</VerticalStackLayout>
Clicked="OnUpdateMaximumButtonClicked" />
</HorizontalStackLayout>
<Label
Text="Edge case - Same Minimum, Maximum and Value"
Style="{StaticResource Headline}"/>
<Slider
Maximum="100"
Minimum="100"
Value="100" />
</VerticalStackLayout>
</ScrollView>
</views:BasePage.Content>
</views:BasePage>
9 changes: 8 additions & 1 deletion src/Core/src/Platform/Windows/SliderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,14 @@ public static class SliderExtensions
{
static void UpdateIncrement(this Slider nativeSlider, ISlider slider)
{
double stepping = Math.Min((slider.Maximum - slider.Minimum) / 1000, 1);
var difference = slider.Maximum - slider.Minimum;

double stepping = 1;

// Setting the Slider SmallChange property to 0 would throw an System.ArgumentException.
if (difference != 0)
stepping = Math.Min((difference) / 1000, 1);

nativeSlider.StepFrequency = stepping;
nativeSlider.SmallChange = stepping;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,40 @@
using System.Threading.Tasks;
using Microsoft.Maui.DeviceTests.Stubs;
using Microsoft.UI.Xaml.Controls;
using Xunit;

namespace Microsoft.Maui.DeviceTests
{
public partial class SliderHandlerTests
{
// https://github.com/dotnet/maui/issues/12405
[Theory(DisplayName = "Platform Slider SmallChange Initializes Correctly")]
[InlineData(0, 1, 0)]
[InlineData(0, 1, 0.5)]
[InlineData(0, 1, 1)]
[InlineData(0, 100, 0)]
[InlineData(0, 100, 1)]
[InlineData(0, 100, 5)]
[InlineData(0, 100, 50)]
[InlineData(0, 100, 100)]
[InlineData(0, 100, 10000)]
[InlineData(0, 100, -10000)]
[InlineData(0, 10000, 10000)]
[InlineData(0, 10000, -10000)]
public async Task SmallChangeInitializesCorrectly(double min, double max, double value)
{
var slider = new SliderStub()
{
Maximum = max,
Minimum = min,
Value = value
};

var expected = await GetValueAsync(slider, GetSmallChange);

Assert.True(expected != 0);
}

Slider GetNativeSlider(SliderHandler sliderHandler) =>
sliderHandler.PlatformView;

Expand All @@ -17,5 +46,8 @@ double GetNativeMinimum(SliderHandler sliderHandler) =>

double GetNativeMaximum(SliderHandler sliderHandler) =>
GetNativeSlider(sliderHandler).Maximum;

double GetSmallChange(SliderHandler sliderHandler) =>
GetNativeSlider(sliderHandler).SmallChange;
}
}
}