Skip to content

Commit cda2873

Browse files
committed
performance improvements for Slider
1 parent 4ba122b commit cda2873

1 file changed

Lines changed: 22 additions & 8 deletions

File tree

src/TemplateUI/Controls/Slider/Slider.cs

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ public class Slider : TemplatedView
1414
BoxView _progress;
1515
ContentView _thumb;
1616
double _previousPosition;
17+
double? _previousVal = null;
18+
1719

1820
public event EventHandler<ValueChangedEventArgs> ValueChanged;
1921

@@ -113,7 +115,7 @@ protected override void OnPropertyChanged([CallerMemberName] string propertyName
113115
protected override void OnSizeAllocated(double width, double height)
114116
{
115117
base.OnSizeAllocated(width, height);
116-
UpdateValue();
118+
UpdateValue(true);
117119
}
118120

119121
void UpdateIsEnabled()
@@ -122,16 +124,16 @@ void UpdateIsEnabled()
122124
{
123125
var panGestureRecognizer = new PanGestureRecognizer();
124126
panGestureRecognizer.PanUpdated += OnThumbPanUpdated;
125-
_thumb?.GestureRecognizers?.Add(panGestureRecognizer);
127+
_thumb.GestureRecognizers.Add(panGestureRecognizer);
126128
}
127129
else
128130
{
129131
_trackBackground.GestureRecognizers.Clear();
130-
_thumb?.GestureRecognizers?.Clear();
132+
_thumb.GestureRecognizers.Clear();
131133
}
132134
}
133135

134-
void UpdateValue()
136+
void UpdateValue(bool isNecessary = false)
135137
{
136138
var min = Minimum;
137139
var max = Maximum;
@@ -144,9 +146,12 @@ void UpdateValue()
144146
}
145147
ValueChanged?.Invoke(this, new ValueChangedEventArgs(valChecked));
146148

149+
if (!isNecessary && _previousVal == val)
150+
return;
151+
147152
var half = ThumbHalfWidth;
148-
var position = ConvertRangeValue(val, min, max, half, _trackBackground.Width - half) - half;
149-
_thumb.TranslationX = _progress.WidthRequest = position;
153+
var thumbCenterpostion = ConvertRangeValue(val, min, max, half, _trackBackground.Width -half);
154+
SetThumbPosition(thumbCenterpostion - half, thumbCenterpostion, val);
150155
}
151156

152157
void OnThumbPanUpdated(object sender, PanUpdatedEventArgs e)
@@ -167,8 +172,10 @@ void OnThumbPanUpdated(object sender, PanUpdatedEventArgs e)
167172
if (Device.RuntimePlatform == Device.Android)
168173
totalX += _thumb.TranslationX;
169174

170-
var position = CheckValueByRange(totalX + half, half, maxPosition);
171-
Value = ConvertRangeValue(position, half, maxPosition, Minimum, Maximum);
175+
var thumbCenterPostion = CheckValueByRange(totalX + half, half, maxPosition);
176+
var val = ConvertRangeValue(thumbCenterPostion, half, maxPosition, Minimum, Maximum);
177+
SetThumbPosition(thumbCenterPostion - half, thumbCenterPostion, val);
178+
Value = val;
172179
break;
173180
case GestureStatus.Completed:
174181
case GestureStatus.Canceled:
@@ -184,5 +191,12 @@ double ConvertRangeValue(double oldVal, double oldMin, double oldMax, double min
184191

185192
double CheckValueByRange(double val, double min, double max)
186193
=> val <= min ? min : val >= max ? max : val;
194+
195+
void SetThumbPosition(double thumbPosition, double progrssWidth, double val)
196+
{
197+
_previousVal = val;
198+
_thumb.TranslationX = thumbPosition;
199+
_progress.WidthRequest = progrssWidth;
200+
}
187201
}
188202
}

0 commit comments

Comments
 (0)