@@ -37,49 +37,50 @@ pub struct Stepper {
37
37
min : f64 ,
38
38
step : f64 ,
39
39
wrap : bool ,
40
- /// A closure that will be invoked when the value changed.
41
- value_changed : Box < dyn Fn ( & mut EventCtx , & mut f64 , & Env ) > ,
42
40
/// Keeps track of which button is currently triggered.
43
41
increase_active : bool ,
44
42
decrease_active : bool ,
45
43
timer_id : TimerToken ,
46
44
}
47
45
48
46
impl Stepper {
49
- pub fn new ( value_changed : impl Fn ( & mut EventCtx , & mut f64 , & Env ) + ' static ) -> Self {
47
+ pub fn new ( ) -> Self {
50
48
Stepper {
51
49
max : std:: f64:: MAX ,
52
50
min : std:: f64:: MIN ,
53
51
step : 1.0 ,
54
52
wrap : false ,
55
- value_changed : Box :: new ( value_changed) ,
56
53
increase_active : false ,
57
54
decrease_active : false ,
58
55
timer_id : TimerToken :: INVALID ,
59
56
}
60
57
}
61
58
59
+ /// Set the stepper's maximum value.
62
60
pub fn max ( mut self , max : f64 ) -> Self {
63
61
self . max = max;
64
62
self
65
63
}
66
64
65
+ /// Set the stepper's minimum value.
67
66
pub fn min ( mut self , min : f64 ) -> Self {
68
67
self . min = min;
69
68
self
70
69
}
71
70
71
+ /// Set the steppers amount by which the value increases or decreases.
72
72
pub fn step ( mut self , step : f64 ) -> Self {
73
73
self . step = step;
74
74
self
75
75
}
76
76
77
+ /// Set whether the stepper should wrap around the minimum/maximum values.
77
78
pub fn wrap ( mut self , wrap : bool ) -> Self {
78
79
self . wrap = wrap;
79
80
self
80
81
}
81
82
82
- fn change_value ( & mut self , ctx : & mut EventCtx , data : & mut f64 , env : & Env ) {
83
+ fn change_value ( & mut self , _ctx : & mut EventCtx , data : & mut f64 , _env : & Env ) {
83
84
// increase/decrease value depending on which button is currently active
84
85
let delta = if self . increase_active {
85
86
self . step
@@ -89,13 +90,9 @@ impl Stepper {
89
90
0.0
90
91
} ;
91
92
92
- let old_data = * data;
93
93
* data = ( * data + delta) . max ( self . min ) . min ( self . max ) ;
94
94
95
- if ( * data - old_data) . abs ( ) > EPSILON {
96
- // callback
97
- ( self . value_changed ) ( ctx, data, env) ;
98
- } else if self . wrap {
95
+ if self . wrap {
99
96
if ( * data - self . min ) . abs ( ) < EPSILON {
100
97
* data = self . max
101
98
} else {
@@ -105,6 +102,12 @@ impl Stepper {
105
102
}
106
103
}
107
104
105
+ impl Default for Stepper {
106
+ fn default ( ) -> Self {
107
+ Self :: new ( )
108
+ }
109
+ }
110
+
108
111
impl Widget < f64 > for Stepper {
109
112
fn paint ( & mut self , paint_ctx : & mut PaintCtx , _data : & f64 , env : & Env ) {
110
113
let rounded_rect =
0 commit comments