eval.rs 907 B

123456789101112131415161718192021222324252627282930313233
  1. use dioxus::prelude::*;
  2. fn main() {
  3. dioxus_desktop::launch(app);
  4. }
  5. fn app(cx: Scope) -> Element {
  6. let eval = dioxus_desktop::use_eval(cx);
  7. let script = use_state(cx, String::new);
  8. let output = use_state(cx, String::new);
  9. cx.render(rsx! {
  10. div {
  11. p { "Output: {output}" }
  12. input {
  13. placeholder: "Enter an expression",
  14. value: "{script}",
  15. oninput: move |e| script.set(e.value.clone()),
  16. }
  17. button {
  18. onclick: move |_| {
  19. to_owned![script, eval, output];
  20. cx.spawn(async move {
  21. if let Ok(res) = eval(script.to_string()).await {
  22. output.set(res.to_string());
  23. }
  24. });
  25. },
  26. "Execute"
  27. }
  28. }
  29. })
  30. }