forked from linebender/druid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
slider.rs
69 lines (62 loc) · 2.32 KB
/
slider.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Copyright 2019 The xi-editor Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use druid::widget::{Align, Button, Checkbox, Flex, Label, Padding, ProgressBar, Slider};
use druid::{AppLauncher, Data, Lens, LensWrap, Widget, WindowDesc};
#[derive(Clone, Data, Lens)]
struct DemoState {
value: f64,
double: bool,
}
fn build_widget() -> impl Widget<DemoState> {
let label = Label::new(|data: &DemoState, _env: &_| {
if data.double {
format!("2x the value: {0:.2}", data.value * 2.0)
} else {
format!("actual value: {0:.2}", data.value)
}
});
let checkbox = LensWrap::new(Checkbox::new(), DemoState::double);
let checkbox_label = Label::new("double the value");
let row = Flex::row()
.with_child(checkbox, 0.0)
.with_child(Padding::new(5.0, checkbox_label), 1.0);
let bar = LensWrap::new(ProgressBar::new(), DemoState::value);
let slider = LensWrap::new(Slider::new(), DemoState::value);
let button_1 = Button::sized(
"increment ",
|_ctx, data: &mut DemoState, _env| data.value += 0.1,
200.0,
100.0,
);
let button_2 = Button::new("decrement ", |_ctx, data: &mut DemoState, _env| {
data.value -= 0.1
});
Flex::column()
.with_child(Padding::new(5.0, bar), 1.0)
.with_child(Padding::new(5.0, slider), 1.0)
.with_child(Padding::new(5.0, label), 1.0)
.with_child(Padding::new(5.0, row), 1.0)
.with_child(Padding::new(5.0, Align::right(button_1)), 0.0)
.with_child(Padding::new(5.0, button_2), 1.0)
}
fn main() {
let window = WindowDesc::new(build_widget);
AppLauncher::with_window(window)
.use_simple_logger()
.launch(DemoState {
value: 0.7f64,
double: false,
})
.expect("launch failed");
}