-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcustom-widget.rs
More file actions
56 lines (50 loc) · 1.46 KB
/
custom-widget.rs
File metadata and controls
56 lines (50 loc) · 1.46 KB
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
use kas::prelude::*;
use kas::widgets::{AccessLabel, Button, Row, Text, format_value};
#[derive(Clone, Debug)]
struct Increment(i32);
#[impl_self]
mod Counter {
#[widget]
#[layout(column![
self.display.align(AlignHints::CENTER),
self.buttons,
])]
struct Counter {
core: widget_core!(),
#[widget(&self.count)]
display: Text<i32, String>,
#[widget]
buttons: Row<[Button<AccessLabel>; 2]>,
count: i32,
}
impl Self {
fn new(count: i32) -> Self {
Counter {
core: Default::default(),
display: format_value!("{}"),
buttons: Row::new([
Button::label_msg("-", Increment(-1)),
Button::label_msg("+", Increment(1)),
]),
count,
}
}
}
impl Events for Self {
type Data = ();
fn handle_messages(&mut self, cx: &mut EventCx, data: &()) {
if let Some(Increment(incr)) = cx.try_pop() {
self.count += incr;
cx.update(self.as_node(data));
}
}
}
}
fn main() -> kas::runner::Result<()> {
env_logger::init();
let window = Window::new(Counter::new(0), "Counter");
let theme = kas::theme::SimpleTheme::new();
let mut app = kas::runner::Runner::with_theme(theme).build(())?;
let _ = app.config_mut().font.set_size(24.0);
app.with(window).run()
}