use std::cell::RefCell;
use std::rc::Rc;
use saudade::{App, Bevel, Button, Container, Label, Painter, Rect, Theme, Widget, WindowConfig};
const W: i32 = 220;
const H: i32 = 64;
fn main() {
let count = Rc::new(RefCell::new(0i32));
let display = Rc::new(RefCell::new(Label::new(Rect::new(28, 24, 64, 16), "0")));
let button = Button::new(Rect::new(120, 20, 84, 24), "Count")
.default(true)
.on_click({
let count = count.clone();
let display = display.clone();
move |cx| {
*count.borrow_mut() += 1;
display.borrow_mut().text = count.borrow().to_string();
cx.request_paint();
}
});
let root = Container::new(W, H)
.add(Bevel::sunken(Rect::new(16, 18, 88, 28)))
.add(SharedLabel(display.clone()))
.add(button);
App::new(WindowConfig::new("Counter", W, H), root)
.with_theme(Theme::windows_31())
.run();
}
struct SharedLabel(Rc<RefCell<Label>>);
impl Widget for SharedLabel {
fn bounds(&self) -> Rect {
self.0.borrow().bounds()
}
fn paint(&mut self, painter: &mut Painter, theme: &Theme) {
self.0.borrow_mut().paint(painter, theme);
}
fn layout(&mut self, bounds: Rect) {
self.0.borrow_mut().layout(bounds);
}
}