Skip to content

Commit 0e8a6a7

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

File tree

3 files changed

+24
-15
lines changed

3 files changed

+24
-15
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: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
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
};
2120
use std::time::{Duration, Instant};
2221

@@ -91,11 +90,11 @@ impl Stepper {
9190
}
9291

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

98-
let height = base_state.size().height;
97+
let height = paint_ctx.size().height;
9998
let width = env.get(theme::BASIC_WIDGET_HEIGHT);
10099
let button_size = Size::new(width, height / 2.);
101100

@@ -164,7 +163,7 @@ impl Widget<f64> for Stepper {
164163
))
165164
}
166165

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

170169
match event {

0 commit comments

Comments
 (0)