eval.rs 987 B

12345678910111213141516171819202122232425262728293031323334353637
  1. use dioxus::prelude::*;
  2. use dioxus_desktop::EvalResult;
  3. fn main() {
  4. dioxus_desktop::launch(app);
  5. }
  6. fn app(cx: Scope) -> Element {
  7. let script = use_state(cx, String::new);
  8. let eval = dioxus_desktop::use_eval(cx);
  9. let future: &UseRef<Option<EvalResult>> = use_ref(cx, || None);
  10. if future.read().is_some() {
  11. let future_clone = future.clone();
  12. cx.spawn(async move {
  13. if let Some(fut) = future_clone.with_mut(|o| o.take()) {
  14. println!("{:?}", fut.await)
  15. }
  16. });
  17. }
  18. cx.render(rsx! {
  19. div {
  20. input {
  21. placeholder: "Enter an expression",
  22. value: "{script}",
  23. oninput: move |e| script.set(e.value.clone()),
  24. }
  25. button {
  26. onclick: move |_| {
  27. let fut = eval(script);
  28. future.set(Some(fut));
  29. },
  30. "Execute"
  31. }
  32. }
  33. })
  34. }