1#![allow(dead_code)]
2
3use std::time::Duration;
4
5use anyhow::Result;
6
7use salvation::{
8 event::LayoutEvent,
9 impl_widget_common,
10 layout::SizeHintMode,
11 system::add_interval,
12 types::Rect,
13 widgets::{
14 button::Button, column::Column, label::Label, padding_box::PaddingBox,
15 scroll_area::ScrollArea, text_input::TextInput, Widget, WidgetCommon, WidgetExt, WidgetId,
16 },
17 App,
18};
19use winit::window::Window;
20
21struct AnotherWidget {
22 common: WidgetCommon,
23 counter: i32,
24}
25
26impl AnotherWidget {
27 fn new() -> Self {
28 let mut this = Self {
29 counter: 0,
30 common: WidgetCommon::new(),
31 };
32 let mut button = Button::new("another button");
33 button.on_triggered(this.callback(|this, _event| {
34 this.counter += 1;
35 println!("counter: {}", this.counter);
36 let label = Label::new(format!("counter: {}", this.counter))
37 .with_window(Window::default_attributes().with_title("example"))
38 .boxed();
39 this.common_mut().add_child(label, Default::default());
40 Ok(())
41 }));
42 this.common_mut()
43 .add_child(button.boxed(), Default::default());
44 this
45 }
46}
47
48impl Widget for AnotherWidget {
49 impl_widget_common!();
50
51 fn recalculate_size_hint_x(&mut self, mode: SizeHintMode) -> Result<i32> {
52 Ok(self.common_mut().children[0].widget.size_hint_x(mode))
53 }
54 fn recalculate_size_x_fixed(&mut self) -> bool {
55 self.common_mut().children[0].widget.size_x_fixed()
56 }
57 fn recalculate_size_hint_y(&mut self, size_x: i32, mode: SizeHintMode) -> Result<i32> {
58 Ok(self.common_mut().children[0]
59 .widget
60 .size_hint_y(size_x, mode))
61 }
62 fn recalculate_size_y_fixed(&mut self) -> bool {
63 self.common_mut().children[0].widget.size_y_fixed()
64 }
65 fn handle_layout(&mut self, event: LayoutEvent) -> Result<()> {
66 self.common.set_child_rect(
67 0,
68 event
69 .new_rect_in_window()
70 .map(|r| Rect::from_pos_size(Default::default(), r.size)),
71 )
72 }
73}
74
75struct RootWidget {
76 common: WidgetCommon,
77 button_id: WidgetId<Button>,
78 column2_id: WidgetId<Column>,
79 button21_id: WidgetId<Button>,
80 button22_id: WidgetId<Button>,
81 flag_column: bool,
82 flag_button21: bool,
83 i: i32,
84 label2_id: WidgetId<Label>,
85}
86
87impl RootWidget {
88 fn new() -> Self {
89 let mut common = WidgetCommon::new();
90 let mut root = Column::new();
91
92 root.add_child(TextInput::new("Hello, Rust! 🦀😂\n").boxed());
93 root.add_child(TextInput::new("Hebrew name Sarah: שרה.").boxed());
94
95 let btn1 = Button::new("btn1")
112 .with_auto_repeat(true)
113 .with_on_triggered(
114 common
115 .id
116 .callback(|this: &mut Self, event| this.button_clicked(event, 1)),
117 )
118 .with_id();
119
120 root.add_child(btn1.widget.boxed());
121
122 root.add_child(
123 Button::new("btn2")
124 .with_on_triggered(
125 common
126 .id
127 .callback(|this: &mut Self, event| this.button_clicked(event, 2)),
128 )
129 .boxed(),
130 );
131
132 let button21 = Button::new("btn21")
133 .with_on_triggered(common.id.callback(|_: &mut Self, _| {
134 println!("click!");
135 Ok(())
136 }))
137 .with_id();
138
139 let button22 = Button::new("btn22").with_id();
140
141 let column2 = Column::new()
142 .with_child(button21.widget.boxed())
143 .with_child(button22.widget.boxed())
144 .with_id();
145 root.add_child(column2.widget.boxed());
146
147 let btn3 = AnotherWidget::new().boxed();
148 root.add_child(btn3);
149
150 let label2 = Label::new("ok").with_id();
161 root.add_child(label2.widget.boxed());
162
163 let mut content = Column::new();
164 for i in 1..=80 {
165 content.add_child(Button::new(format!("btn{i}")).boxed());
166 }
167
168 root.add_child(ScrollArea::new(content.boxed()).boxed());
169
170 common.add_child(
171 PaddingBox::new(root.boxed())
172 .with_window(Window::default_attributes().with_title("example"))
173 .boxed(),
174 Default::default(),
175 );
176 add_interval(
177 Duration::from_secs(2),
178 common.id.callback(|this: &mut Self, _| this.inc()),
179 );
180
181 RootWidget {
182 common,
183 button_id: btn1.id,
184 column2_id: column2.id,
185 button21_id: button21.id,
186 button22_id: button22.id,
187 flag_column: true,
188 flag_button21: true,
189 i: 0,
190 label2_id: label2.id,
191 }
192 }
193
194 fn inc(&mut self) -> Result<()> {
195 self.i += 1;
196 if let Ok(widget) = self.common.widget(self.button21_id) {
197 widget.set_text(format!("i = {}", self.i));
198 }
199 Ok(())
200 }
201
202 fn button_clicked(&mut self, data: String, k: u32) -> Result<()> {
203 println!("callback! {:?}, {}", data, k);
204 let button = self.common.widget(self.button_id)?;
205 button.set_text(format!("ok {}", if k == 1 { "1" } else { "22222" }));
206
207 if k == 1 {
208 self.flag_column = !self.flag_column;
209 self.common
210 .widget(self.column2_id)?
211 .set_enabled(self.flag_column);
212 println!("set enabled {:?} {:?}", self.column2_id, self.flag_column);
213 } else {
214 self.flag_button21 = !self.flag_button21;
215 self.common
216 .widget(self.button21_id)?
217 .set_enabled(self.flag_button21);
218 println!(
219 "set enabled {:?} {:?}",
220 self.button21_id, self.flag_button21
221 );
222 }
223 Ok(())
224 }
225}
226
227impl Widget for RootWidget {
228 impl_widget_common!();
229
230 fn recalculate_size_hint_x(&mut self, _mode: SizeHintMode) -> Result<i32> {
231 Ok(0)
232 }
233
234 fn recalculate_size_hint_y(&mut self, _size_x: i32, _mode: SizeHintMode) -> Result<i32> {
235 Ok(0)
236 }
237}
238
239fn main() {
240 if std::env::var("RUST_LOG").is_err() {
241 std::env::set_var("RUST_LOG", "info")
242 }
243 env_logger::init();
244
245 App::new().run(|| RootWidget::new().boxed()).unwrap();
280}