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
#![windows_subsystem = "windows"]
use druid::widget::Slider;
use druid::widget::{CrossAxisAlignment, Flex, Label, TextBox};
use druid::{AppLauncher, Data, Env, Lens, LocalizedString, Widget, WidgetExt, WindowDesc};
pub fn main() {
let main_window = WindowDesc::new(ui_builder())
.title(LocalizedString::new("lens-demo-window-title").with_placeholder("Lens Demo"));
let data = MyComplexState {
term: "hello".into(),
scale: 0.0,
};
AppLauncher::with_window(main_window)
.launch(data)
.expect("launch failed");
}
#[derive(Clone, Debug, Data, Lens)]
struct MyComplexState {
#[lens(name = "term_lens")]
term: String,
scale: f64,
}
fn ui_builder() -> impl Widget<MyComplexState> {
let searchbar = TextBox::new().lens(MyComplexState::term_lens);
let slider = Slider::new().lens(MyComplexState::scale);
let label = Label::new(|d: &MyComplexState, _: &Env| format!("{}: {:.2}", d.term, d.scale));
Flex::column()
.cross_axis_alignment(CrossAxisAlignment::Center)
.with_child(label)
.with_default_spacer()
.with_child(
Flex::row()
.cross_axis_alignment(CrossAxisAlignment::Center)
.with_child(searchbar)
.with_default_spacer()
.with_child(slider),
)
.center()
}