@@ -47,6 +47,7 @@ class UIBaseSlider(UIInteractiveWidget, metaclass=ABCMeta):
47
47
size_hint_min: Minimum size hint of the slider.
48
48
size_hint_max: Maximum size hint of the slider.
49
49
style: Used to style the slider for different states.
50
+ step: Smallest change the slider value can move by.
50
51
**kwargs: Passed to UIInteractiveWidget.
51
52
52
53
"""
@@ -67,6 +68,7 @@ def __init__(
67
68
size_hint_min = None ,
68
69
size_hint_max = None ,
69
70
style : Union [Mapping [str , UISliderStyle ], None ] = None ,
71
+ step : Union [float , None ] = None ,
70
72
** kwargs ,
71
73
):
72
74
super ().__init__ (
@@ -81,7 +83,8 @@ def __init__(
81
83
** kwargs ,
82
84
)
83
85
84
- self .value = value
86
+ self .step = step
87
+ self .value = self ._apply_step (value )
85
88
self .min_value = min_value
86
89
self .max_value = max_value
87
90
@@ -95,6 +98,13 @@ def __init__(
95
98
96
99
self .register_event_type ("on_change" )
97
100
101
+ def _apply_step (self , value : float ):
102
+ if self .step :
103
+ inverse = 1 / self .step
104
+ return round (value * inverse ) / inverse
105
+
106
+ return value
107
+
98
108
def _x_for_value (self , value : float ):
99
109
"""Provides the x coordinate for the given value."""
100
110
@@ -110,7 +120,9 @@ def norm_value(self):
110
120
@norm_value .setter
111
121
def norm_value (self , value ):
112
122
"""Normalized value between 0.0 and 1.0"""
113
- self .value = min (value * (self .max_value - self .min_value ) + self .min_value , self .max_value )
123
+ self .value = self ._apply_step (
124
+ min (value * (self .max_value - self .min_value ) + self .min_value , self .max_value )
125
+ )
114
126
115
127
@property
116
128
def _thumb_x (self ):
@@ -254,7 +266,7 @@ class UISlider(UIStyledWidget[UISliderStyle], UIBaseSlider):
254
266
width: Width of the slider.
255
267
height: Height of the slider.
256
268
style: Used to style the slider for different states.
257
-
269
+ step: Smallest change the slider value can move by.
258
270
"""
259
271
260
272
UIStyle = UISliderStyle
@@ -294,6 +306,7 @@ def __init__(
294
306
size_hint_min = None ,
295
307
size_hint_max = None ,
296
308
style : Union [dict [str , UISliderStyle ], None ] = None ,
309
+ step : Union [float , None ] = None ,
297
310
** kwargs ,
298
311
):
299
312
super ().__init__ (
@@ -308,6 +321,7 @@ def __init__(
308
321
size_hint_min = size_hint_min ,
309
322
size_hint_max = size_hint_max ,
310
323
style = style or UISlider .DEFAULT_STYLE ,
324
+ step = step ,
311
325
** kwargs ,
312
326
)
313
327
0 commit comments