15
15
//! A stepper widget.
16
16
17
17
use crate :: {
18
- BaseState , BoxConstraints , Data , Env , Event , EventCtx , LayoutCtx , PaintCtx , Size , TimerToken ,
18
+ BaseState , BoxConstraints , Env , Event , EventCtx , LayoutCtx , PaintCtx , Size , TimerToken ,
19
19
UpdateCtx , Widget ,
20
20
} ;
21
21
use std:: time:: { Duration , Instant } ;
22
22
23
23
use crate :: kurbo:: { BezPath , Rect , RoundedRect } ;
24
- use crate :: piet:: {
25
- FontBuilder , LinearGradient , RenderContext , Text , TextLayout , TextLayoutBuilder , UnitPoint ,
26
- } ;
24
+ use crate :: piet:: { LinearGradient , RenderContext , UnitPoint } ;
27
25
28
26
use crate :: theme;
29
- use crate :: widget:: { Align , Label , LabelText , SizedBox } ;
27
+ use crate :: widget:: Align ;
30
28
use crate :: Point ;
31
29
32
- /// A stepper.
30
+ /// A stepper widget for step-wise increasing and decreasing a value .
33
31
pub struct Stepper {
34
32
max : f64 ,
35
33
min : f64 ,
@@ -67,6 +65,7 @@ impl Stepper {
67
65
}
68
66
69
67
fn change_value ( & mut self , ctx : & mut EventCtx , data : & mut f64 , env : & Env ) {
68
+ // increase/decrease value depending on which button is currently active
70
69
let delta = if self . increase_active {
71
70
self . step
72
71
} else if self . decrease_active {
@@ -79,6 +78,7 @@ impl Stepper {
79
78
* data = ( * data + delta) . min ( self . min ) . max ( self . max ) ;
80
79
81
80
if old_data != * data {
81
+ // callback
82
82
( self . value_changed ) ( ctx, data, env) ;
83
83
} else {
84
84
if self . wrap {
@@ -93,7 +93,7 @@ impl Stepper {
93
93
}
94
94
95
95
impl Widget < f64 > for Stepper {
96
- fn paint ( & mut self , paint_ctx : & mut PaintCtx , base_state : & BaseState , data : & f64 , env : & Env ) {
96
+ fn paint ( & mut self , paint_ctx : & mut PaintCtx , base_state : & BaseState , _data : & f64 , env : & Env ) {
97
97
let rounded_rect =
98
98
RoundedRect :: from_origin_size ( Point :: ORIGIN , base_state. size ( ) . to_vec2 ( ) , 4. ) ;
99
99
@@ -105,12 +105,12 @@ impl Widget<f64> for Stepper {
105
105
paint_ctx. clip ( rounded_rect) ;
106
106
107
107
// draw buttons for increase/decrease
108
- let mut increase_button_origin = Point :: ORIGIN ;
108
+ let increase_button_origin = Point :: ORIGIN ;
109
109
let mut decrease_button_origin = Point :: ORIGIN ;
110
110
decrease_button_origin. y += height / 2. ;
111
111
112
- let increase_rect = Rect :: from_origin_size ( increase_button_origin, button_size) ;
113
- let decrease_rect = Rect :: from_origin_size ( decrease_button_origin, button_size) ;
112
+ let increase_button_rect = Rect :: from_origin_size ( increase_button_origin, button_size) ;
113
+ let decrease_button_rect = Rect :: from_origin_size ( decrease_button_origin, button_size) ;
114
114
115
115
let active_gradient = LinearGradient :: new (
116
116
UnitPoint :: TOP ,
@@ -126,31 +126,31 @@ impl Widget<f64> for Stepper {
126
126
127
127
// draw buttons that are currently triggered as active
128
128
if self . increase_active {
129
- paint_ctx. fill ( increase_rect , & active_gradient) ;
129
+ paint_ctx. fill ( increase_button_rect , & active_gradient) ;
130
130
} else {
131
- paint_ctx. fill ( increase_rect , & inactive_gradient) ;
131
+ paint_ctx. fill ( increase_button_rect , & inactive_gradient) ;
132
132
} ;
133
133
134
134
if self . decrease_active {
135
- paint_ctx. fill ( decrease_rect , & active_gradient) ;
135
+ paint_ctx. fill ( decrease_button_rect , & active_gradient) ;
136
136
} else {
137
- paint_ctx. fill ( decrease_rect , & inactive_gradient) ;
137
+ paint_ctx. fill ( decrease_button_rect , & inactive_gradient) ;
138
138
} ;
139
139
140
140
// draw up and down triangles
141
- let mut increase_arrow = BezPath :: new ( ) ;
142
- increase_arrow . move_to ( Point :: new ( 4. , height / 2. - 4. ) ) ;
143
- increase_arrow . line_to ( Point :: new ( width - 4. , height / 2. - 4. ) ) ;
144
- increase_arrow . line_to ( Point :: new ( width / 2. , 4. ) ) ;
145
- increase_arrow . close_path ( ) ;
146
- paint_ctx. fill ( increase_arrow , & env. get ( theme:: LABEL_COLOR ) ) ;
147
-
148
- let mut decrease_arrow = BezPath :: new ( ) ;
149
- decrease_arrow . move_to ( Point :: new ( 4. , height / 2. + 4. ) ) ;
150
- decrease_arrow . line_to ( Point :: new ( width - 4. , height / 2. + 4. ) ) ;
151
- decrease_arrow . line_to ( Point :: new ( width / 2. , height - 4. ) ) ;
152
- decrease_arrow . close_path ( ) ;
153
- paint_ctx. fill ( decrease_arrow , & env. get ( theme:: LABEL_COLOR ) ) ;
141
+ let mut increase_button_arrow = BezPath :: new ( ) ;
142
+ increase_button_arrow . move_to ( Point :: new ( 4. , height / 2. - 4. ) ) ;
143
+ increase_button_arrow . line_to ( Point :: new ( width - 4. , height / 2. - 4. ) ) ;
144
+ increase_button_arrow . line_to ( Point :: new ( width / 2. , 4. ) ) ;
145
+ increase_button_arrow . close_path ( ) ;
146
+ paint_ctx. fill ( increase_button_arrow , & env. get ( theme:: LABEL_COLOR ) ) ;
147
+
148
+ let mut decrease_button_arrow = BezPath :: new ( ) ;
149
+ decrease_button_arrow . move_to ( Point :: new ( 4. , height / 2. + 4. ) ) ;
150
+ decrease_button_arrow . line_to ( Point :: new ( width - 4. , height / 2. + 4. ) ) ;
151
+ decrease_button_arrow . line_to ( Point :: new ( width / 2. , height - 4. ) ) ;
152
+ decrease_button_arrow . close_path ( ) ;
153
+ paint_ctx. fill ( decrease_button_arrow , & env. get ( theme:: LABEL_COLOR ) ) ;
154
154
}
155
155
156
156
fn layout (
0 commit comments