From c43ee1c52d212e74aaaa02105af4c2f6f5ac277f Mon Sep 17 00:00:00 2001 From: Jan-Peter Zurek Date: Thu, 25 Nov 2021 15:35:14 +0100 Subject: [PATCH] Fix slider behaviour for bigger thumbs Thumb now moves synchronously with pointer instead of relativ to track position. --- src/Avalonia.Controls/Slider.cs | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/Avalonia.Controls/Slider.cs b/src/Avalonia.Controls/Slider.cs index 6419981fb12..6af40b02812 100644 --- a/src/Avalonia.Controls/Slider.cs +++ b/src/Avalonia.Controls/Slider.cs @@ -331,16 +331,17 @@ private void TrackPressed(object sender, PointerPressedEventArgs e) } } - private void MoveToPoint(PointerPoint x) + private void MoveToPoint(PointerPoint posOnTrack) { var orient = Orientation == Orientation.Horizontal; - - var pointDen = orient ? _track.Bounds.Width : _track.Bounds.Height; - // Just add epsilon to avoid NaN in case 0/0 - pointDen += double.Epsilon; - - var pointNum = orient ? x.Position.X : x.Position.Y; - var logicalPos = MathUtilities.Clamp(pointNum / pointDen, 0.0d, 1.0d); + var thumbLength = (orient + ? _track.Thumb.Bounds.Width + : _track.Thumb.Bounds.Height) + double.Epsilon; + var trackLength = (orient + ? _track.Bounds.Width + : _track.Bounds.Height) - thumbLength; + var trackPos = orient ? posOnTrack.Position.X : posOnTrack.Position.Y; + var logicalPos = MathUtilities.Clamp((trackPos - thumbLength * 0.5) / trackLength, 0.0d, 1.0d); var invert = orient ? IsDirectionReversed ? 1 : 0 : IsDirectionReversed ? 0 : 1;