makepad/examples/counter/src/main.rs

66 lines
1.8 KiB
Rust

pub use makepad_widgets;
use makepad_widgets::*;
app_main!(App);
script_mod! {
use mod.prelude.widgets.*
startup() do #(App::script_component(vm)){
ui: Root{
main_window := Window{
window.inner_size: vec2(420, 220)
body +: {
main_view := View{
width: Fill
height: Fill
flow: Down
spacing: 12
align: Center
counter_label := Label{
text: "Count: 0"
draw_text.text_style.font_size: 24
}
}
increment_button := Button{
text: "Increment"
}
}
}
}
}
}
#[derive(Script, ScriptHook)]
pub struct App {
#[live]
ui: WidgetRef,
#[rust]
counter: i32,
}
impl MatchEvent for App {
fn handle_actions(&mut self, cx: &mut Cx, actions: &Actions) {
if self.ui.button(cx, ids!(increment_button)).clicked(actions) {
self.counter += 1;
self.ui.label(cx, ids!(counter_label)).set_text(cx, &format!("Count: {}", self.counter));
}
}
}
impl AppMain for App {
fn script_mod(vm: &mut ScriptVm) -> ScriptValue {
crate::makepad_widgets::script_mod(vm);
// Application data survives Splash/style reloads; the UI definitions
// below are re-evaluated against this same state object.
if !vm.is_reload() {
script_eval!(vm, {mod.state = {counter: 0} true});
}
self::script_mod(vm)
}
fn handle_event(&mut self, cx: &mut Cx, event: &Event) {
self.match_event(cx, event);
self.ui.handle_event(cx, event, &mut Scope::empty());
}
}