Skip to main content

start/
start.rs

1#![allow(dead_code)]
2
3use {
4    anyhow::Result,
5    std::time::Duration,
6    widgem::{
7        impl_widget_base,
8        system::add_interval,
9        widgets::{
10            button::Button, column::Column, label::Label, scroll_area::ScrollArea,
11            text_input::TextInput, window::Window, Widget, WidgetBaseOf, WidgetExt, WidgetId,
12        },
13        App,
14    },
15};
16
17struct AnotherWidget {
18    base: WidgetBaseOf<Self>,
19    counter: i32,
20}
21
22// #[derive(Debug)]
23// enum Abc {
24//     Def,
25// }
26
27// impl FormatKey for Abc {
28//     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
29//         writeln!(f, "{:?}", self)
30//     }
31// }
32
33impl Widget for AnotherWidget {
34    impl_widget_base!();
35
36    fn new(base: WidgetBaseOf<Self>) -> Self {
37        let mut this = Self { counter: 0, base };
38        let callback = this.callback(|this, _event| {
39            this.counter += 1;
40            println!("counter: {}", this.counter);
41            let window = this
42                .base
43                .add_child_with_key::<Window>(("window", this.counter))
44                .set_title("example");
45            println!("window {:?}", window.id());
46            let label = window
47                .base_mut()
48                .add_child::<Label>()
49                .set_column(0)
50                .set_row(0)
51                .set_text(format!("counter: {}", this.counter));
52            println!("label {:?}", label.id());
53            Ok(())
54        });
55        let button = this
56            .base_mut()
57            .add_child_with_key::<Button>("button")
58            .set_column(0)
59            .set_row(1);
60        button.set_text("another button");
61        button.on_triggered(callback);
62        this
63    }
64}
65
66struct RootWidget {
67    base: WidgetBaseOf<Self>,
68    button_id: WidgetId<Button>,
69    column2_id: WidgetId<Column>,
70    button21_id: WidgetId<Button>,
71    button22_id: WidgetId<Button>,
72    flag_column: bool,
73    flag_button21: bool,
74    i: i32,
75    label2_id: WidgetId<Label>,
76}
77
78impl RootWidget {
79    fn inc(&mut self) -> Result<()> {
80        self.i += 1;
81        if let Ok(widget) = self.base.widget(self.button21_id) {
82            widget.set_text(format!("i = {}", self.i));
83        }
84        Ok(())
85    }
86
87    fn button_clicked(&mut self, data: (), k: u32) -> Result<()> {
88        println!("callback! {:?}, {}", data, k);
89        let button = self.base.widget(self.button_id)?;
90        button.set_text(format!("ok {}", if k == 1 { "1" } else { "22222" }));
91
92        if k == 1 {
93            self.flag_column = !self.flag_column;
94            self.base
95                .widget(self.column2_id)?
96                .set_enabled(self.flag_column);
97            println!("set enabled {:?} {:?}", self.column2_id, self.flag_column);
98        } else {
99            self.flag_button21 = !self.flag_button21;
100            self.base
101                .widget(self.button21_id)?
102                .set_enabled(self.flag_button21);
103            println!(
104                "set enabled {:?} {:?}",
105                self.button21_id, self.flag_button21
106            );
107        }
108        Ok(())
109    }
110}
111
112impl Widget for RootWidget {
113    impl_widget_base!();
114
115    fn new(mut base: WidgetBaseOf<Self>) -> Self {
116        let id = base.id();
117
118        let window = base.add_child::<Window>().set_title("example");
119
120        let root = window
121            .base_mut()
122            .add_child::<Column>()
123            .set_column(0)
124            .set_row(0);
125
126        root.base_mut()
127            .add_child::<TextInput>()
128            .set_column(0)
129            .set_row(0)
130            .set_text("Hello, Rust! 🦀😂\n");
131        root.base_mut()
132            .add_child::<TextInput>()
133            .set_column(0)
134            .set_row(1)
135            .set_text("Hebrew name Sarah: שרה.");
136
137        /*
138        let btn = Button::new("btn1")
139            .with_icon(icon)
140            .with_alignment(Al::Right)
141            .with_on_clicked(slot)
142            .split_id()
143            .boxed();
144        root.add(btn.widget);
145
146        Self {
147            btn_id: btn.id,
148        }
149
150
151        */
152
153        let button_id = root
154            .base_mut()
155            .add_child::<Button>()
156            .set_column(0)
157            .set_row(2)
158            .set_text("btn1")
159            .set_auto_repeat(true)
160            .on_triggered(id.callback(|this, event| this.button_clicked(event, 1)))
161            .id();
162
163        root.base_mut()
164            .add_child::<Button>()
165            .set_column(0)
166            .set_row(3)
167            .set_text("btn2")
168            .on_triggered(id.callback(|this, event| this.button_clicked(event, 2)));
169
170        let column2 = root
171            .base_mut()
172            .add_child::<Column>()
173            .set_column(0)
174            .set_row(4);
175        let button21_id = column2
176            .base_mut()
177            .add_child::<Button>()
178            .set_column(0)
179            .set_row(0)
180            .set_text("btn21")
181            .on_triggered(id.callback(|_, _| {
182                println!("click!");
183                Ok(())
184            }))
185            .id();
186
187        let button22_id = column2
188            .base_mut()
189            .add_child::<Button>()
190            .set_column(0)
191            .set_row(1)
192            .set_text("btn22")
193            .id();
194        let column2_id = column2.id();
195
196        root.base_mut()
197            .add_child::<AnotherWidget>()
198            .set_column(0)
199            .set_row(5);
200
201        let label2_id = root
202            .base_mut()
203            .add_child::<Label>()
204            .set_column(0)
205            .set_row(6)
206            .set_text("ok")
207            .id();
208
209        let scroll_area = root
210            .base_mut()
211            .add_child::<ScrollArea>()
212            .set_column(0)
213            .set_row(7);
214        let content = scroll_area.set_content::<Column>();
215        for i in 1..=80 {
216            content
217                .base_mut()
218                .add_child::<Button>()
219                .set_column(0)
220                .set_row(i)
221                .set_text(format!("btn btn btn btn btn btn btn btn btn btn{i}"));
222        }
223
224        add_interval(Duration::from_secs(2), id.callback(|this, _| this.inc()));
225
226        RootWidget {
227            base,
228            button_id,
229            column2_id,
230            button21_id,
231            button22_id,
232            flag_column: true,
233            flag_button21: true,
234            i: 0,
235            label2_id,
236        }
237    }
238}
239
240fn main() {
241    if std::env::var("RUST_LOG").is_err() {
242        std::env::set_var("RUST_LOG", "info")
243    }
244    env_logger::init();
245    App::new()
246        .run(|r| {
247            r.base_mut().add_child::<RootWidget>();
248            Ok(())
249        })
250        .unwrap();
251}