Skip to content

Commit

Permalink
Fix WinUI Slider edge case (#12514) Fixes #12405
Browse files Browse the repository at this point in the history
  • Loading branch information
jsuarezruiz committed Jan 11, 2023
1 parent 2741be0 commit c296e16
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 5 deletions.
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;
}
}
}

0 comments on commit c296e16

Please sign in to comment.