Skip to content

Commit 98c1631

Browse files
committed
Adress stepper review feedback
1 parent ed4f800 commit 98c1631

File tree

2 files changed

+16
-17
lines changed

2 files changed

+16
-17
lines changed

druid/examples/switch.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
use druid::widget::{Align, Flex, Label, Padding, Parse, Stepper, Switch, TextBox};
15+
use druid::widget::{Flex, Label, Padding, Parse, Stepper, Switch, TextBox, WidgetExt};
1616
use druid::{AppLauncher, Data, Lens, LensExt, LensWrap, Widget, WindowDesc};
1717

1818
#[derive(Clone, Data, Lens)]
@@ -31,11 +31,7 @@ fn build_widget() -> impl Widget<DemoState> {
3131
row.add_child(Padding::new(5.0, switch), 0.0);
3232

3333
let stepper = LensWrap::new(
34-
Stepper::new(|_ctx, _data, _env| {})
35-
.max(10.0)
36-
.min(0.0)
37-
.step(0.5)
38-
.wrap(false),
34+
Stepper::new().max(10.0).min(0.0).step(0.5).wrap(false),
3935
DemoState::stepper_value,
4036
);
4137

@@ -45,7 +41,7 @@ fn build_widget() -> impl Widget<DemoState> {
4541
DemoState::stepper_value.map(|x| Some(*x), |x, y| *x = y.unwrap_or(0.0)),
4642
);
4743
textbox_row.add_child(Padding::new(5.0, textbox), 0.0);
48-
textbox_row.add_child(Padding::new(5.0, Align::centered(stepper)), 0.0);
44+
textbox_row.add_child(Padding::new(5.0, stepper.center().padding(5.0)), 0.0);
4945

5046
let mut label_row = Flex::row();
5147

druid/src/widget/stepper.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,49 +37,50 @@ pub struct Stepper {
3737
min: f64,
3838
step: f64,
3939
wrap: bool,
40-
/// A closure that will be invoked when the value changed.
41-
value_changed: Box<dyn Fn(&mut EventCtx, &mut f64, &Env)>,
4240
/// Keeps track of which button is currently triggered.
4341
increase_active: bool,
4442
decrease_active: bool,
4543
timer_id: TimerToken,
4644
}
4745

4846
impl Stepper {
49-
pub fn new(value_changed: impl Fn(&mut EventCtx, &mut f64, &Env) + 'static) -> Self {
47+
pub fn new() -> Self {
5048
Stepper {
5149
max: std::f64::MAX,
5250
min: std::f64::MIN,
5351
step: 1.0,
5452
wrap: false,
55-
value_changed: Box::new(value_changed),
5653
increase_active: false,
5754
decrease_active: false,
5855
timer_id: TimerToken::INVALID,
5956
}
6057
}
6158

59+
/// Set the stepper's maximum value.
6260
pub fn max(mut self, max: f64) -> Self {
6361
self.max = max;
6462
self
6563
}
6664

65+
/// Set the stepper's minimum value.
6766
pub fn min(mut self, min: f64) -> Self {
6867
self.min = min;
6968
self
7069
}
7170

71+
/// Set the steppers amount by which the value increases or decreases.
7272
pub fn step(mut self, step: f64) -> Self {
7373
self.step = step;
7474
self
7575
}
7676

77+
/// Set whether the stepper should wrap around the minimum/maximum values.
7778
pub fn wrap(mut self, wrap: bool) -> Self {
7879
self.wrap = wrap;
7980
self
8081
}
8182

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) {
8384
// increase/decrease value depending on which button is currently active
8485
let delta = if self.increase_active {
8586
self.step
@@ -89,13 +90,9 @@ impl Stepper {
8990
0.0
9091
};
9192

92-
let old_data = *data;
9393
*data = (*data + delta).max(self.min).min(self.max);
9494

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 {
9996
if (*data - self.min).abs() < EPSILON {
10097
*data = self.max
10198
} else {
@@ -105,6 +102,12 @@ impl Stepper {
105102
}
106103
}
107104

105+
impl Default for Stepper {
106+
fn default() -> Self {
107+
Self::new()
108+
}
109+
}
110+
108111
impl Widget<f64> for Stepper {
109112
fn paint(&mut self, paint_ctx: &mut PaintCtx, _data: &f64, env: &Env) {
110113
let rounded_rect =

0 commit comments

Comments
 (0)