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,21 +78,20 @@ 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
- } else {
84
- if self . wrap {
85
- if * data == self . min {
86
- * data = self . max
87
- } else {
88
- * data = self . min
89
- }
83
+ } else if self . wrap {
84
+ if * data == self . min {
85
+ * data = self . max
86
+ } else {
87
+ * data = self . min
90
88
}
91
89
}
92
90
}
93
91
}
94
92
95
93
impl Widget < f64 > for Stepper {
96
- fn paint ( & mut self , paint_ctx : & mut PaintCtx , base_state : & BaseState , data : & f64 , env : & Env ) {
94
+ fn paint ( & mut self , paint_ctx : & mut PaintCtx , base_state : & BaseState , _data : & f64 , env : & Env ) {
97
95
let rounded_rect =
98
96
RoundedRect :: from_origin_size ( Point :: ORIGIN , base_state. size ( ) . to_vec2 ( ) , 4. ) ;
99
97
@@ -105,12 +103,12 @@ impl Widget<f64> for Stepper {
105
103
paint_ctx. clip ( rounded_rect) ;
106
104
107
105
// draw buttons for increase/decrease
108
- let mut increase_button_origin = Point :: ORIGIN ;
106
+ let increase_button_origin = Point :: ORIGIN ;
109
107
let mut decrease_button_origin = Point :: ORIGIN ;
110
108
decrease_button_origin. y += height / 2. ;
111
109
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) ;
110
+ let increase_button_rect = Rect :: from_origin_size ( increase_button_origin, button_size) ;
111
+ let decrease_button_rect = Rect :: from_origin_size ( decrease_button_origin, button_size) ;
114
112
115
113
let active_gradient = LinearGradient :: new (
116
114
UnitPoint :: TOP ,
@@ -126,31 +124,31 @@ impl Widget<f64> for Stepper {
126
124
127
125
// draw buttons that are currently triggered as active
128
126
if self . increase_active {
129
- paint_ctx. fill ( increase_rect , & active_gradient) ;
127
+ paint_ctx. fill ( increase_button_rect , & active_gradient) ;
130
128
} else {
131
- paint_ctx. fill ( increase_rect , & inactive_gradient) ;
129
+ paint_ctx. fill ( increase_button_rect , & inactive_gradient) ;
132
130
} ;
133
131
134
132
if self . decrease_active {
135
- paint_ctx. fill ( decrease_rect , & active_gradient) ;
133
+ paint_ctx. fill ( decrease_button_rect , & active_gradient) ;
136
134
} else {
137
- paint_ctx. fill ( decrease_rect , & inactive_gradient) ;
135
+ paint_ctx. fill ( decrease_button_rect , & inactive_gradient) ;
138
136
} ;
139
137
140
138
// 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 ) ) ;
139
+ let mut increase_button_arrow = BezPath :: new ( ) ;
140
+ increase_button_arrow . move_to ( Point :: new ( 4. , height / 2. - 4. ) ) ;
141
+ increase_button_arrow . line_to ( Point :: new ( width - 4. , height / 2. - 4. ) ) ;
142
+ increase_button_arrow . line_to ( Point :: new ( width / 2. , 4. ) ) ;
143
+ increase_button_arrow . close_path ( ) ;
144
+ paint_ctx. fill ( increase_button_arrow , & env. get ( theme:: LABEL_COLOR ) ) ;
145
+
146
+ let mut decrease_button_arrow = BezPath :: new ( ) ;
147
+ decrease_button_arrow . move_to ( Point :: new ( 4. , height / 2. + 4. ) ) ;
148
+ decrease_button_arrow . line_to ( Point :: new ( width - 4. , height / 2. + 4. ) ) ;
149
+ decrease_button_arrow . line_to ( Point :: new ( width / 2. , height - 4. ) ) ;
150
+ decrease_button_arrow . close_path ( ) ;
151
+ paint_ctx. fill ( decrease_button_arrow , & env. get ( theme:: LABEL_COLOR ) ) ;
154
152
}
155
153
156
154
fn layout (
0 commit comments