dioxus/examples/eval.rs

34 lines
907 B
Rust
Raw Normal View History

2022-03-19 02:11:30 +00:00
use dioxus::prelude::*;
fn main() {
dioxus_desktop::launch(app);
2022-03-19 02:11:30 +00:00
}
fn app(cx: Scope) -> Element {
let eval = dioxus_desktop::use_eval(cx);
2022-12-30 07:06:33 +00:00
let script = use_state(cx, String::new);
let output = use_state(cx, String::new);
2022-03-19 02:11:30 +00:00
cx.render(rsx! {
div {
2022-12-30 07:06:33 +00:00
p { "Output: {output}" }
2022-03-19 02:11:30 +00:00
input {
placeholder: "Enter an expression",
value: "{script}",
oninput: move |e| script.set(e.value.clone()),
}
button {
2022-12-08 16:44:56 +00:00
onclick: move |_| {
2022-12-30 07:06:33 +00:00
to_owned![script, eval, output];
cx.spawn(async move {
if let Ok(res) = eval(script.to_string()).await {
output.set(res.to_string());
}
});
2022-12-08 16:44:56 +00:00
},
2022-03-19 02:11:30 +00:00
"Execute"
}
}
})
}