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
// 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 in the LICENSE-APACHE file or at:
// https://www.apache.org/licenses/LICENSE-2.0
//! Counter example (simple button)
use kas::macros::{make_widget, VoidMsg};
use kas::prelude::*;
use kas::widget::{Label, Row, TextButton, Window};
#[derive(Clone, Debug, VoidMsg)]
enum Message {
Decr,
Incr,
}
fn main() -> Result<(), kas_wgpu::Error> {
env_logger::init();
// Most examples use make_widget! for layout, but here we use the Row widget
// (compare with sync-counter.rs). Note that Row produces (index, child_msg)
// messages, thus we use map_msg to remove the unwanted index.
let buttons = Row::new(vec![
TextButton::new_msg("−", Message::Decr),
TextButton::new_msg("+", Message::Incr),
])
.map_msg(|_, msg| msg.1);
let window = Window::new(
"Counter",
make_widget! {
#[layout(column)]
#[handler(msg = VoidMsg)]
struct {
#[widget(halign=centre)] display: Label<String> = Label::from("0"),
#[widget(handler = handle_button)] buttons -> Message = buttons,
counter: usize = 0,
}
impl {
fn handle_button(&mut self, mgr: &mut Manager, msg: Message)
-> Response<VoidMsg>
{
match msg {
Message::Decr => {
self.counter = self.counter.saturating_sub(1);
*mgr |= self.display.set_string(self.counter.to_string());
}
Message::Incr => {
self.counter = self.counter.saturating_add(1);
*mgr |= self.display.set_string(self.counter.to_string());
}
};
Response::None
}
}
},
);
let theme = kas_theme::ShadedTheme::new().with_font_size(24.0);
kas_wgpu::Toolkit::new(theme)?.with(window)?.run()
}