Skip to content

Commit 45c2a54

Browse files
Fix: Slider default step causes crash on Windows OS (callstack#260)
* Calculate stepFrequency for default step The default value of step is set to zero for all platforms. On Windows OS this approach causes the slider XAML resolution to be at the level of infinite. Further it causes the application to crash whenever user tries to move the slider. To fix that this commit implements the step calculation done when noticing the default value of <Slider/> to be zero (or zero set by user) and return the percentage value of Slider's range (from min to max). * Update README with default step for Windows Due to changing the behavior of Slider's step for Windows the README required an update. The description of default step approach mentiones only the range calculation without the implementation details. * Change min and max values to doubles The integer type was previously used for the min and max values of the Slider on Windows. This was however changed in the delivery of: https://github.com/callstack/react-native-slider/pull/259/files but was partially inverted as the result of rebasing the conflicting changes. As the int->double change is good to have and should be kept, this commit fixes the merge mistake by changing the integer type to double.
1 parent ee45997 commit 45c2a54

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,8 @@ Callback continuously called while the user is dragging the slider.
178178

179179
Step value of the slider. The value should be between 0 and (maximumValue - minimumValue). Default value is 0.
180180

181+
On Windows OS the default value is 1% of slider's range (from `minimumValue` to `maximumValue`).
182+
181183
| Type | Required |
182184
| ------ | -------- |
183185
| number | No |

src/windows/SliderWindows/SliderView.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,9 @@ namespace winrt::SliderWindows::implementation {
9696
if (propertyValue.IsNull()) {
9797
this->ClearValue(xaml::Controls::Slider::StepFrequencyProperty());
9898
}
99-
else if (propertyValue.AsDouble() != 0) {
100-
this->StepFrequency(propertyValue.AsDouble());
101-
}
10299
else {
103-
this->StepFrequency(c_stepDefault);
100+
auto&& step = CalculateStepFrequencyPercentageValue(propertyValue.AsDouble());
101+
this->StepFrequency(step);
104102
}
105103
}
106104
else if (propertyName == "inverted") {
@@ -237,4 +235,13 @@ namespace winrt::SliderWindows::implementation {
237235
});
238236
}
239237
}
238+
239+
const double SliderView::CalculateStepFrequencyPercentageValue(const double& stepPropertyValue) const noexcept {
240+
if (stepPropertyValue != 0) {
241+
return stepPropertyValue;
242+
}
243+
else {
244+
return (m_maxValue - m_minValue) / 100.f;
245+
}
246+
}
240247
}

src/windows/SliderWindows/SliderView.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,11 @@ namespace winrt::SliderWindows::implementation {
3434
void OnManipulationCompletedHandler(
3535
winrt::Windows::Foundation::IInspectable const& sender,
3636
xaml::Input::ManipulationCompletedRoutedEventArgs const& args);
37-
38-
double m_value, m_maxValue, m_minValue;
39-
const double c_stepDefault = 0.1;
37+
38+
const double CalculateStepFrequencyPercentageValue(const double& stepPropertyValue) const noexcept;
39+
40+
double m_maxValue, m_minValue;
41+
double m_value;
4042
};
4143
}
4244

0 commit comments

Comments
 (0)