Skip to content

Commit

Permalink
fix: Fix Slider thumb not visible when Value is at maximum
Browse files Browse the repository at this point in the history
There was a bug occurring on iOS where the indicator Rectangle was having its size set from within SizeChanged before the Thumb had a chance to be arranged, leaving it to take the entire space and leave none for the Thumb.

As a workaround, return the Width/Height if ActualWidth/ActualHeight haven't been set yet.
  • Loading branch information
davidjohnoliver committed Sep 25, 2020
1 parent 38fec06 commit b9d0083
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using static Private.Infrastructure.TestServices;
#if NETFX_CORE
using Uno.UI.Extensions;
#elif __IOS__
using UIKit;
#elif __MACOS__
using AppKit;
#else
using Uno.UI;
#endif

namespace Uno.UI.RuntimeTests.Tests.Windows_UI_Xaml_Controls
{
[TestClass]
[RunsOnUIThread]
public class Given_Slider
{
[TestMethod]
public async Task When_Value_At_Maximum()
{
var slider = new Slider { Minimum = 0, Maximum = 100, Value = 100, Orientation = Orientation.Horizontal, Width = 320, VerticalAlignment = VerticalAlignment.Stretch };
WindowHelper.WindowContent = slider;

await WindowHelper.WaitForIdle();

var thumb = slider.FindFirstChild<Thumb>();

Assert.IsNotNull(thumb);

Assert.IsTrue(thumb.ActualWidth > 0);
Assert.IsTrue(thumb.ActualHeight > 0);
}
}
}
28 changes: 26 additions & 2 deletions src/Uno.UI/UI/Xaml/Controls/Slider/Slider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -278,20 +278,44 @@ private void ApplyValueToSlide()
{
if (_horizontalThumb != null && _horizontalDecreaseRect != null)
{
var maxWidth = ActualWidth - _horizontalThumb.ActualWidth;
var maxWidth = ActualWidth - GetHorizontalThumbWidth();
_horizontalDecreaseRect.Width = (float)((Value - Minimum) / (Maximum - Minimum)) * maxWidth;
}
}
else
{
if (_verticalThumb != null && _verticalDecreaseRect != null)
{
var maxHeight = ActualHeight - _verticalThumb.ActualHeight;
var maxHeight = ActualHeight - GetVerticalThumbHeight();
_verticalDecreaseRect.Height = (float)((Value - Minimum) / (Maximum - Minimum)) * maxHeight;
}
}
}

private double GetHorizontalThumbWidth()
{
// In some cases, because of the timing of SizeChanged, this may be called before the thumb has been measured. If so, we rely on the fact
// that the dimensions are hard-coded by the UWP and Fluent styles.
if (_horizontalThumb.ActualWidth == 0 && !double.IsNaN(_horizontalThumb.Width))
{
return _horizontalThumb.Width;
}

return _horizontalThumb.ActualWidth;
}

private double GetVerticalThumbHeight()
{
// In some cases, because of the timing of SizeChanged, this may be called before the thumb has been measured. If so, we rely on the fact
// that the dimensions are hard-coded by the UWP and Fluent styles.
if (_verticalThumb.ActualHeight == 0 && !double.IsNaN(_verticalThumb.Height))
{
return _verticalThumb.Height;
}

return _verticalThumb.ActualHeight;
}

/// <summary>
/// Get the snap frequency, given the current values of SnapsTo,
/// StepFrequency and TickFrequency, and sanitizing the value
Expand Down

0 comments on commit b9d0083

Please sign in to comment.