Skip to content

Commit 1c3d431

Browse files
committed
Add example for connecting stepper widget with textbox
1 parent 1d38711 commit 1c3d431

File tree

3 files changed

+27
-17
lines changed

3 files changed

+27
-17
lines changed

druid/examples/switch.rs

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

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

1818
#[derive(Clone, Data, Lens)]
1919
struct DemoState {
@@ -30,22 +30,30 @@ fn build_widget() -> impl Widget<DemoState> {
3030
row.add_child(Padding::new(5.0, switch_label), 0.0);
3131
row.add_child(Padding::new(5.0, switch), 0.0);
3232

33-
let label_stepper = LensWrap::new(
33+
let stepper = LensWrap::new(
3434
Stepper::new(0.0, 10.0, 0.25, true, |_ctx, _data, _env| {}),
35-
lenses::demo_state::stepper_value,
35+
DemoState::stepper_value,
3636
);
3737

38-
let mut stepper_row = Row::new();
38+
let mut textbox_row = Flex::row();
39+
let textbox = LensWrap::new(
40+
Parse::new(TextBox::new()),
41+
DemoState::stepper_value.map(|x| Some(*x), |x, y| *x = y.unwrap_or(0.0)),
42+
);
43+
textbox_row.add_child(Padding::new(5.0, textbox), 0.0);
44+
textbox_row.add_child(Padding::new(5.0, stepper), 0.0);
45+
46+
let mut label_row = Flex::row();
3947

40-
let label = DynLabel::new(|data: &DemoState, _env| {
48+
let label = Label::new(|data: &DemoState, _env: &_| {
4149
format!("Stepper value: {0:.2}", data.stepper_value)
4250
});
4351

44-
stepper_row.add_child(Padding::new(5.0, label), 0.0);
45-
stepper_row.add_child(Padding::new(5.0, label_stepper), 0.0);
52+
label_row.add_child(Padding::new(5.0, label), 0.0);
4653

4754
col.add_child(Padding::new(5.0, row), 1.0);
48-
col.add_child(Padding::new(5.0, stepper_row), 1.0);
55+
col.add_child(Padding::new(5.0, textbox_row), 1.0);
56+
col.add_child(Padding::new(5.0, label_row), 1.0);
4957
col
5058
}
5159

druid/src/widget/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ mod scroll;
3131
mod sized_box;
3232
mod slider;
3333
mod split;
34+
mod stepper;
3435
#[cfg(feature = "svg")]
3536
#[cfg_attr(docsrs, doc(cfg(feature = "svg")))]
3637
mod svg;
@@ -55,6 +56,7 @@ pub use scroll::Scroll;
5556
pub use sized_box::SizedBox;
5657
pub use slider::Slider;
5758
pub use split::Split;
59+
pub use stepper::Stepper;
5860
#[cfg(feature = "svg")]
5961
#[cfg_attr(docsrs, doc(cfg(feature = "svg")))]
6062
pub use svg::{Svg, SvgData};

druid/src/widget/stepper.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
//! A stepper widget.
1616
1717
use crate::{
18-
BaseState, BoxConstraints, Env, Event, EventCtx, LayoutCtx, PaintCtx, Size, TimerToken,
19-
UpdateCtx, Widget,
18+
BoxConstraints, Env, Event, EventCtx, LayoutCtx, PaintCtx, Size, TimerToken, UpdateCtx, Widget,
2019
};
20+
use std::f64::EPSILON;
2121
use std::time::{Duration, Instant};
2222

2323
use crate::kurbo::{BezPath, Rect, RoundedRect};
@@ -77,11 +77,11 @@ impl Stepper {
7777
let old_data = *data;
7878
*data = (*data + delta).min(self.min).max(self.max);
7979

80-
if old_data != *data {
80+
if (*data - old_data).abs() > EPSILON {
8181
// callback
8282
(self.value_changed)(ctx, data, env);
8383
} else if self.wrap {
84-
if *data == self.min {
84+
if (*data - self.min).abs() < EPSILON {
8585
*data = self.max
8686
} else {
8787
*data = self.min
@@ -91,11 +91,11 @@ impl Stepper {
9191
}
9292

9393
impl Widget<f64> for Stepper {
94-
fn paint(&mut self, paint_ctx: &mut PaintCtx, base_state: &BaseState, _data: &f64, env: &Env) {
94+
fn paint(&mut self, paint_ctx: &mut PaintCtx, _data: &f64, env: &Env) {
9595
let rounded_rect =
96-
RoundedRect::from_origin_size(Point::ORIGIN, base_state.size().to_vec2(), 4.);
96+
RoundedRect::from_origin_size(Point::ORIGIN, paint_ctx.size().to_vec2(), 4.);
9797

98-
let height = base_state.size().height;
98+
let height = paint_ctx.size().height;
9999
let width = env.get(theme::BASIC_WIDGET_HEIGHT);
100100
let button_size = Size::new(width, height / 2.);
101101

@@ -164,7 +164,7 @@ impl Widget<f64> for Stepper {
164164
))
165165
}
166166

167-
fn event(&mut self, event: &Event, ctx: &mut EventCtx, data: &mut f64, env: &Env) {
167+
fn event(&mut self, ctx: &mut EventCtx, event: &Event, data: &mut f64, env: &Env) {
168168
let height = env.get(theme::BORDERED_WIDGET_HEIGHT);
169169

170170
match event {

0 commit comments

Comments
 (0)